(1). kube-dns
kubernetes提供了service的概念可以通过VIP访问pod提供的服务,但是在使用的时候还有一个问题:怎么知道某个应用的 VIP?
比如我们有两个应用:app和db,每个应用通过service暴露出端口提供服务,app需要连接到db应用,我们只知道db应用的名称,但是并不知道它的VIP地址. 一直在想:app应用能否直接使用服务的名字直接访问?我们都知道,通过名称到IP的解析叫DNS,而kubernetes也提供了kube-dns来解决这个问题.
(2). 查看kube-system namespace是否有kube-dns
# 查看所有的命名空间
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 4d12h
kube-node-lease Active 4d12h
kube-public Active 4d12h
kube-system Active 4d12h
# 查看kube-system namespace下所有的内容(pod/service)
# 你会发现有一个service(kube-dns),它的CLUSTER-IP为:10.1.0.10
[root@master ~]# kubectl get all -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-bccdc95cf-9vfbh 1/1 Running 11 4d12h
pod/coredns-bccdc95cf-kw752 1/1 Running 11 4d12h
pod/etcd-master 1/1 Running 11 4d12h
pod/kube-apiserver-master 1/1 Running 11 4d12h
pod/kube-controller-manager-master 1/1 Running 22 4d12h
pod/kube-flannel-ds-7p25f 1/1 Running 11 4d11h
pod/kube-flannel-ds-946hf 1/1 Running 12 4d11h
pod/kube-flannel-ds-x8xhj 1/1 Running 11 4d12h
pod/kube-proxy-949bj 1/1 Running 12 4d11h
pod/kube-proxy-g7pxw 1/1 Running 11 4d11h
pod/kube-proxy-mf658 1/1 Running 11 4d12h
pod/kube-scheduler-master 1/1 Running 22 4d12h
pod/kubernetes-dashboard-fb6fb4cdd-h4wnj 1/1 Running 13 4d10h
# kube-dns
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kube-dns ClusterIP 10.1.0.10 <none> 53/UDP,53/TCP,9153/TCP 4d12h
service/kubernetes-dashboard NodePort 10.1.167.148 <none> 443:30001/TCP 4d10h
(3). 查看default namespace 下所有的pod和service
default namespace有两类型的Pod(hello-world和web), 其中,hello-world pod有相应对应的service(service/hello-world).
那么需要求证的是:web pod能否访问:hello-world的service呢? 即:在web容器内部,能否:ping hello-world(service/hello).
查看能否通过service名称找到IP(10.1.232.6)
# 查看default命名
[root@master ~]# kubectl get pods,svc -n default
NAME READY STATUS RESTARTS AGE
pod/hello-world-5f8d77c9f7-589kd 1/1 Running 3 9h
pod/hello-world-5f8d77c9f7-hg5fx 1/1 Running 4 9h
pod/web-697d9c7964-hxpqh 1/1 Running 1 6h16m
pod/web-697d9c7964-sd4ww 1/1 Running 1 6h16m
pod/web-697d9c7964-x2f4d 1/1 Running 1 6h16m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello-world NodePort 10.1.232.6 <none> 9090:31355/TCP 35h
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 4d12h
(4). 通过Service名称访问服务
# 进入web pod内部
[root@master ~]# kubectl exec -it web-697d9c7964-hxpqh /bin/bash
# 查看web pod容器的DNS解析(发现:nameserver指向了kube-system namespace下的kube-dns)
[root@web-697d9c7964-hxpqh /]# cat /etc/resolv.conf
nameserver 10.1.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
# 尝试ping Service的名称
[root@web-697d9c7964-hxpqh /]# ping hello-world
PING hello-world.default.svc.cluster.local (10.1.232.6) 56(84) bytes of data.
64 bytes from hello-world.default.svc.cluster.local (10.1.232.6): icmp_seq=1 ttl=127 time=985 ms
# 访问service名称(VIP)
# 实际在定义service的时候,可以不用定义:port,默认就是:80,这样就可以不用填端口
# service发布时,是会在宿主机(Node)上创建相应的监听端口,80端口只能被一个service所占用.
[root@web-697d9c7964-hxpqh /]# curl http://hello-world:9090/hello
Hello World-3.0.0-SNAPSHOT!!!test-hello
(5). 总结
在K8S容器内部,就可以直接通过服务(service)名称直接访问.
发布Service时,是会在Node(宿主机)上绑定端口,而80端口只能在一台宿主机上进行绑定,也就是只能被一个Service所使用.
缺陷:在访问时,是需要service名称+port来访问.