目錄
HPA相關(guān)知識
?HPA(Horizontal Pod Autoscaling)Pod 水平自動伸縮,Kubernetes 有一個 HPA 的資源,HPA 可以根據(jù) CPU 利用率自動伸縮一個 Replication Controller、 Deployment 或者Replica Set 中的 Pod 數(shù)量。
(1)HPA 基于 Master 上的 kube-controller-manager 服務(wù)啟動參數(shù) horizontal-pod-autoscaler-sync-period 定義的時長(默認(rèn)為30秒),周期性的檢測 Pod 的 CPU 使用率。
(2)HPA 與之前的 RC、Deployment 一樣,也屬于一種 Kubernetes 資源對象。通過追蹤分析 RC 控制的所有目標(biāo) Pod 的負(fù)載變化情況, 來確定是否需要針對性地調(diào)整目標(biāo)Pod的副本數(shù),這是HPA的實現(xiàn)原理。
(3)metrics-server 也需要部署到集群中, 它可以通過 resource metrics API 對外提供度量數(shù)據(jù)。
?
HPA部署和運(yùn)用
進(jìn)行HPA的部署和配置
//在所有 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
mastrt節(jié)點(diǎn)拖入components.yaml
#部署完畢后,可以通過命令來監(jiān)視pod的資源占用
kubectl top pods
kubectl top nodes
HPA伸縮的測試演示
創(chuàng)建一個用于測試的pod資源
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
#設(shè)置資源限制。使用hpa必須添加資源限制字段,否則無法判斷。
---
apiVsersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-centos7
spec:
scaleTargetRef:
apiVersion: apps/v1
#表示需要監(jiān)控的類型是什么
kind: Deployment
name: centos-test
#這里表示你需要監(jiān)控誰
minReplicas: 1
#表示最小有幾個
maxReplicas: 5
#超過副本最大有幾個
targetCPUUtilzationPercentage: 50
//設(shè)定cpu使用的閥值。高于50%縮容,低于50%擴(kuò)容
進(jìn)入容器占用2個cpu
stress --cpu 2
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ī)制問題。
如果業(yè)務(wù)的峰值較高,回收的策略太積極的話,可能會產(chǎn)生業(yè)務(wù)的崩潰。
pod的副本數(shù)擴(kuò)縮容有兩種方式:
1、 手動的方式修改控制器的副本數(shù)。
命令行可以通過 kubectl scale deployment pod名稱 --replicas=5
修改yaml文件。通過apply -f部署更新
2、 自動擴(kuò)縮容HPA
hpa監(jiān)控的是cpu
資源限制
pod的資源限制:在部署pod的時候加入resources字段,通過limits/request來對pod進(jìn)行限制。
除了pod的資源限制還有命名空間的資源限制
命名空間 資源限制
lucky-zzr項目---部署在test1的命名空間,如果lucky-zzr不做限制,或者命名空間不做限制,他依然會占滿所有集群資源
k8s集群部署pod的最大數(shù)量:10000個
實驗舉例
vim ns.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: centos-test2
namespace: test1
labels:
test: centos2
spec:
replicas: 11
selector:
matchLabels:
test: centos2
template:
metadata:
labels:
test: centos2
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: 1000m
memory: 512Mi
---
apiVersion: v1
kind: ResourceQuota
metadata:
name: ns-resource
namespace: test1
spec:
hard:
#硬限制
pods: "10"
#表示在這個命名空間內(nèi)只能部署10個pod
requests.cpu: "2"
#最多只能占用多個個cpu
requests.memory: 1Gi
#最多只能占用多少內(nèi)存
limits.cpu: "4"
#最大需要多少cpu
limits.memory: 2Gi
#最大需要多少內(nèi)容
configmaps: "10"
#當(dāng)前命名空間內(nèi)能創(chuàng)建最大的configmap的數(shù)量 10個
persistentvolumeclaims: "4"
#當(dāng)前命名空間只能使用4個pvc
secrets: "9"
#創(chuàng)建加密的secrets。只能9個
services: "5"
#創(chuàng)建service只能5個
services.nodeports: "2"
#nodeport類型的svc只能2個
設(shè)置副本數(shù)為11個測試。當(dāng)命名空間限制了之后,最多只能部署10個
通過命名空間的方式對容器進(jìn)行限制
apiVersion: apps/v1
kind: Deployment
metadata:
name: centos-test
namespace: test2
Labels:
test: centos1
spec:
replicas: 1
selector:
matcjhLabels:
test: centos1
template:
metdata:
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"]
---
apiVersion: v1
kind: LimitRange
#表示使用limitrange來進(jìn)行資源控制的類型。
metadata:
name: test2-centos
namespace: test2
spec:
limits:
default:
memory: 512Mi
cpu: "1"
defaultRequest:
memory: 256Mi
cpu: "0.5"
type: Container
#default-->limit
#defaultRequest--->request
#type支持Container ,pod ,pvc
通過命名空間對pod進(jìn)行統(tǒng)一限制:
好處是不需要對每個pod進(jìn)行限制
缺點(diǎn)是不夠靈活
HPA自動伸縮如果使用nodeName的方式將固定在一個node上觀察擴(kuò)容之后,閥值是否會下降?
實驗舉例:
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: 1000m
memory: 512Mi
nodeName: node01
#設(shè)置資源限制。使用hpa必須添加資源限制字段,否則無法判斷
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-centos
spec:
scaleTargetRef:
apiVersion: apps/v1
#表示需要監(jiān)控的類型是什么,基于什么控制器創(chuàng)建的
kind: Deployment
name: centos-test
#這里表示你需要監(jiān)控誰
minReplicas: 1
#表示最小有幾個
maxReplicas: 5
#超過副本最大有幾個
targetCPUUtilizationPercentage: 50
#設(shè)定cpu使用的閥值
測試即使在同一個node節(jié)點(diǎn)上閥值還是會下降。實驗完成
總結(jié)
HPA自動擴(kuò)縮容
命名空間的兩種方式:
ResourceQuota:可以對命名空間進(jìn)行資源限制
LimitRange:直接聲明在命名空間中創(chuàng)建的pod,容器的資源限制。這是一種統(tǒng)一限制。所有的pod都受這個條件的制約。
只要是在命名空間內(nèi)不管創(chuàng)建多少,都需要使用我聲明的資源限制。
pod的資源限制:resources、limit
pod的資源限制是我們創(chuàng)建時候聲明好的,這時必加選項。
對命名空間、使用cpu、內(nèi)存一定會做限制
命名空間的資源限制:ResourceQuota
一般是對命名空間的cpu和內(nèi)存做限制
命名空間統(tǒng)一資源限制:LimitRange
核心:pod一定要做資源限制否則會占用集群的全部資源,命名空間也需要做限制否則還是會占用集群的全部資源。防止整個集群的資源被一個服務(wù)或者一個命名空間占滿。文章來源:http://www.zghlxwxcb.cn/news/detail-821423.html
HPA自動伸縮
?文章來源地址http://www.zghlxwxcb.cn/news/detail-821423.html
到了這里,關(guān)于k8s---HPA 命名空間資源限制的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!