hpa应用例子

环境:

OS:Centos 7

k8s:1.24.0

 

1.开启 API Aggregator

该步骤只在master节点上操作

如下图所示添加如下项目

vi /etc/kubernetes/manifests/kube-apiserver.yaml

- --enable-aggregator-routing=true

 文件保存后k8s会自动重启

 

2.安装metrics-server

k8s默认是没有安装该组件的,需要单独安装,安装参考如下连接

https://www.cnblogs.com/hxlasky/p/18818497

 

3.安装ab工具

在k8s其中一个节点上安装即可

yum install httpd-tools

确保有如下命令

[root@node2 home]# which ab
/usr/bin/ab

 

4.部署deployment

我这里使用nginx做为部署例子使用

提前创建好名称空间

kubectl create namespace ns-hpa

[root@master hpa]# more nginx-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: ns-hpa
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: registry.cn-shenzhen.aliyuncs.com/hxlk8s/nginx:stable-alpine
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: "200m"
          requests:
            cpu: "100m"

 

kubectl apply -f nginx-deployment.yaml

 

4.部署service对外服务(nodeport)

[root@master hpa]# more nginx-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-sc
  namespace: ns-hpa
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - name: nginx-port
    port: 80
    targetPort: 80
    protocol: TCP
    nodePort: 30000

kubectl apply -f nginx-nodeport.yaml

 

这里部署好了之后,尝试使用如下命令进行访问

[root@node2 home]# curl http://192.168.1.105:30000/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

或是浏览器访问

 

 

5.部署hpa

[root@master hpa]# more hpa.yaml 
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-deployment-hpa
  namespace: ns-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-deployment
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 10

这里设置通过cpu利用率进行扩容,超过10%就会触发扩容

kubectl apply -f hpa.yaml

 

6.压测模拟触发

先观察pod运行情况

[root@master hpa]# kubectl get pods -n ns-hpa -w
NAME                               READY   STATUS    RESTARTS        AGE
nginx-deployment-cb5988cbd-tchrh   1/1     Running   1 (6h41m ago)   22h
nginx-deployment-cb5988cbd-94pbp   0/1     Pending   0               0s
nginx-deployment-cb5988cbd-94pbp   0/1     Pending   0               0s
nginx-deployment-cb5988cbd-z8bq4   0/1     Pending   0               0s
nginx-deployment-cb5988cbd-bpr8s   0/1     Pending   0               0s
nginx-deployment-cb5988cbd-z8bq4   0/1     Pending   0               0s
nginx-deployment-cb5988cbd-bpr8s   0/1     Pending   0               0s

 

执行压测命令

[root@node2 home]# ab -n 100000 -c 800 http://192.168.1.105:30000/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.105 (be patient)

可以看到pod进行了扩容

[root@master ~]# kubectl get pods -A
NAMESPACE         NAME                                             READY   STATUS    RESTARTS        AGE
cert-manager      cert-manager-bcc6fc947-g4jvd                     1/1     Running   6 (12m ago)     7d22h
cert-manager      cert-manager-cainjector-74b489f78f-pmh8m         1/1     Running   9 (12m ago)     7d22h
cert-manager      cert-manager-webhook-68dc7956b4-n2j4j            1/1     Running   6 (6h46m ago)   7d22h
kube-system       calico-kube-controllers-6b668cd977-lv54j         1/1     Running   5 (6h46m ago)   7d23h
kube-system       calico-node-gpdrp                                1/1     Running   6 (6h46m ago)   7d23h
kube-system       calico-node-klbrm                                1/1     Running   5 (6h46m ago)   7d23h
kube-system       calico-node-vtmch                                1/1     Running   5 (6h46m ago)   7d23h
kube-system       coredns-c676cc86f-6rrp4                          1/1     Running   6 (6h46m ago)   7d21h
kube-system       coredns-c676cc86f-pjl52                          1/1     Running   5 (6h46m ago)   7d21h
kube-system       etcd-master                                      1/1     Running   6 (6h46m ago)   7d21h
kube-system       kube-apiserver-master                            1/1     Running   0               12m
kube-system       kube-controller-manager-master                   1/1     Running   6 (12m ago)     7d21h
kube-system       kube-proxy-ptb4h                                 1/1     Running   6 (6h46m ago)   7d21h
kube-system       kube-proxy-v6b8x                                 1/1     Running   5 (6h46m ago)   7d21h
kube-system       kube-proxy-xjmxk                                 1/1     Running   5 (6h46m ago)   7d21h
kube-system       kube-scheduler-master                            1/1     Running   6 (12m ago)     7d21h
kube-system       metrics-server-7667bfbfbb-vrn6z                  1/1     Running   0               75m
middleware        milvus-dev-milvus-datanode-555c8859fc-nqdjw      1/1     Running   4 (6h46m ago)   6d22h
middleware        milvus-dev-milvus-indexnode-68f89cbb65-bj6p8     1/1     Running   4 (6h46m ago)   6d22h
middleware        milvus-dev-milvus-mixcoord-5879ddb6bf-zq5qb      1/1     Running   4 (6h46m ago)   6d22h
middleware        milvus-dev-milvus-proxy-6d7656657f-pwm29         1/1     Running   4 (6h46m ago)   6d22h
middleware        milvus-dev-milvus-querynode-0-5cd5fb96b6-j5xzc   1/1     Running   4 (6h46m ago)   6d22h
milvus-operator   milvus-operator-75bccd859c-gxj2j                 1/1     Running   13 (12m ago)    7d22h
ns-hpa            nginx-deployment-cb5988cbd-446pq                 1/1     Running   0               3m46s
ns-hpa            nginx-deployment-cb5988cbd-6l2k7                 1/1     Running   0               4m1s
ns-hpa            nginx-deployment-cb5988cbd-94pbp                 1/1     Running   0               4m16s
ns-hpa            nginx-deployment-cb5988cbd-bpr8s                 1/1     Running   0               4m16s
ns-hpa            nginx-deployment-cb5988cbd-m6fkq                 1/1     Running   0               4m1s
ns-hpa            nginx-deployment-cb5988cbd-nn24p                 1/1     Running   0               4m1s
ns-hpa            nginx-deployment-cb5988cbd-pbdmk                 1/1     Running   0               3m46s
ns-hpa            nginx-deployment-cb5988cbd-rbzh2                 1/1     Running   0               4m1s
ns-hpa            nginx-deployment-cb5988cbd-tchrh                 1/1     Running   1 (6h46m ago)   22h
ns-hpa            nginx-deployment-cb5988cbd-z8bq4                 1/1     Running   0               4m16s
ns-mysql          hxl-mysql-deploy-5c579f8d6f-qp55t                1/1     Running   3 (6h46m ago)   2d22h
velero            velero-75cd89cf9b-fjvcw                          1/1     Running   4 (6h46m ago)   6d22h

 

压测完成后,过1-2分钟,hpa会进行自动缩容。

 

posted @ 2025-04-10 16:02  slnngk  阅读(25)  评论(0)    收藏  举报