本文档详细介绍如何在Kubernetes环境中实现Go-ES应用的自动扩缩容,包括水平Pod自动扩缩容(HPA)、垂直Pod自动扩缩容(VPA)和集群自动扩缩容。
自动扩缩容是指根据负载变化自动调整计算资源的过程,主要目标是:
Kubernetes提供了多种自动扩缩容机制,适合不同的应用场景和需求。
HPA通过增加或减少Pod副本数来实现应用的水平扩展。
HPA控制器定期(默认15秒)检查指定的指标,并根据目标值计算所需的副本数:
所需副本数 = ceil[当前副本数 * (当前指标值 / 目标指标值)]
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: go-es-api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: go-es-api
minReplicas: 2 # 最小副本数
maxReplicas: 10 # 最大副本数
behavior: # 扩缩容行为定制
scaleUp:
stabilizationWindowSeconds: 60 # 稳定窗口期
policies:
- type: Percent
value: 100
periodSeconds: 15
scaleDown:
stabilizationWindowSeconds: 300 # 缩容稳定窗口
policies:
- type: Percent
value: 10
periodSeconds: 60
metrics:
- type: Resource # 基于CPU使用率
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource # 基于内存使用率
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
为API服务设置HPA:
稳定窗口调优:
扩缩策略调优:
VPA自动调整单个Pod的CPU和内存请求与限制,适合无法水平扩展的应用。
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: go-es-mysql-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: mysql
updatePolicy:
updateMode: Auto # 自动应用推荐
resourcePolicy:
containerPolicies:
- containerName: '*'
minAllowed: # 最小资源限制
cpu: 100m
memory: 50Mi
maxAllowed: # 最大资源限制
cpu: 1
memory: 500Mi
controlledResources: ["cpu", "memory"]
对于Go-ES项目,VPA适用于:
Cluster Autoscaler自动调整Kubernetes集群中的节点数量。
apiVersion: apps/v1
kind: Deployment
metadata:
name: cluster-autoscaler
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: cluster-autoscaler
template:
metadata:
labels:
app: cluster-autoscaler
spec:
serviceAccountName: cluster-autoscaler
containers:
- image: k8s.gcr.io/autoscaling/cluster-autoscaler:v1.22.0
name: cluster-autoscaler
command:
- ./cluster-autoscaler
- --v=4
- --stderrthreshold=info
- --cloud-provider=aws
- --scale-down-utilization-threshold=0.5
- --scale-down-delay-after-add=10m
- --skip-nodes-with-local-storage=false
- --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/>
--scale-down-utilization-threshold
:节点利用率低于该值时考虑缩容(默认0.5)--scale-down-delay-after-add
:新增节点后多久可以考虑缩容(默认10分钟)--scale-down-unneeded-time
:节点空闲多久后可以被移除(默认10分钟)KEDA是一个基于事件的自动扩缩容系统,可以根据外部事件源触发扩缩容。
# 使用Helm安装
helm repo add kedacore https://kedacore.github.io/charts
helm repo update
helm install keda kedacore/keda --namespace keda --create-namespace
例如,基于RabbitMQ队列的自动扩缩容:
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: go-es-worker-scaler
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: go-es-worker
pollingInterval: 15 # 轮询间隔(秒)
cooldownPeriod: 30 # 冷却期(秒)
minReplicaCount: 0 # 最小副本数(可以为零)
maxReplicaCount: 30 # 最大副本数
triggers:
- type: rabbitmq
metadata:
protocol: amqp
queueName: task-queue
host: amqp://guest:guest@rabbitmq:5672/
queueLength: '10' # 每个副本处理的队列消息数
在Go-ES项目中,KEDA适用于:
正确设置Pod的资源请求和限制是自动扩缩容的基础:
resources:
requests:
cpu: 100m # 请求CPU资源,作为HPA判断基础
memory: 128Mi # 请求内存资源
limits:
cpu: 500m # CPU资源上限
memory: 512Mi # 内存资源上限
readinessProbe:
httpGet:
path: /api/v1/health
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
API服务:
数据库服务:
Elasticsearch服务:
异步工作器:
Prometheus指标:
告警配置:
使用如下工具模拟负载测试自动扩缩容:
# 使用hey工具模拟HTTP负载
hey -n 10000 -c 100 http://go-es-api-service/api/v1/products
# 使用JMeter或Locust进行复杂场景测试
# 监控Pod副本数变化
kubectl get hpa go-es-api-hpa -w
# 查看自动扩缩容事件
kubectl describe hpa go-es-api-hpa
# 监控节点资源使用率
kubectl top nodes
HPA不扩容:
缩容太慢:
节点自动扩缩容失败:
# 检查HPA状态和当前指标
kubectl describe hpa go-es-api-hpa
# 检查metrics-server是否正常
kubectl get pods -n kube-system | grep metrics-server
# 检查自定义指标可用性
kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1"
# 查看Cluster Autoscaler日志
kubectl logs -n kube-system -l app=cluster-autoscaler
通过合理配置这些自动扩缩容机制,您的Go-ES应用可以根据负载自动调整资源,既能满足高峰期需求,又能在低峰期释放资源,实现资源利用率和性能的最佳平衡。