← Về danh sách bài học Bài 5/25

🔗 Bài 5: Service - Kết Nối Pods

⏱️ Thời gian: 25 phút | 📚 Độ khó: Dễ

🎯 Sau bài học này, bạn sẽ:

1. Tại Sao Cần Service?

Pods có IP riêng, nhưng khi Pod restart thì IP thay đổi!

📌 Vấn đề:
• Pod IP không cố định
• Có nhiều Pods (replicas), gọi Pod nào?
• Load balancing giữa các Pods?

Service giải quyết bằng cách tạo một địa chỉ IP/DNS ổn định và tự động load balance đến các Pods.

2. Các Loại Service

Type Mô tả Truy cập từ
ClusterIP IP nội bộ cluster (mặc định) Trong cluster
NodePort Expose qua port của Node Ngoài cluster (Node IP:Port)
LoadBalancer Tạo LB external (cloud) Internet (External IP)
ExternalName Map đến DNS bên ngoài DNS alias

3. ClusterIP (Mặc Định)

Chỉ truy cập được từ trong cluster. Dùng cho giao tiếp nội bộ.

# service-clusterip.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: ClusterIP          # Mặc định, có thể bỏ
  selector:
    app: nginx             # Chọn pods có label này
  ports:
    - protocol: TCP
      port: 80             # Port của Service
      targetPort: 80       # Port của container
# Apply
kubectl apply -f service-clusterip.yaml

# Kiểm tra
kubectl get services
# NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
# nginx-service   ClusterIP   10.96.100.50            80/TCP    30s

# Test từ trong cluster
kubectl run test-pod --image=busybox -it --rm -- wget -O- http://nginx-service
# hoặc: wget -O- http://nginx-service.default.svc.cluster.local
💡 Service Discovery:
K8s tự động tạo DNS: <service-name>.<namespace>.svc.cluster.local
Trong cùng namespace, chỉ cần: <service-name>

4. NodePort

Expose Service qua port của tất cả Nodes (30000-32767).

# service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80             # Port nội bộ
      targetPort: 80       # Port container
      nodePort: 30080      # Port trên Node (30000-32767)
# Apply
kubectl apply -f service-nodeport.yaml

kubectl get services
# NAME             TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
# nginx-nodeport   NodePort   10.96.50.100           80:30080/TCP   30s

# Truy cập (với Minikube)
minikube ip                    # Lấy IP của node
# 192.168.49.2

# Truy cập: http://192.168.49.2:30080

# Hoặc dùng minikube service
minikube service nginx-nodeport --url

5. LoadBalancer

Tạo external load balancer (chỉ hoạt động trên cloud: AWS, GCP, Azure...).

# service-loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-lb
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
# Apply
kubectl apply -f service-loadbalancer.yaml

kubectl get services
# NAME       TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
# nginx-lb   LoadBalancer   10.96.30.200   203.0.113.50     80:31234/TCP   60s

# Truy cập: http://203.0.113.50
⚠️ Lưu ý:
• LoadBalancer chỉ hoạt động trên cloud có hỗ trợ
• Trên Minikube: dùng minikube tunnel để test
• Mỗi LoadBalancer = 1 IP = $ (tính tiền trên cloud)

6. Thực Hành: Deploy + Service

# app-complete.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.24
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  type: NodePort
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30000
# Deploy tất cả
kubectl apply -f app-complete.yaml

# Kiểm tra
kubectl get all -l app=web

# Test
minikube service web-service --url

7. Các Lệnh Quan Trọng

# Xem services
kubectl get services
kubectl get svc              # Viết tắt

# Chi tiết
kubectl describe service nginx-service

# Tạo service nhanh từ deployment
kubectl expose deployment nginx-deployment --port=80 --type=NodePort

# Xem endpoints (pods được service chọn)
kubectl get endpoints nginx-service

# Xóa
kubectl delete service nginx-service

📝 Tóm Tắt Bài Học