K8s 自动扩缩容与监控
配置 HPA 自动扩缩容 + Prometheus + Grafana 监控,构建可观测的 K8s 集群
你将学到
- 配置 Metrics Server
- 使用 HPA 实现自动扩缩容
- 部署 Prometheus 监控
- 使用 Grafana 可视化指标
前置知识
架构设计
┌─────────────────────────────────────────┐
│ Grafana │
│ (可视化仪表盘) │
└───────────────┬─────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Prometheus │
│ (指标收集和存储) │
└───────────────┬─────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Metrics Server │
│ (资源指标收集) │
└───────────────┬─────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ HPA │
│ (自动扩缩容) │
└───────────────┬─────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Pods │
│ (应用实例) │
└─────────────────────────────────────────┘
实现步骤
第一步:安装 Metrics Server
# 安装 Metrics Server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# 验证安装
kubectl get deployment metrics-server -n kube-system
kubectl top nodes
kubectl top pods
第二步:配置 HPA
# hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: webapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: webapp
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
# 应用 HPA
kubectl apply -f hpa.yaml
# 查看 HPA 状态
kubectl get hpa
kubectl describe hpa webapp-hpa
第三步:部署 Prometheus
# 添加 Helm 仓库
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
# 安装 Prometheus
helm install prometheus prometheus-community/kube-prometheus-stack \
--namespace monitoring \
--create-namespace \
--set grafana.adminPassword=admin123
第四步:访问 Grafana
# 获取 Grafana 服务
kubectl get svc -n monitoring
# 端口转发
kubectl port-forward -n monitoring svc/prometheus-grafana 3000:80
# 访问 http://localhost:3000
# 用户名: admin
# 密码: admin123
第五步:配置自定义指标
# servicemonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: webapp-monitor
namespace: monitoring
spec:
selector:
matchLabels:
app: webapp
endpoints:
- port: http
path: /metrics
interval: 15s
完整项目结构
k8s-monitoring/
├── k8s/
│ ├── deployment.yaml # 应用部署
│ ├── service.yaml # 服务
│ ├── hpa.yaml # 自动扩缩容配置
│ └── servicemonitor.yaml # 监控配置
├── helm/
│ └── values.yaml # Helm 配置
└── grafana/
└── dashboard.json # Grafana 仪表盘
最佳实践
- 合理设置阈值:CPU 70%、内存 80% 是常用值
- 稳定窗口:防止频繁扩缩容
- 资源请求:确保设置了 resources.requests
- 监控告警:配置关键指标的告警规则
- 日志收集:使用 EFK 或 Loki 收集日志
常见问题
Q: HPA 不生效怎么办? A: 检查 Metrics Server 是否正常运行,确保 Pod 设置了 resources.requests。
Q: 如何查看扩缩容历史?
A: kubectl describe hpa webapp-hpa 查看 Events。
Q: 如何自定义 Grafana 仪表盘? A: 导入 JSON 仪表盘或使用 PromQL 查询创建。
扩展挑战
- 配置基于自定义指标的 HPA
- 实现 VPA(垂直自动扩缩容)
- 配置告警通知(邮件/Slack)
相关课程
| 课程 | 相关章节 |
|---|---|
| K8s | 水平自动扩缩容(HPA) |
| K8s | Metrics Server |
| K8s | 监控与日志 |
| K8s | Deployment |