본문 바로가기
INFRA/DevOps

[K8s] Headless Service

by BTC_프로틴 2022. 12. 6.

Headless Service란?

  • ClusterIP가 없는 서비스로 단일 진입점이 필요 없을 때 사용
  • Service와 연결된 pod의 endpoint로 DNS 레코드가 생성됨 (DNS Resolving Service 지원)
  • Pod의 DNS 주소 : pod-ip-addr.namespace.pod.cluster.local

<이미지 출처 :
https://www.youtube.com/watch?v=ilQSgu8qt0o&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=27 >

  • definition
apiVersion: v1
kind: Service
metadata:
  name: headless-service
spec:
  type: ClusterIP
  clusterIP: None       #clusterIP를 none으로 설정하면 Headless service
  selector:
    app: webui
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

 

[ Hands - on ]

  • Deployment로 pod 생성
vi deploy-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webui
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
kubectl create -f deploy-nginx.yaml
  • pod 생성 확인
kubectl get pods -o wide 
NAME                     READY   STATUS    RESTARTS   AGE     IP              NODE                NOMINATED NODE   READINESS GATES
webui-6d75c5dd9b-5d49s   1/1     Running   0          3m17s   192.168.221.7   node2.example.com   <none>           <none>
webui-6d75c5dd9b-c6d7f   1/1     Running   0          3m17s   192.168.11.70   node1.example.com   <none>           <none>
webui-6d75c5dd9b-svz44   1/1     Running   0          3m17s   192.168.11.69   node1.example.com   <none>           <none>
  • Headless service yaml 파일 생성 및 실행
vi headless-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  name: headless-service
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    app: webui
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
kubectl create -f headless-nginx.yaml
  • Headless service 확인
kubectl get service
NAME               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
headless-service   ClusterIP   None         <none>        80/TCP    43s
kubernetes         ClusterIP   10.96.0.1    <none>        443/TCP   7m18s
kubectl describe service headless-service
Name:              headless-service
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=webui
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                None
IPs:               None
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         192.168.11.69:80,192.168.11.70:80,192.168.221.7:80
Session Affinity:  None
Events:            <none>
  • DNS resolving service 확인을 위한 pod 생성
kubectl run testpod -it --image=centos:7 /bin/bash
  • Pod Endpoint DNS service 조회
cat /etc/resolve.conf
search default.svc.cluster.local svc.cluster.local cluster.local 1k4nxixadzqeld12xo3irisf2e.syx.internal.cloudapp.net
nameserver 10.96.0.10
options ndots:5
curl 192-168-11-69.default.pod.cluster.local

 

 

참고 :

https://www.youtube.com/watch?v=ilQSgu8qt0o&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=27

댓글