How do I set up a RabbitMQCluster with persistent storage for message durability?
In this guide, we will set up a RabbitMQ cluster with persistent storage in a Kubernetes environment using Pulumi. This ensures that the messages are durably stored and can survive pod restarts and failures.
We will use the Pulumi Kubernetes provider to create the necessary resources, including a PersistentVolume
, PersistentVolumeClaim
, and a StatefulSet
for RabbitMQ.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Define a PersistentVolume for RabbitMQ
const rabbitmqPv = new k8s.core.v1.PersistentVolume("rabbitmq-pv", {
spec: {
capacity: {
storage: "10Gi"
},
accessModes: ["ReadWriteOnce"],
persistentVolumeReclaimPolicy: "Retain",
hostPath: {
path: "/mnt/data"
}
}
});
// Define a PersistentVolumeClaim for RabbitMQ
const rabbitmqPvc = new k8s.core.v1.PersistentVolumeClaim("rabbitmq-pvc", {
spec: {
accessModes: ["ReadWriteOnce"],
resources: {
requests: {
storage: "10Gi"
}
}
}
});
// Define a StatefulSet for RabbitMQ
const rabbitmqStatefulSet = new k8s.apps.v1.StatefulSet("rabbitmq", {
spec: {
serviceName: "rabbitmq",
replicas: 3,
selector: {
matchLabels: {
app: "rabbitmq"
}
},
template: {
metadata: {
labels: {
app: "rabbitmq"
}
},
spec: {
containers: [{
name: "rabbitmq",
image: "rabbitmq:3.8.9",
ports: [{
containerPort: 5672,
name: "amqp"
}],
volumeMounts: [{
name: "rabbitmq-storage",
mountPath: "/var/lib/rabbitmq"
}]
}]
}
},
volumeClaimTemplates: [{
metadata: {
name: "rabbitmq-storage"
},
spec: {
accessModes: ["ReadWriteOnce"],
resources: {
requests: {
storage: "10Gi"
}
}
}
}]
}
});
// Export the StatefulSet name
export const statefulSetName = rabbitmqStatefulSet.metadata.name;
Key Points
- PersistentVolume (PV): Defines the storage resource in the cluster.
- PersistentVolumeClaim (PVC): Requests storage resources defined by the PV.
- StatefulSet: Manages the deployment and scaling of a set of pods, and provides guarantees about the ordering and uniqueness of these pods.
Summary
We have successfully set up a RabbitMQ cluster with persistent storage using Pulumi and Kubernetes. This setup ensures that RabbitMQ messages are durably stored and can survive pod restarts and failures.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.