Kubernetes HPA测试
前言:
Kubernetes HPA是指k8s水平擴縮容,一般指的是一個部署好的應(yīng)用指定了請求cpu內(nèi)存限制,超過這個限制量會自動進行擴容pod。接下來我們親自試驗hpa吧。
準(zhǔn)備工作:
在試驗之前,我們需要給k8s集群安裝指標(biāo)服務(wù)(metrics server)。
如果是Mac環(huán)境大家可以直接用brew 安裝 helm
brew install helm再用helm 安裝 metrics server。不過需要中途編輯部署文件。加上部分command內(nèi)容。編輯好后跟vi編輯器一樣的。:wq可退出編輯。它會自動幫你重新部署的。
? ? ? ? command:
? ? ? ? ? - /metrics-server
? ? ? ? ? - --kubelet-insecure-tls
? ? ? ? ? - --kubelet-preferred-address-types=InternalIP,Hostname,ExternalIP
? ? ? ? ? - --metric-resolution=30s
https://github.com/kubernetes-sigs/metrics-server
考慮到部署yaml鏡像來源于google。我這邊為大家提前準(zhǔn)備好了。
默認鏡像地址:k8s.gcr.io/metrics-server/metrics-server:v0.3.7
?阿里鏡像地址:registry.cn-shanghai.aliyuncs.com/k8sgcrio_containers/metrics-server:v0.3.7
?部署文件地址:https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.7/components.yaml
?下載不了的話我貼在下面👇。需要注意的是,里面有個command記得加哦。復(fù)制我的準(zhǔn)沒錯!我用的版本1.18以上版本,其他版本可能還是有bug的。如果你們安裝不上,可以嘗試部署在master節(jié)點上,你們要把污點去掉。然后再把每臺master節(jié)點做個標(biāo)記。后面可以用nodeSelector進行部署匹配。只有需要部署在master的應(yīng)用才推薦這樣使用,部署好記得再還原污點。
執(zhí)行如下命令:
kubectl taint nodes --all node-role.kubernetes.io/master- kubectl label nodes master00 node-role=master kubectl label nodes master01 node-role=master kubectl label nodes master02 node-role=master還原污點命令:
kubectl taint nodes master00 node-role.kubernetes.io/master=:NoSchedule kubectl taint nodes master01 node-role.kubernetes.io/master=:NoSchedule kubectl taint nodes master02 node-role.kubernetes.io/master=:NoSchedule?
?components.yaml
--- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata:name: system:aggregated-metrics-readerlabels:rbac.authorization.k8s.io/aggregate-to-view: "true"rbac.authorization.k8s.io/aggregate-to-edit: "true"rbac.authorization.k8s.io/aggregate-to-admin: "true" rules: - apiGroups: ["metrics.k8s.io"]resources: ["pods", "nodes"]verbs: ["get", "list", "watch"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata:name: metrics-server:system:auth-delegator roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: system:auth-delegator subjects: - kind: ServiceAccountname: metrics-servernamespace: kube-system --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata:name: metrics-server-auth-readernamespace: kube-system roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: extension-apiserver-authentication-reader subjects: - kind: ServiceAccountname: metrics-servernamespace: kube-system --- apiVersion: apiregistration.k8s.io/v1beta1 kind: APIService metadata:name: v1beta1.metrics.k8s.io spec:service:name: metrics-servernamespace: kube-systemgroup: metrics.k8s.ioversion: v1beta1insecureSkipTLSVerify: truegroupPriorityMinimum: 100versionPriority: 100 --- apiVersion: v1 kind: ServiceAccount metadata:name: metrics-servernamespace: kube-system --- apiVersion: apps/v1 kind: Deployment metadata:name: metrics-servernamespace: kube-systemlabels:k8s-app: metrics-server spec:selector:matchLabels:k8s-app: metrics-servertemplate:metadata:name: metrics-serverlabels:k8s-app: metrics-serverspec:serviceAccountName: metrics-servervolumes:# mount in tmp so we can safely use from-scratch images and/or read-only containers- name: tmp-diremptyDir: {}containers:- name: metrics-serverimage: registry.cn-shanghai.aliyuncs.com/k8sgcrio_containers/metrics-server:v0.3.7imagePullPolicy: IfNotPresentcommand:- /metrics-server- --kubelet-insecure-tls- --kubelet-preferred-address-types=InternalIP,Hostname,ExternalIP- --metric-resolution=30sargs:- --cert-dir=/tmp- --secure-port=4443ports:- name: main-portcontainerPort: 4443protocol: TCPsecurityContext:readOnlyRootFilesystem: truerunAsNonRoot: truerunAsUser: 1000volumeMounts:- name: tmp-dirmountPath: /tmp #如果有啥問題,可以嘗試安裝master節(jié)點#nodeSelector:#node-role: master --- apiVersion: v1 kind: Service metadata:name: metrics-servernamespace: kube-systemlabels:kubernetes.io/name: "Metrics-server"kubernetes.io/cluster-service: "true" spec:selector:k8s-app: metrics-serverports:- port: 443protocol: TCPtargetPort: main-port --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata:name: system:metrics-server rules: - apiGroups:- ""resources:- pods- nodes- nodes/stats- namespaces- configmapsverbs:- get- list- watch --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata:name: system:metrics-server roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: system:metrics-server subjects: - kind: ServiceAccountname: metrics-servernamespace: kube-system開始部署:
[root@client ~]# kubectl apply -f components.yaml [root@client ~]# kubectl get pods -n kube-system|grep metrics metrics-server-5987bb5dff-kwzl9 1/1 Running 0 38m試驗:
hpa.yaml
apiVersion: apps/v1 kind: Deployment metadata:name: php-apache spec:selector:matchLabels:run: php-apachereplicas: 1template:metadata:labels:run: php-apachespec:containers:- name: php-apacheimage: registry.cn-shanghai.aliyuncs.com/k8sgcrio_containers/hpa-exampleports:- containerPort: 80resources:limits:cpu: 500mrequests:cpu: 200m --- apiVersion: v1 kind: Service metadata:name: php-apachelabels:run: php-apache spec:ports:- port: 80selector:run: php-apache?部署hpa示例
[root@client ~]# kubectl apply -f hpa.yaml deployment.apps/php-apache created service/php-apache created [root@client ~]# kubectl get pods NAME READY STATUS RESTARTS AGE php-apache-b6968c487-ldmml 1/1 Running 0 55s [root@client ~]# kubectl top pods NAME CPU(cores) MEMORY(bytes) php-apache-b6968c487-ldmml 1m 7Mi?創(chuàng)建水平Pod自動縮放器,參數(shù)cpu-percent指的是cpu的百分比。超過%50即擴容1個pod。min是最小pod數(shù),max最大pod數(shù)。
創(chuàng)建好,一般要個30s左右才能獲取到相關(guān)指標(biāo)。你們可以看到TARGETS欄,會有<unknow>。給它點時間!
[root@client ~]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 horizontalpodautoscaler.autoscaling/php-apache autoscaled [root@client ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache <unknown>/50% 1 10 0 8s [root@client ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache <unknown>/50% 1 10 0 13s [root@client ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache <unknown>/50% 1 10 0 15s [root@client ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache 0%/50% 1 10 1 19s [root@client ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache 0%/50% 1 10 1 22s [root@client ~]#再開一個窗口。我們需要對部署的應(yīng)用進行請求負載。
[root@client ~]# kubectl run -it --rm load-generator --image=busybox /bin/sh kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. If you don't see a command prompt, try pressing enter. / # while true; do wget -q -O- http://php-apache; done OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!現(xiàn)在繼續(xù)查看hpa信息
[root@client ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache 251%/50% 1 10 4 6m26s [root@client ~]# kubectl get pods NAME READY STATUS RESTARTS AGE load-generator-dbf8dc997-lhjwh 1/1 Running 0 2m48s php-apache-b6968c487-6kq6g 1/1 Running 0 101s php-apache-b6968c487-cpbs4 1/1 Running 0 116s php-apache-b6968c487-kszmp 1/1 Running 0 101s php-apache-b6968c487-ldmml 1/1 Running 0 12m [root@client ~]#大家可以看到Replicas開始增加了,也就是pod數(shù)量。TARGES百分比也在增加。這說明負載測試成功了!
接下來暫停負載程序,按住ctrl+c即可。等個五分鐘吧!!!然后hpa就自動縮回去。
[root@client ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache 0%/50% 1 10 1 14m [root@client ~]#好了,試驗結(jié)束!
參考官方文章:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
總結(jié)
以上是生活随笔為你收集整理的Kubernetes HPA测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Arduino使用旋转编码器模块】
- 下一篇: app2sd 与 A2SD+