คู่มือผู้ใช้ Kubernetes
แนวคิดพื้นฐาน
Pod
Pod เป็นหน่วยที่ปรับใช้งานได้เล็กที่สุดใน Kubernetes แทนคอนเทนเนอร์หนึ่งตัวหรือมากกว่าที่ทำงานในคลัสเตอร์ โดยทั่วไป Pod มีคอนเทนเนอร์เดียว แต่ยังสามารถมีคอนเทนเนอร์ที่เชื่อมโยงกันอย่างแน่นหนาหลายตัวได้
Deployment
Deployment ใช้จัดการแอปพลิเคชันที่ไม่มีสถานะ ให้ความสามารถในการอัปเดตแบบประกาศ (declarative) สำหรับการปรับใช้งาน การปรับขนาด และการอัปเดตที่ง่ายดาย
Service
Service ให้จุดเชื่อมต่อเครือข่ายที่เสถียรสำหรับชุดของ Pod โดยคงความเสถียรแม้ที่อยู่ IP ของ Pod เปลี่ยนแปลง
Namespace
Namespace แบ่งทรัพยากรของคลัสเตอร์ออกเป็นหลายพาร์ติชันเชิงตรรกะ เหมาะสำหรับทีมหรือโครงการหลายชุดที่ใช้คลัสเตอร์เดียวกัน
คำสั่งที่พบบ่อย
การจัดการคลัสเตอร์
# View cluster information
kubectl cluster-info
# View nodes
kubectl get nodes
# View all Pods
kubectl get pods --all-namespaces
# View cluster resource usage
kubectl top nodes
kubectl top pods
การปรับใช้งานแอปพลิเคชัน
# Create a Deployment
kubectl create deployment nginx --image=nginx
# View Deployments
kubectl get deployments
# Scale
kubectl scale deployment nginx --replicas=3
# Update image
kubectl set image deployment/nginx nginx=nginx:1.21
# Rollback
kubectl rollout undo deployment/nginx
# View rollout status
kubectl rollout status deployment/nginx
การจัดการ Service
# Expose a Service
kubectl expose deployment nginx --port=80 --type=LoadBalancer
# View Services
kubectl get services
# Delete Service
kubectl delete service <service-name>
การดีบัก
# View Pod logs
kubectl logs <pod-name>
# Execute command in container
kubectl exec -it <pod-name> -- /bin/bash
# Describe resource details
kubectl describe pod <pod-name>
# Port forward for debugging
kubectl port-forward <pod-name> 8080:80
Manifest ของทรัพยากร
Kubernetes รองรับการจัดการทรัพยากรแบบประกาศโดยใช้ไฟล์ YAML ต่อไปนี้เป็นนิยามทรัพยากรที่พบบ่อย:
ตัวอย่าง Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:latest
ports:
- containerPort: 8080
resources:
limits:
memory: "128Mi"
cpu: "500m"
requests:
memory: "64Mi"
cpu: "250m"
ตัวอย่าง Service
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
ตัวอย่าง ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
database_url: "postgres://db:5432/myapp"
cache_enabled: "true"
ตัวอย่าง Secret
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
stringData:
username: admin
password: changeme
การจัดการวงจรชีวิตของแอปพลิเคชัน
การปรับใช้งานแอปพลิเคชัน
# Deploy using YAML file
kubectl apply -f deployment.yaml
# View deployment status
kubectl get pods -l app=my-app
การอัปเดตแอปพลิเคชัน
# Update image version
kubectl set image deployment/my-app my-app=my-app:v2
# View update progress
kubectl rollout status deployment/my-app
การ rollback
# Rollback to previous version
kubectl rollout undo deployment/my-app
# Rollback to specific revision
kubectl rollout undo deployment/my-app --to-revision=2
# View revision history
kubectl rollout history deployment/my-app
การปรับขนาด
# Manual scaling
kubectl scale deployment my-app --replicas=5
# Auto-scaling (HPA)
kubectl autoscale deployment my-app --min=2 --max=10 --cpu-percent=80
การจัดการที่จัดเก็บข้อมูล
การสร้าง PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
storageClassName: standard
hostPath:
path: /data/my-app
การสร้าง PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
การใช้ PVC ใน Pod
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-app
image: my-app
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: my-pvc
การจัดการการกำหนดค่า
การใช้ ConfigMap
# Create ConfigMap from file
kubectl create configmap my-config --from-file=config.properties
# Create from env file
kubectl create configmap my-config --from-env-file=env.properties
# Use in Pod
kubectl apply -f pod-with-configmap.yaml
การใช้ Secret
# Create Secret from file
kubectl create secret generic my-secret --from-file=credentials.json
# Use in Pod
kubectl apply -f pod-with-secret.yaml
โควตาและการจำกัดทรัพยากร
ResourceQuota
apiVersion: v1
kind: ResourceQuota
metadata:
name: my-quota
spec:
hard:
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
pods: "20"
LimitRange
apiVersion: v1
kind: LimitRange
metadata:
name: my-limits
spec:
limits:
- max:
cpu: "2"
memory: 1Gi
min:
cpu: 100m
memory: 128Mi
default:
cpu: 500m
memory: 512Mi
defaultRequest:
cpu: 200m
memory: 256Mi
type: Container
การตรวจสอบและการบันทึก
การตรวจสอบ
# View resource usage with kubectl top
kubectl top nodes
kubectl top pods
# View detailed status
kubectl describe node <node-name>
kubectl describe pod <pod-name>
การบันทึก
# View Pod logs
kubectl logs my-app-pod
# Follow logs in real-time
kubectl logs -f my-app-pod
# View logs of previous container instance
kubectl logs --previous my-app-pod
แนวปฏิบัติที่ดีที่สุด
- ใช้การกำหนดค่าแบบประกาศเพื่อจัดการโครงสร้างพื้นฐาน
- ตั้งค่าการจำกัดและการร้องขอทรัพยากรที่เหมาะสม
- จัดระเบียบทรัพยากรโดยใช้ป้ายกำกับ (label)
- ล้างทรัพยากรที่ไม่ได้ใช้เป็นประจำ
- ใช้ namespace เพื่อแยกสภาพแวดล้อมที่แตกต่างกัน
- กำหนดค่า health check probe
- ใช้ RBAC เพื่อควบคุมสิทธิ์
- สำรองข้อมูล etcd เป็นประจำ
