掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
Horizontal Pod Autoscaler(HPA?,Pod水平自動伸縮),根據(jù)平均 CPU 利用率、平均內(nèi)存利用率或你指定的任何其他自定義指標自動調(diào)整 Deployment? 、ReplicaSet? 或 StatefulSet? 或其他類似資源,實現(xiàn)部署的自動擴展和縮減,讓部署的規(guī)模接近于實際服務(wù)的負載。HPA不適用于無法縮放的對象,例如DaemonSet。

10余年的榆社網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整榆社建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“榆社網(wǎng)站設(shè)計”,“榆社網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
官方文檔:https://kubernetes.io/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale/
實際生產(chǎn)中,一般使用這四類指標:
默認情況下,Horizontal Pod Autoscaler 控制器會從一系列的 API 中檢索度量值。集群管理員需要確保下述條件,以保證 HPA 控制器能夠訪問這些 API:
Kubernetes Metrics Server:
# 添加這行
# --enable-aggregator-routing=true
### 修改每個 API Server 的 kube-apiserver.yaml 配置開啟 Aggregator Routing:修改 manifests 配置后 API Server 會自動重啟生效。
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 # 加上該啟動參數(shù),不加可能會報錯
image: registry.aliyuncs.com/google_containers/metrics-server:v0.6.1 # 鏡像地址根據(jù)情況修改
imagePullPolicy: IfNotPresent
...
metrics-server pod無法啟動,出現(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ù)跳過證書校驗
開始安裝
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
從最基本的角度來看,Pod 水平自動擴縮控制器根據(jù)當前指標和期望指標來計算擴縮比例。
期望副本數(shù) = ceil[當前副本數(shù) * (當前指標 / 期望指標)]
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對象默認行為
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 # 最大擴容到10個節(jié)點(pod)
minReplicas: 1 # 最小擴容1個節(jié)點(pod)
metrics:
- resource:
name: cpu
target:
averageUtilization: 40 # CPU 平局資源使用率達到40%就開始擴容,低于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
進入apache官網(wǎng) http://httpd.apache.org/ 下載apache即可,或者直接通過yum安裝apache都行,這里選擇最簡單的方式y(tǒng)um安裝
yum install httpd -y
開始壓測
ab -n 100000 -c 800 http://local-168-182-112:30080/
#-c:并發(fā)數(shù)
#-n:總請求數(shù)
? ?

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流