前言

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

service的域名

完整的service域名解析是:..svc. 其中,servicename为service名称,namespace为service所处的命名空间,clusterdomain是k8s集群设计的域名后缀,默认为cluster.local。
一般的,在生产环境中,我们可以直接简写为.即可,后面的部分保持默认即可。如果pod与svc是在同一个命名空间,那么直接写svc即可,如 。

演示示例

下面,我们通过创建一个deployment和service,然后创建一个测试pod,在测试pod中通过访问service域名的形式访问应用,验证service域名是否正常。如下所示

# 创建一个deployment,有3个副本
cat > deployment-nginx.yaml << EOF     
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
EOF

#创建一个service,用于反向代理上面创建的deployment的pod
cat > svc-deployment-nginx.yaml << EOF
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
EOF
 
kubectl apply -f svc-deployment-nginx.yaml
kubectl apply -f deployment-nginx.yaml 

在pod中测试直接访问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>
/ # 

wget http://svc-deployment-nginx.default.svc.cluster.local:80		#完整的写法
wget http://svc-deployment-nginx.default:80			#带命名空间写法
wget http://svc-deployment-nginx:80					#如果pod与svc在同一个命名空间,可以将命名空间省略不写

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐