首页 > 其他分享 >k8s 基于hpa弹性伸缩实验

k8s 基于hpa弹性伸缩实验

时间:2022-11-28 17:59:02浏览次数:66  
标签:kubectl 伸缩 name mem demo hpa k8s

HPA 基本原理

 kubectl scale 命令可以来实现 Pod 的扩缩容功能,但是这个毕竟是完全手动操作的,要应对线上的各种复杂情况,我们需要能够做到自动化去感知业务,来自动进行扩缩容。为此,Kubernetes 也为我们提供了这样的一个资源对象:Horizontal Pod Autoscaling(Pod 水平自动伸缩),简称HPA,HPA 通过监控分析一些控制器控制的所有 Pod 的负载变化情况来确定是否需要调整 Pod 的副本数量,这是 HPA 最基本的原理:

 

 

 

我们可以简单的通过 kubectl autoscale 命令来创建一个 HPA 资源对象,HPA 基本原理(可通过 kube-controller-manager 的--horizontal-pod-autoscaler-sync-period 参数进行设置),查询指定的资源中的 Pod 资源使用率,并且与创建时设定的值和指标做对比,从而实现自动伸缩的功能。

Metrics Server

在 HPA 的第一个版本中,我们需要 Heapster 提供 CPU 和内存指标,在 HPA v2 过后就需要安装 Metrcis Server 了,Metrics Server 可以通过标准的 Kubernetes API 把监控数据暴露出来,有了 Metrics Server 之后,我们就完全可以通过标准的 Kubernetes API 来访问我们想要获取的监控数据了:

https://10.96.0.1/apis/metrics.k8s.io/v1beta1/namespaces/<namespace-name>/pods/<pod-name>

比如当我们访问上面的 API 的时候,我们就可以获取到该 Pod 的资源数据,这些数据其实是来自于 kubelet 的 Summary API 采集而来的。不过需要说明的是我们这里可以通过标准的 API 来获取资源监控数据,并不是因为 Metrics Server 就是 APIServer 的一部分,而是通过 Kubernetes 提供的 Aggregator 汇聚插件来实现的,是独立于 APIServer 之外运行的。

HPA Metrics Server

 

HPA 基于 CPU自动扩缩容

[root@k8s-master01 k8s]# cat hpa-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hpa-demo
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: 50Mi
            cpu: 50m

创建hpa

 kubectl autoscale deployment hpa-demo --cpu-percent=10 --min=1 --max=10

查看hpa情况。

[root@k8s-master01 k8s]# kubectl get hpa
NAME       REFERENCE             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-demo   Deployment/hpa-demo   0%/10%    1         10        4          23s

执行访问

 kubectl exec -it busybox -- /bin/sh

$ kubectl run -it --image busybox test-hpa --restart=Never --rm /bin/sh
If you don't see a command prompt, try pressing enter.
/ # while true; do wget -q -O- http://10.244.4.97; done

 

查看hpa使用情况。

[root@k8s-master01 k8s]# kubectl get hpa
NAME       REFERENCE             TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
hpa-demo   Deployment/hpa-demo   548%/10%   1         10        1          11m

[root@k8s-master01 k8s]# kubectl get pods
NAME                      READY   STATUS              RESTARTS              AGE
busybox                   1/1     Running             336 (<invalid> ago)   13d
hpa-demo-f5597c99-4bnfp   0/1     ContainerCreating   0                     4s
hpa-demo-f5597c99-gxx7w   1/1     Running             0                     11m
hpa-demo-f5597c99-h99s5   0/1     ContainerCreating   0                     4s
hpa-demo-f5597c99-kbw95   0/1     ContainerCreating   0                     4s
hpa-demo-f5597c99-kg8vl   1/1     Running             0                     19s
hpa-demo-f5597c99-kt2m6   0/1     ContainerCreating   0                     4s
hpa-demo-f5597c99-rxrq5   1/1     Running             0                     19s
hpa-demo-f5597c99-wzl8f   1/1     Running             0                     19s

 

HPA 基于 内存自动扩缩容

[root@k8s-master01 k8s]# cat hpa-mem-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hpa-mem-demo
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
      - name: increase-mem-script
        configMap:
          name: increase-mem-config
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: increase-mem-script
          mountPath: /etc/script
        resources:
          requests:
            memory: 50Mi
            cpu: 50m
        securityContext:
          privileged: true

 

[root@k8s-master01 k8s]# cat increase-mem-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: increase-mem-config
data:
  increase-mem.sh: |
    #!/bin/bash
    mkdir /tmp/memory  
    mount -t tmpfs -o size=40M tmpfs /tmp/memory  
    dd if=/dev/zero of=/tmp/memory/block  
    sleep 60
    rm /tmp/memory/block  
    umount /tmp/memory  
    rmdir /tmp/memory

 

[root@k8s-master01 k8s]# cat hpa-mem.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-hpa
spec:
  maxReplicas: 5
  metrics:
  - resource:
      name: memory
      target:
        averageUtilization: 60
        type: Utilization
    type: Resource
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: hpa-mem-demo

[root@k8s-master01 k8s]# kubectl get hpa
NAME        REFERENCE                 TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
nginx-hpa   Deployment/hpa-mem-demo   24%/60%   1         5         1          73s

[root@k8s-master01 k8s]# kubectl exec -it hpa-mem-demo-6f6455888b-w8s5j -- /bin/bash

root@hpa-mem-demo-6f6455888b-w8s5j:/# source /etc/script/increase-mem.sh
dd: writing to '/tmp/memory/block': No space left on device
81921+0 records in
81920+0 records out
41943040 bytes (42 MB, 40 MiB) copied, 0.870494 s, 48.2 MB/s

 

[root@k8s-master01 ~]# kubectl get hpa
NAME        REFERENCE                 TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
nginx-hpa   Deployment/hpa-mem-demo   108%/60%   1         5         2          4m13s

[root@k8s-master01 ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS              AGE
busybox                         1/1     Running   342 (<invalid> ago)   14d
hpa-mem-demo-6f6455888b-h5f6w   1/1     Running   0                     2m15s
hpa-mem-demo-6f6455888b-tzztc   1/1     Running   0                     105s
hpa-mem-demo-6f6455888b-w8s5j   1/1     Running   0                     5h22m

 

标签:kubectl,伸缩,name,mem,demo,hpa,k8s
From: https://www.cnblogs.com/fenghua001/p/16932874.html

相关文章

  • k8s容器中通过Prometheus Operator部署Kafka Exporter监控Kafka集群
    写在前面在按照下面步骤操作之前,请先确保服务器已经部署k8s,prometheus,prometheusoperator以及kafka集群,关于这些环境的部署,可以自行查找相关资料安装部署,本文档便不在此......
  • 在非k8s 环境下 的应用 使用 Dapr Sidekick for .NET
    在k8s环境下,通过Operator可以管理Daprsidecar,在虚拟机环境下,我们也是非常需要这样的一个管理组件,类似下图:​​​​在这张图片中,在上图左面,我们看到了“dapr.exe”、我们......
  • 卸载K8S集群
    一、配置命令自动补全yuminstall-ybash-completionsource/usr/share/bash-completion/bash_completionsource<(kubectlcompletionbash)echo"source<(kubect......
  • 【云原生】无VIP稳定性和可扩展性更强的k8s高可用方案讲解与实战操作
    目录一、概述二、架构三、开始部署1)节点信息2)前期准备(所有节点)1、配置hosts2、配置互信3、时间同步4、关闭防火墙5、禁用SELinux6、关闭swap7、设置bridge-nf-call-iptable......
  • kubeasz 安装二进制 k8s
    一、环境软件版本:服务版本操作系统Ubuntu22.04.1LTS运行时containerd.io1.6.8kubeasz3.4.1k8sv1.25.3网络插件calicov3.23.3节点说明:类型IP主机名服务/组件集群控制节点......
  • K8S curl: (35) SSL received a record that exceeded the maximum permissible leng
    K8S curl:(35)SSLreceivedarecordthatexceededthemaximumpermissiblelength现象  分析分析排查之后发现是访问的端口不对,访问的30838为http映射端口,查......
  • k8s部署应用精简示例(nginx)模板
    创建一个namespace1kind:Namespace2apiVersion:v13metadata:4name:nginx-test创建一个configmap1apiVersion:v12kind:ConfigMap3metadata:4......
  • K8S日志报错-01未初始化
    Nov2712:57:35k8s-master01systemd:Unitkubelet.serviceenteredfailedstate.Nov2712:57:35k8s-master01systemd:kubelet.servicefailed.Nov2712:57:45k8......
  • K8S初始化失败-01
    [root@k8s-master01~]#kubeadminit--config/root/new.yaml--upload-certs[init]UsingKubernetesversion:v1.20.15[preflight]Runningpre-flightcheckserrore......
  • k8s--数据存储、HostPath、NFS 存储
    HostPath在使用EmptyDir的时候,EmptyDir中数据不会被持久化,它会随着pod的结束而销毁,如果想简单的将数据持久化到主机中,可以选择HostPathHostPath就是将Node主机......