k8s autoscale命令

Posted caonw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s autoscale命令相关的知识,希望对你有一定的参考价值。

1> 创建测试资源

1.命令获取deployment yaml模板

kubectl create deployment web --image=nginx:1.14 -o yaml --dry-run  >deploy-web.yaml 

修改deployment -web模板,添加pod限制

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - image: nginx:1.14
        name: nginx
        resources:
          limits:
            cpu: 200m
            memory: 200Mi
          requests:
            cpu: 200m
            memory: 200Mi

创建deployment-web

[root@k8s-master1 hpa]# kubectl create -f deploy-web.yaml 

2.生成autoscale  yaml模板

[root@k8s-master1 hpa]# kubectl autoscale deployment web  --min=2 --max=10 -o yaml --dry-run >hpa-web.yaml

修改hpa-web模板

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: web
spec:
  maxReplicas: 10
  minReplicas: 2
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  targetCPUUtilizationPercentage: 40

创建hpa-web

[root@k8s-master1 hpa]# kubectl create -f hpa-web.yaml 
horizontalpodautoscaler.autoscaling/web created

查看信息(web已经自动扩容为2个)

[root@k8s-master1 hpa]# kubectl get pod,hpa 
NAME                          READY   STATUS    RESTARTS   AGE
pod/busybox-97b9f6974-grtzd   1/1     Running   0          22h
pod/test-74695b8467-8lwrq     1/1     Running   0          12m
pod/web-5f8765dd69-dzv6l      1/1     Running   0          5m44s
pod/web-5f8765dd69-pq2nv      1/1     Running   0          5m58s

NAME                                      REFERENCE        TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/web   Deployment/web   0%/40%    2         10        2          23s

3.创建service yaml模板

[root@k8s-master1 hpa]# kubectl create service  nodeport web --tcp=80:80 -o yaml --dry-run >service-web.yaml

 修改service-web模板

apiVersion: v1
kind: Service
metadata:
  labels:
    app: web
  name: web
spec:
  ports:
  - name: web
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: NodePort

创建service

[root@k8s-master1 hpa]# kubectl create -f service-web.yaml 
service/web created

2> 查看刚刚创建的资源

[root@k8s-master1 ~]# kubectl get pod,deploy,svc,hpa  -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
pod/busybox-97b9f6974-grtzd   1/1     Running   0          22h   10.244.169.154   k8s-node2   <none>           <none>
pod/test-74695b8467-8lwrq     1/1     Running   0          21m   10.244.107.194   k8s-node3   <none>           <none>
pod/web-5f8765dd69-dzv6l      1/1     Running   0          14m   10.244.36.98     k8s-node1   <none>           <none>
pod/web-5f8765dd69-pq2nv      1/1     Running   0          14m   10.244.107.195   k8s-node3   <none>           <none>

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES       SELECTOR
deployment.apps/busybox   1/1     1            1           2d20h   busybox      busybox      run=busybox
deployment.apps/test      1/1     1            1           21m     test         nginx:1.14   run=test
deployment.apps/web       2/2     2            2           24m     nginx        nginx:1.14   app=web

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE     SELECTOR
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        42d     <none>
service/web          NodePort    10.0.0.81    <none>        80:30779/TCP   4m21s   app=web

NAME                                      REFERENCE        TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/web   Deployment/web   0%/40%    2         10        2          9m25s

3> 安装ab进行测试

[root@k8s-master1 ~]# ab -n 1000000 -c 10 -k http://10.0.0.81/
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 10.0.0.81 (be patient)

 

 查看pod日志

[root@k8s-master1 ~]# kubectl logs web-5f8765dd69-dzv6l -f
10.23.215.240 - - [09/Jul/2020:06:33:17 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.23.215.240 - - [09/Jul/2020:06:33:17 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.23.215.240 - - [09/Jul/2020:06:33:17 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.23.215.240 - - [09/Jul/2020:06:33:17 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"
10.23.215.240 - - [09/Jul/2020:06:33:17 +0000] "GET / HTTP/1.0" 200 612 "-" "ApacheBench/2.3" "-"

 查看pod

技术图片

 

以上是关于k8s autoscale命令的主要内容,如果未能解决你的问题,请参考以下文章

Kubernetes -- Horizontal Pod Autoscaler

kubectl 命令详解:autoscale

如何为 kops 在 AWS 上安装的 k8s 做集群自动扩缩器?

使用 Prometheus 适配器的 Horizo​​ntal Pod Autoscaler (HPA) 自定义指标(单位是如何定义的?)

k8s HPA自动收缩

Kubernetes 节点 CPU 利用率