Skip to content

Thực hành: Service & Networking

🎯 Mục tiêu

🎯 Sau bài thực hành này, bạn sẽ:

  • Tạo ClusterIP Service và kiểm tra DNS resolution
  • Cấu hình NodePort để truy cập từ bên ngoài cluster
  • Thiết lập Ingress với host-based và path-based routing

Mô tả bài tập

Bạn có 2 ứng dụng: frontend (nginx) và backend (httpbin). Nhiệm vụ là thiết lập networking để frontend gọi được backend qua DNS, và expose cả hai qua Ingress.

Yêu cầu

Bài 1: ClusterIP Service

Tạo Deployment + ClusterIP Service cho backend:

yaml
# backend-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: httpbin
        image: kennethreitz/httpbin:latest
        ports:
        - containerPort: 80
---
# TODO: Tạo Service ClusterIP cho backend
# Service phải selector đúng label và expose port 80

Kiểm tra: Từ Pod khác, chạy nslookup backend.default.svc.cluster.local phải resolve được.

Bài 2: NodePort Service

Tạo NodePort Service cho frontend:

  • Expose port 80 của container
  • NodePort trong range 30000-32767
yaml
apiVersion: v1
kind: Service
metadata:
  name: frontend-nodeport
spec:
  type: # TODO
  selector:
    app: frontend
  ports:
  - port: 80
    targetPort: 80
    nodePort: # TODO: Chọn port

Kiểm tra: Truy cập http://<minikube-ip>:<nodeport> phải thấy NGINX welcome page.

Bài 3: Ingress Routing

Tạo Ingress resource route traffic dựa trên path:

  • / → frontend Service (port 80)
  • /api → backend Service (port 80)
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      # TODO: Cấu hình 2 path rules

Yêu cầu: Bật NGINX Ingress Controller trước (minikube addons enable ingress).

Gợi ý

💡 Xem gợi ý
  • Bài 1: ClusterIP là type mặc định. Selector phải khớp chính xác với Pod labels
  • Bài 2: Dùng minikube ip để lấy IP Node. NodePort range: 30000-32767
  • Bài 3: pathType: Prefix cho path matching. Chú ý rewrite-target annotation để backend nhận path đúng
  • Debug: kubectl get endpoints <service> để kiểm tra Service có tìm thấy Pod không

Lời giải

✅ Xem lời giải

Bài 1: Backend ClusterIP Service

yaml
apiVersion: v1
kind: Service
metadata:
  name: backend
spec:
  type: ClusterIP
  selector:
    app: backend
  ports:
  - port: 80
    targetPort: 80

Kiểm tra DNS:

bash
kubectl run dns-test --rm -it --image=busybox -- nslookup backend.default.svc.cluster.local

Bài 2: Frontend NodePort

yaml
apiVersion: v1
kind: Service
metadata:
  name: frontend-nodeport
spec:
  type: NodePort
  selector:
    app: frontend
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080

Bài 3: Ingress

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              number: 80
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: backend
            port:
              number: 80

Liên kết liên quan