k8s 基于hpa弹性伸缩实验
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
基于 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
posted on 2022-11-28 17:41 FLOWERS_WAN 阅读(158) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了