k8s pod 自动水平扩展

1. 当前环境

kubernetes        v1.17
metrics-server    v0.3.6

要实现hpa,metrics-server 需要部署到集群中, 它可以通过 resource metrics API 对外提供度量数据,Horizontal Pod Autoscaler 正是根据此 API 来获取度量数据。

2. 部署metrics-server

k8s部署参考网站 https://github.com/kubernetes-sigs/metrics-server/tree/master/deploy/kubernetes

  • metrics-server不能使用,报错不能解析node节点的主机名
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
  • metrics-server报错,x509,证书是非信任的
- --kubelet-insecure-tls

加在 args 的参数里面,args 片段如下

args:
    - --cert-dir=/tmp
    - --secure-port=4443
    - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
    - --kubelet-insecure-tls

3. 部署一个应用与hpa

hpa 全拼 horizontal pod autoscaler,可以实现pod根据条件实现水平扩展,比如cpu、内存、访问量等。
测试一个根据cpu的水平扩展,cpu限制的片段,限制cpu为0.02,100mCPU就是100 miliCPU,等价于0.1CPU

resources:
    limits:
        cpu: "20m"
    requests:
        cpu: "20m"

部署一个hpa

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: spring-boot-demo-deployment
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: spring-boot-demo-deployment
  minReplicas: 1
  maxReplicas: 2
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 15
status:
  conditions: null
  observedGeneration: 1
  currentReplicas: 1
  desiredReplicas: 1
  currentMetrics:
  - type: Resource
    resource:
      name: cpu
      current:
        averageUtilization: 0
        averageValue: 0

当cpu利用率超过15%,pod会水平扩展成2个,当cpu降下来后pod又会收缩成1个。
当pod每秒超过服务1000个数据包请求时,触发扩展,样本如下。

- type: Pods
   pods:
      metric:
        name: packets-per-second
      targetAverageValue: 1k

参考网站 https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/

posted @   whyfate  阅读(1016)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
点击右上角即可分享
微信分享提示