版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scylhy/article/details/85342452
关于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
- 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