Kubernetes for Complete Beginners - A Gentle Introduction

Kubernetes conceptual illustration Illustration image from Aspecto

Hey, there! Have you ever wondered how companies manage to run and extend their programs smoothly across several servers? Enter Kubernetes, an open-source container orchestration platform that is transforming the way we deploy and manage applications. In this beginner-friendly guide, I’ll try to demystify Kubernetes by walking you through a basic sample project.

What is Kubernetes, anyway?

Think about Kubernetes (also known as K8s) as a sharp application manager. A good manager coordinates team members, assigns responsibilities, and ensures that everything works well, and Kubernetes does the same for your application containers. It automates the deployment, scaling, and management of containerized applications on many computers.

Key Concepts for Beginners

Before diving deeper, let’s understand some fundamental concepts:

  1. Containers: Think of these as lightweight, standalone packages that contain everything needed to run a piece of software, including the code, runtime, libraries, and system tools.

  2. Pods: The smallest deployable unit in Kubernetes. A pod can contain one or more containers that share resources and are always scheduled together.

  3. Nodes: The physical or virtual machines that run your applications.

  4. Cluster: A group of nodes managed by Kubernetes.

Why Should You Care About Kubernetes?

Kubernetes solves several common problems in application deployment:

Setting Up Your First Kubernetes Environment

Let’s start with setting up a local Kubernetes environment for learning purposes.

Prerequisites

Step 1: Install Docker Desktop

First, download and install Docker Desktop from the official website.

Step 2: Install kubectl

For Windows (using PowerShell):

curl -LO "https://dl.k8s.io/release/v1.28.0/bin/windows/amd64/kubectl.exe"

For macOS (using Homebrew):

brew install kubectl

Step 3: Install minikube

For Windows (using PowerShell):

winget install minikube

For macOS:

brew install minikube

Step 4: Start Your Local Cluster

minikube start

Verify your setup:

kubectl get nodes

You should see output similar to:

NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   1m    v1.28.0

Your First Kubernetes Project: Deploying a Simple Web Application

Let’s deploy a basic web application to understand how Kubernetes works in practice.

Step 1: Create a Simple Web Application

First, let’s create a simple Node.js application. Create a new file named app.js:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello from Kubernetes!');
});

app.listen(port, () => {
  console.log(`App running on port ${port}`);
});

Step 2: Create a Dockerfile

Create a Dockerfile in the same directory:

FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

Step 3: Create Kubernetes Deployment File

Create a file named deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
      - name: web-app
        image: web-app:1.0
        ports:
        - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: web-app-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 3000
  selector:
    app: web-app

Step 4: Deploy Your Application

# Build the Docker image
docker build -t web-app:1.0 .

# Load the image into minikube
minikube image load web-app:1.0

# Apply the Kubernetes configuration
kubectl apply -f deployment.yaml

# Expose the service
minikube service web-app-service

Understanding What Just Happened

Let’s break down what we did:

  1. We created a simple web application
  2. We containerized it using Docker
  3. We defined a Kubernetes Deployment that:
    • Creates 3 replicas of our application
    • Labels them for identification
    • Specifies which container image to use
  4. We created a Service to make our application accessible

Common Kubernetes Commands

Here are some essential commands to help you manage your Kubernetes cluster:

# Get all running pods
kubectl get pods

# Get detailed information about a pod
kubectl describe pod [pod-name]

# View logs for a pod
kubectl logs [pod-name]

# Scale your deployment
kubectl scale deployment web-app --replicas=5

# Delete a deployment
kubectl delete deployment web-app

Best Practices for Beginners

  1. Start Small: Begin with simple applications and gradually increase complexity
  2. Use Labels: Properly label your resources for better organization
  3. Monitor Resources: Regularly check the health and status of your deployments
  4. Use Version Control: Keep your Kubernetes configuration files in version control
  5. Practice Locally: Use minikube to practice and experiment safely

Common Pitfalls to Avoid

Next Steps

Now that you have a basic understanding of Kubernetes, here are some suggestions for furthering your knowledge:

  1. Learn about ConfigMaps and Secrets for managing configuration
  2. Explore Kubernetes volumes for persistent storage
  3. Study Kubernetes networking concepts
  4. Practice creating more complex deployments

Conclusion

Kubernetes might seem overwhelming at first, but by starting with the basics and gradually building your knowledge, you can harness its power to deploy and manage applications efficiently. Remember, every expert was once a beginner, so don’t be afraid to experiment and learn from mistakes.

Ready to dive deeper? Check out the official Kubernetes documentation for more detailed information and advanced concepts. Happy containerizing!

~ boolean engineer

© 2024 Jeff Dauda

jeff@boolean.engineer

Instagram 𝕏 GitHub