免费搭建博客网站,无备案网站 阿里联盟,京东app下载平台,彩票网站net网站开发HPA#xff1a;Horizontal Pod Autoscaling是pod的水平自动伸缩。是k8s自带的模块
pod占用CPU的比率到达一定的阈值会触发伸缩机制。
replication controller#xff1a;副本控制器。控制pod的副本数
deployment controller#xff1a;节点控制器。部署pod
hpa控制副本的…HPAHorizontal Pod Autoscaling是pod的水平自动伸缩。是k8s自带的模块
pod占用CPU的比率到达一定的阈值会触发伸缩机制。
replication controller副本控制器。控制pod的副本数
deployment controller节点控制器。部署pod
hpa控制副本的数量以及控制如何部署pod hpa基于kube-controller-manager服务。周期性检测pod的cpu使用率默认是30秒 hpa和replication controller以及deployment controller都属于k8s的资源对象。通过跟踪分析副本控制器和deployment的pod负载变化。针对性的调整目标副本数。
阀值正常情况下pod的副本数以及达到阀值之后pod的扩容最大数量。 metrics-server部署到集群中
实验部署
将metrics-server传入每个节点
docker load -i metrics-server.tarmaster01---
kubectl apply -f components.yaml
vim hpa-test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: centos-testlabels:test: centos1
spec:replicas: 1selector:matchLabels:test: centos1template:metadata:labels:test: centos1spec:containers:- name: centosimage: centos:7command: [/bin/bash, -c, yum -y install epel-release;yum -y install stress;sleep 3600]resources:limits:cpu: 1000mmemory: 512Mi
#设置资源限制。使用hpa必须添加资源限制字段否则无法判断---apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:name: hpa-centos
spec:scaleTargetRef:apiVersion: apps/v1
#表示需要监控的类型是什么,基于什么控制器创建的kind: Deploymentname: centos-test
#这里表示你需要监控谁minReplicas: 1
#表示最小有几个maxReplicas: 5
#超过副本最大有几个targetCPUUtilizationPercentage: 50
#设定cpu使用的阀值kubectl apply -f hpa-test.yaml进入容器使容器占满2个cpu测试 此时停止扩充测试缩容 HPA的规则 定义pod的时候必须要有资源限制否则HPA无法进行监控 扩容是即时的只要超过阀值就会立刻扩容不是立刻扩容到最大副本数。他会在最小值和最大值波动如果扩容数量满足了需求则不会在扩容。 缩容是缓慢的。如果业务的峰值较高回收的策略太积极的话可能会产生业务的崩溃。
周期性的获取数据缩容的机制问题。
如果业务的峰值较高回收的策略太积极的话可能会产生业务的崩溃。
pod的副本数扩缩容有两种方式
1、 手动的方式修改控制器的副本数。 命令行可以通过 kubectl scale deployment pod名称 --replicas5 修改yaml文件。通过apply -f部署更新
2、 自动扩缩容HPA
hpa监控的是cpu
资源限制
pod的资源限制在部署pod的时候加入resources字段通过limits/request来对pod进行限制。
除了pod的资源限制还有命名空间的资源限制
命名空间资源限制
如果你有一个lucky-cloud项目部署在test1的命名空间。如果lucky-cloud不做限制或者命名空间不做限制他依然会占满所有集群资源。
k8s集群部署pod的最大数量1万个
实验举例
vim ns.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: centos-test2namespace: test1labels:test: centos2
spec:replicas: 11selector:matchLabels:test: centos2template:metadata:labels:test: centos2spec:containers:- name: centosimage: centos:7command: [/bin/bash, -c, yum -y install epel-release;yum -y install stress;sleep 3600]resources:limits:cpu: 1000mmemory: 512Mi---apiVersion: v1
kind: ResourceQuota
metadata:name: ns-resourcenamespace: test1
spec:hard:
#硬限制pods: 10
#表示在这个命名空间内只能部署10个podrequests.cpu: 2
#最多只能占用多个个cpurequests.memory: 1Gi
#最多只能占用多少内存limits.cpu: 4
#最大需要多少cpulimits.memory: 2Gi
#最大需要多少内容configmaps: 10
#当前命名空间内能创建最大的configmap的数量 10个persistentvolumeclaims: 4
#当前命名空间只能使用4个pvcsecrets: 9
#创建加密的secrets。只能9个services: 5
#创建service只能5个services.nodeports: 2
#nodeport类型的svc只能2个
设置副本数为11个测试。当命名空间限制了之后最多只能部署10个 kubectl describe ns test1
#查看命名空间的限制 通过命名空间的方式对容器进行限制
实验举例
vim ns2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: centos-testnamespace: test2labels:test: centos2
spec:replicas: 1selector:matchLabels:test: centos1template:metadata:labels:test: centos1spec:containers:- name: centosimage: centos:7command: [/bin/bash, -c, yum -y install epel-release;yum -y install stress;sleep 3600]---apiVersion: v1
kind: LimitRange
#表示使用limitrange来进行资源控制的类型
metadata:name: test2-limitnamespace: test2
spec:limits:- default:memory: 512Micpu: 1defaultRequest:memory: 256Micpu: 0.5type: Container
#对所有部署在这个命名空间内的容器统一进行资源限制
#default: limit
#defaultRequest: request
#type: Container、Pod、Pvc都可以 通过命名空间对pod进行统一限制
好处是不需要对每个pod进行限制
缺点是不够灵活
HPA自动伸缩如果使用nodeName的方式将固定在一个node上观察扩容之后阀值是否会下降
实验举例
apiVersion: apps/v1
kind: Deployment
metadata:name: centos-testlabels:test: centos1
spec:replicas: 1selector:matchLabels:test: centos1template:metadata:labels:test: centos1spec:containers:- name: centosimage: centos:7command: [/bin/bash, -c, yum -y install epel-release;yum -y install stress;sleep 3600]resources:limits:cpu: 1000mmemory: 512MinodeName: node01
#设置资源限制。使用hpa必须添加资源限制字段否则无法判断---apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:name: hpa-centos
spec:scaleTargetRef:apiVersion: apps/v1
#表示需要监控的类型是什么,基于什么控制器创建的kind: Deploymentname: centos-test
#这里表示你需要监控谁minReplicas: 1
#表示最小有几个maxReplicas: 5
#超过副本最大有几个targetCPUUtilizationPercentage: 50
#设定cpu使用的阀值占满第二个pod测试 他们都处在同一个节点上
查看是阀值是否会下降 测试即使在同一个node节点上阀值还是会下降。实验完成
总结
HPA自动扩缩容
命名空间的两种方式 ResourceQuota可以对命名空间进行资源限制 LimitRange直接声明在命名空间中创建的pod容器的资源限制。这是一种统一限制。所有的pod都受这个条件的制约。
只要是在命名空间内不管创建多少都需要使用我声明的资源限制。
pod的资源限制resources、limit pod的资源限制是我们创建时候声明好的这时必加选项。 对命名空间、使用cpu、内存一定会做限制
命名空间的资源限制ResourceQuota 一般是对命名空间的cpu和内存做限制
命名空间统一资源限制LimitRange 核心pod一定要做资源限制否则会占用集群的全部资源命名空间也需要做限制否则还是会占用集群的全部资源。防止整个集群的资源被一个服务或者一个命名空间占满。
HPA自动伸缩