关于kubernetes pods dns解析问题

时间:2018-10-26 08:01:25

标签: kubernetes coredns

我的pod名称空间是kube-test,但是服务名称是此默认值

[root@kube01 iyunv]# kubectl get pods -n kube-test
NAME                                              READY     STATUS    RESTARTS   AGE
business-org-deployment-c84b7d9f6-55vpl           1/1       Running   0          21h

使用defaulc解析(business-org.default.svc.cluster.local)

[root@kube01 iyunv]# kubectl exec business-org-deployment-c84b7d9f6-55vpl -n kube-apabi -it /bin/bash

[root@business-org-deployment-c84b7d9f6-55vpl /]# ping business-org.default.svc.cluster.local
PING business-org.default.svc.cluster.local (10.254.56.71) 56(84) bytes of data.
64 bytes from business-org.default.svc.cluster.local (10.254.56.71): icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from business-org.default.svc.cluster.local (10.254.56.71): icmp_seq=2 ttl=64 time=0.047 ms
64 bytes from business-org.default.svc.cluster.local (10.254.56.71): icmp_seq=3 ttl=64 time=0.049 ms
64 bytes from business-org.default.svc.cluster.local (10.254.56.71): icmp_seq=4 ttl=64 time=0.053 ms

无法使用kube-test(business-org.kube-test.svc.cluster.local)进行解析

[root@business-org-deployment-c84b7d9f6-55vpl /]# ping business-org.kube-test.svc.cluster.local
ping: business-org.kube-test.svc.cluster.local: Name or service not known

cat business-org.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: business-org
  name: business-org
spec:
  ports:
  - port: 9051
    protocol: TCP
    targetPort: 9051
  selector:
    app: business-org
  type: ClusterIP
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: business-org-deployment
  namespace: kube-test
spec:
  replicas: 1
  template: 
    metadata:
      labels:
        app: business-org
    spec:
      containers:
      - name: business-org
        image: harbor.test.com/test/business-org:201810251757_11f8bd9
        ports:
        - containerPort: 9051

1 个答案:

答案 0 :(得分:2)

ping: business-org.kube-test.svc.cluster.local: Name or service not known

明确表示在命名空间buisness-org中没有服务kube-test

以下YAML将解决此问题:

apiVersion: v1
kind: Service
metadata:
  namespace: kube-test <---
  labels:
    app: business-org
  name: business-org
spec:
  ports:
  - port: 9051
    protocol: TCP
    targetPort: 9051
  selector:
    app: business-org
  type: ClusterIP