Setup MongoDB statefulset on AWS Fargate

Hi everyone I am Manshu Sharma and today we will see how can we deploy the mongoDB on AWS fargate as a statefulset with help of the mongoDB community operator but before we start let's have a quick overview of two important terms statefulset, stateful application.

What actually statefulset is in Kubernetes?
StatefulSet is a Kubernetes resource used to manage stateful applications and It manages the scaling, ordering and uniqueness of each pod. It requires headless service and we are responsible for creating this service.

What actually is stateful applications?
Stateful applications are those programs that save client data from the activities of one session for use in the next session. Consider it as an ongoing periodic conversation with the same person. Some examples of stateful applications are MySQL,  MongoDB, FTP server,  Redis Cache, Kafka and any Login service that stores client authentication data on the server, labelling clients as having a “connected” or “disconnected” state. It also stores information about previous requests from the same client.

A diagrammatical representation of Kubernetes statefulset resource.
In the above diagram, we can see that there is one headless service for routing and two statefulset pods with different PVC and PV resources. Each resource has its own different session and has a completely different isolated environment.

Overview completed. Now let's see the setup of the mongoDB application as a statefulset using MongoDB community operator.

Installation
The MongoDB Community Kubernetes Operator allows you to deploy secure MongoDB Replica Sets in your Kubernetes cluster.
  • Before we can deploy MongoDB, we need to ensure that we have created the required CustomResourceDefinition.
    • kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml
  • Create a namespace for our deployment.
    • kubectl create namespace mongodb
  • Install the latest version of the operator.
    • kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml
  • Add authorizations.

    • kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/config/rbac/role_binding.yaml 
    • kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/config/rbac/service_account.yaml
    • kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/config/rbac/role.yaml
Congratulation we have successfully set up our mongoDB community operator.
Note: This above operation requires cluster-admin permissions.
    Deploying a SCRAM Enabled Replica Set

    MongoDB community operator creates secure SCRAM-SHA-256 enabled deployments by default. This means that we need to define our users and what roles we want them to have, alongside a set of credentials for the user to use.
    We can create the user's credentials in the form of a Kubernetes Secret
    • kubectl create secret generic my-mongodb-user-password -n mongodb --from-literal="admin-password=TXs3ZsuIqT-pQFvwxOec"
    Once we have created the secret, we can deploy a MongoDB replica set but wait✋ without persistent volumes the data will not be retained permanently and so as fargate supports efs csi driver we are going to use it as our persistent volume support. For this, we going to create two resources multiple times which are persistent volume and persistent volume claim. We also need to install AWS EFS CSI DRIVER in our eks and storage class. Here below is the full code which will deploy storage class, PV and PVC.

    Once we have created our PV and PVC we are now ready to deploy MongoDBCommunity statefulset



    Comments

    Popular posts from this blog

    How to pass parameters in webhook?

    Access and modify all the resources of our Wiki.js using WikiJS API

    Fahrenheit to Celsius