除了直接用cluster ip,以及上面说到的NodePort模式来访问Service,我们还可以用K8s的DNS来访问
# 我们前面装好的CoreDNS,来提供K8s集群的内部DNS访问
[root@master1 ~]# kubectl -n kube-system get deployment,pod|grep dns
deployment.apps/coredns 1/1 1 1 6d16h
pod/coredns-5787695b7f-42754 1/1 Running 0 2d18h
pod/node-local-dns-2k5jn 1/1 Running 0 6d16h
pod/node-local-dns-5rkcg 1/1 Running 0 6d16h
pod/node-local-dns-pwpb8 1/1 Running 0 6d16h
# coredns是一个DNS服务器,每当有新的Service被创建的时候,coredns就会添加该Service的DNS记录,然后我们通过serviceName.namespaceName就可以来访问到对应的pod了,下面来演示下:
[root@master1 ~]# kubectl run -it --rm busybox --image=busybox -- sh # --rm代表等我退出这个pod后,它会被自动删除,当作一个临时pod在用
If you don't see a command prompt, try pressing enter.
/ # ping nginx.default
PING nginx.default (10.68.142.234): 56 data bytes
64 bytes from 10.68.142.234: seq=0 ttl=64 time=0.108 ms
64 bytes from 10.68.142.234: seq=1 ttl=64 time=0.199 ms
64 bytes from 10.68.142.234: seq=2 ttl=64 time=0.147 ms
^C
--- nginx.default ping statistics ---
7 packets transmitted, 7 packets received, 0% packet loss
round-trip min/avg/max = 0.108/0.143/0.199 ms
/ # wget nginx.default
Connecting to nginx.default (10.68.142.234:80)
saving to 'index.html'
index.html 100% |*****************************************************************************************************************************| 612 0:00:00 ETA
'index.html' saved
/ # cat index.html
v11111