环境准备
部署metric-server参考https://www.jianshu.com/p/f4e1a2665c83
测试
创建负载yaml
创建nginx.yaml,内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
resources:
limits:
cpu: 20m
memory: 50Mi
requests:
cpu: 20m
memory: 50Mi
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
创建负载
kubectl apply -f nginx.yaml
创建hpa yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1
maxReplicas: 10
behavior:
scaleDown:
stabilizationWindowSeconds: 60
selectPolicy: Max
policies:
- type: Pods
value: 1
periodSeconds: 15
- type: Percent
value: 20
periodSeconds: 15
scaleUp:
stabilizationWindowSeconds: 0
selectPolicy: Max
policies:
- type: Pods
value: 3
periodSeconds: 15
- type: Percent
value: 100
periodSeconds: 15
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 10
创建hpa
kubectl apply -f hpa.yaml
创建压测pod
kubectl run bench --rm --image=busybox -it -- sh
执行压测
while true;do wget -q -O /dev/null nginx; done;
验证
等待一段时间,查看nginx pod
kubectl get pod -l app=nginx
提示
housekeeping_interval默认是10秒最大15秒(即cadvisor采集指标间隔),可以通过--housekeeping-interval调整
metric-resolution默认60秒(即metric-server采集kubelet指标间隔),可以通过metric-server的--metric-resolution=15s调整
--horizontal-pod-autoscaler-sync-period默认15秒(即hpa资源调协的间隔),可以通过controller-manager的--horizontal-pod-autoscaler-sync-period=10s调整
即默认最短需要等待85秒
网友评论