首页 > 其他分享 >52、Pod-弹性伸缩-HPA-HorizontalPodAutoscaler、metrics-server

52、Pod-弹性伸缩-HPA-HorizontalPodAutoscaler、metrics-server

时间:2023-04-10 14:15:18浏览次数:47  
标签:kubectl get deployment server metrics HorizontalPodAutoscaler hpa 52

Kubernetes学习目录

1、安装metrics-server

1.1、项目地址

https://github.com/kubernetes-sigs/metrics-server

当前版本:v0.6.3

主要用于获取资源的参数,不然HPA无法使用

1.2、下载yaml资源配置清单

wget https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.6.3/components.yaml

1.3、修改配置文件

1.3.1、准备离线镜像并且修改yaml

docker pull registry.k8s.io/metrics-server/metrics-server:v0.6.3
docker tag registry.k8s.io/metrics-server/metrics-server:v0.6.3 192.168.10.33:80/k8s/hpa/metrics-server:v0.6.3
docker push 192.168.10.33:80/k8s/hpa/metrics-server:v0.6.3


]# sed -i 's#registry.k8s.io/metrics-server/metrics-server:v0.6.3#192.168.10.33:80/k8s/hpa/metrics-server:v0.6.3#g' components.yaml 

]# grep 'image' components.yaml 
        image: 192.168.10.33:80/k8s/hpa/metrics-server:v0.6.3
        imagePullPolicy: IfNotPresent

1.3.2、修改启动命令

]# vi components.yaml
      - args:
        - --cert-dir=/tmp
        - --secure-port=4443
        - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
        - --kubelet-use-node-status-port
        - --metric-resolution=15s
        - --kubelet-insecure-tls  # 增加此行参数
        image: 192.168.10.33:80/k8s/hpa/metrics-server:v0.6.3
        imagePullPolicy: IfNotPresent

1.4、应用资源配置清单

]# kubectl apply -f components.yaml 
serviceaccount/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
service/metrics-server created
deployment.apps/metrics-server created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created

1.5、查询运行状态

]# kubectl -n kube-system get pods -o wide | grep metrics 
metrics-server-5b7dc79d77-6cfzv            1/1     Running   0                4m29s   10.244.4.124    node2     <none>           <none>

]# kubectl -n kube-system get svc
NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
kube-dns         ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP   7d13h
metrics-server   ClusterIP   10.97.76.234   <none>        443/TCP                  86m

2、HPA-实践

2.1、基本知识

2.1.1、HPA工作机制

2.1.2、前提需知

1、报错信息
]# kubectl describe hpa scalar
Events:
  Type     Reason                        Age                From                       Message
  ----     ------                        ----               ----                       -------
  Warning  FailedGetResourceMetric       12s (x2 over 27s)  horizontal-pod-autoscaler  failed to get cpu utilization: unable to get metrics for resource 
cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)
  Warning  FailedComputeMetricsReplicas  12s (x2 over 27s)  horizontal-pod-autoscaler  invalid metrics (1 invalid out of 1), first error is: failed to get cpu 
resource metric value: failed to get cpu utilization: unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not 
find the requested resource (get pods.metrics.k8s.io) 2、报错信息,TARGETS显示unknown ]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE deployment-hpa Deployment/deployment-hpa <unknown>/50% 1 10 1 106m 以上原因:没有安装metrics-server导致的。

2.2、创建deployment资源

2.2.1、定义资源配置清单且应用

kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-hpa
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rs-test
  template:
    metadata:
      labels:
        app: rs-test
    spec:
      containers:
      - name: nginxpod-test
        image: 192.168.10.33:80/k8s/pod_test:v0.1
        resources:
          limits:
            cpu: 100m
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
EOF

2.2.2、查询运行情况

]# kubectl get deployments.apps 
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
deployment-hpa   1/1     1            1           114m

]# kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
deployment-hpa-88878d778-d8hn9   1/1     Running   0          114m

2.3、创建HPA资源

2.3.1、参考官方文档

https://kubernetes.io/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/

2.3.2、v2版本语法-定义资源配置清单且应用

kubectl apply -f - <<EOF
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: scale
  namespace: default
spec:
  maxReplicas: 10
  minReplicas: 1
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deployment-hpa
EOF

# 注意:autoscaling/v2beta2 在v1.23之后不可以用,1.23+需要使用autoscaling/v2

2.3.3、v1版本语法-定义资源配置清单且应用

kubectl apply -f - <<EOF
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: deployment-hpa
  namespace: default
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deployment-hpa
  targetCPUUtilizationPercentage: 50
EOF

2.3.4、怎么写hpa yaml

]# kubectl autoscale deployment deployment-hpa --cpu-percent=50 --min=1 --max=10 --dry-run=client -o yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  creationTimestamp: null
  name: deployment-hpa
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: deployment-hpa
  targetCPUUtilizationPercentage: 50
status:
  currentReplicas: 0
  desiredReplicas: 0

2.3.5、查询运行结果

]# kubectl get hpa
NAME    REFERENCE                   TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
scale   Deployment/deployment-hpa   1%/50%    1         10        1          3m49s

2.4、压力测试pod

2.4.1、开启监控hpa

]# kubectl get hpa -w
NAME    REFERENCE                   TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
scale   Deployment/deployment-hpa   1%/50%    1         10        1          4m32s

2.4.2、进入pod进行压测

]# kubectl exec -it deployment-hpa-88878d778-d8hn9 -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"

2.4.3、观察hpa状态

]# kubectl get hpa -w
NAME    REFERENCE                   TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
scale   Deployment/deployment-hpa   1%/50%    1         10        1          4m32s
scale   Deployment/deployment-hpa   50%/50%   1         10        1          6m46s
scale   Deployment/deployment-hpa   100%/50%   1         10        1          7m1s

# 此时cpu跑到50+

2.4.4、查询deployment状态

]# kubectl  get deployments.apps 
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
deployment-hpa   2/2     2            2           140m

2.4.5、查询pod

]# kubectl  get pods
NAME                             READY   STATUS    RESTARTS   AGE
deployment-hpa-88878d778-d8hn9   1/1     Running   0          140m
deployment-hpa-88878d778-s5p5f   1/1     Running   0          19s
# 发现pod已经自动创建多一个出来了

2.5、自动缩容

# CPU负载下来,隔一段时间后,会自动缩容
]# kubectl get hpa
NAME    REFERENCE                   TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
scale   Deployment/deployment-hpa   1%/50%    1         10        1          13m

 

标签:kubectl,get,deployment,server,metrics,HorizontalPodAutoscaler,hpa,52
From: https://www.cnblogs.com/ygbh/p/17302398.html

相关文章

  • 【LeeCode】523.连续的子数组和
    【题目描述】给你一个整数数组 nums 和一个整数 k ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组:子数组大小 至少为2 ,且子数组元素总和为 k 的倍数。如果存在,返回 true ;否则,返回 false 。如果存在一个整数 n ,令整数 x 符合 x=n*k ,则称 x......
  • 52、K8S-监控机制-Prometheus【理论知识】
    Kubernetes学习目录1、监控基础1.1、监控机制1.1.1、数据层面我们如果要监控数据,首先得有数据,也就是说,这些数据应该可以被记录下来,或者被暴露出来,数据常见的产生、直接或间接暴露方式的方式如下:1、硬件本身的记录信息-以文件或者以内存属性的方式存在2、应用业务的接口-......
  • HJ52_计算字符串的编辑距离_动态规划_动态规划可视化
    思路:该题目符合最优解拥有最优子解,符合动态规划算法要求.2思路:操作方法有3种,替换、插入、删除。把a字符串编辑成b字符串的距离。3假设空字符串开始编辑作为bottom边界。4a字符串作为深度,b作为宽度。5沿宽度遍历为add,沿深度遍历为delete,斜角为change6判断是否相......
  • FP5217兼容TPS61178,内置MOS双节锂电池升压输出12V/3A异步升压芯片
    FP5217是一顆非同步电流模式DC-DC升压转换器,内置MOS,输入低启动电压2.5V与电压工作范围5V~24V,单节锂电池3V~4.2V应用,能精准地反馈电压1.2V,内置软启动时间,外部可编程工作频率,可编程电感器峰值电流限制将电阻从CSPin连接到GND。封装:TSSOP-14(EP)。应用:蓝牙音响,大功率拉杆音箱,应......
  • LibreOJ526. 「LibreOJ β Round #4」子集
    简要题意给出一个长度为\(n\)的序列\(a\)。你需要选出它的一个子集\(S\)。使其满足对于任意两个元素\(i,j\)。满足:\[\gcd(i,j)\cdot\gcd(i+1,j+1)\neq1\]输出满足条件的子集大小的最大值。\(1\leqn\leq500,1\leqa_i\leq10^{18}\)思路这道题比CodeforcesGym1......
  • H3C S5500V2-52C-EI SNMP 配置
    1、登录设备2、配置system-viewsnmp-agentcommunityreadsimpleAdmin-123snmp-agentsys-infoversionv2cv3snmp-agenttarget-hosttrapaddressudp-domain172.16.24.110paramssecuritynameAdmin-123v2csnmp-agenttrapsourceVlan-interface1023、查看配......
  • K8S Metrics Server安装
     kubectlapply-f metrics-server-components.yaml apiVersion:v1kind:ServiceAccountmetadata:labels:k8s-app:metrics-servername:metrics-servernamespace:kube-system---apiVersion:rbac.authorization.k8s.io/v1kind:ClusterRolemetada......
  • 洛谷P1552 [APIO2012] 派遣 题解 左偏树
    题目链接:https://www.luogu.com.cn/problem/P1552题目大意:每次求子树中薪水和不超过\(M\)的最大节点数。解题思路:使用左偏树维护一个大根堆。首先定义一个Node的结构体:structNode{ints[2],c,sz,dis;longlongsum;Node(){};Node(int_c){s......
  • 6·2HTTPS协议概述|6·32HTTPS使用成本|6·42HTTPS对性能的影响|6·52HTTPS常见问题
    HTTPS可以认为是HTTPS+TLSTLS是传输层加密协议,它的前身是SSL协议HTTPS功能介绍 内容加密 非对称密钥交换 对称内容加密 身份认证 数字证书 数据完整性 HTTPS使用成本 证书费用以及更新维护 HT......
  • 算法随想Day52【单调栈】| LC503-下一个更大元素Ⅱ、LC42-接雨水
    LC503.下一个更大元素Ⅱ对于“每日温度”,相当于对nums数组,进行了两次遍历。用i%size所得余数作为下标,且循环的圈数为size*2vector<int>nextGreaterElements(vector<int>&nums){intsize=nums.size();vector<int>result(size,-1);vector<int>sta;......