HPA认识

Pod 自动扩缩容

        Kubernetes提供了这样一个资源对象: Horizontal Pod Autoscaling Pod水平自动伸缩),简称HPA。HAP通过监控分析RC或者Deployment控制的所有Pod的负载变化情况来确定是否需要调整Pod的副本数量,这是HPA最基本的原理。

img
        HPA在kubernetes集群中被设计成一个Kubernetes API资源和控制器,可以通过kubectl autoscale命令来创建一个HPA资源对象,HPA Controller默认15s轮询一次(可通过kube-controller-manager的标志–horizontal-pod-autoscaler-sync-period进行设置),查询指定的资源(RC或者Deployment)中Pod的资源使用率,并且与创建时设定的值和指标做对比,从而实现自动伸缩的功能。
详细介绍

Pod水平自动伸缩练习

        由于Horizontal Pod Autoscaler使用此API收集指标,因此需要在群集中部署metrics-server监视以通过资源指标API提供指标,

运行php-apache服务器

首先,我们将开始运行该映像的部署,并将其服务公开

1
2
3
4
$ kubectl run php-apache --image=0layfolk0/hpa-example --requests=cpu=200m --limits=cpu=500m --expose --port=80
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
service/php-apache created
deployment.apps/php-apache created

创建水平Pod自动缩放器

        当服务运行以后。我们将使用kubectl autoscale创建自动 缩放器。以下命令将创建一个水平Pod自动缩放器,该缩放器将维护由我们在这些说明的第一步中创建的php-apache部署控制的Pod的1至10个副本。粗略地说,HPA将(通过部署)增加或减少副本数,以将所有Pod的平均CPU利用率维持在50%(因为每个pod通过kubectl运行请求200毫核,这意味着平均CPU利用率为100毫-核心)。算法更多信息

1
2
$ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
horizontalpodautoscaler.autoscaling/php-apache autoscaled

我们可以通过运行以下命令检查自动定标器的当前状态:

1
2
3
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 0%/50% 1 10 1 14s

注释: 由于我们没有向服务器发送任何请求,因此当前CPU消耗为0%(“ CURRENT”列显示了由相应部署控制的所有Pod的平均值)。

增加压力测试

        现在我们要对php-apache做压力测试来观看自动缩放如何对增加的负载做出反应,我们将启动一个容器,并将无限循环的查询发送到php-apache服务。

1
2
3
4
5
6
$ kubectl run -i --tty load-generator --image=busybox /bin/sh
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
If you don't see a command prompt, try pressing enter.

/ # while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done
OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!O

在一分钟左右的时间内,我们应该通过执行以下命令来看到更高的CPU负载:

1
2
3
4
5
6
7
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 250%/50% 1 10 1 9m12s

$ kubectl get deployment php-apache
NAME READY UP-TO-DATE AVAILABLE AGE
php-apache 3/5 5 3 88m

这里由于网络问题和pull 镜像太慢了,我就直接结束了测试

停止压力测试

我们在busybox容器的终端里面执行<Ctrl> + C来结束压力测试,然后我们在观察结果

1
2
3
4
5
6
7
$  kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 91%/50% 1 10 5 10m

$ kubectl get deployment php-apache
NAME READY UP-TO-DATE AVAILABLE AGE
php-apache 2/2 2 2 99m

自动缩放多个指标和自定义指标

nginx 测试

        利用之前Deployment里面的nginx做测试,我们只需要吧之前的yaml文件稍作修改即可

修改nginx-deployment.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ cat > nginx-deployment.yaml <<EOF
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx-deploy
labels:
k8s-app: nginx-deploy
spec:
replicas: 1
minReadySeconds: 5
revisionHistoryLimit: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: nginx-deploy
spec:
containers:
- name: nginx-deploy
image: nginx:1.13.3
ports:
- containerPort: 80
resources:
requests:
memory: "200Mi"
cpu: "200m"
EOF

新建立nginx-deploy-hpa.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ cat > nginx-deploy-hpa.yaml <<EOF
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: nginx-deploy
namespace: default
spec:
maxReplicas: 5
minReplicas: 1
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: nginx-deploy
targetCPUUtilizationPercentage: 10
status:
currentCPUUtilizationPercentage: 8
currentReplicas: 1
desiredReplicas: 0
  • 执行创建

    1
    2
    $ kubectl apply -f nginx-deployment.yaml
    $ kubectl apply -f kubectl apply -f nginx-deploy-hpa.yaml
  • 查看验证

    1
    2
    3
    4
    5
    6
    7
    8
    $ kubectl get hpa
    NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
    nginx-deploy Deployment/nginx-deploy 0%/10% 1 5 2 45s

    $ kubectl get rs
    NAME DESIRED CURRENT READY AGE
    load-generator-7fbcc7489f 1 1 1 8m28s
    nginx-deploy-d494b9564 2 2 2 13m
  • 执行压力测试

    1
    2
    3
    4
    5
    $ kubectl run -i --tty load-generator --image=busybox /bin/sh
    kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
    If you don't see a command prompt, try pressing enter.

    / # while true; do wget -q -O- http://172.30.224.5:80; done
  • 查看效果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ kubectl get hpa
    NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
    nginx-deploy Deployment/nginx-deploy 28%/10% 1 5 4 4m48s

    $ kubectl get rs
    NAME DESIRED CURRENT READY AGE
    load-generator-7fbcc7489f 1 1 1 12m
    nginx-deploy-d494b9564 5 5 5 18m

    $ kubectl get hpa
    NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
    nginx-deploy Deployment/nginx-deploy 16%/10% 1 5 5 5m39s
  • 结束压测
    等待一会查看结果

    1
    2
    3
    4
    5
    6
    7
    8
    $ kubectl get hpa
    NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
    nginx-deploy Deployment/nginx-deploy 0%/10% 1 5 1 12m

    $ kubectl get rs
    NAME DESIRED CURRENT READY AGE
    load-generator-7fbcc7489f 1 1 1 19m
    nginx-deploy-d494b9564 1 1 1 25m
坚持原创技术分享,您的支持将鼓励我继续创作!
0%