赞
踩
externalName Service是k8s中一个特殊的service类型,它不需要指定selector去选择哪些pods实例提供服务,而是使用DNS CNAME机制把自己CNAME到你指定的另外一个域名上,你可以提供集群内的名字,比如mysql.db.svc这样的建立在db命名空间内的mysql服务,也可以指定http://mysql.example.com这样的外部真实域名。
编辑external_name.yaml文件如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Service
metadata:
name: search
namespace: dev
spec:
type: ExternalName
externalName: www.baidu.com
使用如下命令创建资源
[root@master service]# kubectl apply -f external_name.yaml
namespace/dev created
service/search created
[root@master service]#
查看资源如下:
[root@master service]# kubectl get service -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
search ExternalName <none> www.baidu.com <none> 11s
[root@master service]#
此时,在集群内部的pod中就可以通过 search.dev.svc.cluster.local 访问www.baidu.com了
下面验证一下,首先登录一个pod查看 /etc/resolve.conf
[root@master service]# kubectl exec -it pod/pc-deployment-5ffc5bf56c-9bg6w -n dev /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@pc-deployment-5ffc5bf56c-9bg6w:/#
root@pc-deployment-5ffc5bf56c-9bg6w:/# cat /etc/resolv.conf
nameserver 10.96.0.10
search dev.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
root@pc-deployment-5ffc5bf56c-9bg6w:/#
然后使用dig命令验证
[root@master service]# dig @10.96.0.10 search.dev.svc.cluster.local
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.9 <<>> @10.96.0.10 search.dev.svc.cluster.local
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62861
;; flags: qr aa rd; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;search.dev.svc.cluster.local. IN A
;; ANSWER SECTION:
search.dev.svc.cluster.local. 30 IN CNAME www.baidu.com.
www.baidu.com. 30 IN CNAME www.a.shifen.com.
www.a.shifen.com. 30 IN A 110.242.68.3
www.a.shifen.com. 30 IN A 110.242.68.4
;; Query time: 5 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Wed Apr 06 23:48:14 CST 2022
;; MSG SIZE rcvd: 219
[root@master service]#
使用如下命令删除即可
[root@master service]# kubectl delete -f external_name.yaml
namespace "dev" deleted
service "search" deleted
[root@master service]#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。