文章目录
1. Horizontal Pod Autoscaler(HPA) pod水平自动伸缩
官网:https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
Kubernetes有一个 HPA(Horizontal Pod Autoscaler) 的资源,可以实现基于CPU使用率的Pod自动伸缩的功能。
(1)HPA基于Master Node上的kube-controller-manager服务启动参数horizontal-pod-autoscaler-sync-period定义的时长(默认为30秒),周期性的检测Pod的CPU使用率。
如果需要设置horizontal-pod-autoscaler-sync-period
可以在Master Node上的/etc/default/kube-controller-manager
中修改。
(2)HPA 与之前的 RC、Deployment 一样,也属于一种 Kubernetes 资源对象。通过追踪分析 RC 控制的所有目标 Pod 的负载变化情况,来确定是否需要针对性地调整目标Pod的副本数,这是HPA的实现原理。
(3)metrics-server 也需要部署到集群中, 它可以通过 resource metrics API 对外提供度量数据。
2. HPA实例
2.1 以cpu的使用率判断pod的扩容和缩容
(1)运行 php-apache 服务器并暴露服务
首先,我们先启动一个 deployment 来运行这个镜像并暴露一个服务
[root@server1 harbor]# docker pull 0layfolk0/hpa-example
[root@server1 harbor]# docker tag 0layfolk0/hpa-example:latest reg.westos.org/library/hpa-example:latest
[root@server1 harbor]# docker push reg.westos.org/library/hpa-example
[root@server1 hpa]# vim php-apache.yaml
[root@server1 hpa]# cat php-apache.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-apache
spec:
selector:
matchLabels:
run: php-apache
replicas: 1
template:
metadata:
labels:
run: php-apache
spec:
containers:
- name: php-apache
image: hpa-example
ports:
- containerPort: 80
resources: //资源限制
limits:
cpu: 500m
requests:
cpu: 200m // 节点的cpu至少要有0.2cpu
---
apiVersion: v1
kind: Service //服务
metadata:
name: php-apache
labels:
run: php-apache
spec:
ports:
- port: 80
selector:
run: php-apache
其中需要的镜像hpa-example,已经拉至harbor仓库
(2)运行部署
[kubeadm@server1 hpa]$ vim php-apache.yaml
[kubeadm@server1 hpa]$ kubectl apply -f php-apache.yaml
deployment.apps/php-apache created
service/php