K8S headless 服务

  1. 定义
    在某些场景下,开发人员可能不想使用 Service 提供的负载均衡功能,而希望自己来控制负载均衡策略,针对这种情况,Kubernetes 提供了 Headless Service,这类 Service 不会分配 ClusterIP,如果想要访问 Service,只能通过 service 的域名进行查询。
    因为没有 ClusterIP,kube-proxy 并不处理此类服务,因为没有 load balancing 或 proxy 代理设置,在访问服务的时候回返回后端的全部的 Pods IP 地址,主要用于开发者自己根据 pods 进行负载均衡器的开发 (设置了 selector)。

  2. 创建 headless 服务
    将 service spec 中 的 clusterIP 字段设置为 None 会使服务成为 headless 服务,因为 Kubernetes 不会为其分配集群 IP,DNS 服务器将返回 podIP 而不是单个服务 IP。

open1-headless.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: dev
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: open1-headless
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: open1-headless
  template:
    metadata:
      labels:
        app: open1-headless
    spec:
      containers:
      - name: open1-headless
        image: openresty:1.21.4.1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: open1-headless
  namespace: dev
spec:
  selector:
    app: open1-headless
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
open2-headless.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: open2-headless
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: open2-headless
  template:
    metadata:
      labels:
        app: open2-headless
    spec:
      containers:
      - name: open2-headless
        image: openresty:1.21.4.1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: open2-headless
  namespace: dev
spec:
  selector:
    app: open2-headless
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80

查看

[root@master ~/openresty]#kubectl get pods,svc,deploy -o wide -n dev
NAME                                  READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
pod/open1-headless-79b845f7fc-bkv4g   1/1     Running   0          14s   10.244.0.8    master   <none>           <none>
pod/open1-headless-79b845f7fc-jqtqv   1/1     Running   0          14s   10.244.1.9    node01   <none>           <none>
pod/open1-headless-79b845f7fc-m24x2   1/1     Running   0          14s   10.244.2.8    node02   <none>           <none>
pod/open2-headless-69bb4dcf5c-l9gtc   1/1     Running   0          14s   10.244.1.10   node01   <none>           <none>
pod/open2-headless-69bb4dcf5c-n6bdn   1/1     Running   0          14s   10.244.0.9    master   <none>           <none>
pod/open2-headless-69bb4dcf5c-qw8mr   1/1     Running   0          14s   10.244.2.9    node02   <none>           <none>

NAME                     TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/open1-headless   ClusterIP   None         <none>        80/TCP    15s   app=open1-headless
service/open2-headless   ClusterIP   None         <none>        80/TCP    14s   app=open2-headless

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS       IMAGES               SELECTOR
deployment.apps/open1-headless   3/3     3            3           15s   open1-headless   openresty:1.21.4.1   app=open1-headless
deployment.apps/open2-headless   3/3     3            3           14s   open2-headless   openresty:1.21.4.1   app=open2-headless
  1. 访问服务

headless 服务因为没有 ClusterIP,所以不能使用 ip 去访问了,只能使用域名,而且只能在容器内部生效,域名格式:
[service的名字].[命名空间].svc.cluster.local

进入容器

[root@master ~]#kubectl exec -it open1-headless-79b845f7fc-m24x2 bash -n dev
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@open1-headless-79b845f7fc-m24x2 /]# cat /etc/resolv.conf 
nameserver 10.96.0.10
search dev.svc.cluster.local svc.cluster.local cluster.local localdomain
options ndots:5

域名解析

[root@open1-headless-79b845f7fc-m24x2 /]# dig @10.96.0.10 open1-headless.dev.svc.cluster.local

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

;; ANSWER SECTION:
open1-headless.dev.svc.cluster.local. 30 IN A   10.244.1.9
open1-headless.dev.svc.cluster.local. 30 IN A   10.244.2.8
open1-headless.dev.svc.cluster.local. 30 IN A   10.244.0.8

;; Query time: 1 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Sat Aug 20 05:34:34 UTC 2022
;; MSG SIZE  rcvd: 221

通过域名解析可以看到三个后端 podIP

[root@open1-headless-79b845f7fc-m24x2 /]# nslookup open1-headless 10.96.0.10 
Server:     10.96.0.10
Address:    10.96.0.10#53

Name:   open1-headless.dev.svc.cluster.local
Address: 10.244.1.9
Name:   open1-headless.dev.svc.cluster.local
Address: 10.244.0.8
Name:   open1-headless.dev.svc.cluster.local
Address: 10.244.2.8

通过域名访问另一个服务

[root@open1-headless-79b845f7fc-m24x2 /]# telnet open2-headless 80
Trying 10.244.2.9...
Connected to open2-headless.
Escape character is '^]'.
^CConnection closed by foreign host.
[root@open1-headless-79b845f7fc-m24x2 /]# ping open2-headless -c 3
PING open2-headless.dev.svc.cluster.local (10.244.1.10) 56(84) bytes of data.
64 bytes from 10-244-1-10.open2-headless.dev.svc.cluster.local (10.244.1.10): icmp_seq=1 ttl=62 time=0.438 ms
64 bytes from 10-244-1-10.open2-headless.dev.svc.cluster.local (10.244.1.10): icmp_seq=2 ttl=62 time=0.454 ms
64 bytes from 10-244-1-10.open2-headless.dev.svc.cluster.local (10.244.1.10): icmp_seq=3 ttl=62 time=0.470 ms

--- open2-headless.dev.svc.cluster.local ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.438/0.454/0.470/0.013 ms
  1. 详细配置
apiVersion: v1
kind: Service
metadata:
  name: open-headless
  namespace: dev
spec:
  clusterIP: None
  ports:
  - name: open-headless
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: open-headless
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: open-headless 
  namespace: dev
  labels:
    app: open-headless
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: open-headless
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: open-headless
    spec:
      containers:
      - name: open-headless
        image: openresty:1.21.4.1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: open-headless
          protocol: TCP
        resources:
          limits:
            cpu: 1000m
            memory: 1024Mi
          requests:
            cpu: 200m
            memory: 128Mi
        livenessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 5
          timeoutSeconds: 5 
          periodSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        readinessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 5
          timeoutSeconds: 5
          periodSeconds: 5
          successThreshold: 1
          failureThreshold: 5
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容