34、k8s-service类型-第五种-ExternalName-将外部服务接入到集群内部

概念:ExternalName类型的service用于引入集群外的服务、它通过externalname属性指定外部一个服务的地址、然后在集群内部访问此service就可以访问到外部的服务了

案例

1、新建ExternalName的yaml文件:vim service-externalname.yaml
-------------------------------------------------------
apiVersion: v1
kind: Service
metadata:
  name: service-externalname
  namespace: dev
spec:
  type: ExternalName
  externalName: www.baidu.com
-----------------------------------------------------------
2、创建externalname:kubectl create -f service-externalname.yaml
3、查看:kubectl get svc service-externalname -ndev
----------------------------------------------------------------
NAME                   TYPE           CLUSTER-IP   EXTERNAL-IP     PORT(S)   AGE
service-externalname   ExternalName   <none>       www.baidu.com   <none>    46s

#其中EXTERNAL-IP    是指向外部的地址
---------------------------------------------------------------------------------
4、查看详情:kubectl describe svc service-externalname -ndev

5、域名解析访问:dig @10.96.0.10 service-externalname.dev.svc.cluster.local
    ·注意这个10.96.0.10 IP是域名解析服务通过进到pod里面获取:查看pod里面的文件:cat /etc/resolv.conf
-------------------------------------------------------------------------------------
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.14 <<>> @10.96.0.10 service-externalname.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: 10492
;; 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:
;service-externalname.dev.svc.cluster.local. IN A

;; ANSWER SECTION:
service-externalname.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       14.119.104.254
www.a.shifen.com.       30      IN      A       14.119.104.189

;; Query time: 35 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Mon Sep 04 23:22:33 CST 2023
;; MSG SIZE  rcvd: 247
---------------------------------------------------------------------------------------------

 

posted @ 2024-07-01 00:15  little小新  阅读(53)  评论(0编辑  收藏  举报