In this section, we will explore how to deploy Docker containers in Kubernetes. Kubernetes is a powerful orchestration tool that automates the deployment, scaling, and management of containerized applications. By the end of this module, you will understand the basics of Kubernetes and how to deploy Docker containers using Kubernetes.

Key Concepts

  1. Kubernetes Overview

    • Kubernetes Architecture
    • Key Components: Nodes, Pods, Services, Deployments
    • Kubernetes Cluster
  2. Setting Up a Kubernetes Cluster

    • Minikube for Local Development
    • Kubernetes on Cloud Providers (GKE, EKS, AKS)
  3. Deploying Docker Containers

    • Creating a Deployment
    • Exposing a Deployment
    • Managing Deployments
  4. Practical Example

    • Deploying a Sample Application
  5. Exercises

    • Hands-on practice with solutions

Kubernetes Overview

Kubernetes Architecture

Kubernetes is designed to manage containerized applications across multiple hosts. It provides mechanisms for deployment, maintenance, and scaling of applications. The key components of Kubernetes include:

  • Nodes: The worker machines in Kubernetes, which can be either virtual or physical.
  • Pods: The smallest and simplest Kubernetes object. A Pod represents a single instance of a running process in your cluster.
  • Services: An abstraction that defines a logical set of Pods and a policy by which to access them.
  • Deployments: A higher-level abstraction that manages Pods and ReplicaSets, providing declarative updates to applications.

Kubernetes Cluster

A Kubernetes cluster consists of a set of worker machines, called nodes, that run containerized applications. Every cluster has at least one worker node.

Setting Up a Kubernetes Cluster

Minikube for Local Development

Minikube is a tool that makes it easy to run Kubernetes locally. It runs a single-node Kubernetes cluster inside a VM on your laptop.

  1. Install Minikube:

    curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    chmod +x minikube
    sudo mv minikube /usr/local/bin/
    
  2. Start Minikube:

    minikube start
    

Kubernetes on Cloud Providers

For production environments, you can use managed Kubernetes services like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Azure Kubernetes Service (AKS).

Deploying Docker Containers

Creating a Deployment

A Deployment provides declarative updates to applications. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate.

  1. Create a Deployment YAML file (deployment.yaml):

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

    kubectl apply -f deployment.yaml
    

Exposing a Deployment

To make your application accessible from outside the Kubernetes cluster, you need to expose it as a Kubernetes Service.

  1. Create a Service YAML file (service.yaml):

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

    kubectl apply -f service.yaml
    

Managing Deployments

You can manage your deployments using kubectl commands:

  • Scale a Deployment:

    kubectl scale deployment my-app --replicas=5
    
  • Update a Deployment:

    kubectl set image deployment/my-app my-app=my-docker-image:v2
    

Practical Example

Let's deploy a sample Nginx application.

  1. Create a Deployment YAML file (nginx-deployment.yaml):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.2
            ports:
            - containerPort: 80
    
  2. Apply the Deployment:

    kubectl apply -f nginx-deployment.yaml
    
  3. Create a Service YAML file (nginx-service.yaml):

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-service
    spec:
      selector:
        app: nginx
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: LoadBalancer
    
  4. Apply the Service:

    kubectl apply -f nginx-service.yaml
    

Exercises

Exercise 1: Deploy a Custom Application

  1. Create a Docker image for a simple web application.
  2. Push the Docker image to Docker Hub.
  3. Create a Kubernetes Deployment YAML file for your application.
  4. Create a Kubernetes Service YAML file to expose your application.
  5. Deploy your application to a Kubernetes cluster using kubectl.

Solution

  1. Dockerfile:

    FROM node:14
    WORKDIR /app
    COPY . .
    RUN npm install
    CMD ["node", "app.js"]
    
  2. Build and Push Docker Image:

    docker build -t your-dockerhub-username/your-app:latest .
    docker push your-dockerhub-username/your-app:latest
    
  3. Deployment YAML (your-app-deployment.yaml):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: your-app-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: your-app
      template:
        metadata:
          labels:
            app: your-app
        spec:
          containers:
          - name: your-app
            image: your-dockerhub-username/your-app:latest
            ports:
            - containerPort: 3000
    
  4. Service YAML (your-app-service.yaml):

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

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

Conclusion

In this module, we covered the basics of deploying Docker containers in Kubernetes. We explored the Kubernetes architecture, set up a local Kubernetes cluster using Minikube, and deployed a sample application. We also provided a practical exercise to reinforce the learned concepts. In the next module, we will delve into scaling and load balancing in Kubernetes.

© Copyright 2024. All rights reserved