Kubernetes is a type of container orchestration engine that enables you to scale, deploy and run containerized services. Kubernetes ETCD is a simple distributed key/store where Kubernetes stores all the data such as several workloads, their state, namespaces configurations, etc. It’s only accessible from the API server for security reasons. Whenever something needs to be stored inside the database of Kubernetes, or when you’re making changes to your environment, every communication is going through that API server. This is essentially the central management entity that receives all the requests and acts as a frontend to the Cluster.
Docker enables you to run your applications using a runtime in an isolated environment and package images using the standard packaging format. Once you package your application with the runtime, it can run on your local machine, dev environment, staging, or production environments. Containerization makes everything possible and easier for engineering teams and developers.
In this tutorial, we will be deploying MongoDB and Mongo Express Applications. These two applications in particular demonstrate a simple setup of a web application and its database effectively and are flexible (meaning you can apply this to any similar setup you have).
- Database Credentials
- Kubernetes and Docker installed on your local machine
Here, you will learn how to create a mongo-express deployment, and how to connect it to your backend mongo db application.
- We will start off by creating a MongoDB Pod, as well as a Service to enable us to communicate to that pod. For this, create an internal service, so no other external services will have the ability to talk to our Pods, just the internal service within it and components inside the same cluster.
- In addition, we will make use of the Database URL of MongoDB, so that your mongo-express application can connect and communicate with it. We also need the username and password of the database so it can authenticate with our application.
1. Start off by creating a Kubernetes cluster (we covered this topic in a previous blog post, to read it click here). Confirm if your cluster is created by getting all the components in your cluster using
kubectl get all.
2. Next, create your Mongo db deployment. I created a copy of the MongoDB deployment in this Github gist.
`apiVersion: apps/v1 kind: Deployment # mongodb deployment metadata: name: mongodb-deployment labels: app: mongodb spec: replicas: 1 selector: matchLabels: app: mongodb template: metadata: labels: app: mongodb spec: # mongodb deployment spec containers: - name: mongodb-container image: mongo # mongo image name ports: # mongodb container ports - containerPort: 27017 env: # mongodb container env variables - name: MONGO_INITDB_ROOT_USERNAME valueFrom: # mongodb env variable value from secret secretKeyRef: name: mongodb-secret key: mongodb-root-username - name: MONGO_INITDB_ROOT_PASSWORD valueFrom: # mongodb ev variable from secret secretKeyRef: name: mongodb-secret key: mongodb-root-password`
In our deployment file, the container will be called
mongodb-container. You can give it any name you want to call it. We also specified our environmental variables below the container port.
MONGO_INITDB_ROOT_USERNAME will be our username and
MONGO_INITDB_ROOT_PASSWORD will be our password.
We can check how to use the container from the official Mongo image from Docker hub. From the image, we will get the specific port number, external configuration, and server instance. The default port of the MongoDB container is
3. Our Deployment file will then be checked into a repository (at this point, we are not to write any username or password inside the configuration). We will create a
secret.yaml file from which we will reference the value of your Mongo password and username values. You can also see the
secret.yaml configuration in this GitHub gist.
apiVersion: v1 kind: Secret metadata: name: mongodb-secret type: Opaque data: mongodb-root-username: bW9uZ291c2VybmFtZQ== mongodb-root-password: bW9uZ29wYXNzd29yZA==
In our Secret file, we have a kind Secret with metadata that specifies the name. The type Opaque is our default type which is the most basic key:value Secret type. When you create your secret configuration, the value must be base64 encoded. In your terminal, encode your username using
echo -n ‘mongousername’ | base64.
Copy the value it generates for you and that will be your username.
Repeat the same thing for your password.
When you are done, copy the values to your secrets file and save it.
4. Next, create your
secret.yaml configuration because we will reference the
secret.yaml configuration in our deployment file. Apply the
secret.yaml configuration using
kubectl apply -f secret.yaml in your terminal.
You can confirm if your secret was created using
kubectl get secret.
Now we have our Secret, we can then reference it in our deployment configuration file. Attach the root username and password as we have in our deployment file.
5. When you are done, apply your deployment file using
kubectl apply -f <name_of_your_deployment_file>.
You can confirm if your components are running using
kubectl get all.
Our pod is also running here.
6. Next, we are going to create an internal service so that other components can talk to MongoDB. In our Service configuration, we want it to connect to the Pod using the Selector spec in our configuration file. The targetPort in your Service file must be the same as the containerPort in your deployment.
Apply your service file using
kubectl apply -f <name_of_service>.
apiVersion: v1 kind: Service metadata: name: mongodb-service spec: selector: app: mongodb ports: - protocol: TCP port: 27017 targetPort: 27017
Confirm if your service was created using
kubectl get svc and if it’s listening on port
Check if your service was attached to the right Pod using
kubectl describe svc mongodb-service.
Configure and Create Mongo-Express Deployment
7. Next, we are going to create our Mongo-Express deployment and external configuration in which our database URL will be attached. You can copy the MongoDB-Express deployment from this GitHub gist. The image name: is
mongo-express and the port is
apiVersion: apps/v1 kind: Deployment metadata: name: mongo-express labels: app: mongo-express spec: replicas: 1 selector: matchLabels: app: mongo-express template: metadata: labels: app: mongo-express spec: containers: - name: mongo-express image: mongo-express ports: - containerPort: 8081 env: - name: ME_CONFIG_MONGODB_ADMINUSERNAME valueFrom: secretKeyRef: name: mongodb-secret key: mongodb-root-username - name: ME_CONFIG_MONGODB_ADMINPASSWOR valueFrom: secretKeyRef: name: mongodb-secret key: mongodb-root-password - name: ME_CONFIG_MONGODB_SERVER valueFrom: configMapKeyRef: name: mongodb-configmap key: database_url
In our Mongo-express deployment, you will reference which database it should connect to using the database address or internal service. We will also authenticate our deployment using the right credentials.
The value of your
ME_CONFIG_MONGODB_SERVER is your database server,
ME_CONFIG_MONGODB_ADMINUSERNAME will be the same username, and password we defined in our secret file.
- Attach your database URL, it can be attached as a configMap or Value. If you use a ConfigMap, you can centralize your DB so that other components can make use of it.
8. Next, create your configMap which will contain your MongoDB server address. Your database URL will be the name of your MongoDB service. When you’re done, create and apply your configMap to the cluster so that you will be able to reference it in your Mongo-Express deployment file.
apiVersion: v1 kind: configMap metadata: name: mongodb-configmap data: database_url: mongodb-service
Apply your configMap using
kubectl apply -f <name_of_configMap_file>.
9. Next, apply your Mongo-Express deployment using
kubectl apply -f <name_of_mongo-express_file>.
Confirm if your mongo-express pod and deployment is running.
10. You can get the logs of your pods using
kubectl logs <name_of_pod>.
Access Mongo-Express from your Browser
11. To access Mongo-Express from your browser, you’ll need an external service for mongo-express. The external service can also be in the deployment file. ServicePort is
8081 and targetPort is also the containerPort used in your mongo-express deployment
apiVersion: v1 kind: Service metadata: name: mongo-express-service spec: selector: app: mongo-express type: LoadBalancer ports: - protocol: TCP port: 8081 targetPort: 8081 nodePort: 30000
To make it an externalService, we will need to use a LoadBalancer. The LoadBalancer accepts external requests by assigning the service an external IP address. We will also attach a nodePort to it, and that will be the port where the external service will be open. The nodePort will be the port you will paste in your browser to access your mongo-express service.
12. Confirm if your external service was created using
kubectl get service.
You can see the IP of our external service The load balancer will give you an internal and external IP address.
13. Next, paste your external IP address in your browser with the mongo-express port.
You can see that your
service is now available.