关于java:Kubernetes使用Keda进行弹性伸缩更合理利用资源

45次阅读

共计 4475 个字符,预计需要花费 12 分钟才能阅读完成。

我最新最全的文章都在 南瓜慢说 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/k…)下载 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/p…


欢送关注微信公众号 <南瓜慢说>,将继续为你更新 …

多读书,多分享;多写作,多整顿。

正文完
 0