As a developer with Docker experience, you already understand the basics of containerization—packaging applications with their dependencies into a standardized unit.

Now, it’s time to take the next step: Kubernetes.

Kubernetes, or K8s, is a powerful container orchestration system that can manage your containerized applications at scale.

This guide will help you move from Docker to Kubernetes. It will show you how to use Kubernetes to improve your development and deployment work.

Docker vs. Kubernetes

Docker

Kubernetes

Practical Guide to Kubernetes for Docker Users

Step 1: Install Kubernetes

To get started with Kubernetes, you need to install it. The easiest way to do this locally is using Minikube, which sets up a local Kubernetes cluster. Below are the instructions for Windows, Linux, and macOS.

For Windows:

# Download the Minikube installer from the official Minikube releases page
# https://github.com/kubernetes/minikube/releases

# Run the installer and follow the instructions.

# Start Minikube
minikube start

For Linux:

# First, install the required dependencies
sudo apt-get update -y
sudo apt-get install -y curl apt-transport-https

# Download the Minikube binary
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

# Install Minikube
sudo install minikube-linux-amd64 /usr/local/bin/minikube

# Start Minikube
minikube start

For macOS:

# Install Minikube
brew install minikube

# Start Minikube
minikube start

Alternatives to Minikube

While Minikube is a popular choice for running Kubernetes locally, there are other alternatives you might consider:

Following these instructions, you can have a local Kubernetes cluster up and running on your preferred operating system. Additionally, you can explore the alternatives if Minikube does not meet your specific needs.

Step 2: Understanding Pods

In Docker, you typically run a single container using the docker run command. In Kubernetes, the equivalent is running a pod. A pod is the smallest deployable unit in Kubernetes and can contain one or more containers.

Sample Pod Configuration:

# pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx

To create this pod, use the kubectl command:

kubectl apply -f pod.yaml

You can check the status of the pod with:

kubectl get pods

To get detailed information about the pod, use:

kubectl describe pod my-pod

To view the logs for the pod, use:

kubectl logs my-pod

To run commands inside a running container of the pod, use:

kubectl exec -it my-pod -- /bin/bash

Step 3: Deployments

Deployments in Kubernetes provide a higher-level abstraction for managing a group of pods, including features for scaling and updating your application.

Sample Deployment Configuration:

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx

Create the deployment with:

kubectl apply -f deployment.yaml

You can check the status of your deployment with:

kubectl get deployments

To get detailed information about the deployment, use the following command:

kubectl describe deployment my-deployment

Step 4: Services

Services in Kubernetes expose your pods to the network. There are different types of services:

Sample Service Configuration:

# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

Apply the service configuration:

kubectl apply -f service.yaml

Check the service with:

kubectl get services

Now we can access the service, and forward a port from your local machine to the service port in the cluster:

kubectl port-forward service/my-service 8080:80

This command forwards the local port 8080 to port 80 of the service my-service.

Open your web browser and navigate to http://127.0.0.1:8080.

You should see the Nginx welcome page, confirming that the service is accessible.

Step 5: Scaling Applications

Scaling an application in Kubernetes is straightforward and can be done using the kubectl scale command. This allows you to easily increase or decrease the number of pod replicas for your deployment.

Scaling Command:

kubectl scale deployment my-deployment --replicas=5

To verify the scaling operation:

kubectl get deployments

As you can see on the screen, the number of replicas was increased from 3 to 5

Step 6: Updating Applications

One of the key features of Kubernetes is the ability to perform rolling updates. This allows you to update the application version without downtime.

Updating Deployment:

Edit the deployment to update the container image:

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx:latest  # Update the image tag here

Apply the changes:

kubectl apply -f deployment.yaml

Kubernetes will perform a rolling update, replacing the old pods with new ones gradually.

Step 7: Namespace Management

Namespaces in Kubernetes provide a way to divide cluster resources between multiple users or applications. This is particularly useful in large environments.

Creating a Namespace

kubectl create namespace my-namespace

You can then create resources within this namespace by specifying the -n flag:

kubectl apply -f pod.yaml -n my-namespace

Check the resources within a namespace:

kubectl get pods -n my-namespace

Step 8: Clean up

To delete everything you created, execute the following commands:

kubectl delte -f pod.yaml
kubectl delete -f deployment.yaml
kubectl delete -f service.yaml

These commands will remove the pod, deployment, and service you created during this guide.

Conclusion

Migrating from Docker to Kubernetes involves understanding new concepts and tools, but the transition is manageable with the right guidance. Kubernetes offers powerful features for scaling, managing, and updating your applications, making it an essential tool for modern cloud-native development. By following this practical guide, you’ll be well on your way to harnessing the full potential of Kubernetes.

Happy orchestrating!