Horizontal Pod Autoscaling:po的水平自動伸縮
這是k8s自帶的模塊
pod占用cpu比例達(dá)到一定的閥值,會觸發(fā)伸縮機(jī)制。
根據(jù)cpu的閥值觸發(fā)伸縮機(jī)制
replication controller 副本控制器 控制pod的副本數(shù)
deployment controller 節(jié)點(diǎn)控制器 部署pod
hpa控制副本的數(shù)量,以及如何控制部署pod
1、hpa基于kube-controll-manager服務(wù),周期性的檢測pod的cpu使用率,默認(rèn)30秒檢測一次
2、hpa和replication controller,deployment controller都屬于k8s的資源對象。通過跟蹤分析副本控制器和deployment的pod的負(fù)載變化,針對性的調(diào)整目標(biāo)pod的副本數(shù)
閥值:在正常情況下,pod的副本數(shù),以及達(dá)到閥值之后,pod的擴(kuò)容最大數(shù)量
3、組件:metrics-server 部署到集群中,對外提供度量的數(shù)據(jù)
HPA的規(guī)則:
1、定義pod時候必須要有資源限制,否則hpa無法進(jìn)行監(jiān)控
2、擴(kuò)容是即時的,只要超過閥值會立刻擴(kuò)容,不是立刻擴(kuò)容到最大副本數(shù),會在最大值和最小值波動,如果擴(kuò)容的數(shù)量滿足了需求,不會在擴(kuò)容
3、縮容是緩慢的,如果業(yè)務(wù)的峰值較高,回收的策略太積極的話,可能會產(chǎn)生業(yè)務(wù)的崩潰,縮容的速度比較慢的
周期性的獲取數(shù)據(jù),縮容的機(jī)制
HPA的部署運(yùn)用
進(jìn)行HPA的部署設(shè)置
//在所有 Node 節(jié)點(diǎn)上傳 metrics-server.tar 鏡像包到 /opt 目錄
cd /opt/
docker load -i metrics-server.tar
#在主master節(jié)點(diǎn)上執(zhí)行
kubectl apply -f components.yaml
HPA伸縮的測試演示
apiVersion: apps/v1
kind: Deployment
metadata:
name: centos-test
labels:
test: centos1
spec:
replicas: 1
selector:
matchLabels:
test: centos1
template:
metadata:
labels:
test: centos1
spec:
containers:
- name: centos
image: centos:7
command: ["/bin/bash", "-c", "yum -y install epel-release;yum -y install stress;sleep 3600"]
resources:
limits:
cpu: "1"
memory: 512Mi
---
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-centos7
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: centos-test
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 50
命名空間資源限
ResourceQuota:命名空間進(jìn)行資源限制
apiVersion: apps/v1
kind: Deployment
metadata:
name: centos-test1
namespace: test1
labels:
test: centos2
spec:
replicas: 6
selector:
matchLabels:
test: centos2
template:
metadata:
labels:
test: centos2
spec:
nodeSelector:
kubernetes.io/hostname: node01
containers:
- name: centos
image: centos:7
command: ["/bin/bash", "-c", "yum -y install epel-release;yum -y install stress;sleep 3600"]
resources:
limits:
cpu: "1"
memory: 512Mi
---
apiVersion: v1
kind: ResourceQuota
metadata:
name: ns-resource
namespace: test1
spec:
hard:
pods: "10"
requests.memory: 1Gi
limits.cpu: "4"
limits.memory: 2Gi
configmaps: "10"
#在當(dāng)前這個命名空間能創(chuàng)建最大configmap數(shù)量10個
persistentvolumeclaims: "4"
#當(dāng)前命名空間只能使用4個pvc
secrets: "9"
#創(chuàng)建賈母的secret只能9個
services: "5"
#創(chuàng)建service個數(shù)只能五個
services.nodeports: "2"
#創(chuàng)建nodeport類型的svc只能兩個
只能在命名空間創(chuàng)建兩個service第三個就不創(chuàng)建了因?yàn)橄拗屏俗疃嘀荒軇?chuàng)建兩個
第二種LimitRange
apiVersion: apps/v1
kind: Deployment
metadata:
name: centos-test3
namespace: test3
labels:
test: centos3
spec:
replicas: 1
selector:
matchLabels:
test: centos3
template:
metadata:
labels:
test: centos3
spec:
containers:
- name: centos3
image: centos:7
command: ["/bin/bash", "-c", "yum -y install epel-release;yum -y install stress;sleep 3600"]
---
apiVersion: v1
kind: LimitRange
metadata:
name: test3-limit
namespace: test3
spec:
limits:
- default:
memory: 512Mi
cpu: "1"
defaultRequest:
memory: 256Mi
cpu: "0.5"
type: Container
pod的副本數(shù)擴(kuò)縮容,有兩種方式:
1、手動方式kubectl scale deployment nginx1 --replicas=5,kubectl edit,修改yaml文件 apply -f
2、自動擴(kuò)縮容 hpa 監(jiān)控指標(biāo)是cpu和內(nèi)存沒關(guān)系
資源限制
pod資源限制
命名空間資源限
ucky-cloud項(xiàng)目--部署在test1的命名空間,如果lucky-cloud不做限制,或者命名空間不做限制,他會依然會沾滿所有集群資源
k8s集群部署pod的最大數(shù)量:10000
busybox:就是服務(wù)最小化的centos 4M
哪些服務(wù)會部署在k8s當(dāng)中
中間件 kafka: 6
redis: 3
選好節(jié)點(diǎn) 用nodeName固定在一個pod上,擴(kuò)容之后閥值是否會下降
apiVersion: apps/v1
kind: Deployment
metadata:
name: centos-test8
labels:
test: centos8
spec:
replicas: 3
selector:
matchLabels:
test: centos8
template:
metadata:
labels:
test: centos8
spec:
nodeSelector:
kubernetes.io/hostname: node01
containers:
- name: centos8
image: centos:7
command: ["/bin/bash", "-c", "yum -y install epel-release;yum -y install stress;sleep 3600"]
resources:
limits:
cpu: "2"
memory: 512Mi
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-centos7
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: centos-test8
minReplicas: 1
maxReplicas: 5
targetCPUUtilizationPercentage: 50
總結(jié)
HPA的自動擴(kuò)縮容
命令空間兩種方式
ResourceQuota:可以對命名空間進(jìn)行資源限制
第二種LimitRange:直接聲明在命名空間當(dāng)中創(chuàng)建pod,容器的資源限制,只是一種統(tǒng)一的限制,所有的pod都受這個條件的制約
pod資源限制 一般是我們創(chuàng)建的時候聲明好的,必加選項(xiàng)
resources
limit
命名空間資源限制:對命名空間使用cpu和內(nèi)存一定會做限制通過
核心:防止整個集群的資源被一個服務(wù)或者一個命名空間沾滿
ResourceQuata文章來源:http://www.zghlxwxcb.cn/news/detail-820730.html
命名空間統(tǒng)一資源限制在pod LimitRange文章來源地址http://www.zghlxwxcb.cn/news/detail-820730.html
到了這里,關(guān)于Kubernetes/k8s之HPA,命名空間資源限制的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!