3-how-to-deploy-a-docker-containerized-application-with-kubernetes.html

How to Deploy a Docker Containerized Application with Kubernetes

In the fast-evolving world of software development, deploying applications efficiently and reliably is paramount. Containerization has become a cornerstone of modern application deployment, with Docker leading the charge. However, to truly harness the power of containerization at scale, Kubernetes emerges as the go-to orchestration tool. In this article, we will delve into how to deploy a Docker containerized application using Kubernetes, complete with code examples, step-by-step instructions, and troubleshooting tips.

What is Docker?

Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. These containers bundle the application code along with its dependencies, ensuring that it runs consistently across different environments.

Key Benefits of Docker

  • Isolation: Applications run in their own environment, eliminating dependency conflicts.
  • Portability: Docker containers can run on any machine that has Docker installed.
  • Efficiency: Containers share the host OS kernel, making them lightweight compared to traditional virtual machines.

What is Kubernetes?

Kubernetes, often abbreviated as K8s, is an open-source orchestration platform for automating the deployment, scaling, and management of containerized applications. It manages clusters of containers, helping developers and operations teams ensure that applications are running efficiently and reliably.

Key Benefits of Kubernetes

  • Automated Scaling: Kubernetes can automatically scale your application up or down based on traffic.
  • Self-Healing: If a container fails, Kubernetes can automatically restart or replace it.
  • Load Balancing: Kubernetes distributes network traffic to ensure stable application performance.

Use Cases for Docker and Kubernetes

  • Microservices Architecture: Use Docker for each microservice and Kubernetes to manage their deployment.
  • DevOps Practices: Streamline CI/CD pipelines with Docker containers and Kubernetes orchestration.
  • Hybrid Cloud Deployments: Run Docker containers on-premises or in the cloud, then manage them with Kubernetes.

Steps to Deploy a Docker Containerized Application with Kubernetes

Let’s go through a step-by-step guide to deploying a simple Node.js application containerized with Docker on a Kubernetes cluster.

Prerequisites

Before you begin, ensure you have the following:

  • Docker installed on your machine.
  • Kubernetes cluster (you can use Minikube for local development).
  • kubectl command-line tool installed.

Step 1: Create a Simple Node.js Application

First, let’s create a simple Node.js application. Create a file named app.js and add the following code:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('Hello, Kubernetes!');
});

app.listen(port, () => {
    console.log(`App running at http://localhost:${port}`);
});

Step 2: Create a Dockerfile

Next, create a Dockerfile in the same directory as your app.js file. This file will define how to build your Docker image.

# Use the official Node.js image.
FROM node:14

# Set the working directory.
WORKDIR /usr/src/app

# Copy package.json and install dependencies.
COPY package*.json ./
RUN npm install

# Copy the application code.
COPY . .

# Expose the port the app runs on.
EXPOSE 3000

# Command to run the application.
CMD ["node", "app.js"]

Step 3: Build the Docker Image

Now, build your Docker image with the following command:

docker build -t my-node-app .

Step 4: Test Your Docker Container

Before deploying to Kubernetes, test your Docker container locally:

docker run -p 3000:3000 my-node-app

Visit http://localhost:3000 in your browser to see if it runs correctly.

Step 5: Push the Docker Image to a Registry

To deploy your application on Kubernetes, you need to push your Docker image to a container registry. For example, if you are using Docker Hub:

docker login
docker tag my-node-app yourusername/my-node-app
docker push yourusername/my-node-app

Step 6: Create a Kubernetes Deployment

Create a file named deployment.yaml to define your Kubernetes deployment configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-node-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-node-app
  template:
    metadata:
      labels:
        app: my-node-app
    spec:
      containers:
      - name: my-node-app
        image: yourusername/my-node-app
        ports:
        - containerPort: 3000

Step 7: Apply the Deployment Configuration

Use kubectl to apply your deployment configuration:

kubectl apply -f deployment.yaml

Step 8: Expose the Application

To access your application from outside the Kubernetes cluster, create a service. Create a file named service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: my-node-app-service
spec:
  type: NodePort
  ports:
    - port: 3000
      targetPort: 3000
      nodePort: 30001
  selector:
    app: my-node-app

Apply the service configuration:

kubectl apply -f service.yaml

Step 9: Access Your Application

Find out the IP address of your Kubernetes cluster (for Minikube, you can use minikube ip) and access your application at http://<minikube-ip>:30001.

Troubleshooting Tips

  • Check Pod Status: Use kubectl get pods to check if your pods are running.
  • View Logs: Use kubectl logs <pod-name> to view logs for debugging.
  • Describe Resources: Use kubectl describe pod <pod-name> for more detailed information.

Conclusion

Deploying a Docker containerized application with Kubernetes can seem daunting at first, but with the right steps and tools, it becomes a streamlined process. By following this guide, you should now have a basic understanding of how to create, containerize, and deploy a Node.js application using Docker and Kubernetes. Embracing these technologies helps you automate deployment processes, scale applications seamlessly, and ultimately enhance your development workflow. Happy deploying!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.