Join our Discord Server
Avinash Bendigeri Avinash is a developer-turned Technical writer skilled in core content creation. He has an excellent track record of blogging in areas like Docker, Kubernetes, IoT and AI.

Docker Compose vs. Kubernetes: A Comparative Analysis with a Sample Application Example

3 min read

In the ever-evolving landscape of container orchestration, Docker Compose and Kubernetes stand out as two of the most popular tools used by developers and DevOps professionals to manage and deploy applications. While both tools serve the purpose of managing containerized applications, they cater to different use cases and complexities of deployment environments. In this blog post, we will dive into a comparative analysis of Docker Compose and Kubernetes, followed by a practical example of deploying a simple application using both tools.

Understanding Docker Compose

Docker Compose is a tool designed to define and run multi-container Docker applications. With a simple YAML file, developers can configure their application’s services, networks, and volumes, and then start the application with a single command: docker-compose up. Docker Compose is ideal for development, testing, and staging environments, as it simplifies the process of deploying and managing application stacks.

Also Read: How Docker Compose is different from Dockerfile

Key Features of Docker Compose:

  • Simplicity and Speed: Ideal for small-scale projects and development environments.
  • Configuration in YAML File: Defines services, networks, and volumes in a docker-compose.yml file.
  • Integrated Environment: Easily integrates with the Docker ecosystem.

Understanding Kubernetes

Kubernetes, on the other hand, is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. Kubernetes is highly scalable and extensible, making it suitable for production environments, especially when dealing with complex applications that require high availability, scalability, and resilience.

Key Features of Kubernetes:

  • High Availability: Ensures that applications are always running without downtime.
  • Scalability: Easily scales applications up or down based on demand.
  • Service Discovery and Load Balancing: Automatically distributes traffic to ensure stability and efficiency.
  • Self-healing: Automatically restarts failed containers, replaces, and reschedules containers when nodes die.

Docker Compose vs. Kubernetes: When to Use Which?

The choice between Docker Compose and Kubernetes depends on the scale and complexity of the application:

  • Use Docker Compose when you’re working on development, testing, or small-scale projects that require quick iteration. It’s also suitable for learning container concepts and for applications that do not require scaling.
  • Use Kubernetes when you’re deploying applications in production, especially if they require high availability, scalability, and automated deployment across multiple hosts. Kubernetes is the go-to for microservices architectures and complex applications.

Sample Application Example

Let’s consider a simple web application that consists of a Python Flask app serving as a backend and a Redis database for caching.

Prerequisite

Sample Flask Application (app.py)

Create a simple Flask application that interacts with Redis. First, ensure you have Flask and Redis libraries in your Python environment.

from flask import Flask
from redis import Redis

app = Flask(__name__)
redis = Redis(host='redis', port=6379)

@app.route('/')
def hello():
    redis.incr('hits')
    return 'This page has been visited %s times.' % redis.get('hits').decode('utf-8')

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

Dockerfile for Flask App

To build the flask-app image mentioned in your Docker Compose file, create a Dockerfile in the same directory as your Flask application (app.py).

FROM python:3.8-alpine
WORKDIR /code
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

requirements.txt

List the Python packages your Flask app requires:

flask
redis

Deploying with Docker Compose

  1. Create a docker-compose.yml file

services:
  web:
    image: flask-app
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - redis
  redis:
    image: "redis:alpine"
  1. Build and run your application
docker compose up --build

This configuration tells Docker to build the Flask app from the current directory, link it to a Redis service, and expose the app on port 5000.

curl localhost:5000
This page has been visited 3 times

Deploying with Kubernetes

  1. Create a deployment for the Flask app (flask-deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: flask-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: flask-app
  template:
    metadata:
      labels:
        app: flask-app
    spec:
      containers:
      - name: flask-app
        image: flask-app
        ports:
        - containerPort: 5000
  1. Create a service to expose the Flask app (flask-service.yaml)
apiVersion: v1
kind: Service
metadata:
  name: flask-app
spec:
  type: LoadBalancer
  ports:
  - port: 5000
    targetPort: 5000
  selector:
    app: flask-app

Deploy Redis on Kubernetes


apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  selector:
    matchLabels:
      app: redis
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:alpine
        ports:
        - containerPort: 6379

---
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  type: ClusterIP
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis

Deploy the Flask app and Redis on Kubernetes

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

This configuration deploys the Flask app and Redis database on Kubernetes, ensuring high availability and scalability.

Verifying if all the Pods are up and running

kubectl get po -A
NAMESPACE     NAME                                     READY   STATUS    RESTARTS   AGE
default       flask-app-6d59c58c45-2s4bb               1/1     Running   0          9s
default       flask-app-6d59c58c45-gghdt               1/1     Running   0          9s
default       redis-6d67947767-q5779                   1/1     Running   0          3m20s
kube-system   coredns-76f75df574-2jn7x                 1/1     Running   0          17d
kube-system   coredns-76f75df574-ljvbw                 1/1     Running   0          17d
kube-system   etcd-docker-desktop                      1/1     Running   0          17d
kube-system   kube-apiserver-docker-desktop            1/1     Running   0          17d
kube-system   kube-controller-manager-docker-desktop   1/1     Running   0          17d
kube-system   kube-proxy-sr55m                         1/1     Running   0          17d
kube-system   kube-scheduler-docker-desktop            1/1     Running   0          17d
kube-system   storage-provisioner                      1/1     Running   0          17d
kube-system   vpnkit-controller                        1/1     Running   0          17d
curl localhost:5000
This page has been visited 1 times

Conclusion

While Docker Compose offers simplicity and speed for development environments, Kubernetes provides robust, scalable solutions for production deployments. The choice between them should be based on your project’s requirements, infrastructure, and scalability needs. Our example illustrates the fundamental differences in deploying a simple application using both Docker Compose and Kubernetes, highlighting how each tool can be utilized effectively in different scenarios.

Have Queries? Join https://launchpass.com/collabnix

Avinash Bendigeri Avinash is a developer-turned Technical writer skilled in core content creation. He has an excellent track record of blogging in areas like Docker, Kubernetes, IoT and AI.

“sh: next: command not found” in Next.js Development? Here’s…

Struggling with the "sh: next: command not found" error in your Next.js project? Learn how to fix it by running Next.js locally or globally....
Avinash Bendigeri
1 min read
Join our Discord Server
Index