DevOps Sessions - Week 12 - Containers
devops containers docker microservices 17-10-2024
DevOps Sessions - Week 12 - Containers
Welcome to Week 12 of our “Becoming a DevOps Engineer” series! This week, we will focus on containers, a transformative technology that enables developers to package applications and their dependencies into a standardized unit for software development. Containers offer consistency across multiple environments and streamline the deployment process. We will explore the basics of containers, key concepts, and popular tools like Docker and Kubernetes. Let’s dive in!
Session Overview
1. Introduction to Containers
- What are Containers?
- Benefits of Containers in DevOps
2. Docker
- Overview of Docker
- Setting Up Docker
- Creating and Managing Containers with Docker
3. Kubernetes
- Overview of Kubernetes
- Setting Up Kubernetes
- Managing Containers with Kubernetes
4. Container Orchestration
- What is Container Orchestration?
- Importance in DevOps
5. Practical Examples
- Building a Docker Image
- Deploying a Containerized Application with Kubernetes
6. Best Practices and Tools
- Best Practices for Using Containers
- Popular Container Tools
1. Introduction to Containers
What are Containers?
Containers are lightweight, portable, and self-sufficient units that include an application and all its dependencies. They run consistently across various environments, from a developer’s local machine to production.
Benefits of Containers in DevOps
- Consistency: Ensure applications run the same in development, testing, and production.
- Isolation: Separate applications and their dependencies, preventing conflicts.
- Scalability: Easily scale applications up or down based on demand.
- Portability: Run containers on any system that supports the container runtime.
2. Docker
Overview of Docker
Docker is an open-source platform that automates the deployment, scaling, and management of applications in containers. It simplifies the creation and management of containers.
Setting Up Docker
-
Install Docker:
- Follow the installation instructions for your operating system from the official Docker website.
-
Verify Installation:
docker --version
Creating and Managing Containers with Docker
-
Create a 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"]
-
Build the Docker Image:
docker build -t my-python-app .
-
Run the Docker Container:
docker run -p 4000:80 my-python-app
-
Manage Containers:
docker ps # List running containers docker stop <ID> # Stop a running container docker rm <ID> # Remove a container
3. Kubernetes
Overview of Kubernetes
Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It groups containers into logical units for easy management and discovery.
Setting Up Kubernetes
-
Install Minikube: Minikube is a tool that sets up a local Kubernetes cluster on your machine.
- Follow the installation instructions from the official Minikube website.
-
Start Minikube:
minikube start
Managing Containers with Kubernetes
-
Create a Deployment:
apiVersion: apps/v1 kind: Deployment metadata: name: my-python-app spec: replicas: 2 selector: matchLabels: app: my-python-app template: metadata: labels: app: my-python-app spec: containers: - name: my-python-app image: my-python-app ports: - containerPort: 80
-
Apply the Deployment:
kubectl apply -f deployment.yaml
-
Expose the Deployment:
kubectl expose deployment my-python-app --type=LoadBalancer --port=4000 --target-port=80
-
Manage Kubernetes Resources:
kubectl get pods # List running pods kubectl delete pod <NAME> # Delete a pod kubectl get services # List services
4. Container Orchestration
What is Container Orchestration?
Container orchestration automates the deployment, management, scaling, and networking of containers. It ensures applications run efficiently in a distributed environment.
Importance in DevOps
- Scalability: Automatically scale applications based on demand.
- Resilience: Ensure high availability and disaster recovery.
- Efficiency: Optimize resource usage across multiple containers and hosts.
5. Practical Examples
Building a Docker Image
-
Create a Dockerfile:
FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 CMD ["node", "server.js"]
-
Build the Image:
docker build -t my-node-app .
-
Run the Container:
docker run -p 8080:8080 my-node-app
Deploying a Containerized Application with Kubernetes
-
Create a Deployment:
apiVersion: apps/v1 kind: Deployment metadata: name: my-node-app spec: replicas: 3 selector: matchLabels: app: my-node-app template: metadata: labels: app: my-node-app spec: containers: - name: my-node-app image: my-node-app ports: - containerPort: 8080
-
Apply the Deployment:
kubectl apply -f deployment.yaml
-
Expose the Deployment:
kubectl expose deployment my-node-app --type=LoadBalancer --port=8080 --target-port=8080
6. Best Practices and Tools
Best Practices for Using Containers
- Use Lightweight Base Images: Minimize the image size to reduce attack surface and improve performance.
- Avoid Running as Root: Run applications as a non-root user for better security.
- Keep Images Updated: Regularly update base images and dependencies.
- Use Multi-Stage Builds: Optimize the build process to produce leaner images.
Popular Container Tools
- Docker: For building and running containers.
- Kubernetes: For orchestrating containerized applications.
- Helm: For managing Kubernetes applications.
- Rancher: For managing Kubernetes clusters.
- Podman: For managing containers without a daemon.
By mastering containers with tools like Docker and Kubernetes, you can ensure your applications are portable, scalable, and consistent across environments. Stay tuned for next week’s session, where we will explore container orchestration. Happy containerizing!