Headless Service trong Kubernetes là gì ?

Headless Service trong Kubernetes là gì ?Cuongquach.com | Thông thường khi chúng ta muốn truy cập các ứng dụng trong Kubernetes, chúng ta sẽ khởi tạo một Service để load-balance lưu lượng xuống các Pod ứng dụng. Nhưng có cách nào giúp chúng ta có thể truy cập trực tiếp các Pod IP thông qua DNS endpoint nội bộ không ? Có thể câu trả lời là “Headless Service” trong Kubernetes đấy.

headless-service-kubernetes

Headless Service là gì trong Kubernetes ?

Đôi khi bạn sẽ chẳng cần tính năng Service ClusterIP Load-balancing hoặc một địa chỉ IP Service cụ thể. Thay vào đó bạn có mong muốn được truy cập trực tiếp đến các dịch vụ của Pod thay vì thông qua một lớp Proxy thường thấy với Service abstraction.

Bạn chỉ cần cấu hình phần “.spec.clusterIP” là “None” là bạn đã quy định Service của bạn là loại Headless. Khi đó Cluster IP sẽ không được cấp phát cho Service mà bạn khai báo, kube-proxy sẽ không xử lý đối tượng Service của bạn. Lúc này DNS của Service sẽ trả về thông tin là các địa chỉ IP của Pod khớp với Selector.

Cấu hình Headless Service trong Kubernetes

Chúng ta sẽ cùng lab để hiểu hơn về loại Service Headless :

  • Tạo một Deployment Nginx với 4 pod.
  • Tạo một ClusterIP Service để có thể truy cập cân bằng tải 4 pod Nginx thông qua DNS ClusterIP Service với 1 IP cụ thể.
  • Tạo một Headless Service để có thể truy cập cân bằng tải 4 Pod Nginx thông qua DNS Headless Service đến trực tiếp các IP Private của Pod.

Chúng ta tạo ra một Deployment Nginx với Replica là 4, tương đương 4 Pod.

# vi deployment-nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 80

# kubectl apply -f deployment-nginx.yaml
deployment.apps/nginx created

Kiểm tra các Pod Nginx đang chạy nào.

# kubectl get pods -l app=nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-59c9f8dff-8rgbs 1/1 Running 0 39s 10.1.0.7 docker-desktop <none> <none>
nginx-59c9f8dff-9r42j 1/1 Running 0 39s 10.1.0.8 docker-desktop <none> <none>
nginx-59c9f8dff-fktcq 1/1 Running 0 39s 10.1.0.10 docker-desktop <none> <none>
nginx-59c9f8dff-s4vnw 1/1 Running 0 39s 10.1.0.9 docker-desktop <none> <none>

Kiểm tra các Pod IP.

# kubectl get pods -l app=nginx -o yaml | grep podIP:
podIP: 10.1.0.7
podIP: 10.1.0.8
podIP: 10.1.0.10
podIP: 10.1.0.9

Giờ ta sẽ tạo một Service với loại mặc định là ClusterIP, khai báo listen ở port 80 (nginx) TCP.

# vi svc-clusterip-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  name: svc-clusterip-nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: nginx

# kubectl apply -f svc-clusterip-nginx.yaml
service/svc-clusterip-nginx created

Kiểm tra thông tin Service Cluster IP nginx.

# kubectl get svc svc-clusterip-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-clusterip-nginx ClusterIP 10.106.79.1 <none> 80/TCP 14s

Chúng ta tạo tiếp một Service với loại là Headless Service, khai báo listen ở port 80 (nginx) TCP.

# vi svc-headless-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  name: svc-headless-nginx
  labels:
    app: nginx
spec:
  clusterIP: None # <-- Don't forget!!
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: nginx

# kubectl apply -f svc-headless-nginx.yaml
service/svc-headless-nginx created

Kiểm tra thông tin Service Headless nginx.

# kubectl get svc svc-headless-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-headless-nginx ClusterIP None <none> 80/TCP 46s

Cuối cùng chúng ta đã chuẩn bị xong 2 loại Service Kubernetes gồm ClusterIPHeadless. Hãy tạo một pod dnsutils và mở terminal shell trên pod đó để kiểm tra tính năng nào.

# vi dnsutils.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dnsutils
  namespace: default
spec:
  containers:
  - name: dnsutils
    image: gcr.io/kubernetes-e2e-test-images/dnsutils:1.3
    command:
      - sleep
      - "36000"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always

# kubectl apply -f dnsutils.yaml
pod/dnsutils created

Mở terminal shell vào con Pod dnsutils nào.

# kubectl get pods dnsutils
NAME READY STATUS RESTARTS AGE
dnsutils 1/1 Running 0 32s

# kubectl exec -it dnsutils /bin/sh
/ #

Giờ chúng ta sẽ phân giải tên miền nội bộ Service ClusterIP. Bạn sẽ thấy DNS phân giải về đúng địa chỉ ClusterIP sẽ được dùng để load balance xuống các Pod IP.

/ # nslookup svc-clusterip-nginx
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: svc-clusterip-nginx.default.svc.cluster.local
Address: 10.106.79.1

Thử curl tên miền nội bộ Service ClusterIP.

/ # apk update
/ # apk add curl -y
/ # curl svc-clusterip-nginx
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Kế đến phân giải tên miền nội bộ của Service Headless. Bạn sẽ thấy DNS phân giải về danh sách các Pod IP, giúp bạn truy cập trực tiếp đến địa chỉ IP của Pod , không thông qua bất kì Proxy Load Balance nào. Việc load balance sẽ thực hiện ở DNS Round-robin.

# nslookup svc-headless-nginx
Server: 10.96.0.10
Address: 10.96.0.10#53

Name: svc-headless-nginx.default.svc.cluster.local
Address: 10.1.0.8
Name: svc-headless-nginx.default.svc.cluster.local
Address: 10.1.0.10
Name: svc-headless-nginx.default.svc.cluster.local
Address: 10.1.0.7
Name: svc-headless-nginx.default.svc.cluster.local
Address: 10.1.0.9

# curl svc-headless-nginx
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Vậy là bạn đã hiểu sự khác nhau giữa Headless ServiceClusterIP Service trong Kubernetes rồi phải không nào ? Hãy vận dụng tuỳ vào các tình huống cụ thể phát sinh trong môi trường ứng dụng của bạn nhé.

Nguồn: https://cuongquach.com/

Previous articleEbook Kubernetes Past, Present and Future – eMag (PDF)
Next articleEbook Service Mesh Past, Present and Future – InfoQ eMag (PDF)
Bạn đang theo dõi website "https://cuongquach.com/" nơi lưu trữ những kiến thức tổng hợp và chia sẻ cá nhân về Quản Trị Hệ Thống Dịch Vụ & Mạng, được xây dựng lại dưới nền tảng kinh nghiệm của bản thân mình, Quách Chí Cường. Hy vọng bạn sẽ thích nơi này !