k8s系列---Service之ExternalName用法
Posted dribs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s系列---Service之ExternalName用法相关的知识,希望对你有一定的参考价值。
需求:需要两个不同的namespace之间的不同pod可以通过name的形式访问
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
[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.sai.corp <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
以上是关于k8s系列---Service之ExternalName用法的主要内容,如果未能解决你的问题,请参考以下文章