Kubernetes 服务 DNS 无法访问

Kubernetes Service DNS Unreachable

我正处于学习 Kubernetes 的阶段,尝试创建一个服务并通过 ping / curl 访问它。

第 1 步:在默认命名空间

中创建部署 mytomcat
alias k='kubectl'
k create deployment mytomcat --image=tomcat

第 2 步:在默认命名空间中创建了一个带有 curl 的部署 busybox

apiVersion: v1
kind: Pod
metadata:
  name: mybusybox
  namespace: default
spec:
  containers:
  - image: yauritux/busybox-curl
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
    name: busybox
  restartPolicy: Always

k apply -f busybox.yaml
k exec mybusybox -t -i -- sh

第 3 步:Ping mytomcat pods 成功了

ping 10.1.1.11
PING 10.1.1.11 (10.1.1.11): 56 data bytes
64 bytes from 10.1.1.11: seq=0 ttl=64 time=0.207 ms

第 4 步:创建服务以在 tomcat

上公开端口 8080
k expose deployment mytomcat --port=8080 

Step5:获取所有资源的统计信息

k get all -o wide
NAME                            READY   STATUS    RESTARTS   AGE   IP          NODE             NOMINATED NODE   READINESS GATES
pod/mybusybox                   1/1     Running   0          10m   10.1.1.14   docker-desktop   <none>           <none>
pod/mytomcat-6bfbf4c889-xqkg9   1/1     Running   0          39m   10.1.1.11   docker-desktop   <none>           <none>

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE     SELECTOR
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    42m     <none>
service/mytomcat     ClusterIP   10.102.120.90   <none>        8080/TCP   6m40s   app=mytomcat

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES   SELECTOR
deployment.apps/mytomcat   1/1     1            1           39m   tomcat       tomcat   app=mytomcat

NAME                                  DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES   SELECTOR
replicaset.apps/mytomcat-6bfbf4c889   1         1         1       39m   tomcat       tomcat   app=mytomcat,pod-template-hash=6bfbf4c889

第 7 步:curl 服务到达 tomcat pod 失败。 这是预期的输出吗?因为它说错误代码 404。我没有 tomcat 服务器的工作想法。

    curl http://mytomcat:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.37</h3></body></html>/home

任何人都可以告诉我哪里做错了,或者建议好的资源来简单地开始学习。

谢谢。

这根本不是 DNS Unreachable 问题或 DNS 问题。这是预料之中的,因为您尚未在 tomcat 上部署 war 文件,因此 HTTP Status 404 – Not Found

请参考这个问题,了解如何在 kubernetes 中的 tomcat 运行 上部署 war 文件。