zl程序教程

您现在的位置是:首页 >  其他

当前栏目

k8s集群中service的域名解析、pod的域名解析

2023-04-18 16:15:59 时间

前言

在k8s集群中,service和pod都可以通过域名的形式进行相互通信,换句话说,在k8s集群内,通过service和pod的域名,可以直接访问内部应用,不必在通过service ip地址进行通信,一般的,我们创建service的时候不建议指定service的clusterIP,而是让k8s自动为service分配一个clusterIP,这样,service的IP是自动分配,但是service名字总是固定的吧,这样在集群内部就可以直接通过service的域名来连接即可,如前端pod应用直接通过service域名来连接后端pod。

service的域名

<servicename>.<namespace>.svc.<clusterdomain> 其中,servicename为service名称,namespace为service所处的命名空间,clusterdomain是k8s集群设计的域名后缀,默认为cluster.local
演示示例:
下面,我们通过创建一个deployment和service,然后创建一个测试pod,在测试pod中通过访问service域名的形式访问应用,验证service域名是否正常。如下所示:

# 创建一个deployment,有3个副本
[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
#创建一个service,用于反向代理上面创建的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
#创建一个pod用于测试
[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]# 
# 在测试pod中直接访问service的域名
[root@master service]# kubectl exec -it pod-command -- /bin/sh			#进入到测试pod中
/ # wget http://svc-deployment-nginx.default.svc.cluster.local:80		#这个pod没有curl命令,所以通过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文件,说明通过service域名访问是正常的
<!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>
/ # 

以上,说明,在pod内部通过访问service域名的形式访问其他服务,service的域名解析是正常的。
注意:不要在宿主机上curl service的域名,这样是访问不通的,因为service的域名解析是k8s集群内部的,不是外部所主机的。service的域名解析是供k8s集群内部应用之间进行访问的。

pod的域名解析(存疑)

pod的DNS域名格式为:<pod-ip>.<namespace>.pod.<clusterdomain> ,其中,pod-ip需要使用-将ip直接的点替换掉,namespace为pod所在的命名空间,clusterdomain是k8s集群设置的域名后缀,一般默认为 cluster.local ,演示如下:10-244-1-223.default.pod.cluster.local
对应deployment、deamonset等创建的pod,还可以<pod-ip>.<deployment-name>.<namespace>.svc.<clusterdomain> 访问。
对于StatefulSet创建的pod,statefulset.spec.serviceName字段解释,其pod的域名为:pod-specific-string.serviceName.default.svc.cluster.local

总结

service的域名解析很重要,我们只需要在k8s集群内部访问service的域名即可,因为service的名称总是固定的,既然是固定的,那么可以直接通过访问service的域名方式来访问service。