Kubernetes使用Keda進行彈性伸縮,更合理利用資源
我最新最全的文章都在 南瓜慢說 www.pkslow.com ,歡迎大家來喝茶!
1 簡介
Kubernetes自帶的HPA是隻支援CPU/MEM的,很多時候我們並不根據這兩項指標來進行伸縮資源。比如消費者不斷處理MQ的訊息,我們希望MQ如果堆積過多,就啟動更多的消費者來處理任務。而Keda
給了我們很多選擇。
KEDA 是 Kubernetes 基於事件驅動的自動伸縮工具,通過 KEDA 我們可以根據需要處理的事件數量來驅動 Kubernetes 中任何容器的擴充套件。KEDA 可以直接部署到任何 Kubernetes 叢集中和標準的元件一起工作。
Keda所支援的事件源非常豐富,本文我們以RabbitMQ為例進行演示。
2 安裝Keda
安裝的方法很多,我們直接通過yaml檔案來安裝,這樣還可以修改映象地址等。先從( https://github.com/kedacore/keda/releases/download/v2.2.0/keda-2.2.0.yaml )下載yaml檔案,然後執行:
$ kubectl apply -f ~/Downloads/keda-2.2.0.yaml namespace/keda created customresourcedefinition.apiextensions.k8s.io/clustertriggerauthentications.keda.sh created customresourcedefinition.apiextensions.k8s.io/scaledjobs.keda.sh created customresourcedefinition.apiextensions.k8s.io/scaledobjects.keda.sh created customresourcedefinition.apiextensions.k8s.io/triggerauthentications.keda.sh created serviceaccount/keda-operator created clusterrole.rbac.authorization.k8s.io/keda-external-metrics-reader created clusterrole.rbac.authorization.k8s.io/keda-operator created rolebinding.rbac.authorization.k8s.io/keda-auth-reader created clusterrolebinding.rbac.authorization.k8s.io/keda-hpa-controller-external-metrics created clusterrolebinding.rbac.authorization.k8s.io/keda-operator created clusterrolebinding.rbac.authorization.k8s.io/keda:system:auth-delegator created service/keda-metrics-apiserver created deployment.apps/keda-metrics-apiserver created deployment.apps/keda-operator created apiservice.apiregistration.k8s.io/v1beta1.external.metrics.k8s.io created
檢查一下是否都已經啟動完成:
$ kubectl get all -n keda NAME READY STATUS RESTARTS AGE pod/keda-metrics-apiserver-55dc9f9498-smc2d 1/1 Running 0 2m41s pod/keda-operator-59dcf989d6-pxcbb 1/1 Running 0 2m41s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/keda-metrics-apiserver ClusterIP 10.104.255.44 <none> 443/TCP,80/TCP 2m41s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/keda-metrics-apiserver 1/1 1 1 2m42s deployment.apps/keda-operator 1/1 1 1 2m42s NAME DESIRED CURRENT READY AGE replicaset.apps/keda-metrics-apiserver-55dc9f9498 1 1 1 2m42s replicaset.apps/keda-operator-59dcf989d6 1 1 1 2m42s
也可以看到映象多了:
$ docker images | grep keda
ghcr.io/kedacore/keda-metrics-apiserver 2.2.0 a43d40453368 6 weeks ago 95.3MB
ghcr.io/kedacore/keda 2.2.0 42b88f042914 6 weeks ago 83MB
如果要解除安裝請執行:
$ kubectl delete -f ~/Downloads/keda-2.2.0.yaml
3 安裝RabbitMQ
為了快速安裝,也方便日後刪除,我們通過Helm
來安裝RabbitMQ。
檢視可用的chart:
$ helm search repo rabbit
執行安裝:
$ helm install azure-rabbitmq azure/rabbitmq
檢查一下:
$ helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
azure-ingress default 1 2021-02-14 01:21:07.212107 +0800 CST deployed nginx-ingress-1.41.3 v0.34.1
azure-rabbitmq default 1 2021-05-05 11:29:06.979437 +0800 CST deployed rabbitmq-6.18.2 3.8.2
使用者名稱為user
,密碼獲取如下:
$ echo "Password : $(kubectl get secret --namespace default azure-rabbitmq -o jsonpath="{.data.rabbitmq-password}" | base64 --decode)"
Password : YNsEayx8w2
4 測試
部署消費者,注意這裡有個MQ連線資訊和加密,要根據自己情況修改。
$ kubectl apply -f src/main/kubernetes/deploy-consumer.yaml
secret/rabbitmq-consumer-secret created
deployment.apps/rabbitmq-consumer created
scaledobject.keda.sh/rabbitmq-consumer created
triggerauthentication.keda.sh/rabbitmq-consumer-trigger created
檢視deployment,發現是沒有Pod建立,因為還不需要處理,MQ現在的佇列為0。
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
azure-ingress-nginx-ingress-controller 1/1 1 1 80d
azure-ingress-nginx-ingress-default-backend 1/1 1 1 80d
rabbitmq-consumer 0/0 0 0 131m
部署生產者,往MQ傳送訊息:
$ kubectl apply -f src/main/kubernetes/deploy-publisher-job.yaml
job.batch/rabbitmq-publish created
可以看到,慢慢消費者就起來了,並且建立了越來越多的Pod來處理MQ:
$ kubectl get deployments rabbitmq-consumer
NAME READY UP-TO-DATE AVAILABLE AGE
rabbitmq-consumer 1/1 1 1 167m
$ kubectl get deployments rabbitmq-consumer
NAME READY UP-TO-DATE AVAILABLE AGE
rabbitmq-consumer 3/4 4 3 168m
$ kubectl get deployments rabbitmq-consumer
NAME READY UP-TO-DATE AVAILABLE AGE
rabbitmq-consumer 4/8 8 4 168m
$ kubectl get deployments rabbitmq-consumer
NAME READY UP-TO-DATE AVAILABLE AGE
rabbitmq-consumer 6/8 8 6 169m
$ kubectl get deployments rabbitmq-consumer
NAME READY UP-TO-DATE AVAILABLE AGE
rabbitmq-consumer 0/0 0 0 171m
檢視Deployment的Event也可以看到結果:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 5m55s (x2 over 172m) deployment-controller Scaled up replica set rabbitmq-consumer-7b477f78b4 to 1
Normal ScalingReplicaSet 5m6s deployment-controller Scaled up replica set rabbitmq-consumer-7b477f78b4 to 4
Normal ScalingReplicaSet 4m6s deployment-controller Scaled up replica set rabbitmq-consumer-7b477f78b4 to 8
Normal ScalingReplicaSet 3m5s deployment-controller Scaled up replica set rabbitmq-consumer-7b477f78b4 to 16
Normal ScalingReplicaSet 3m3s (x2 over 172m) deployment-controller Scaled down replica set rabbitmq-consumer-7b477f78b4 to 0
處理完成後,又會回到0了。
總結
程式碼請檢視:https://github.com/LarryDpk/pkslow-samples
歡迎關注微信公眾號<南瓜慢說>,將持續為你更新...
多讀書,多分享;多寫作,多整理。