关于k8s解析不到pod dns信息

关于k8s解析不到pod dns信息

这其实是自己对k8s的dns解析不理解,service和pod在k8s中的dns记录形式不同, service是通过名字的,pod是用ip的(192-168-0-1的形式)。但是,通过pod-ip访问,很傻,已经知道ip,那还查什么,于是乎,又有了 pod-hostname.subdomain.ns.cluster.local的方式;需要在pod spec中配置 hostname和subdomain。

官网demo使用headless-svc,实际上用其他svc也可以,文末用clusterIP的svc演示。

  • busybox:1.28,latest版本不行
apiVersion: v1
kind: Service
metadata:
  name: default-subdomain
spec:
  selector:
    name: busybox
  clusterIP: None
  ports:
  - name: foo # Actually, no port is needed.
    port: 1234
    targetPort: 1234
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox1
  labels:
    name: busybox
spec:
  hostname: busybox-1
  subdomain: default-subdomain
  containers:
  - image: busybox:1.28
    command:
      - sleep
      - "3600"
    name: busybox
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox2
  labels:
    name: busybox
spec:
  hostname: busybox-2
  subdomain: default-subdomain
  containers:
  - image: busybox:1.28
    command:
      - sleep
      - "3600"
    name: busybox
  • service
[root@cce-demo1522483688765-00274 cka]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
http         ClusterIP   10.247.44.63     <none>        8080/TCP   3d
kubernetes   ClusterIP   10.247.0.1       <none>        443/TCP    3d
mydb         ClusterIP   10.247.6.172     <none>        80/TCP     2h
myservice    ClusterIP   10.247.148.200   <none>        80/TCP     2h
[root@cce-demo1522483688765-00274 cka]# kubectl exec -it busybox1 -- nslookup http
Server:    10.247.3.10
Address 1: 10.247.3.10 kube-dns.kube-system.svc.cluster.local

Name:      http
Address 1: 10.247.44.63 http.default.svc.cluster.local
[root@cce-demo1522483688765-00274 cka]# 
  • pod
[root@cce-demo1522483688765-00274 cka]# kubectl get pods -owide
NAME                    READY     STATUS    RESTARTS   AGE       IP            NODE
busybox1                1/1       Running   0          28m       172.16.0.32   192.168.0.151
busybox2                1/1       Running   0          21m       172.16.0.26   192.168.0.151
counter                 1/1       Running   0          1h        172.16.0.22   192.168.0.151
http-7767dd48cf-cpthr   1/1       Running   0          1d        172.16.0.31   192.168.0.151
http-7767dd48cf-f5mfd   1/1       Running   0          1d        172.16.0.29   192.168.0.151
http2-fbb647ddf-gv267   1/1       Running   0          1d        172.16.0.30   192.168.0.151
myapp-pod2              1/1       Running   2          2h        172.16.0.16   192.168.0.151
non-persistent-redis    1/1       Running   1          1h        172.16.0.20   192.168.0.151
task-pv-pod             1/1       Running   0          1h        172.16.0.19   192.168.0.151
[root@cce-demo1522483688765-00274 cka]# kubectl exec -it busybox1 -- nslookup 172-16-0-22.default.pod.cluster.local
Server:    10.247.3.10
Address 1: 10.247.3.10 kube-dns.kube-system.svc.cluster.local

Name:      172-16-0-22.default.pod.cluster.local
Address 1: 172.16.0.22
[root@cce-demo1522483688765-00274 cka]# 


root@lhys:~# kubectl exec -it busybox1 -- nslookup default-subdomain
Server:    10.247.3.10
Address 1: 10.247.3.10 coredns.kube-system.svc.cluster.local

Name:      default-subdomain
Address 1: 172.16.0.9 busybox-2.default-subdomain.default.svc.cluster.local
Address 2: 172.16.0.8 busybox-1.default-subdomain.default.svc.cluster.local
root@lhys:~# kubectl exec -it busybox1 -- nslookup busybox-2.default-subdomain
Server:    10.247.3.10
Address 1: 10.247.3.10 coredns.kube-system.svc.cluster.local

Name:      busybox-2.default-subdomain
Address 1: 172.16.0.9 busybox-2.default-subdomain.default.svc.cluster.local


使用clusterIP svc配置pod的hostname、subdomain

  1. 关键点在于为pod配置hostname和subdomain,subdomain为svc
  2. 并且保证svc的selector能够找到pod的label;比如下边例子中svc的selector key:value用run:hello-nginx,pods的label key:value用run:hello-nginx,因此svc能够找到pods

SVC:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2019-07-02T05:15:07Z"
  labels:
    app: ng-pods
  name: ng-pods
  namespace: default
spec:
  clusterIP: 10.101.41.59
  ports:
  - name: 80-8899
    port: 80
    protocol: TCP
    targetPort: 8899
  selector:
    run: hello-nginx  #重要,用来关联pods
  type: ClusterIP

POD:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: hello-nginx  #label 和svc的selector关联
  name: hello-nginx
spec:
  containers:
  - image: nginx
    name: hello-nginx
  restartPolicy: Always
  hostname: hello-nginx  #hostname
  subdomain: ng-pods  #配置的svc

测试:

root@node1:~# kubectl get svc
NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes        ClusterIP   10.96.0.1        <none>        443/TCP          2d21h
ng-pods           ClusterIP   10.101.41.59     <none>        80/TCP           9m37s
root@node1:~# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
busybox                       1/1     Running   69         2d21h
hello-nginx                   1/1     Running   0          6m16s
root@node1:~# kubectl exec -it busybox -- nslookup ng-pods
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      ng-pods
Address 1: 10.101.41.59 ng-pods.default.svc.cluster.local
root@node1:~# kubectl exec -it busybox -- nslookup hello-nginx.ng-pods
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      hello-nginx.ng-pods
Address 1: 10.244.0.14 10-244-0-14.my-nginx.default.svc.cluster.local
root@node1:~# 

你可能感兴趣的:(云计算/大数据,kubernetes)