代码改变世界

k8s系列---Service之ExternalName用法

2019-10-15 11:44  dribs  阅读(12710)  评论(0编辑  收藏  举报

需求:需要两个不同的namespace之间的不同pod可以通过name的形式访问

实现方式:

A:在其他pod内ping [svcname].[namespace] ping出来到结果就是svc的ip地址

B:通过externalname,把对方到[svcname].[namespace].svc.cluster.local,绑定到externalname定义到字符串上

1:创建A名称空间的pod

[root@k8s-m lihongxing]# cat myapp.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  namespace: cjy-test
spec:
  replicas: 1
  selector: #标签选择器
    matchLabels: #匹配的标签为
      app: myapp
      release: canary
  template:
    metadata:
      labels:
        app: myapp #和上面的myapp要匹配
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80

  

2:创建无头service,无头服务直接把请求解析到后端pod上,如果创建NodePort,ClusterIP那么后续验证就不要ping了,因为这个解析到是svc的IP,直接telnet加端口,或者curl 业务加端口验证

dig -t A myapp-svc.cjy-test.svc.cluster.local @172.16.132.10

  

[root@k8s-m lihongxing]# cat myapp-svc-headless.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myapp-svc
  namespace: cjy-test
spec:
  selector:
    app: myapp #挑选的pod还是myapp。一个pod可以有多个service
    release: canary
  clusterIP: None #None表示是无头service
  ports:
  - port: 39320 #service ip中的端口
    targetPort: 80 #容器ip中的端口

  

3:创建ExternalName

[root@k8s-m lihongxing]# cat myapp-svc-extername.yaml 
kind: Service
apiVersion: v1
metadata:
  name: myapp-svcname
  namespace: cjy-test
spec:
  type: ExternalName
  externalName: myapp-svc.cjy2-test.svc.cluster.local

  

在另一个名称空间的创建方式同上三步

两个名称空间都做完后,验证

[root@k8s-m ~]# kubectl get pods -n cjy-test
NAME                           READY   STATUS    RESTARTS   AGE
myapp-deploy-f4db5d79c-zdgvv   1/1     Running   0          116m
myapp-f4db5d79c-xfs9l          1/1     Running   0          47m
[root@k8s-m ~]# kubectl exec -it   myapp-f4db5d79c-xfs9l -n cjy-test  -- /bin/sh 
/ # nslookup myapp-svcname
nslookup: can't resolve '(null)': Name does not resolve

Name:      myapp-svcname
Address 1: 172.16.129.11 172-16-129-11.maypp.cjy2-test.svc.cluster.local
/ # ping myapp-svcname
PING myapp-svcname (172.16.129.11): 56 data bytes
64 bytes from 172.16.129.11: seq=0 ttl=64 time=0.534 ms
^C
--- myapp-svcname ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.534/0.534/0.534 ms

/ # nslookup myapp-svc.cjy2-test.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

Name:      myapp-svc.cjy2-test.svc.cluster.local
Address 1: 172.16.129.11 172-16-129-11.maypp.cjy2-test.svc.cluster.local

/ # exit
command terminated with exit code 127

[root@k8s-m ~]# kubectl get pods -n cjy2-test -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP              NODE              NOMINATED NODE   READINESS GATES
myapp-f4db5d79c-6mpsm   1/1     Running   0          50m   172.16.129.11   k8s-w1   <none>           <none>
[root@k8s-m ~]# kubectl get svc -n cjy2-test -o wide
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE    SELECTOR
maypp       ClusterIP   172.16.135.109   <none>        6381/TCP    101m   app=myapp
myapp-svc   ClusterIP   None             <none>        39321/TCP   55m    app=myapp,release=canary
[root@k8s-m ~]# kubectl get svc -n cjy-test
NAME            TYPE           CLUSTER-IP       EXTERNAL-IP                             PORT(S)     AGE
maypp           ClusterIP      172.16.134.116   <none>                                  6380/TCP    105m
myapp-svc       ClusterIP      None             <none>                                  39320/TCP   69m
myapp-svcname   ExternalName   <none>           myapp-svc.cjy2-test.svc.cluster.local   <none>      62m