Deploying and Scaling Microservices in Kubernetes

Delve into the world of microservices with Kubernetes! This tutorial guides you through deploying, scaling, and managing microservices applications. Discover practical steps and best practices to efficiently run and scale your applications in Kubernetes clusters. Ideal for developers aiming to elevate their container orchestration skills!

Kubernetes is an open-source orchestration tool for managing containerized applications. In our previous tutorial, “A Closer Look at Kubernetes,” you learned the basics of Kubernetes. In this tutorial, you’ll apply concepts from earlier tutorials to build, deploy, and manage an end-to-end microservices application in Kubernetes. The example application used here is a “To-Do List” app written in Node.js with MongoDB as the database, the same application from the “Building Containerized Applications” tutorial.

Prerequisites

To complete this tutorial, you will need:

  • A Kubernetes cluster, which you can configure in the third part of this tutorial series, “Getting Started with Kubernetes.”
  • An active Docker Hub account to store the image.
  • Git installed on your local machine. Follow the “Contributing to Open Source: Getting Started with Git” tutorial to install and set up Git on your computer.

Step 1 – Creating an Image with a Dockerfile

We’ll start by containerizing the web application, packaging it into a Docker image. First, navigate to your home directory and use Git to clone this tutorial’s sample application from its official repository on GitHub.

cd ~
git clone https://github.com/janakiramm/todo-app.git

Build the container image from the Dockerfile. Use the -t switch to tag the image with your registry username, image name, and an optional tag.

docker build -t sammy/todo .

The output confirms that the image has been successfully created and tagged. Check if the image was created by running `docker images`.

Step 2 – Deploying the MongoDB Pod in Kubernetes

The application uses MongoDB to store the to-do lists created through the web application. To run MongoDB in Kubernetes, we need to package it as a Pod. Create a new YAML file called `db-pod.yaml`:

Add the following code, which defines a Pod with a container based on MongoDB. We expose port 27017, the default MongoDB port.

apiVersion: v1
kind: Pod
metadata:
name: db
labels:
name: mongo
app: todoapp
spec:
containers:
– image: mongo
name: mongo
ports:
– name: mongo
containerPort: 27017
volumeMounts:
– name: mongo-storage
mountPath: /data/db
volumes:
– name: mongo-storage
hostPath:
path: /data/db

Run the following command to create a Pod.

kubectl create -f db-pod.yaml

Verify the creation of the Pod.

Step 3 – Deploying the Node.js Web Application as a Pod

Package the Docker image created in the first step of this tutorial as a Pod and deploy it in the cluster. This will serve as the front-end web application layer accessible to end-users.

Create a new YAML file called `web-pod.yaml`:

Add the following code, which defines a Pod with a container based on the `sammy/todo-app` Docker image. It is exposed over TCP port 3000.

apiVersion: v1
kind: Pod
metadata:
name: web
labels:
name: web
app: todoapp
spec:
containers:
– image: sammy/todo-app
name: myweb
ports:
– containerPort: 3000

Run the following command to create the Pod:

kubectl create -f web-pod.yaml

Verify the creation of the Pod:

Step 4 – Scaling the Web Application

A Replica Set ensures that a minimum number of Pods are always running in the cluster. When a Pod is packaged as a Replica Set, Kubernetes will always maintain the minimum number of Pods defined in the specification.

Let’s delete the current Pod and recreate two Pods using the Replica Set.

Then create a new Replica Set declaration.

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: web
labels:
name: web
app: todoapp
spec:
replicas: 2
template:
metadata:
labels:
name: web
spec:
containers:
– name: web
image: sammy/todo-app
ports:
– containerPort: 3000

Create the Replica Set:

kubectl create -f web-rs.yaml

Check the number of Pods:

Conclusion

In this tutorial, you applied concepts from this series to package, deploy, and scale a microservices application. In the next part of this series, you’ll learn how to make MongoDB highly available by running it as a StatefulSet.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

centron Managed Cloud Hosting in Deutschland

How to Calculate BLEU Score in Python?

Python
How to Calculate BLEU Score in Python? BLEU score in Python is a metric that measures the goodness of Machine Translation models. Though originally it was designed for only translation…