HorizontalPodAutoscaler, k8s的版本是1.14.
需求是这样的,如果应用的cpu使用率到达60%上则自动增加应用数,如果cpu使用率下降则自动将应用数减少。
先定义Deployment,如下List-1,加上Limit限制资源,好等会做压测的时候自动扩容。
List-1
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: {{ template "consumer.fullname" . }}-mjduan
labels:
app: {{ template "consumer.name" . }}-mjduan
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ template "consumer.name" . }}-mjduan
template:
metadata:
labels:
app: {{ template "consumer.name" . }}-mjduan
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
ports:
- name: http
containerPort: 8082
protocol: TCP
resources:
requests:
cpu: 10m
memory: 100Mi
limits:
cpu: 1000m
memory: 1024Mi
来看Hpa编排文件,如下List-2,kind是deployment,name的值取List-1中的值。
List-2
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: {{ template "consumer.fullname" . }}-mjduan-autoscaling
spec:
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: {{ template "consumer.name" . }}-mjduan
minReplicas: 1
maxReplicas: 8
targetCPUUtilizationPercentage: 60
这里ingress就省略不给出了。
部署后,来看Hpa和pod数,如下List-3和List-4,都是1.
List-3
mjduan@mjduan:~/.kube$ kubectl get hpa -n test
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
consumer-mjduan-mjduan-autoscaling Deployment/consumer-mjduan-mjduan 20%/60% 1 8 1 5m57s
List-4
mjduan@mjduan:~/.kube$ kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
consumer-mjduan-mjduan-5c6bbff646-k9f4d 1/1 Running 0 7m1s
现在来用webbench做下压测,让cpu使用率提升
List-5
./webbench -c 200 -t 60 http://mjduan.rde.x/index/demo?emp_no=091078
再来看下Hpa和pod数变为了8,随着访问量增多,cpu使用率提升,Hpa自动将pod进行扩容了。
List-6
#hpa的复制数变为了8
mjduan@mjduan:~/.kube$ kubectl get hpa -n test
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
consumer-mjduan-mjduan-autoscaling Deployment/consumer-mjduan-mjduan 3670%/60% 1 8 8 7m37s
#pod数变为了8
mjduan@mjduan:~/.kube$ kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
consumer-mjduan-mjduan-5c6bbff646-425nz 1/1 Running 0 4m15s
consumer-mjduan-mjduan-5c6bbff646-7wb4l 1/1 Running 0 4m
consumer-mjduan-mjduan-5c6bbff646-d477h 1/1 Running 0 4m15s
consumer-mjduan-mjduan-5c6bbff646-k9f4d 1/1 Running 0 11m
consumer-mjduan-mjduan-5c6bbff646-pc4xh 1/1 Running 0 4m
consumer-mjduan-mjduan-5c6bbff646-q56ml 1/1 Running 0 4m
consumer-mjduan-mjduan-5c6bbff646-swc88 0/1 ContainerCreating 0 4m
consumer-mjduan-mjduan-5c6bbff646-vxqfb 1/1 Running 0 4m15s
再隔断时间,来看hpa和pod数,变少了,因为访问量减少,k8s自动按Hpa的规则将pod减少了。
List-7
#复制数变为了5
mjduan@mjduan:~/.kube$ kubectl get hpa -n test
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
consumer-mjduan-mjduan-autoscaling Deployment/consumer-mjduan-mjduan 25%/60% 1 8 5 15m
#pod数在减少
mjduan@mjduan:~/.kube$ kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
consumer-mjduan-mjduan-5c6bbff646-425nz 1/1 Running 0 8m8s
consumer-mjduan-mjduan-5c6bbff646-7wb4l 1/1 Terminating 0 7m53s
consumer-mjduan-mjduan-5c6bbff646-d477h 1/1 Running 0 8m8s
consumer-mjduan-mjduan-5c6bbff646-k9f4d 1/1 Running 0 15m
consumer-mjduan-mjduan-5c6bbff646-pc4xh 1/1 Running 0 7m53s
consumer-mjduan-mjduan-5c6bbff646-q56ml 1/1 Terminating 0 7m53s
consumer-mjduan-mjduan-5c6bbff646-vxqfb 1/1 Running 0 8m8s
Reference
- https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
- https://blog.csdn.net/mailjoin/article/details/79679944
来源:oschina
链接:https://my.oschina.net/u/2518341/blog/3216765