当前位置:   article > 正文

Kubernetes----ExternalName类型的Service

externalname

【原文链接】

一、ExternalName类型Service简介

externalName Service是k8s中一个特殊的service类型,它不需要指定selector去选择哪些pods实例提供服务,而是使用DNS CNAME机制把自己CNAME到你指定的另外一个域名上,你可以提供集群内的名字,比如mysql.db.svc这样的建立在db命名空间内的mysql服务,也可以指定http://mysql.example.com这样的外部真实域名。

二、ExternalName类型Service实例

编辑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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

使用如下命令创建资源

[root@master service]# kubectl apply -f external_name.yaml
namespace/dev created
service/search created
[root@master service]#
  • 1
  • 2
  • 3
  • 4

查看资源如下:

[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]#
  • 1
  • 2
  • 3
  • 4

此时,在集群内部的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:/#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

然后使用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]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

三、删除资源

使用如下命令删除即可

[root@master service]# kubectl delete -f external_name.yaml
namespace "dev" deleted
service "search" deleted
[root@master service]#
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/189795
推荐阅读
相关标签
  

闽ICP备14008679号