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
-
Kubernetes Overview
- Kubernetes Architecture
- Key Components: Nodes, Pods, Services, Deployments
- Kubernetes Cluster
-
Setting Up a Kubernetes Cluster
- Minikube for Local Development
- Kubernetes on Cloud Providers (GKE, EKS, AKS)
-
Deploying Docker Containers
- Creating a Deployment
- Exposing a Deployment
- Managing Deployments
-
Practical Example
- Deploying a Sample Application
-
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.
-
Install Minikube:
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 chmod +x minikube sudo mv minikube /usr/local/bin/
-
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.
-
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
-
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.
-
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
-
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.
-
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
-
Apply the Deployment:
kubectl apply -f nginx-deployment.yaml
-
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
-
Apply the Service:
kubectl apply -f nginx-service.yaml
Exercises
Exercise 1: Deploy a Custom Application
- Create a Docker image for a simple web application.
- Push the Docker image to Docker Hub.
- Create a Kubernetes Deployment YAML file for your application.
- Create a Kubernetes Service YAML file to expose your application.
- Deploy your application to a Kubernetes cluster using
kubectl
.
Solution
-
Dockerfile:
FROM node:14 WORKDIR /app COPY . . RUN npm install CMD ["node", "app.js"]
-
Build and Push Docker Image:
docker build -t your-dockerhub-username/your-app:latest . docker push your-dockerhub-username/your-app:latest
-
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
-
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
-
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.
Docker: From Beginner to Advanced
Module 1: Introduction to Docker
- What is Docker?
- Installing Docker
- Docker Architecture
- Basic Docker Commands
- Understanding Docker Images
- Creating Your First Docker Container
Module 2: Working with Docker Images
- Docker Hub and Repositories
- Building Docker Images
- Dockerfile Basics
- Managing Docker Images
- Tagging and Pushing Images
Module 3: Docker Containers
- Running Containers
- Container Lifecycle
- Managing Containers
- Networking in Docker
- Data Persistence with Volumes
Module 4: Docker Compose
- Introduction to Docker Compose
- Defining Services in Docker Compose
- Docker Compose Commands
- Multi-Container Applications
- Environment Variables in Docker Compose
Module 5: Advanced Docker Concepts
- Docker Networking Deep Dive
- Docker Storage Options
- Docker Security Best Practices
- Optimizing Docker Images
- Docker Logging and Monitoring
Module 6: Docker in Production
- CI/CD with Docker
- Orchestrating Containers with Docker Swarm
- Introduction to Kubernetes
- Deploying Docker Containers in Kubernetes
- Scaling and Load Balancing