8-deploying-a-multi-container-application-with-docker-compose-and-kubernetes.html

Deploying a Multi-Container Application with Docker Compose and Kubernetes

In today’s fast-paced software development landscape, deploying applications efficiently and effectively is paramount. With the rise of microservices architecture, managing multi-container applications has become essential. This article will walk you through the process of deploying a multi-container application using Docker Compose and Kubernetes, providing code examples, step-by-step instructions, and actionable insights for developers.

Understanding Docker Compose and Kubernetes

What is Docker Compose?

Docker Compose is a tool that simplifies the management of multi-container Docker applications. It allows developers to define and run multiple containers using a single configuration file, typically named docker-compose.yml. With this file, you can specify the services, networks, and volumes your application needs, making it easier to manage dependencies and configurations.

What is Kubernetes?

Kubernetes, often abbreviated as K8s, is an open-source platform designed to automate the deployment, scaling, and management of containerized applications. It provides a robust framework for running applications in a clustered environment, ensuring high availability and resilience.

Use Cases for Multi-Container Applications

Utilizing Docker Compose and Kubernetes is particularly beneficial in the following scenarios:

  • Microservices Architecture: When applications consist of multiple services that need to communicate with each other.
  • Development and Testing: Quickly set up and tear down environments for testing.
  • Scalability: Easily scale applications by adding or removing containers as needed.

Step-by-Step Guide: Deploying a Multi-Container Application

Step 1: Setting Up Docker Compose

First, ensure that you have Docker and Docker Compose installed on your machine. You can verify your installations by running:

docker --version
docker-compose --version

Next, create a new directory for your project:

mkdir my-multi-container-app
cd my-multi-container-app

Step 2: Defining the Docker Compose File

Create a docker-compose.yml file in your project directory. For this example, let’s deploy a simple Node.js application with a MongoDB database.

version: '3.8'

services:
  web:
    image: node:14
    command: npm start
    volumes:
      - ./app:/usr/src/app
    ports:
      - "3000:3000"
    depends_on:
      - db

  db:
    image: mongo:4.0
    ports:
      - "27017:27017"
    volumes:
      - dbdata:/data/db

volumes:
  dbdata:

Step 3: Creating Your Application Code

Create a folder named app in your project directory:

mkdir app

Inside the app folder, create a simple package.json file:

{
  "name": "myapp",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1",
    "mongoose": "^5.10.9"
  }
}

Next, create an index.js file in the same folder:

const express = require('express');
const mongoose = require('mongoose');
const app = express();
const PORT = process.env.PORT || 3000;

mongoose.connect('mongodb://db:27017/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });

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

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

Step 4: Running Your Application with Docker Compose

Now that your docker-compose.yml and application code are ready, you can run your application with the following command:

docker-compose up

This command will pull the necessary images, create the containers, and start your application. You should see output indicating that both the web server and the database are running.

Step 5: Accessing Your Application

Open your web browser and navigate to http://localhost:3000. You should see "Hello, World!" displayed. This confirms that your Node.js application is successfully communicating with the MongoDB container.

Deploying to Kubernetes

Once you have tested your application locally with Docker Compose, you can deploy it to Kubernetes.

Step 1: Installing kubectl

Ensure you have kubectl installed to manage your Kubernetes clusters. You can verify the installation with:

kubectl version --client

Step 2: Creating Kubernetes Manifests

Create a new directory for your Kubernetes manifests:

mkdir k8s
cd k8s

Create a deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: multi-container-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: multi-container-app
  template:
    metadata:
      labels:
        app: multi-container-app
    spec:
      containers:
      - name: web
        image: yourdockerhubusername/myapp:latest
        ports:
        - containerPort: 3000
      - name: db
        image: mongo:4.0
        ports:
        - containerPort: 27017

And a service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: multi-container-app
spec:
  type: NodePort
  ports:
    - port: 3000
      targetPort: 3000
  selector:
    app: multi-container-app

Step 3: Deploying to Kubernetes

Apply your manifests using the following command:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

Step 4: Accessing Your Application in Kubernetes

To access your application, you’ll need to find the NodePort assigned by Kubernetes. Run:

kubectl get services

Once you have the port, you can access your application via http://<Node-IP>:<Node-Port>.

Conclusion

Deploying a multi-container application with Docker Compose and Kubernetes is a powerful approach to building scalable and resilient applications. By using Docker Compose for local development and Kubernetes for production deployment, you can streamline your workflow and enhance collaboration among development teams.

With this comprehensive guide, you now have the tools and knowledge needed to implement your multi-container applications effectively. Happy coding!

SR
Syed
Rizwan

About the Author

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