一、HPA
HPA:
pod的数量进行扩缩容
针对对控制器创建的pod
deployment:
replicas:
静态:edit
yaml:apply -f
HPA:基于cpu的利用率
来实现pod的数量大的自动伸缩。
Horizontal Pod Autoscaling
yaml 文件------主流------>必须要有资源控制的字段才能生效。
命令行 生成HPA的配置。
yaml必要条件:
前置必要条件:控制器创建,而且必须能设置副本数
配置的必要条件:必须要声明pod的资源控制
1、metrices-server hpa使用的依赖环境。k8s使用集群资源的集中查询器。收集资源使用数据,给hpa,scheduller使用
echo 1 > /proc/sys/vm/drop_caches # 释放页面缓存
echo 2 > /proc/sys/vm/drop_caches # 释放目录项和inode缓存
echo 3 > /proc/sys/vm/drop_caches # 释放所有缓存
2、扩容和缩容的速度:
扩容一旦达到阈值,会立即扩容,缩容速度相对较慢。
为了保证pod的正常工作,扩容必须要快。
缩容的时候为了保证pod的资源突然又变大了,可以继续维持pod的数量,在一定时间之内,pod占用的资源维持在较低的比率,然后开始慢慢缩容。
[root@master01 opt]# mkdir hap
[root@master01 opt]# cd /opt/
[root@node01 opt]# rz -E ##所有节点
rz waiting to receive.
[root@node01 opt]# ls
metrics-server.tar
[root@master01 opt]# docker load -i metrics-server.tar ##所有节点
[root@master01 opt]# cd hap/
[root@master01 hap]# rz -E
rz waiting to receive.
[root@master01 hap]# ls
components.yaml
[root@master01 hap]# vim components.yaml
[root@master01 hap]# kubectl apply -f components.yaml
[root@master01 hap]# kubectl get pod -n kube-system
[root@master01 hap]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master01 536m 13% 3405Mi 44%
node01 64m 1% 2204Mi 28%
node02 81m 2% 2559Mi 33%
[root@master01 hap]# kubectl explain hpa
KIND: HorizontalPodAutoscaler
VERSION: autoscaling/v1
[root@master01 hap]# vim hpa.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hpa-test1
labels:
hpa: test1
spec:
replicas: 1
selector:
matchLabels:
hpa: test1
template:
metadata:
labels:
hpa: test1
spec:
containers:
- name: centos
image: centos:7
command: ["/bin/bash", "-c", "yum install -y epel-release --nogpgcheck && yum install -y stress --nogpgcheck && sleep 3600"]
volumeMounts:
- name: yum1
mountPath: /etc/yum.repos.d/
resources:
limits:
cpu: "1"
memory: 512Mi
volumes:
- name: yum1
hostPath:
path: /etc/yum.repos.d/
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-centos1
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa-test1
#就是用来匹配需要监控器类型和名称
#资源占用率高,要扩容,扩容的数量
#资源占用率低,要缩容,最小的保持数量
#定义扩缩容的指标,阈值。
minReplicas: 1
maxReplicas: 6
targetCPUUtilizationPercentage: 50
#占用50%的cpu
[root@master01 hap]# kubectl logs -f hpa-test1-7558dfb467-bv85l
Loaded plugins: fastestmirror, ovl
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
[root@master01 hap]# cd /etc/yum.repos.d/
[root@master01 yum.repos.d]# ls
backup CentOS-Debuginfo.repo CentOS-Vault.repo kubernetes.repo
Centos-7.repo CentOS-fasttrack.repo docker-ce.repo local.repo
CentOS-Base.repo CentOS-Media.repo epel.repo
CentOS-CR.repo CentOS-Sources.repo epel-testing.repo
[root@master01 yum.repos.d]# rm -rf local.repo
------------------------报错,yum问题------------
[root@master01 hap]# kubectl logs -f hpa-test1-7558dfb467-r8t9t
Loaded plugins: fastestmirror, ovl
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
One of the configured repositories failed (Unknown),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
1. Contact the upstream for the repository and get them to fix the problem.
2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
3. Run the command with the repository temporarily disabled
yum --disablerepo=<repoid> ...
4. Disable the repository permanently, so yum won't use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:
yum-config-manager --disable <repoid>
or
subscription-manager repos --disable=<repoid>
5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot find a valid baseurl for repo: base/7/x86_64
[root@master01 hap]# kubectl delete -f hpa.yaml
deployment.apps "hpa-test1" deleted
horizontalpodautoscaler.autoscaling "hpa-centos1" deleted
[root@master01 hap]# kubectl apply -f hpa.yaml
deployment.apps/hpa-test1 created
horizontalpodautoscaler.autoscaling/hpa-centos1 created
[root@master01 hap]# kubectl apply -f hpa.yaml
deployment.apps/hpa-test1 created
horizontalpodautoscaler.autoscaling/hpa-centos1 created
[root@master01 hap]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hpa-test1-7558dfb467-r8t9t 0/1 Error 0 2s 10.244.1.3 node01 <none> <none>
---------------------------------------------
查看日志发现yum源找不到
--------------------所有节点都需要配置yum源------------
[root@master01 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2024-09-12 10:58:54-- http://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 58.222.47.242, 58.222.47.240, 121.228.130.155, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|58.222.47.242|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”
100%[========================================>] 2,523 --.-K/s 用时 0s
2024-09-12 10:58:54 (250 MB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])
[root@node01 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@node02 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
---------------------重新配置yum源------------------
-----------------------或者文本不配置epel-------------
[root@master01 hap]# vim hpa.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hpa-test1
labels:
hpa: test1
spec:
replicas: 1
selector:
matchLabels:
hpa: test1
template:
metadata:
labels:
hpa: test1
spec:
containers:
- name: centos
image: centos:7
command: ["/bin/bash", "-c", "yum install -y stress --nogpgcheck && sleep 3600"]
volumeMounts:
- name: yum1
mountPath: /etc/yum.repos.d/
resources:
limits:
cpu: "1"
memory: 512Mi
volumes:
- name: yum1
hostPath:
path: /etc/yum.repos.d/
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-centos1
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa-test1
#就是用来匹配需要监控器类型和名称
#资源占用率高,要扩容,扩容的数量
#资源占用率低,要缩容,最小的保持数量
#定义扩缩容的指标,阈值。
minReplicas: 1
maxReplicas: 6
targetCPUUtilizationPercentage: 50
#占用50%的cpu
-----------------------------------
[root@master01 hap]# kubectl delete -f hpa.yaml
deployment.apps "hpa-test1" deleted
horizontalpodautoscaler.autoscaling "hpa-centos1" deleted
[root@master01 hap]# kubectl apply -f hpa.yaml
deployment.apps/hpa-test1 created
horizontalpodautoscaler.autoscaling/hpa-centos1 created
[root@master01 hap]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hpa-test1-7558dfb467-zwhh7 1/1 Running 0 4s 10.244.2.16 node02 <none> <none>
[root@master01 opt]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
hpa-centos1 Deployment/hpa-test1 0%/50% 1 6 1 29m
-------------------------------------
[root@master01 hap]# kubectl logs -f hpa-test1-7558dfb467-zwhh7
Loaded plugins: fastestmirror, ovl
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib i