In this module, we will explore Docker and Kubernetes, two essential tools for modern CI/CD pipelines. Docker is a platform for developing, shipping, and running applications in containers, while Kubernetes is an orchestration system for automating the deployment, scaling, and management of containerized applications.

Introduction to Docker

What is Docker?

Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers package an application and its dependencies, ensuring consistency across multiple environments.

Key Concepts of Docker

  • Images: Read-only templates used to create containers. Images are built from Dockerfiles.
  • Containers: Instances of Docker images that run applications.
  • Dockerfile: A script containing a series of instructions on how to build a Docker image.
  • Docker Hub: A cloud-based registry service for sharing Docker images.

Example: Creating a Simple Docker Container

Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Building and Running the Docker Container

# Build the Docker image
docker build -t my-python-app .

# Run the Docker container
docker run -p 4000:80 my-python-app

Explanation

  • FROM: Specifies the base image.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files from the host to the container.
  • RUN: Executes commands in the container.
  • EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
  • ENV: Sets environment variables.
  • CMD: Provides the command to run within the container.

Introduction to Kubernetes

What is Kubernetes?

Kubernetes, often abbreviated as K8s, is an open-source platform designed to automate deploying, scaling, and operating application containers. It groups containers into logical units for easy management and discovery.

Key Concepts of Kubernetes

  • Pods: The smallest deployable units in Kubernetes, which can contain one or more containers.
  • Nodes: Worker machines in Kubernetes, which can be either virtual or physical.
  • Clusters: A set of nodes managed by Kubernetes.
  • Services: An abstraction that defines a logical set of Pods and a policy by which to access them.
  • Deployments: Provide declarative updates to applications.

Example: Deploying a Docker Container with Kubernetes

Deployment YAML File

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-python-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-python-app
  template:
    metadata:
      labels:
        app: my-python-app
    spec:
      containers:
      - name: my-python-app
        image: my-python-app:latest
        ports:
        - containerPort: 80

Service YAML File

apiVersion: v1
kind: Service
metadata:
  name: my-python-app-service
spec:
  selector:
    app: my-python-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

Explanation

  • apiVersion: Specifies the API version.
  • kind: Specifies the type of Kubernetes object (e.g., Deployment, Service).
  • metadata: Provides metadata about the object, such as its name.
  • spec: Defines the desired state of the object.
  • replicas: Specifies the number of pod replicas.
  • selector: Selects the pods that the service targets.
  • template: Defines the pod template.

Deploying to Kubernetes

# Apply the deployment configuration
kubectl apply -f deployment.yaml

# Apply the service configuration
kubectl apply -f service.yaml

# Check the status of the deployment
kubectl get deployments

# Check the status of the service
kubectl get services

Practical Exercise

Exercise: Deploying a Web Application with Docker and Kubernetes

Task

  1. Create a Dockerfile for a simple web application.
  2. Build and run the Docker container locally.
  3. Write Kubernetes deployment and service YAML files.
  4. Deploy the application to a Kubernetes cluster.

Solution

  1. Dockerfile

    FROM node:14
    WORKDIR /app
    COPY . /app
    RUN npm install
    EXPOSE 3000
    CMD ["npm", "start"]
    
  2. Build and Run Docker Container

    docker build -t my-web-app .
    docker run -p 3000:3000 my-web-app
    
  3. Deployment YAML File

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-web-app
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: my-web-app
      template:
        metadata:
          labels:
            app: my-web-app
        spec:
          containers:
          - name: my-web-app
            image: my-web-app:latest
            ports:
            - containerPort: 3000
    
  4. Service YAML File

    apiVersion: v1
    kind: Service
    metadata:
      name: my-web-app-service
    spec:
      selector:
        app: my-web-app
      ports:
        - protocol: TCP
          port: 80
          targetPort: 3000
      type: LoadBalancer
    
  5. Deploy to Kubernetes

    kubectl apply -f deployment.yaml
    kubectl apply -f service.yaml
    kubectl get deployments
    kubectl get services
    

Conclusion

In this module, we covered the basics of Docker and Kubernetes, including their key concepts and practical examples. Docker allows you to package applications in containers, ensuring consistency across environments. Kubernetes helps you manage and orchestrate these containers at scale. By understanding these tools, you can enhance your CI/CD pipelines, making your software delivery process more efficient and reliable.

© Copyright 2024. All rights reserved