With the release of Stateful Sets and custom Storage Classes, Kubernetes can automate all of the underlying infra required to run a MongoDB Replica Sets
- Have a Kubernetes Cluster created with at least version 1.5.1
- Have admin access to the cluster
The storage class will create the Volumes backing the MongoDB Replica Sets.
You can create Storage Classes with different provisioners depending on your Kubernetes environment. There are provisioners for Google Cloud, AWS, Azure, GlusterFS, OpenStack Cinder, vSphere, Ceph RBD, and Quobyte. Pick the right one for your deployment.
For example, this YAML uses a Google Cloud SSD Persistent Disk, and has the name "fast"
Create the Storage Class with the kubectl
tool.
kubectl apply -f googlecloud_ssd.yaml
Replace googlecloud_ssd.yaml
with another configuration file if you are not using Google Cloud. For example, if you are running Kubernetes on Azure, you can use the Azure SSD YAML. It uses the same name, "fast", so your application does not need to understand the underlying platform.
kubectl apply -f azure_ssd.yaml
If you are using Minikube for local testing:
kubectl apply -f minikube_hostpath.yaml
Verify that the Storage Class is created
$ kubectl get storageclass
NAME TYPE
fast kubernetes.io/gce-pd
The example YAML creates a Headless Service and a Stateful Set. It uses the Storage Class created in the previous step, and provisions a 100Gi volume per replica. Modify these values as you see fit.
kubectl apply -f mongo-statefulset.yaml
Verify that the Stateful Set is created
$ kubectl get statefulset
NAME DESIRED CURRENT AGE
mongo 3 0 12m
The Stateful Set controller will spin up each replica one at a time. Eventually, all three will be created.
$ kubectl get statefulset
NAME DESIRED CURRENT AGE
mongo 3 3 12m
You can also verify that the Volumes were created as well.
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
mongo-persistent-storage-mongo-0 Bound pvc-af87f9d5-d3ab-11e6-8cf2-42010af0018d 100Gi RWO 12m
mongo-persistent-storage-mongo-1 Bound pvc-af8cef48-d3ab-11e6-8cf2-42010af0018d 100Gi RWO 12m
mongo-persistent-storage-mongo-2 Bound pvc-af8f1d24-d3ab-11e6-8cf2-42010af0018d 100Gi RWO 12m
Each MongoDB Replica Set will have its own DNS address. This will take the format <pod-name>.<service-name>
.
For our example, the DNS addresses to use will be:
mongo-0.mongo
mongo-1.mongo
mongo-2.mongo
Put these in your connection url. For example:
mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo:27017/dbname_?'