Tutorials  /  Container & Cloud

Deploying and Scaling Microservices in Kubernetes

Ccentron Redaktion · November 2024 ·6 min read ·Container & Cloud, Tutorial

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`.

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`:

#nano 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.

kubectl get pods

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`:

nano 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:

kubectl get pods

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.

kubectl delete pod web

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:

kubectl get pods

K8

Matching infrastructure at centron

From container to cluster: managed Kubernetes with cStack starts at €29.99 per month – control plane, autoscaler and traffic included. Explore managed Kubernetes →

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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Container & Cloud
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren