← Về danh sách bài học
Bài 3/25
📦 Bài 3: Pod - Đơn Vị Cơ Bản
🎯 Sau bài học này, bạn sẽ:
- Hiểu Pod là gì và tại sao là đơn vị nhỏ nhất
- Tạo Pod bằng file YAML
- Hiểu Pod lifecycle và các trạng thái
- Biết về multi-container pods
1. Pod Là Gì?
Pod là đơn vị nhỏ nhất và cơ bản nhất trong Kubernetes. Một Pod đại diện cho một hoặc nhiều containers chạy cùng nhau trên một node.
📌 Đặc điểm của Pod:
• Các containers trong Pod chia sẻ network (cùng IP)
• Chia sẻ storage volumes
• Chia sẻ namespace
• Luôn chạy trên cùng một node
• Các containers trong Pod chia sẻ network (cùng IP)
• Chia sẻ storage volumes
• Chia sẻ namespace
• Luôn chạy trên cùng một node
Tại sao cần Pod?
Kubernetes không quản lý container trực tiếp mà quản lý Pod. Điều này cho phép:
- Nhóm các containers phụ thuộc lẫn nhau
- Chia sẻ tài nguyên giữa containers
- Quản lý lifecycle chung
2. Tạo Pod Đầu Tiên
Cách 1: Imperative (lệnh trực tiếp)
# Tạo pod nhanh
kubectl run nginx-pod --image=nginx --port=80
# Kiểm tra
kubectl get pods
# Xem chi tiết
kubectl describe pod nginx-pod
# Xóa pod
kubectl delete pod nginx-pod
Cách 2: Declarative (file YAML) - Khuyên dùng
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-nginx
labels:
app: nginx
environment: dev
spec:
containers:
- name: nginx
image: nginx:1.24
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
# Apply file YAML
kubectl apply -f pod.yaml
# Kiểm tra
kubectl get pods
# NAME READY STATUS RESTARTS AGE
# my-nginx 1/1 Running 0 10s
# Xem logs
kubectl logs my-nginx
# Exec vào pod
kubectl exec -it my-nginx -- /bin/bash
💡 Best Practice:
Luôn dùng file YAML (declarative) trong thực tế vì có thể version control, review, và tái sử dụng.
Luôn dùng file YAML (declarative) trong thực tế vì có thể version control, review, và tái sử dụng.
3. Cấu Trúc YAML Chi Tiết
apiVersion: v1 # Phiên bản API
kind: Pod # Loại resource
metadata: # Thông tin metadata
name: my-pod # Tên pod (bắt buộc)
namespace: default # Namespace (mặc định: default)
labels: # Labels để phân loại
app: my-app
version: v1
annotations: # Thông tin bổ sung
description: "Pod demo"
spec: # Đặc tả pod
restartPolicy: Always # Always | OnFailure | Never
containers: # Danh sách containers
- name: main # Tên container
image: nginx:1.24 # Image từ registry
imagePullPolicy: IfNotPresent # Always | Never | IfNotPresent
ports:
- containerPort: 80
protocol: TCP
env: # Biến môi trường
- name: ENV
value: "production"
resources: # Giới hạn tài nguyên
requests: # Tối thiểu cần
memory: "64Mi"
cpu: "100m"
limits: # Tối đa được dùng
memory: "128Mi"
cpu: "500m"
4. Pod Lifecycle
| Phase | Mô tả |
|---|---|
| Pending | Pod được accept nhưng chưa có container nào chạy |
| Running | Pod đã được bind vào node và ít nhất 1 container đang chạy |
| Succeeded | Tất cả containers hoàn thành thành công |
| Failed | Tất cả containers dừng, ít nhất 1 thất bại |
| Unknown | Không lấy được trạng thái (node mất kết nối) |
Container States
- Waiting: Đang đợi (pull image, mount volume...)
- Running: Container đang chạy
- Terminated: Container đã dừng
# Xem trạng thái chi tiết
kubectl describe pod my-nginx
# Theo dõi pods realtime
kubectl get pods -w
5. Multi-Container Pods
Một Pod có thể chứa nhiều containers khi chúng cần:
- Chia sẻ network và storage
- Chạy cùng nhau và liên kết chặt
- Có sidecar pattern (logging, proxy...)
# multi-container-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: web-with-sidecar
spec:
containers:
# Container chính
- name: web
image: nginx:1.24
ports:
- containerPort: 80
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx
# Sidecar container (đọc logs)
- name: log-reader
image: busybox
command: ['sh', '-c', 'tail -f /logs/access.log']
volumeMounts:
- name: shared-logs
mountPath: /logs
volumes:
- name: shared-logs
emptyDir: {}
⚠️ Lưu ý:
Chỉ dùng multi-container khi thực sự cần thiết. Phần lớn trường hợp, mỗi Pod nên có 1 container.
Chỉ dùng multi-container khi thực sự cần thiết. Phần lớn trường hợp, mỗi Pod nên có 1 container.
6. Các Lệnh Quan Trọng
# Tạo/Update pod từ file
kubectl apply -f pod.yaml
# Xem pods
kubectl get pods
kubectl get pods -o wide # Thêm thông tin IP, Node
kubectl get pods -o yaml # Output YAML
kubectl get pods --show-labels # Hiện labels
# Chi tiết pod
kubectl describe pod <tên-pod>
# Logs
kubectl logs <tên-pod>
kubectl logs <tên-pod> -c <tên-container> # Multi-container
kubectl logs -f <tên-pod> # Follow
# Exec vào pod
kubectl exec -it <tên-pod> -- /bin/sh
# Port forward (debug local)
kubectl port-forward <tên-pod> 8080:80
# Xóa pod
kubectl delete pod <tên-pod>
kubectl delete -f pod.yaml
📝 Tóm Tắt Bài Học
- Pod là đơn vị nhỏ nhất, chứa 1+ containers
- Containers trong Pod chia sẻ network và storage
- Dùng file YAML để định nghĩa Pod (declarative)
- Lifecycle: Pending → Running → Succeeded/Failed
- Multi-container: Dùng cho sidecar patterns