Docker Fundamentals Kubernetes
Welcome to this comprehensive, student-friendly guide to understanding Docker and Kubernetes! 🚀 Whether you’re a beginner or have some experience with coding, this tutorial will help you grasp the core concepts and get hands-on with practical examples. Don’t worry if this seems complex at first; we’ll break it down step-by-step. Let’s dive in! 🏊♂️
What You’ll Learn 📚
- Core concepts of Docker and Kubernetes
- Key terminology and definitions
- Simple to complex examples
- Common questions and answers
- Troubleshooting tips
Introduction to Docker and Kubernetes
Docker is a platform that allows developers to package applications into containers—standardized executable components that combine application source code with the operating system libraries and dependencies required to run that code in any environment. Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications. Think of Docker as the tool to create containers and Kubernetes as the tool to manage them.
Key Terminology
- Container: A lightweight, standalone, executable package of software that includes everything needed to run it.
- Image: A read-only template used to create containers.
- Pod: The smallest deployable units of computing in Kubernetes, which can contain one or more containers.
- Cluster: A set of nodes (machines) that run containerized applications managed by Kubernetes.
Getting Started with Docker
Simple Example: Hello World
# Pull the hello-world image from Docker Hub
docker pull hello-world
# Run the hello-world container
docker run hello-world
In this example, we pull a simple image called hello-world from Docker Hub and run it as a container. This is the simplest way to verify that Docker is installed correctly on your system.
Progressively Complex Examples
Example 1: Running a Simple Web Server
# Pull the nginx image
docker pull nginx
# Run the nginx container
docker run -d -p 8080:80 nginx
Here, we pull the nginx image and run it, mapping port 80 inside the container to port 8080 on your local machine. This allows you to access the web server via your web browser.
Example 2: Building a Custom Docker Image
# Create a Dockerfile
# Use the official Python image 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
# Run app.py when the container launches
CMD ["python", "app.py"]
This Dockerfile describes how to build a Docker image for a Python application. It sets up a working directory, copies application files, installs dependencies, exposes a port, and specifies the command to run the application.
Introduction to Kubernetes
Simple Example: Deploying a Pod
# Create a YAML file for a simple pod
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx
This YAML file defines a simple Kubernetes pod running an nginx container. It specifies the API version, kind, metadata, and the container details.
Progressively Complex Examples
Example 1: Deploying a Deployment
# Create a YAML file for a deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
This YAML file defines a Kubernetes deployment that manages a set of identical pods (replicas) running the nginx container. Deployments provide declarative updates to applications.
Example 2: Creating a Service
# Create a YAML file for a service
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
This YAML file defines a Kubernetes service that exposes the nginx deployment to the network. Services allow you to expose your application to external traffic.
Common Questions and Answers
- What is the difference between Docker and Kubernetes?
Docker is used to create and run containers, while Kubernetes is used to manage and orchestrate those containers across a cluster of machines.
- Why use containers instead of virtual machines?
Containers are lightweight and share the host system’s kernel, making them faster to start and more efficient in resource usage compared to virtual machines.
- How do I install Docker?
Visit the official Docker documentation for installation instructions specific to your operating system: Get Docker.
- What is a Kubernetes cluster?
A Kubernetes cluster is a set of nodes that run containerized applications managed by Kubernetes. It consists of a control plane and one or more worker nodes.
- How do I scale an application in Kubernetes?
You can scale an application by adjusting the number of replicas in a deployment. Use the
kubectl scale
command or update the deployment YAML file.
Troubleshooting Common Issues
If you encounter issues with Docker not starting, check if the Docker daemon is running. You can restart it using
sudo systemctl restart docker
on Linux.
Lightbulb moment: Remember, Docker is like a shipping container for your code, and Kubernetes is the port authority managing all the containers!
For more detailed troubleshooting, refer to the official Docker and Kubernetes documentation.
Practice Exercises
- Try creating a Dockerfile for a simple Node.js application and build an image from it.
- Deploy a multi-container application using Kubernetes and expose it using a service.
- Experiment with scaling a Kubernetes deployment up and down.
Remember, practice makes perfect! Keep experimenting and don’t hesitate to reach out to the community for help. Happy coding! 🎉