掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流
作者:liugp 2023-02-08 07:55:33
云計(jì)算
云原生 對(duì)于自定義指標(biāo),將使用 custom.metrics.k8s.io API。它由其他度量指標(biāo)方案廠商的“適配器(Adapter)” API 服務(wù)器提供。檢查你的指標(biāo)管道以查看是否有可用的 Kubernetes 指標(biāo)適配器。

Horizontal Pod Autoscaler(HPA?,Pod水平自動(dòng)伸縮),根據(jù)平均 CPU 利用率、平均內(nèi)存利用率或你指定的任何其他自定義指標(biāo)自動(dòng)調(diào)整 Deployment? 、ReplicaSet? 或 StatefulSet? 或其他類(lèi)似資源,實(shí)現(xiàn)部署的自動(dòng)擴(kuò)展和縮減,讓部署的規(guī)模接近于實(shí)際服務(wù)的負(fù)載。HPA不適用于無(wú)法縮放的對(duì)象,例如DaemonSet。
官方文檔:https://kubernetes.io/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale/
實(shí)際生產(chǎn)中,一般使用這四類(lèi)指標(biāo):
默認(rèn)情況下,Horizontal Pod Autoscaler 控制器會(huì)從一系列的 API 中檢索度量值。集群管理員需要確保下述條件,以保證 HPA 控制器能夠訪問(wèn)這些 API:
Kubernetes Metrics Server:
# 添加這行
# --enable-aggregator-routing=true
### 修改每個(gè) API Server 的 kube-apiserver.yaml 配置開(kāi)啟 Aggregator Routing:修改 manifests 配置后 API Server 會(huì)自動(dòng)重啟生效。
cat /etc/kubernetes/manifests/kube-apiserver.yaml
GitHub地址:https://github.com/kubernetes-sigs/metrics-server/releases下載
wget https://github.com/kubernetes-sigs/metrics-server/releases/download/metrics-server-helm-chart-3.8.2/components.yaml
修改
...
template:
metadata:
labels:
k8s-app: metrics-server
spec:
containers:
- args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --kubelet-insecure-tls # 加上該啟動(dòng)參數(shù),不加可能會(huì)報(bào)錯(cuò)
image: registry.aliyuncs.com/google_containers/metrics-server:v0.6.1 # 鏡像地址根據(jù)情況修改
imagePullPolicy: IfNotPresent
...
metrics-server pod無(wú)法啟動(dòng),出現(xiàn)日志unable to fully collect metrics: ... x509: cannot validate certificate for because ... it doesn't contain any IP SANs ...?解決方法:在metrics-server中添加--kubelet-insecure-tls參數(shù)跳過(guò)證書(shū)校驗(yàn)
開(kāi)始安裝
kubectl apply -f components.yaml
kubectl get pod -n kube-system | grep metrics-server
# 查看
kubectl get pod -n kube-system | grep metrics-server
# 查看node和pod資源使用情況
kubectl top nodes
kubectl top pods
從最基本的角度來(lái)看,Pod 水平自動(dòng)擴(kuò)縮控制器根據(jù)當(dāng)前指標(biāo)和期望指標(biāo)來(lái)計(jì)算擴(kuò)縮比例。
期望副本數(shù) = ceil[當(dāng)前副本數(shù) * (當(dāng)前指標(biāo) / 期望指標(biāo))]
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
behavior:
scaleDown:
policies:
- type: Pods
value: 4
periodSeconds: 60
- type: Percent
value: 10
periodSeconds: 60
stabilizationWindowSeconds: 300
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
HPA對(duì)象默認(rèn)行為
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 100
periodSeconds: 15
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
- type: Pods
value: 4
periodSeconds: 15
selectPolicy: Max
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-nginx
spec:
maxReplicas: 10 # 最大擴(kuò)容到10個(gè)節(jié)點(diǎn)(pod)
minReplicas: 1 # 最小擴(kuò)容1個(gè)節(jié)點(diǎn)(pod)
metrics:
- resource:
name: cpu
target:
averageUtilization: 40 # CPU 平局資源使用率達(dá)到40%就開(kāi)始擴(kuò)容,低于40%就是縮容
# 設(shè)置內(nèi)存
# AverageValue:40
type: Utilization
type: Resource
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa-nginx
---
apiVersion: v1
kind: Service
metadata:
name: hpa-nginx
spec:
type: NodePort
ports:
- name: "http"
port: 80
targetPort: 80
nodePort: 30080
selector:
service: hpa-nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hpa-nginx
spec:
replicas: 1
selector:
matchLabels:
service: hpa-nginx
template:
metadata:
labels:
service: hpa-nginx
spec:
containers:
- name: hpa-nginx
image: nginx:latest
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 200m
memory: 200Mi
主要參數(shù)解釋如下:
執(zhí)行
kubectl apply -f test.yaml
進(jìn)入apache官網(wǎng) http://httpd.apache.org/ 下載apache即可,或者直接通過(guò)yum安裝apache都行,這里選擇最簡(jiǎn)單的方式y(tǒng)um安裝
yum install httpd -y
開(kāi)始?jí)簻y(cè)
ab -n 100000 -c 800 http://local-168-182-112:30080/
#-c:并發(fā)數(shù)
#-n:總請(qǐng)求數(shù)
? ?

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流