前言
在k8s集群中,service和pod都可以通過(guò)域名的形式進(jìn)行相互通信,換句話(huà)說(shuō),在k8s集群內(nèi),通過(guò)service和pod的域名,可以直接訪(fǎng)問(wèn)內(nèi)部應(yīng)用,不必在通過(guò)service ip地址進(jìn)行通信,一般的,我們創(chuàng)建service的時(shí)候不建議指定service的clusterIP,而是讓k8s自動(dòng)為service分配一個(gè)clusterIP,這樣,service的IP是自動(dòng)分配,但是service名字總是固定的吧,這樣在集群內(nèi)部就可以直接通過(guò)service的域名來(lái)連接即可,如前端pod應(yīng)用直接通過(guò)service域名來(lái)連接后端pod。
service的域名
完整的service域名解析是:<servicename>.<namespace>.svc.<clusterdomain>
其中,servicename為service名稱(chēng),namespace為service所處的命名空間,clusterdomain是k8s集群設(shè)計(jì)的域名后綴,默認(rèn)為cluster.local。
一般的,在生產(chǎn)環(huán)境中,我們可以直接簡(jiǎn)寫(xiě)為<servicename>.<namespace>
即可,后面的部分保持默認(rèn)即可。如果pod與svc是在同一個(gè)命名空間,那么直接寫(xiě)svc即可,如 <servicename>
。
#查看k8s集群設(shè)置的域名后綴
cat /opt/kubernetes/config/kubelet-config.yml | grep -i clusterDomain #二進(jìn)制安裝的k8s集群,可以這樣查看
cat /etc/kubernetes/kubelet.conf #kubeadm安裝的k8s集群,各個(gè)節(jié)點(diǎn)的kubelet.conf文件中的字段clusterDomain
kubectl -n kube-system get cm coredns -oyaml #coredns cm里面也可以看到
kubectl exec -it deployment-busybox-567674bd67-lmrgw -- cat /etc/resolv.conf #直接看pod里面的resolv.conf文件亦可
演示示例:
下面,我們通過(guò)創(chuàng)建一個(gè)deployment和service,然后創(chuàng)建一個(gè)測(cè)試pod,在測(cè)試pod中通過(guò)訪(fǎng)問(wèn)service域名的形式訪(fǎng)問(wèn)應(yīng)用,驗(yàn)證service域名是否正常。如下所示:
# 創(chuàng)建一個(gè)deployment,有3個(gè)副本
[root@master service]# vim deployment-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
env: dev
tiar: front
name: deployment-nginx
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:1.7.9
imagePullPolicy: IfNotPresent
name: nginx-container
ports:
- containerPort: 80
name: http
protocol: TCP
restartPolicy: Always
#創(chuàng)建一個(gè)service,用于反向代理上面創(chuàng)建的deployment的pod
[root@master service]# vim svc-deployment-nginx.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: svc-deployment-nginx
namespace: default
spec:
ports:
- name: nginx-port
nodePort: 30080
port: 80
protocol: TCP
targetPort: http
selector:
app: nginx
type: NodePort
#創(chuàng)建一個(gè)pod用于測(cè)試
[root@master service]# cat ../pod/pod-busybox.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-command
labels:
env: dev
namespace: default
spec:
nodeName: node2
containers:
- image: busybox
name: busybox-container
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;"]
resources:
limits:
cpu: 2
memory: 2G
requests:
cpu: 1
memory: 500M
[root@master service]#
# 在測(cè)試pod中直接訪(fǎng)問(wèn)service的域名
[root@master service]# kubectl exec -it pod-command -- /bin/sh #進(jìn)入到測(cè)試pod中
/ # wget http://svc-deployment-nginx.default.svc.cluster.local:80 #這個(gè)pod沒(méi)有curl命令,所以通過(guò)wget命令下載
Connecting to svc-deployment-nginx.default.svc.cluster.local:80 (10.111.193.190:80) #下載成功
saving to 'index.html'
index.html 100% |*******************************************************************************************************************************************************************************************| 612 0:00:00 ETA
'index.html' saved
/ # cat index.html #下載成功,這是nginx的index文件,說(shuō)明通過(guò)service域名訪(fǎng)問(wèn)是正常的
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
/ #
wget http://svc-deployment-nginx.default.svc.cluster.local:80 #完整的寫(xiě)法
wget http://svc-deployment-nginx.default:80 #帶命名空間寫(xiě)法
wget http://svc-deployment-nginx:80 #如果pod與svc在同一個(gè)命名空間,可以將命名空間省略不寫(xiě)
以上,說(shuō)明,在pod內(nèi)部通過(guò)訪(fǎng)問(wèn)service域名的形式訪(fǎng)問(wèn)其他服務(wù),service的域名解析是正常的。
注意:不要在宿主機(jī)上curl service的域名,這樣是訪(fǎng)問(wèn)不通的,因?yàn)閟ervice的域名是由k8s集群dns 這個(gè)pod解析的,不是外部宿主機(jī)的dns服務(wù)器解析的。所以,你直接在宿主機(jī)命令行curl service域名,肯定是行不通的。service的域名解析是供k8s集群內(nèi)部pod應(yīng)用之間進(jìn)行訪(fǎng)問(wèn)的。換句話(huà)說(shuō),service域名僅供pod內(nèi)使用。
pod的域名解析
pod的DNS域名格式為:<pod-ip>.<namespace>.pod.<clusterdomain>
,其中,pod-ip
需要使用-將ip直接的點(diǎn)替換掉,namespace為pod所在的命名空間,clusterdomain是k8s集群設(shè)置的域名后綴,一般默認(rèn)為 cluster.local
,如果沒(méi)有改變k8s集群默認(rèn)的域名后綴,則可以省略該后綴不寫(xiě)。除此之外,其他的均不可省略,這一點(diǎn)與svc域名有所不同。
演示如下:
#進(jìn)入default命名空間的busybox pod里面,測(cè)試下載文件
kubectl -n default exec -it deployment-busybox-567674bd67-lmrgw -- sh
wget 10-244-166-167.helm.pod.cluster.local:80 #可以正常下載,這里下載的是helm命名空間里的IP為10.244.166.167的pod
wget 10-244-166-167.helm.pod:80 #可以正常下載,這里把k8s集群設(shè)置的域名后綴默認(rèn)省略了
wget 10-244-166-143.default.pod:80 #可以正常下載,這里下載的是default命名空間里的IP為10.244.166.143的pod
wget 10-244-166-143.default:80 #報(bào)錯(cuò)了,錯(cuò)誤寫(xiě)法,說(shuō)明不能省略pod關(guān)鍵字
wget 10-244-166-143:80 #報(bào)錯(cuò)了,錯(cuò)誤寫(xiě)法,說(shuō)明不能省略命名空間和pod關(guān)鍵字
對(duì)應(yīng)deployment、deamonset等創(chuàng)建的pod,還可以<pod-ip>.<deployment-name>.<namespace>.svc.<clusterdomain>
訪(fǎng)問(wèn)。(這個(gè)一直測(cè)試失敗,不指定是不是書(shū)中寫(xiě)錯(cuò)了,所以這點(diǎn)存疑)
對(duì)于StatefulSet創(chuàng)建的pod,statefulset.spec.serviceName字段解釋如下:
[root@matser ~]# kubectl explain statefulset.spec.serviceName
KIND: StatefulSet
VERSION: apps/v1
FIELD: serviceName <string>
DESCRIPTION:
serviceName is the name of the service that governs this StatefulSet. This
service must exist before the StatefulSet, and is responsible for the
network identity of the set. Pods get DNS/hostnames that follow the
pattern: pod-specific-string.serviceName.default.svc.cluster.local where
"pod-specific-string" is managed by the StatefulSet controller.
[root@matser ~]#
也就是說(shuō)sts創(chuàng)建的pod,其pod的域名為:pod-specific-string.serviceName.default.svc.cluster.local
,而pod-specific-string就是pod的名稱(chēng)。
例如:redis-sts-0.redis-svc.default.svc.cluster.local:6379,redis-sts-1.redis-svc.default.svc.cluster.local:6379,redis-sts-2.redis-svc.default.svc.cluster.local:6379,redis-sts-3.redis-svc.default.svc.cluster.local:6379,redis-sts-4.redis-svc.default.svc.cluster.local:6379,redis-sts-5.redis-svc.default.svc.cluster.local:6379
,pod里面的應(yīng)用程序就可以拿這串字符串去連接Redis集群了。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-790600.html
總結(jié)
以上,service的域名解析很重要,我們只需要在k8s集群內(nèi)部的pod里面連接訪(fǎng)問(wèn)service的域名即可,因?yàn)閟ervice的名稱(chēng)總是固定的,既然是固定的,那么可以直接通過(guò)訪(fǎng)問(wèn)service的域名方式來(lái)訪(fǎng)問(wèn)對(duì)應(yīng)的service即可。
至于普通pod的域名解析,可以不用太過(guò)在乎,了解即可,因?yàn)閜od的生命周期是不固定的,隨時(shí)都可能消亡,也就是說(shuō)pod IP隨時(shí)可能變化,所以根本不會(huì)使用pod域名訪(fǎng)問(wèn)pod應(yīng)用。
對(duì)應(yīng)StatefulSet 創(chuàng)建的有狀態(tài)的pod,其pod域名解析是固定格式的,這個(gè)要記著,pod-specific-string.serviceName.default.svc.cluster.local
,pod-specific-string就是pod的名稱(chēng)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-790600.html
到了這里,關(guān)于k8s集群中service的域名解析、pod的域名解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!