My latest and most complete articles are in the pumpkin slow say www.pkslow.com, welcome to tea!
1 introduction
Kubernetes HPA only supports CPU/MEM, most of the time we do not scale resources according to these two metrics. For example, consumers are constantly processing MESSAGES for MQ, and we want MQ to start more consumers to process tasks if it piles up too much. And Keda gives us a lot of options.
KEDA is an event-driven automatic scaling tool for Kubernetes, through which we can drive the expansion of any container in Kubernetes according to the number of events that need to be handled. KEDA can be deployed directly to any Kubernetes cluster to work with standard components.
Keda supports a wealth of event sources, and this article uses RabbitMQ as an example.
2 install Keda
There are many ways to install, we directly through the YAML file to install, so you can also modify the image address. Start with github.com/kedacore/ke… Download the YAML file, then execute:
$kubectl apply-f ~/Downloads/ keda-2.1.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 createdCopy the code
Check to see if everything has been started:
$ 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 2m42sCopy the code
You can also see that there are more mirrors:
$docker images | grep keda GHCR. IO/kedacore/keda - metrics - apiserver 2.2.0 a43d40453368 6 weekes line 95.3 MB Ghcr. IO /kedacore/keda 2.2.0 42b88f042914 6 weeks ago 83MBCopy the code
To uninstall:
$kubectl delete -f ~/Downloads/keda-2.2.0.yamlCopy the code
3 to install the RabbitMQ
For quick installation and easy removal, RabbitMQ will be installed via Helm.
View the available chart:
$ helm search repo rabbit
Copy the code
Perform installation:
$ helm install azure-rabbitmq azure/rabbitmq
Copy the code
Check it out:
$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 The rabbitmq - 6.18.2 3.8.2Copy the code
The user name is user and the password is as follows:
$ echo "Password : $(kubectl get secret --namespace default azure-rabbitmq -o jsonpath="{.data.rabbitmq-password}" | base64 --decode)"
Password : YNsEayx8w2
Copy the code
4 test
Deployment consumer, note that there is an MQ connection information and encryption that needs to be modified to suit your needs.
$ 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
Copy the code
Looking at the Deployment, no Pod has been created because there is no processing yet and MQ is now queued at 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
Copy the code
Deploy producers to send messages to MQ:
$ kubectl apply -f src/main/kubernetes/deploy-publisher-job.yaml
job.batch/rabbitmq-publish created
Copy the code
As you can see, consumers are getting up and creating more and more pods to handle 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 171mCopy the code
You can also see the result by looking at the 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
Copy the code
When we’re done, we’ll go back to 0.
conclusion
Please check the code: github.com/LarryDpk/pk…
Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…
Read more and share more; Write more. Organize more.