· 3 min read

Navigating the Container Ecosystem: An Introduction to Docker and Kubernetes

This blog post aims to provide a comprehensive introduction to Docker and Kubernetes, guiding you through the basics of containerization.

Navigating the Container Ecosystem: An Introduction to Docker and Kubernetes

In the evolving landscape of software development, containerization has revolutionized the way we build, ship, and run applications. Docker and Kubernetes are two cornerstone technologies that have emerged to streamline application deployment and management at scale.

This blog post aims to provide a comprehensive introduction to Docker and Kubernetes, guiding you through the basics of containerization, and demonstrating how to deploy and manage applications efficiently.

1. Understanding Containerization

What is Containerization?

Containerization is a lightweight alternative to full machine virtualization that involves encapsulating an application in a container with its own operating environment. This means that the application can run in isolation but share the host system's kernel, leading to efficient resource utilization.

Benefits

  • Portability: Containers can run consistently across different environments.
  • Efficiency: Containers are lightweight and require fewer resources than virtual machines.
  • Scalability: Easier to scale applications horizontally.
  • Isolation: Each container runs independently, ensuring application isolation.

2. Introduction to Docker

Docker is an open-source platform designed to automate the deployment, scaling, and management of applications using containerization.

Installing Docker

On Windows and macOS

Download and install Docker Desktop from the official website.

On Linux

Follow the instructions for your distribution:

# For Ubuntu
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Verifying the Installation

docker --version

Basic Docker Commands

Running Your First Container

docker run hello-world
  • Explanation: Downloads the hello-world image and runs it in a new container.

Listing Docker Images

docker images

Listing Running Containers

docker ps

Listing All Containers

docker ps -a

Stopping a Running Container

docker stop container_id

Working with Dockerfiles

A Dockerfile is a text document that contains instructions for Docker to build an image.

Sample Dockerfile

# Use an official Node.js runtime as a parent image
FROM node:14
 
# Set the working directory
WORKDIR /usr/src/app
 
# Copy the package.json and install dependencies
COPY package*.json ./
RUN npm install
 
# Copy the rest of the application code
COPY . .
 
# Expose the port
EXPOSE 8080
 
# Run the application
CMD ["node", "app.js"]

Building an Image from a Dockerfile

docker build -t yourusername/your-app .

Running the Docker Image

docker run -p 8080:8080 yourusername/your-app

3. Introduction to Kubernetes

Kubernetes is an open-source platform designed to automate deploying, scaling, and operating containerized applications.

Core Concepts of Kubernetes

  • Pod: The smallest deployable unit in Kubernetes, which can contain one or more containers.
  • Deployment: Manages the desired state of pods.
  • Service: An abstraction that defines a logical set of pods and a policy to access them.
  • Cluster: A set of nodes (machines) where Kubernetes can schedule workloads.

Setting Up a Local Kubernetes Environment

Using Minikube

Minikube allows you to run Kubernetes locally.

Installation

  1. Install Minikube: Minikube Installation Guide
  2. Start Minikube:minikube start

Using Kind

Kind (Kubernetes IN Docker) runs local Kubernetes clusters using Docker containers.

Installation

  1. Install Kind: Kind Installation Guide
  2. Create a Cluster:kind create cluster

4. Deploying Docker Containers with Kubernetes

Creating a Deployment

Sample Deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: yourusername/your-app:latest
        ports:
        - containerPort: 8080

Applying the Deployment

kubectl apply -f deployment.yaml

Exposing Your Application

Creating a Service

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30007

Applying the Service

kubectl apply -f service.yaml

Accessing the Application

  • Minikube:minikube service my-app-service
  • Direct Access:Visit http://localhost:30007 in your browser.

5. Scaling and Managing Applications

Scaling Deployments

Scaling Up

kubectl scale deployment my-app-deployment --replicas=4

Scaling Down

kubectl scale deployment my-app-deployment --replicas=2

Rolling Updates and Rollbacks

Performing a Rolling Update

Update the image in your deployment YAML and apply changes:

kubectl apply -f deployment.yaml

Kubernetes will perform a rolling update, replacing pods incrementally.

Rolling Back

kubectl rollout undo deployment my-app-deployment

6. Monitoring and Logging

Using Kubernetes Dashboard

Enabling the Dashboard

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml

Accessing the Dashboard

kubectl proxy

Visit http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

Logging with Kubectl

Viewing Pod Logs

kubectl logs pod_name

Watching Logs in Real-Time

kubectl logs pod_name -f

7. Conclusion

Docker and Kubernetes have become essential tools for modern DevOps practices, enabling developers to build, deploy, and manage applications efficiently. Docker provides the means to package applications into containers, while Kubernetes offers a robust system for managing those containers at scale.

By understanding the basics outlined in this guide, you're well on your way to leveraging these technologies to streamline your development and deployment workflows.

Happy Containerization!

Written by

Felix Yu

Full stack developer in Melbourne and founder of Pixdyne. Also Shuhai Yu on GitHub and LinkedIn.

About meCase studiesGitHub

Got something to make?
Write to me.

info@felixyu.net