A Complete Demo of Kubernetes

A Complete Demo of Kubernetes

In the previous blog, we showed a basic practice on Kubernetes. In this blog, I am going to demonstrate a complete Kubernetes demo.

In this demo, the requirements are very simple. We deploy MongoDB and MongoExpress and users can access MongoDB through MongoExpress. Here are the steps we need to implement.

  • Deploy the MongoDB to Minikube

  • Create internal service for MongoDB

  • Deploy Mongo Express to Minikube

  • Create external service for Mongo Express

Create Secret

First, we create the secret of MongoDB used by MongoExpress.

We should not explicitly expose secrets in the configuration file, therefore, we need to encode the username and password.

echo -n 'username' | base64
# output is dXNlcm5hbWU=
echo -n 'password' | base64
# output is cGFzc3dvcmQ=

Then we create the file mongo-secret.yaml with the encoded secret.

apiVersion: v1
kind: Secret
metadata:
    name: mongodb-secret
type: Opaque
data:
    mongo-root-username: dXNlcm5hbWU=
    mongo-root-password: cGFzc3dvcmQ=

Then we run the commands to create the secret in Kubernetes.

kubectl apply -f mongo-secret.yaml
kubectl get secret

Deploy MongoDB and Create a Service

We create the mongo.yaml which includes the configuration of both deployment and service. In the container part, we need to specify the root user and password for MongoDB. Based on the MongoDB Image guide, we should use the environment variables MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD. And we specify the secret mongodb-secret we created.

apiVersion: apps/v1  
kind: Deployment  
metadata:  
  name: mongodb-deployment  
  labels:  
    app: mongodb  
spec:  
  replicas: 1  
  selector:  
    matchLabels:  
      app: mongodb  
  template:  
    metadata:  
      labels:  
        app: mongodb  
    spec:  
      containers:  
      - name: mongodb  
        image: mongo  
        ports:  
        - containerPort: 27017  
        env:  
        - name: MONGO_INITDB_ROOT_USERNAME  
          valueFrom:  
            secretKeyRef:  
              name: mongodb-secret  
              key: mongo-root-username  
        - name: MONGO_INITDB_ROOT_PASSWORD  
          valueFrom:   
            secretKeyRef:  
              name: mongodb-secret  
              key: mongo-root-password  
---  
apiVersion: v1  
kind: Service  
metadata:  
  name: mongodb-service  
spec:  
  selector:  
    app: mongodb  
  ports:  
    - protocol: TCP  
      port: 27017  
      targetPort: 27017
kubectl apply -f mongo.yaml
kubectl get all
kubectl get pod --watch
kubectl describe {pod_name}

kubectl get service
kubectl describe service {service_name}

Deploy MongoExpress, Create Service and Config Map

We have created the service for MongoDB, when it comes to MongoExpress, we need to identify the following points.

  • Which database to connect

    • MongoDB Address / Internal Service
  • Which credentials to authenticate

  • How to expose the Mongo Express service to external

Firstly, we create the file mongo-express.yaml to deploy MongoExpress and create its service.

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: mongo-root-username
        - name: ME_CONFIG_MONGODB_ADMINPASSWORD
          valueFrom: 
            secretKeyRef:
              name: mongodb-secret
              key: mongo-root-password
        - name: ME_CONFIG_MONGODB_SERVER
          valueFrom: 
            configMapKeyRef:
              name: mongodb-configmap
              key: database_url
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-express-service
spec:
  selector:
    app: mongo-express
  type: LoadBalancer # external service
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
      nodePort: 30000 # external IP address port

Here "nodePort" is the external IP address port. "LoadBalancer" means it is an external service.

Then we need to specify which database the MongoExpress connects to. We create the config file mongo-configmap.yaml.

apiVersion: v1
kind: ConfigMap
metadata:
  name: mongodb-configmap
data:
  database_url: mongodb-service

Then we apply the configure files and create the relevant pod and service.

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

kubectl get pod
kubectl logs {pod_name}

Then we can run the following service the launch the Mongo Express.

minikube service mongo-express-service

Summary

In this blog, we showed a complete demo of how to deploy MongoDB and MongExpress to Kubernetes.

Reference

Module 10 of DevOps Bootcamp