8. 性能与成本
8.1 弹性伸缩
伸缩策略
智能人力资源系统需要根据负载自动伸缩,确保服务稳定和成本优化:
水平伸缩(Horizontal Scaling)
自动伸缩规则:
- CPU使用率:>80%时扩容,<30%时缩容
- 内存使用率:>80%时扩容,<30%时缩容
- 请求队列长度:>50时扩容,<5时缩容
- 响应时间:P95>2秒时扩容
- 错误率:>1%时扩容
Kubernetes HPA配置:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hr-ai-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hr-ai-service
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
- type: Pods
pods:
metric:
name: request_queue_length
target:
type: AverageValue
averageValue: "10"
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 50
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
- type: Pods
value: 2
periodSeconds: 15
selectPolicy: Max
垂直伸缩(Vertical Scaling)
资源调整规则:
- 根据历史负载预测资源需求
- 在业务高峰期前提前扩容
- 在业务低峰期自动缩容
VPA配置:
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: hr-ai-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: hr-ai-service
updatePolicy:
updateMode: "Auto"
resourcePolicy:
containerPolicies:
- containerName: hr-ai-service
minAllowed:
cpu: 100m
memory: 128Mi
maxAllowed:
cpu: 4
memory: 8Gi