Cấu hình RBAC an toàn cho Cluster Autoscaler và HPA
Hệ thống Auto-Scaling cần quyền truy cập sâu vào API server để đọc metric và tạo/xóa node. Mặc định, các role này thường quá rộng, gây rủi ro bảo mật.
Tác động: Giới hạn quyền truy cập chỉ cho các hành động cần thiết, tuân thủ nguyên tắc Least Privilege.
Kết quả mong đợi: Cluster Autoscaler chỉ có thể tạo node và đọc metric, không thể xóa resource khác.
Tạo file định nghĩa RoleBinding mới cho Cluster Autoscaler với scope hạn chế.
cat > /tmp/autoscaler-rbac-restricted.yaml
Áp dụng file RBAC vào cluster để thay thế cấu hình mặc định.
kubectl apply -f /tmp/autoscaler-rbac-restricted.yaml
Verify: Kiểm tra xem ClusterRole mới đã được tạo và gắn vào ServiceAccount chưa.
kubectl describe clusterrole cluster-autoscaler-restricted
kubectl describe clusterrolebinding cluster-autoscaler-restricted
Triển khai Velero để Backup State và Persistent Volumes
Auto-Scaling làm tăng nguy cơ mất dữ liệu khi node bị lỗi phần cứng hoặc xóa nhầm. Velero giúp backup state của cluster (config, pod spec) và dữ liệu trên Persistent Volume.
Tác động: Tạo cơ chế Disaster Recovery tự động, đảm bảo dữ liệu không mất khi node bị xóa.
Kết quả mong đợi: Cluster Velero chạy trong namespace velero, sẵn sàng thực hiện backup định kỳ.
Chuẩn bị bucket lưu trữ backup. Ở đây sử dụng MinIO (local S3 compatible) hoặc AWS S3. Ví dụ với MinIO chạy trong cluster.
helm repo add minio https://charts.min.io/
helm repo update
helm install minio minio/minio --set rootUser=admin --set rootPassword=password --set defaultBuckets=velero-backups
Cài đặt Velero CLI và Helm chart.
curl -sL https://get.velero.io | bash
Tạo file giá trị cho Velero để kết nối với MinIO.
cat > /tmp/velero-values.yaml
Deploy Velero vào cluster.
helm install velero velero/velero -f /tmp/velero-values.yaml -n velero --create-namespace
Verify: Kiểm tra trạng thái pod Velero và danh sách backup schedules.
kubectl get pods -n velero
velero backup schedule list
Chiến lược khôi phục Cluster sau sự cố phần cứng
Khi node vật lý bị lỗi hoàn toàn, Proxmox sẽ tự động tạo node mới (như đã cấu hình ở phần trước). Velero sẽ khôi phục state của cluster lên node mới.
Tác động: Tự động hóa quy trình Disaster Recovery, giảm thời gian downtime xuống mức thấp nhất.
Kết quả mong đợi: Pod và dữ liệu được khôi phục chính xác trên node mới sau khi node cũ bị mất.
Cấu hình Restore Policy để tự động khôi phục khi phát hiện backup mới nhất.
cat > /tmp/restore-policy.yaml
Tạo script automation để chạy khi node mới join cluster. Script này sẽ kích hoạt restore nếu cần.
cat > /usr/local/bin/velero-auto-restore.sh
Cấu hình CronJob trong Kubernetes để chạy script này mỗi 5 phút.
cat > /tmp/restore-cronjob.yaml
Verify: Kiểm tra CronJob đã chạy và xem log của nó.
kubectl get cronjob velero-auto-restore -n velero
kubectl logs -f $(kubectl get pod -n velero -l app=velero-auto-restore -o jsonpath='{.items[0].metadata.name}')
Xác minh toàn bộ quy trình bảo mật và Backup
Thực hiện test toàn bộ quy trình: tạo pod, kích hoạt backup, xóa node, và quan sát khôi phục.
Tác động: Đảm bảo hệ thống hoạt động đúng như thiết kế trong tình huống thực tế.
Kết quả mong đợi: Pod mới được tạo, backup được lưu, node bị xóa, và pod được khôi phục trên node mới.
Tạo một ứng dụng đơn giản để test.
cat > /tmp/test-app.yaml
Kích hoạt backup thủ công ngay lập tức.
velero backup create manual-backup-test --include-namespaces test-recovery --wait
Xóa node worker để kích hoạt Auto-Scaling và Restore.
kubectl cordon
kubectl drain --ignore-daemonsets --delete-local-data --force
kubectl delete node
Quan sát log của Velero và Proxmox để xác nhận node mới được tạo và dữ liệu được khôi phục.
velero restore get
kubectl get pods -n test-recovery
kubectl get nodes
Điều hướng series:
Mục lục: Series: Xây dựng hạ tầng Kubernetes tự động (Auto-Scaling) với K3s và Helm trên Proxmox
« Phần 9: Troubleshooting và xử lý sự cố Auto-Scaling