kubernetes云平台管理实战:HPA水平自动伸缩(十一)

时间:2023-12-26 13:39:25

一、自动伸缩

1、启动

[root@k8s-master ~]# kubectl autoscale deployment nginx-deployment --max=8 --min=2 --cpu-percent=80
deployment "nginx-deployment" autoscaled

2、查看创建

[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

3、修改副本数为1

[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

二、hpa伸缩

1、编辑horizontalpodautoscaler 文件

[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

2、修改副本为1

[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如下配置

spec:
maxReplicas: 8
minReplicas: 2

3、修改副本数为5

[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

看到自动伸缩的过程了吧!

三、HAP-deployment-rs-rc-pod调用原理

1、什么是hpa

Horizontal Pod Autoscaling可以根据CPU使用率或应用自定义metrics自动扩展Pod数量(支持replication controller、deployment和replica set)。

  • 控制管理器每隔30s(可以通过–horizontal-pod-autoscaler-sync-period修改)查询metrics的资源使用情况
  • 支持三种metrics类型
    • 预定义metrics(比如Pod的CPU)以利用率的方式计算
    • 自定义的Pod metrics,以原始值(raw value)的方式计算
    • 自定义的object metrics
  • 支持两种metrics查询方式:Heapster和自定义的REST API
  • 支持多metrics

客户端;

通过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=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

3、关系原理图 

kubernetes云平台管理实战:HPA水平自动伸缩(十一)

4、小结

1、首先最底层的资源永远都是pod
2、比pod高级一点的资源叫rc副本控制器
3、在pod上面有一个高级的rs
4、那谁来管理rs呢?是deployment
5、HPA自动管理deployment,deployment设置为1,HPA最低设置为3,deployment这就会被自动设计为3个