1、启动
[root@k8s-master ~]# kubectl autoscale deployment nginx-deployment --max=8 --min=2 --cpu-percent=80deployment "nginx-deployment" autoscaled
2、查看创建
[root@k8s-master ~]# kubectl get allNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEdeploy/nginx-deployment 2 2 2 2 13hNAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGEhpa/nginx-deployment Deployment/nginx-deployment 80% 0% 2 8 17sNAME CLUSTER-IP EXTERNAL-IP PORT(S) AGEsvc/kubernetes 10.254.0.1 <none> 443/TCP 2dsvc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1dNAME DESIRED CURRENT READY AGErs/nginx-deployment-2950479891 0 0 0 13hrs/nginx-deployment-3113009173 2 2 2 13hNAME READY STATUS RESTARTS AGEpo/nginx-deployment-3113009173-h5plc 1/1 Running 0 17spo/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
3、修改副本数为1
[root@k8s-master ~]# kubectl edit deployment nginx-deployment修改为1replicas: 1deployment "nginx-deployment" edited[root@k8s-master ~]# kubectl get allNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEdeploy/nginx-deployment 1 1 1 1 13hNAME CLUSTER-IP EXTERNAL-IP PORT(S) AGEsvc/kubernetes 10.254.0.1 <none> 443/TCP 2dsvc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1dNAME DESIRED CURRENT READY AGErs/nginx-deployment-2950479891 0 0 0 13hrs/nginx-deployment-3113009173 1 1 1 13hNAME READY STATUS RESTARTS AGEpo/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
[root@k8s-master ~]# kubectl get horizontalpodautoscaler NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGEnginx-deployment Deployment/nginx-deployment 80% 0% 2 8 1m# Please edit the object below. Lines beginning with a ‘#‘ will be ignored,# and an empty file will abort the edit. If an error occurs while saving this file will be# reopened with the relevant failures.#apiVersion: autoscaling/v1kind: HorizontalPodAutoscalermetadata: creationTimestamp: 2019-01-22T01:00:02Z name: nginx-deployment namespace: default resourceVersion: "41194" selfLink: /apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/nginx-deployment uid: 0c897472-1de1-11e9-9773-000c292bd9e1spec: maxReplicas: 8 minReplicas: 2 scaleTargetRef: apiVersion: extensions/v1beta1 kind: Deployment name: nginx-deployment targetCPUUtilizationPercentage: 80status: currentCPUUtilizationPercentage: 0 currentReplicas: 2 desiredReplicas: 2 lastScaleTime: 2019-01-22T01:00:02Z
[root@k8s-master ~]# kubectl edit deployment nginx-deployment replicas: 1deployment "nginx-deployment" edited[root@k8s-master ~]# kubectl get allNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEdeploy/nginx-deployment 2 2 2 2 13hNAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGEhpa/nginx-deployment Deployment/nginx-deployment 80% 0% 2 8 6mNAME CLUSTER-IP EXTERNAL-IP PORT(S) AGEsvc/kubernetes 10.254.0.1 <none> 443/TCP 2dsvc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1dNAME DESIRED CURRENT READY AGErs/nginx-deployment-2950479891 0 0 0 13hrs/nginx-deployment-3113009173 2 2 2 13hNAME READY STATUS RESTARTS AGEpo/nginx-deployment-3113009173-9hlq1 1/1 Running 0 2spo/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
明明修改为1,怎么还有2个?是因为hpa如下配置
spec: maxReplicas: 8 minReplicas: 2
[root@k8s-master ~]# kubectl edit hpa nginx-deployment修改:spec: maxReplicas: 8 minReplicas: 5horizontalpodautoscaler "nginx-deployment" edited[root@k8s-master ~]# kubectl get allNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEdeploy/nginx-deployment 5 5 5 2 13hNAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGEhpa/nginx-deployment Deployment/nginx-deployment 80% 0% 5 8 8mNAME CLUSTER-IP EXTERNAL-IP PORT(S) AGEsvc/kubernetes 10.254.0.1 <none> 443/TCP 2dsvc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1dNAME DESIRED CURRENT READY AGErs/nginx-deployment-2950479891 0 0 0 13hrs/nginx-deployment-3113009173 5 5 2 13hNAME READY STATUS RESTARTS AGEpo/nginx-deployment-3113009173-97l9c 0/1 ContainerCreating 0 2spo/nginx-deployment-3113009173-9hlq1 1/1 Running 0 2mpo/nginx-deployment-3113009173-qq4h8 0/1 ContainerCreating 0 2spo/nginx-deployment-3113009173-sfp8z 0/1 ContainerCreating 0 2spo/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
看到自动伸缩的过程了吧!
1、什么是hpa
Horizontal Pod Autoscaling可以根据CPU使用率或应用自定义metrics自动扩展Pod数量(支持replication controller、deployment和replica set)。
客户端;
通过kubectl创建一个horizontalPodAutoscaler对象,并存储到etcd中
服务端:
api server:负责接受创建hpa对象,然后存入etcd
hpa controler和其他的controler类似,每30s同步一次,将已经创建的hpa进行一次管理(从heapster获取监控数据,查看是否需要scale, controler的store中就保存着从始至终创建出来的hpa,当做一个缓存),watch hpa有变化也会运行。从heapster中获取scale数据,和hpa对比,计算cup利用率等信息,然后重新调整scale。根据hpa.Spec.ScaleTargetRef.Kind(例如Deployment,然后deployment控制器在调整pod数量),调整其值,发送到apiserver存储到etcd,然后更新hpa到etcd.
2、示例
# 创建pod和service$ kubectl run php-apache --image=gcr.io/google_containers/hpa-example --requests=cpu=200m --expose --port=80service "php-apache" createddeployment "php-apache" created# 创建autoscaler$ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10deployment "php-apache" autoscaled$ kubectl get hpaNAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGEphp-apache Deployment/php-apache/scale 50% 0% 1 10 18s# 增加负载$ kubectl run -i --tty load-generator --image=busybox /bin/shHit enter for command prompt$ while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done# 过一会就可以看到负载升高了$ kubectl get hpaNAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGEphp-apache Deployment/php-apache/scale 50% 305% 1 10 3m# autoscaler将这个deployment扩展为7个pod$ kubectl get deployment php-apacheNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEphp-apache 7 7 7 7 19m# 删除刚才创建的负载增加pod后会发现负载降低,并且pod数量也自动降回1个$ kubectl get hpaNAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGEphp-apache Deployment/php-apache/scale 50% 0% 1 10 11m$ kubectl get deployment php-apacheNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEphp-apache 1 1 1 1 27m
4、小结
1、首先最底层的资源永远都是pod
2、比pod高级一点的资源叫rc副本控制器
3、在pod上面有一个高级的rs
4、那谁来管理rs呢?是deployment
5、HPA自动管理deployment,deployment设置为1,HPA最低设置为3,deployment这就会被自动设计为3个