Email ico
Kubernetes

Kubernetes introduction and step-by-step tutorial

Devendra Nationalwala | 08 Jan 2022

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation.

You can cluster together groups of hosts running Linux® containers, and Kubernetes helps you easily and efficiently manage those clusters. Kubernetes clusters can span hosts across on-premise, public, private, or hybrid clouds. For this reason, Kubernetes is an ideal platform for hosting cloud-native applications that require rapid scaling, like real-time data streaming through Apache Kafka.

Let's jump into action

Here we will create two kubernetes pods that will communicate with each other. One pod will be for Mongo DB database, it will be accessible to other pods but not outside kubernetes environment. The second pod will be mongo db express, a user interface that will access/write data to mongo db. To access in a browser, It will be integrated with external service.


To follow the tutorial, please install these required tools:

Create a directory on your pc where you will be saving .yaml (template) files for each kubernetes component.

Secret

Let's start with creating the first component of this tutorial: Secret. Because Secrets can be created independently of the Pods that use them, there is less risk of the Secret (and its data) being exposed during the workflow of creating, viewing, and editing Pods. Kubernetes, and applications that run in your cluster, can also take additional precautions with Secrets, such as avoiding writing confidential data to nonvolatile storage.

For this tutorial, we will be storing mongo db username and password in base64 format in a secret component.

mongo-secret.yaml

Here dXNlcm5hbWU= is base64 encoding of username. And cGFzc3dvcmQ= is base64 encoding of password; kind is set to Secret.

Run following command to register the yml file as a secret component in kubernetes.

kubectl apply -f mongo-secret.yaml

To see all secrets, you may run following command.

kubectl get secret

Mongo DB Pod & Internal Service

We are creating mongo pod from the official docker image of mongo db. And we pass the secret credentials we have created to environment variables of this image.

mongo-db.yaml

Run the following command to create components based on this .yaml file:

kubectl apply -f mongo-db.yaml

In this yaml file, we have put two components seperated by ---

The first component is of Deployment kind and the second one is of Service kind. The deployment component is mongo db pod. We have set environment variables MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD through the secret we had created in teh first step.

The second component is the internal service and will be used to connect to mongo db pod from mongo express.

Mongo Express Pod & Extrenal Service

We are creating mongo express pod from the official docker image of mongo express. And we pass the secret credentials we have created to environment variables of this image.

Mongo express is a frontend app that will access the DB. To access DB, we need to configure DB url in pod template. We will put this URL in the component called configmap. A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.

mongo-configmap.yaml

Now we will create template file for mongo express pod and external service.

mongo-express.yaml

To set environment variable ME_CONFIG_MONGODB_SERVER we have used value from ConfigMap.

To make mongo express service external, we have added nodePort to the ports list.

Now let's register configmap, mongo express pod and service with the following commmand

kubectl apply -f mongo-configmap.yaml
kubectl apply -f mongo-express.yaml

As we have created all pods and services, we need to link the mongo express external service to minicube.

minikube service mongo-express-service

This command will launch mongo express in your default browser.

Now you can do DML operations in mongo express, and all data will be persisted and retrieved to/from mongo DB pod. However, if mongo db pod terminates, all data stored in pod will be gone. To persist data of pod, you would need to attach Persistent Volume to the pod.