一、启动php
[root@k8s-master ~]# kubectl autoscale deployment nginx-deployment --max=8 --min=2 --cpu-percent=80 deployment "nginx-deployment" autoscaled
二、查看建立node
[root@k8s-master ~]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 2 2 2 2 13h NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE hpa/nginx-deployment Deployment/nginx-deployment 80% 0% 2 8 17s NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d svc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-2950479891 0 0 0 13h rs/nginx-deployment-3113009173 2 2 2 13h NAME READY STATUS RESTARTS AGE po/nginx-deployment-3113009173-h5plc 1/1 Running 0 17s po/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
三、修改副本数为1nginx
[root@k8s-master ~]# kubectl edit deployment nginx-deployment 修改成1 replicas: 1 deployment "nginx-deployment" edited [root@k8s-master ~]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 1 1 1 1 13h NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d svc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-2950479891 0 0 0 13h rs/nginx-deployment-3113009173 1 1 1 13h NAME READY STATUS RESTARTS AGE po/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
[root@k8s-master ~]# kubectl get horizontalpodautoscaler NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE nginx-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/v1 kind: HorizontalPodAutoscaler metadata: 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-000c292bd9e1 spec: maxReplicas: 8 minReplicas: 2 scaleTargetRef: apiVersion: extensions/v1beta1 kind: Deployment name: nginx-deployment targetCPUUtilizationPercentage: 80 status: currentCPUUtilizationPercentage: 0 currentReplicas: 2 desiredReplicas: 2 lastScaleTime: 2019-01-22T01:00:02Z
[root@k8s-master ~]# kubectl edit deployment nginx-deployment replicas: 1 deployment "nginx-deployment" edited [root@k8s-master ~]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 2 2 2 2 13h NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE hpa/nginx-deployment Deployment/nginx-deployment 80% 0% 2 8 6m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d svc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-2950479891 0 0 0 13h rs/nginx-deployment-3113009173 2 2 2 13h NAME READY STATUS RESTARTS AGE po/nginx-deployment-3113009173-9hlq1 1/1 Running 0 2s po/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
明明修改成1,怎么还有2个?是由于hpa以下配置apache
spec: maxReplicas: 8 minReplicas: 2
[root@k8s-master ~]# kubectl edit hpa nginx-deployment 修改: spec: maxReplicas: 8 minReplicas: 5 horizontalpodautoscaler "nginx-deployment" edited [root@k8s-master ~]# kubectl get all NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deploy/nginx-deployment 5 5 5 2 13h NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE hpa/nginx-deployment Deployment/nginx-deployment 80% 0% 5 8 8m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.254.0.1 <none> 443/TCP 2d svc/nginx 10.254.145.15 <nodes> 80:32000/TCP 1d NAME DESIRED CURRENT READY AGE rs/nginx-deployment-2950479891 0 0 0 13h rs/nginx-deployment-3113009173 5 5 2 13h NAME READY STATUS RESTARTS AGE po/nginx-deployment-3113009173-97l9c 0/1 ContainerCreating 0 2s po/nginx-deployment-3113009173-9hlq1 1/1 Running 0 2m po/nginx-deployment-3113009173-qq4h8 0/1 ContainerCreating 0 2s po/nginx-deployment-3113009173-sfp8z 0/1 ContainerCreating 0 2s po/nginx-deployment-3113009173-vckhg 1/1 Running 1 13h
看到自动伸缩的过程了吧!api
一、什么是hpa缓存
Horizontal Pod Autoscaling能够根据CPU使用率或应用自定义metrics自动扩展Pod数量(支持replication controller、deployment和replica set)。bash
客户端;ui
经过kubectl建立一个horizontalPodAutoscaler对象,并存储到etcd中this
服务端:google
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.
二、示例
# 建立pod和service $ kubectl run php-apache --image=gcr.io/google_containers/hpa-example --requests=cpu=200m --expose --port=80 service "php-apache" created deployment "php-apache" created # 建立autoscaler $ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 deployment "php-apache" autoscaled $ kubectl get hpa NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE php-apache Deployment/php-apache/scale 50% 0% 1 10 18s # 增长负载 $ kubectl run -i --tty load-generator --image=busybox /bin/sh Hit enter for command prompt $ while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done # 过一会就能够看到负载升高了 $ kubectl get hpa NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE php-apache Deployment/php-apache/scale 50% 305% 1 10 3m # autoscaler将这个deployment扩展为7个pod $ kubectl get deployment php-apache NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE php-apache 7 7 7 7 19m # 删除刚才建立的负载增长pod后会发现负载下降,而且pod数量也自动降回1个 $ kubectl get hpa NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE php-apache Deployment/php-apache/scale 50% 0% 1 10 11m $ kubectl get deployment php-apache NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE php-apache 1 1 1 1 27m
四、小结
一、首先最底层的资源永远都是pod
二、比pod高级一点的资源叫rc副本控制器
三、在pod上面有一个高级的rs
四、那谁来管理rs呢?是deployment
五、HPA自动管理deployment,deployment设置为1,HPA最低设置为3,deployment这就会被自动设计为3个