Leverage the power of Kubernetes Operators to deploy your own RabbitMQ Cluster
RabbitMQ is the most widely deployed open-source message broker. Before talking about RabbitMQ, let's define what a message broker is. It allows applications and services to communicate with each other and exchange data. The diagram below presents the use case of an event bus using a message broker:
Message brokers are very useful to decouple communications between your components. It gives you the opportunity to create fully distributed and modern architectures. In the message broker family, you can find other popular names like Apache Kafka, Apache ActiveMQ.
RabbitMQ is lightweight and easy to deploy on-premise or in the cloud. It offers interesting features:
- It supports multiple messaging protocols like AMQP, STOMP, MQTT, etc...
- It can be deployed in distributed and federated configurations to meet high-scale and high availability requirements.
- It provides a UI for management and an HTTP API for monitoring.
- It can be extended with many available plugins.
In this tutorial, we will focus on how to deploy a high-available RabbitMQ cluster in a Kubernetes cluster. The choice will be to use Kubernetes Operators to achieve this goal efficiently. We will see the advantages of using them and how to deploy RabbitMQ resources step by step.
Why Use The Kubernetes Operators?
"Operators are software extensions to Kubernetes that make use of custom resources to manage applications and their components." https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
Operators in Kubernetes allow you to extend the cluster's behavior without modifying the code of Kubernetes itself. In our case, the behavior of RabbitMQ will be delegated to the operators. This will save us a lot of time on our side.
The RabbitMQ team develops and maintains two Kubernetes operators :
- RabbitMQ Cluster Kubernetes Operator to automate provisioning, management, and operations of RabbitMQ clusters running on Kubernetes.
- RabbitMQ Messaging Topology Operator to manage the topology of the clusters (Permissions, Users, etc...)
The Operators are installed through CRDs (Custom Resource Definition) in the Kubernetes cluster. Once installed, new resources are known in the cluster such as classical kinds (Pod, Deployment, etc...). You just have to create the YAML manifests to invoke them.
Deploying The RabbitMQ Cluster Kubernetes Operator
The CRD is available in the repository releases. Install it with the
The new resources are inside the
Ensure the resources are all ready to go next.
Create The RabbitMQ Cluster
The RabbitMQ Cluster Operator provides a
RabbitmqCluster kind. You can now create as many RabbitMQ clusters as you want in your Kubernetes cluster. You have just to define them in a YAML manifest like this:
Let's recap the content of the manifest above:
- We create a resource of kind
Rabbitmqcluster. It is defined by the CRD we installed in the previous part.
- The cluster has 3 replicas for high availability and fault tolerance.
- Each pod has clearly defined resources.
- Added plugins:
rabbitmq_managementfor management UI and
rabbitmq_peer_discovery_k8sfor cluster formation. You can find other plugins here: https://www.rabbitmq.com/plugins.html.
- The cluster configuration is done in the
- The type of service used is
ClusterIP. You can configure a
LoadBalancertype if your application is outside the cluster.
- The persistence is configured. The given example runs on a local Kubernetes cluster so the storage class is
hostpath. You can look at the classes available in Kubernetes that will fit your needs: https://kubernetes.io/docs/concepts/storage/storage-classes/
Deploying the RabbitMQ Topology Operator
Let's place to RabbitMQ Topology Operator now. It allows you to manage RabbitMQ specific resources like users and permissions for example: https://www.rabbitmq.com/kubernetes/operator/using-topology-operator.html.
This Operator needs cert-manager to work. Deploy it and ensure all pods are healthy:
Install the CRD:
Here is an example of a manifest to configure the permissions for the guest user to do tests with the authentication:
Install The RabbitMQ Cluster Operator Kubectl Plugin
The RabbitMQ team also provides a kubectl plugin to use the RabbitMQ Cluster Operator. This plugin will automate many interactions with the Kubernetes API and the RabbitMQ cluster operator.
The plugin needs
krew plugin manager installed (Installation here is for MacOSX, check the documentation and adapt for your OS)
PATH environment variable with the location of
Check the installation is ok:
$ kubectl krew
Install the RabbitMQ plugin:
$ kubectl krew install rabbitmq
Check the RabbitMQ plugin:
$ kubectl rabbitmq help
You can retrieve your cluster in the list:
You can also open directly the management UI:
Through this tutorial, we saw the advantage of using Operators to deploy Kubernetes. They allow transparency and provisioning of RabbitMQ clusters easily. It automates a large part of the management and allows to define the RabbitMQ topology through YAML manifests. Finally, we have seen how to set up the kubectl plugin to interact with the operator.