31、k8s-Service的类型-第二种-HeadLiness(属于Cluseter IP类型的)-只支持域名访问

概念:在某些场景中,开发人员不想使用service提供的负载均衡功能、而希望自己来控制负载均衡策略、针对这种情况、kubernetes提供了HeadLiness service
这类service不会分配Cluster IP 、如果想要访问Service、只能通过service的域名访问

测试

1、创建HeadLiness的yaml文件:
-----------------------------------------------------
apiVersion: v1
kind: Service
metadata:
  name: service-headliness
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: None       #将clusterIP设置为None、即可创建headliness service
  type: ClusterIP
  ports:
  - port: 80            #service端口
    targetPort: 80      #pod端口
---------------------------------------------------------------------------------
2、创建headliness service: kubectl create -f service-headliness.yaml
3、查看headliness service: kubectl get svc -ndev -owide
------------------------------------------------------------------------------
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service-headliness   ClusterIP   None           <none>        80/TCP         50s   app=nginx-pod
#这里ClusterIP 为None 故没有分配ip
------------------------------------------------------------------------------------------------------
4、kubectl describe svc -ndev    #发现关联的pod还是有的、虽然没有分配IP

5、如何访问:
    ·先进入headliness service中的pod后台:
    ·查看pods:kubectl get pods -ndev
    ·进入其中一个pod:kubectl exec -it svc-deployment-6696798b78-bzdnr -ndev /bin/sh
    ·查看域名解析文件:cat /etc/resolv.conf    #记下nameserver和search
    ·---------------------------------------------------------
    nameserver 10.96.0.10    #这个IP就是域名解析服务器
    search dev.svc.cluster.local svc.cluster.local cluster.local
    options ndots:5
    -------------------------------------------------------------------------
    ·exit退出pod
    ·安装dig工具: yum install bind-utils -y
    ·访问headliness类型的服务:dig @10.96.0.10 service-headliness.dev.svc.cluster.local
    #解析:10.96.0.10 service-headliness.dev.svc.cluster.local    -->域名解析服务器ip headliness服务名.namespace名.默认值(可配置、不配置默认就是它)
--------------------------------------------------------------------------------------------
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.14 <<>> @10.96.0.10 service-headliness.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: 10505
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;service-headliness.dev.svc.cluster.local. IN A

;; ANSWER SECTION:
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.1.244
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.1.246
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.1.247

;; Query time: 7 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Mon Sep 04 22:39:07 CST 2023
;; MSG SIZE  rcvd: 237
-------------------------------------------------------------------------------------------------------

 

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