扩缩容

安装metrics-server

metrics-server 从kubelet 采集指标并暴露给apiserver ,可为dashboard、hpa、vpa 提供支持

第一步:拉取镜像

k8s 版本v1.23.17

docker pull k8simage/metrics-server:v0.6.1
docker tag k8simage/metrics-server:v0.6.1 k8s.gcr.io/metrics-server/metrics-server:v0.6.1

第二步:部署metric server

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

第三步:修改metrics-server的启动参数

- --kubelet-insecure-tls=true

第四步:功能验证

kubectl top node --sort-by=memory
kubectl top node --sort-by=cpu
验证自动扩缩容-基于cpu使用率

创建用于测试的deploy

kubectl create deployment autoscale --image=centos:7 --dry-run=client -oyaml -- tail -f /dev/null

cat <<EOF|kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: autoscale
name: autoscale
spec:
replicas: 1
selector:
matchLabels:
app: autoscale
template:
metadata:
labels:
app: autoscale
spec:
containers:
- command:
- tail
- -f
- /dev/null
image: centos:7
name: centos
resources:
requests: # 必须要有request资源配置
cpu: 100m
memory: 1Mi
EOF

为deploy创建hpa

kubectl autoscale deploy autoscale --min=1 --max=5 --cpu-percent=90 --dry-run=client -oyaml

cat <<EOF|kubectl apply -f -
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: autoscale
spec:
maxReplicas: 5
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: autoscale
targetCPUUtilizationPercentage: 90
EOF

压力测试观察状态

执行cpu压测 观察扩缩容效果
kubectl exec -it autoscale-8fbf8c769-frrzd -- bash -c 'echo "scale=5000;a(1)"|bc -l -q'
验证扩缩容-基于v2版本内存使用率

HorizontalPodAutoscaler 支持v1和v2两个版本,v1版本支持基于cpu资源使用率的扩缩容,v2版本使用 metrics 数组替代了targetCPUUtilizationPercentage, 可以将更多的指标作为扩缩容的依据。

kubectl explain hpa --api-version='autoscaling/v1'
kubectl explain hpa --api-version='autoscaling/v2'

metrics支持 Resource 、Pod、Object、External 类型,当前只测试出来Resource 类型的使用方法

示例为参照内存使用率执行扩缩容,同样适用于cpu

cat <<EOF|kubectl apply -f -
apiVersion: autoscaling/v2 # 选择v2版本
kind: HorizontalPodAutoscaler
metadata:
name: autoscaler-v2
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: autoscale
minReplicas: 1
maxReplicas: 10
metrics: # v1 版本没有metrics字段
- type: Resource # type=Resource 支持cpu 和 memory
resource:
name: memory
target:
type: Utilization # 支持按照百分比和使用量
averageUtilization: 50
#type: AverageValue # 按照使用量
#averageValue: 512Ki
EOF

参考:
apiserver 开启聚合
kubelet开启webhook认证

prometheus-adapter

hpa | vpa

posted @   mingtian是吧  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示