kubernetes自动扩缩容[HPA控制器 horizontal pod autoscaler]

kubernetes自动扩缩容[HPA控制器 horizontal pod autoscaler]

#查看当前hpa控制器版本:
[root@k8s-master01 ~]# kubectl get apiservices |grep autosca
v1.autoscaling                         Local                        True        17d
v2beta1.autoscaling                    Local                        True        17d
v2beta2.autoscaling                    Local                        True        17d


#版本区别  
v1.autoscaling        : v1是稳定版本,支持自定义cpu指标
v2beta1.autoscaling   : v2bata版本,v2beta1{支持cpu,内存和自定义指标}
v2beta2.autoscaling   : v2beta2版本(支持cpu,内存,自定义指标 custom和额外指标ExternalMerics)

kubernetes自动扩缩容[HPA控制器 horizontal pod autoscaler]

HPA实践

#注意: daemonset是不支持自动扩容的 因为他是在每一个节点部署一个服务,无法自动扩缩容
#使用HPA的前提,必须安装metrics-server或同等效果的服务器指标采集插件
#必须配置requests

#命令创建一个deployment
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --target-port=80 --type=NodePort



#1. 创建一个deployment,并暴露service端口80
kubectl create deployment hpa-nginx --image=registry.cn-beijing.aliyuncs.com/dotbalo/nginx --dry-run=client -oyaml >hpa-nginx.yaml

#2. 添加resources参数:
resources:
  cpu: 10m
  
# hpa-nginx.yaml 整个结果如下:
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: hpa-nginx
  name: hpa-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hpa-nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: hpa-nginx
    spec:
      containers:
      - image: registry.cn-beijing.aliyuncs.com/dotbalo/nginx
        name: nginx
        resources: 
          resources:
            cpu: 10m
status: {}

#3.yaml保存为 hpa-nginx.yaml 并执行
[root@k8s-master01 pod]# kubectl create -f hpa-nginx.yaml 
deployment.apps/hpa-nginx created



#3. 查看deployment状态
[root@k8s-master01 pod]# kubectl get pod -o wide
NAME         READY  STATUS   REST  AGE  IP              NODE         NOMINATED NODE    READINESS GATE
hpa-nginx-w  1/1    Running  0     2m3s 172.27.14.232   k8s-node02   <none>            <none>



#4 检查访问
[root@k8s-master01 pod]# curl 172.27.14.232
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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. 检查pod状态指标
[root@k8s-master01 pod]# kubectl top pod --use-protocol-buffers
NAME                         CPU(cores)   MEMORY(bytes)   
hpa-nginx-58ddb65c8d-mvt9f   0m           4Mi             
nginx-server                 0m           9Mi             
web-0                        0m           9Mi             
web-1                        0m           10Mi            
web-2                        0m           10Mi            


# 6. 添加自动扩缩容配置
[root@k8s-master01 pod]# kubectl autoscale deployment hpa-nginx --cpu-percent=10 --min=1 --max=10
horizontalpodautoscaler.autoscaling/hpa-nginx autoscaled


#检查配置:
[root@k8s-master01 pod]# kubectl get hpa
NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-nginx   Deployment/hpa-nginx   0%/10%    1         10        1          2m5s


[root@k8s-master01 pod]# kubectl get pod -o wide
NAME         READY  STATUS   RESTARTS  AGE  IP             NODE       NOMINATED NODE  READINESS GATES  
hpa-nginx    1/1    Running  0         13m  172.27.14.232  k8s-node02 <none>          <none>
nginx-server 1/1    Running  0         114m 172.17.125.21  k8s-node01 <none>          <none>
web-0        1/1    Running  0         28h  172.17.125.35  k8s-node01 <none>          <none>
web-1        1/1    Running   0        28h  172.27.14.233  k8s-node02 <none>          <none>
web-2        1/1    Running   0        28h  172.27.14.226  k8s-node02 <none>          <none>



#另外开启一个窗口 开始压力测试
while true; do wget -q -O- http://172.27.14.232 > /dev/null; done


# 隔一段时间 检查压力:
[root@k8s-master01 pod]# kubectl get hpa
NAME        REFERENCE              TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
hpa-nginx   Deployment/hpa-nginx   750%/10%   1         10        8          18m



#检查hpa扩缩容得情况:
[root@k8s-master01 pod]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
hpa-nginx-58ddb65c8d-68tf9   1/1     Running   0          47s
hpa-nginx-58ddb65c8d-99rdt   1/1     Running   0          47s
hpa-nginx-58ddb65c8d-c64h2   1/1     Running   0          47s
hpa-nginx-58ddb65c8d-ddzhs   1/1     Running   0          32s
hpa-nginx-58ddb65c8d-lg62l   1/1     Running   0          47s
hpa-nginx-58ddb65c8d-mvt9f   1/1     Running   0          28m
hpa-nginx-58ddb65c8d-p257w   1/1     Running   0          62s
hpa-nginx-58ddb65c8d-pxdv8   1/1     Running   0          62s
hpa-nginx-58ddb65c8d-q4cwc   1/1     Running   0          62s
hpa-nginx-58ddb65c8d-wkkn8   1/1     Running   0          32s
nginx-server                 1/1     Running   0          129m
web-0                        1/1     Running   0          28h
web-1                        1/1     Running   0          28h
web-2                        1/1     Running   0          28h


#已经自动扩容了
[root@k8s-master01 pod]# kubectl get pod -o wide
NAME              READY   STATUS    RESTARTS     IP             NODE         NOMINATED  READINESS GATES
hpa-nginx-68tf9   1/1     Running   0    103s   172.27.14.197   k8s-node02   <none>     <none>
hpa-nginx-99rdt   1/1     Running   0    103s   172.17.125.19   k8s-node01   <none>     <none>
hpa-nginx-c64h2   1/1     Running   0    103s   172.27.14.240   k8s-node02   <none>     <none>
hpa-nginx-ddzhs   1/1     Running   0    88s    172.17.125.32   k8s-node01   <none>     <none>
hpa-nginx-lg62l   1/1     Running   0    103s   172.17.125.33   k8s-node01   <none>     <none>
hpa-nginx-mvt9f   1/1     Running   0    29m    172.27.14.232   k8s-node02   <none>     <none>
hpa-nginx-p257w   1/1     Running   0    118s   172.17.125.6    k8s-node01   <none>     <none>
hpa-nginx-pxdv8   1/1     Running   0    118s   172.17.125.7    k8s-node01   <none>     <none>
hpa-nginx-q4cwc   1/1     Running   0    118s   172.27.14.235   k8s-node02   <none>     <none>
hpa-nginx-wkkn8   1/1     Running   0    88s    172.27.14.237   k8s-node02   <none>     <none>
上一篇:P4062 [Code+#1] Yazid 的新生舞会 - 线段树


下一篇:无重复字符的最长子串JavaScript实现