Kubernetes----HeadLess类型的Service

【原文链接】

一、Headless类型Service简介

在某些场景下,开发人员可能不想使用Service提供的负载均衡功能,而希望自己来控制负载均衡策略,针对这种情况,Kubernetes提供了Headless Service,这类Service不会分配ClusterIP,如果想要访问Service,只能通过service的域名进行查询

二、环境准备

编写deployment.yaml文件,内容如下:

apiVersion: v1
kind: Namespace
metadata:
  name: dev

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pc-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80

然后使用如下命令创建资源

[root@master service]# kubectl apply -f deployment.yaml
namespace/dev created
deployment.apps/pc-deployment created
[root@master service]#

查看创建的资源如下:

[root@master service]# kubectl get deploy,pod -n dev -o wide
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/pc-deployment   3/3     3            3           17m   nginx        nginx:1.17.1   app=nginx-pod

NAME                                 READY   STATUS    RESTARTS   AGE   IP             NODE    NOMINATED NODE   READINESS GATES
pod/pc-deployment-5ffc5bf56c-mqwst   1/1     Running   0          17m   10.244.2.191   node2   <none>           <none>
pod/pc-deployment-5ffc5bf56c-qbznm   1/1     Running   0          17m   10.244.1.53    node1   <none>           <none>
pod/pc-deployment-5ffc5bf56c-zxnsx   1/1     Running   0          17m   10.244.2.190   node2   <none>           <none>
[root@master service]#

三、创建Headless类型的Service

编辑headless.yaml文件,内容如下:

apiVersion: v1
kind: Service
metadata:
  name: service-headless
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80

使用如下命令创建服务

[root@master service]# kubectl apply -f headless.yaml
service/service-headless created
[root@master service]#

查看创建的资源

[root@master service]# kubectl get service,deployment,pod -n dev -o wide
NAME                       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/service-headless   ClusterIP   None         <none>        80/TCP    99s   app=nginx-pod

NAME                            READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
deployment.apps/pc-deployment   3/3     3            3           8m50s   nginx        nginx:1.17.1   app=nginx-pod

NAME                                 READY   STATUS    RESTARTS   AGE     IP             NODE    NOMINATED NODE   READINESS GATES
pod/pc-deployment-5ffc5bf56c-mqwst   1/1     Running   0          8m50s   10.244.2.191   node2   <none>           <none>
pod/pc-deployment-5ffc5bf56c-qbznm   1/1     Running   0          8m50s   10.244.1.53    node1   <none>           <none>
pod/pc-deployment-5ffc5bf56c-zxnsx   1/1     Running   0          8m50s   10.244.2.190   node2   <none>           <none>
[root@master service]#

此时服务因为没有ClusterIP,所以此时不能使用ip去访问了,只能使用域名访问了,即 service-headless.dev.svc.cluster.local,这里面格式是

[service的名字].[命名空间].svc.cluster.local

首先登录pod查看域名解析

[root@master service]# kubectl exec -it pod/pc-deployment-5ffc5bf56c-mqwst -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:/#

然后查看域名解析,具体查看域名的命令如下

[root@master service]# dig @10.96.0.10 service-headless.dev.svc.cluster.local

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.9 <<>> @10.96.0.10 service-headless.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: 62573
;; 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-headless.dev.svc.cluster.local.        IN A

;; ANSWER SECTION:
service-headless.dev.svc.cluster.local. 30 IN A 10.244.2.190
service-headless.dev.svc.cluster.local. 30 IN A 10.244.1.53
service-headless.dev.svc.cluster.local. 30 IN A 10.244.2.191

;; Query time: 0 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Mon Apr 04 11:57:01 CST 2022
;; MSG SIZE  rcvd: 229

[root@master service]#

你可能感兴趣的:(Kubernetes,kubernetes)