Docker and Kubernetes: How to Manage Containers at Scale

Docker and Kubernetes: How to Manage Containers at Scale. In this guide, we introduce Kubernetes, its major advantages in modern application deployment then show you how to deploy and manage containers at scale using Docker and Kubernetes.

All About Kubernetes

Google designed an open source container orchestration platform called Kubernetes to deploy and manage containerized applications. This open source platform is even capable of deploying and scaling several microservices. It offers a strong and flexible framework for controlling workloads and services for containers in a distributed setting.

Basically, it is all about optimization, streamlining the work of software engineers by automating a number of manual DevOps operations. It uses different types of containers and develops a framework to operate these containers in clusters with images created using Docker. The system, sometimes referred to as K8s, handles application failovers, enables load balancing, and more.

Kubernetes has become popular in recent years as a means to meet container orchestration requirements for organizations. Many services are delivered over the network using open APIs and distribution mechanisms. In order to avoid downtime, the systems must be extremely scalable.

In fact, cloud native apps are deployed anywhere with the aid of Kubernetes, which offers pertinent services that cover all these capabilities. Over time, they have emerged as among the best and fastest expanding projects.

Advantages of Kubernetes

1. Self healing Service – For enabling self service Kubernetes on complex enterprise grade clusters, best practices are needed. If a container fails, the self service Kubernetes clusters can restart, replace the container, or reschedule once they notice the dead nodes. Additionally, they aid in killing containers that fail a user defined health check. They try to work on it and only discuss it with clients once the containers are prepared to be used.

2. Highly Portable and Flexible – Kubernetes’ flexible capabilities support continuously delivering your apps, no matter how complex your requirements are, be it local testing or managing a large organization. Kubernetes are practically working with any architecture, including a server on premises, a hybrid cloud, or a public cloud. Kubernetes enables the creation of portable deployments that are operated reliably across several environments. However, this feature is absent from other orchestrators. In fact, workloads are moved to your desired place with ease thanks to this trusted open source platform.

3. Container Orchestration – The management of containerized apps is made easier by Kubernetes, which automates processes like deployment, scaling, and load balancing. Instead of worrying about infrastructure management, it enables you to concentrate on application development. For the optimal use of resources, Kubernetes automatically configures and fits containers onto nodes.

4. Efficiency boost for DevOps in microservices architecture – Development, testing, and deployment are simplified by the integration of containers and access to storage resources across several cloud providers. Virtual machine (VM) images cannot be created as easily or as quickly as container images, which include all of the components a program needs to execute. All of this results in streamlined release and deployment timelines and quicker development.

5. Multi Cloud Features – Several cloud providers support container integration and offer access to storage resources that help simplify the deployment, development, and testing process. It is simpler and more effective to create container images than virtual machine (VM) images, which contain all the components an application needs to execute. All of this results in streamlined release and deployment timelines and quicker development.

Docker and Kubernetes: How to Manage Containers at Scale

In this section, we deploy and scale Express application using Docker and Kubernetes.

Prerequisites

  • A root user or a user with sudo privileges.

Installing Docker

To get the latest version of Docker, you need to install it from the Docker’s official repository. Here is the steps to install the Docker package to your server.

First, install the following packages to install the Docker package via secure HTTPS connection:

				
					apt install apt-transport-https ca-certificates curl software-properties-common -y
				
			

Then, download and import the Docker GPG key to your server.

				
					curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
				
			

Next, add Docker’s official repository to the APT source file.

				
					echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
				
			

After that, update the apt package index using the following command:

				
					apt update -y
				
			

Finally. install the Docker CE package with the apt command.

				
					apt install docker-ce -y
				
			

After the successful installation, check the status of the Docker service using the following command.

				
					systemctl status docker
				
			

You will see the Docker status in the following screen.

Create an Express Application

To create an Express app, you need to install the Node.js package to your server. To get the latest Node.js version, add the NodeSource repository to your server.

				
					curl -sL https://deb.nodesource.com/setup_18.x | bash -
				
			

Next, install the Node.js from the NodeSource repository:

				
					apt install nodejs -y
				
			

Verify the Node.js version to check the installation:

				
					node --version
				
			

You see the installed versions of Node.js in the following output:

				
					v18.17.1
				
			

Then, create a directory for your Express application.

				
					mkdir ~/express-app
				
			

Next, initialize a new Express project using the following command:

				
					cd ~/express-app
npm init -y

				
			

You see the following screen.

Then, install the Express.js framework using the npm command.

				
					npm install express --save
				
			

Next, create a server.js file for your application.

				
					nano server.js
				
			

Add the following code to the file.

				
					
// server.js
const express = require('express');

//Create an app
const app = express();
app.get('/', (req, res) => {
    res.send('Express application is working!\n');
});

//Listen port
const PORT = 8080;
app.listen(PORT);
console.log(`Running on port ${PORT}`);

				
			

Save and close the file. Then, run your Express application using the following command:

				
					node server.js
				
			

Once the application is started, you will see the following output.

				
					Running on port 8080
				
			

Now, open your web browser and verify your Express application using the URL http://server-ip:8080.

Press CTRL+C to stop the application.

Create a Docker Image form Express Application

In this section, dockerize an Express application and run it inside the container. First, create a Dockerfile to define the Express application.

				
					nano Dockerfile
				
			

Add the following code:

				
					# Define Node.js version
FROM node:18

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Define port to expose application for outside world.
EXPOSE 8080
CMD [ "node", "server.js" ]

				
			

Save and close the fiel then create the Docker image of your Express application:

				
					cd /root/express-app
docker build -t express-app .
				
			

After the successful build, you see the following screen.

build express application

Now verify the created Docker image using the command given below:

				
					docker images
				
			

Output:

				
					REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
express-app   latest    13e0a201ae14   6 seconds ago   1.1GB

				
			

Finally, run your Express application container using your Express Docker image:

				
					docker run --name express-app-container -p 80:8080 -d express-app
				
			

Verify your running container with the following command.

				
					docker ps
				
			

This shows your Express container with a listening port:

				
					CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS         PORTS                                   NAMES
811520b27aee   express-app   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds   0.0.0.0:80->8080/tcp, :::80->8080/tcp   express-app-container
				
			

Access your Express application running inside the Docker container via http://server-ip. Next, stop and remove the Express application container using the following command.

				
					docker stop express-app-container
docker rm express-app-container
				
			

Upload Express Image to Docker Hub

You need to upload your Express Docker image in the Docker Hub registry to use it with Kubernetes.

First, log in to your Docker Hub account with the following command.

				
					docker login
				
			

You will be asked to provide your username and password to login to Docker Hub registry.

Next, define a tag for your Docker image to maintain the built version of your image.

				
					docker tag express-app hitjethva/express-app:v1
				
			

Verify the tagged docker image with the following command.

				
					docker images
				
			

Output.

				
					REPOSITORY              TAG       IMAGE ID       CREATED              SIZE
hitjethva/express-app   v1        13e0a201ae14   About a minute ago   1.1GB
express-app             latest    13e0a201ae14   About a minute ago   1.1GB
				
			

Now, push your Express image to the Docker hub registry using the following command.

				
					docker push hitjethva/express-app:v1
				
			

You will see the following screen.

Setting Up a Local Kubernetes Cluster

You can use Minikube to set up a Kubernetes cluster for a development environment. First, download the latest Minikube version from their official release page.

				
					curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
				
			

Then, install the downloaded binary to your system.

				
					sudo install minikube-linux-amd64 /usr/local/bin/minikube
				
			

Next, verify the Minikube version with the following command.

				
					minikube version
				
			

Output.

				
					minikube version: v1.31.2
commit: fd7ecd9c4599bef9f04c0986c4a0187f98a4396e
				
			

Finally, start the Minikube to setup a Kubernetes cluster.

				
					minikube start --driver=docker --force
				
			

If everything is fine, you will see the following screen.

Next, download and install the Kubectl tool to manage your Kubernetes cluster via command line interface.

				
					curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x kubectl
mv kubectl /usr/local/bin/

				
			

Then, verify your Kubernetes cluster using the command given below.

				
					kubectl cluster-info
				
			

You will see your cluster information on the following screen.

To get the node information of your Kubernetes, run the following command.

				
					kubectl get nodes
				
			

You will get the following output.

				
					NAME       STATUS   ROLES           AGE     VERSION
minikube   Ready    control-plane   2m12s   v1.27.4
				
			

Deploy an Express App on Kubernetes

At this point, your Kubernetes cluster is ready to deploy an application. Now, create a deployment file for your Express application.

				
					nano deployment.yaml
				
			

Add the following configuration and specify your Express container image and number of replica.

				
					apiVersion: apps/v1
kind: Deployment
metadata:
  name: express-app-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: express-app
  template:
    metadata:
      labels:
        app: express-app
    spec:
      containers:
      - name: express-app
        image: hitjethva/express-app:v1    

				
			

Save and close the file. Then, apply the above configuration to the Kubernetes cluster.

				
					kubectl apply -f deployment.yaml
				
			

You can now verify your deployment using the following command.

				
					kubectl get pods
				
			

This will show your Express replicas in the following output.

				
					NAME                                      READY   STATUS    RESTARTS   AGE
express-app-deployment-6fb5947748-xttw9   1/1     Running   0          59s
express-app-deployment-6fb5947748-zs78d   1/1     Running   0          59s

				
			

Expose an Express Application

At this point, your application is deployed on the Kubernetes. However, it can not be accessed outside the Kubernetes. So you need to expose your application using the kubectl expose command.

				
					kubectl expose deployment/express-app-deployment --type="NodePort" --port 8080
				
			

You can verify the exposed service using the following command.

				
					kubectl get svc
				
			

This will show you the port of your Express application as shown below.

				
					NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
express-app-deployment   NodePort    10.101.230.78   <none>        8080:31998/TCP   12s
kubernetes               ClusterIP   10.96.0.1       <none>        443/TCP          5m57s
				
			

To see the more information about your deployment, run the following command.

				
					kubectl describe services/express-app-deployment
				
			

You will see the following screen.

Now, verify your Express application running on the Kubernetes using the following command.

				
					curl $(minikube ip):31998
				
			

You will see the following output.

				
					Express application is working!
				
			

Scaling an Express Application

At this point, we have created two pods to run your application. You will need to scale the application when traffic increases. In this section, we scale an Express application to run on 4 pods.

First, verify the existing replica for your application using the following command.

				
					kubectl get rs
				
			

You will see that there are two replicas for your application.

				
					NAME                                DESIRED   CURRENT   READY   AGE
express-app-deployment-6fb5947748   2         2         2       6m42s
				
			

Now, scale the deployment to 4 replicas using the following command.

				
					kubectl scale deployment/express-app-deployment --replicas=4
				
			

Then, verify the new replica number with the following command.

				
					kubectl get rs
				
			

Output.

				
					NAME                                DESIRED   CURRENT   READY   AGE
express-app-deployment-6fb5947748   4         4         4       7m14s

				
			

You can also check the number of changed pods using the following command.

				
					kubectl get pods -o wide
				
			

You will see the 4 pods with different IPs for each pod.

				
					NAME                                      READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
express-app-deployment-6fb5947748-4d22b   1/1     Running   0          32s     10.244.0.5   minikube   <none>           <none>
express-app-deployment-6fb5947748-b7gmp   1/1     Running   0          32s     10.244.0.6   minikube   <none>           <none>
express-app-deployment-6fb5947748-xttw9   1/1     Running   0          7m24s   10.244.0.3   minikube   <none>           <none>
express-app-deployment-6fb5947748-zs78d   1/1     Running   0          7m24s   10.244.0.4   minikube   <none>           <none>
				
			

For more information on scaled deployment, run the following command.

				
					kubectl describe deployments/express-app-deployment
				
			

You will see the following screen.

Docker and Kubernetes: How to Manage Containers at Scale Conclusion

In this post, we have created an Express application, dockerize it then deploy and scale using the Kubernetes platform. Overall, Kubernetes makes managing and scaling containerized applications easier while enhancing the efficiency and flexibility of the deployment of new infrastructure and applications. Apart from supporting various types of infrastructure, Kubernetes even works well with any program that runs containers.

In fact, to meet rising demand and maintain system availability, it intelligently distributes and balances containerized workloads and expands clusters as necessary. Because of its benefits, it is now the industry’s top platform for container orchestration.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x