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.
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.
Before diving deeper, let’s understand some fundamental concepts:
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.
Pods: The smallest deployable unit in Kubernetes. A pod can contain one or more containers that share resources and are always scheduled together.
Nodes: The physical or virtual machines that run your applications.
Cluster: A group of nodes managed by Kubernetes.
Kubernetes solves several common problems in application deployment:
Let’s start with setting up a local Kubernetes environment for learning purposes.
First, download and install Docker Desktop from the official website.
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
For Windows (using PowerShell):
winget install minikube
For macOS:
brew install minikube
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
Let’s deploy a basic web application to understand how Kubernetes works in practice.
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}`);
});
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"]
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
# 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
Let’s break down what we did:
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
Now that you have a basic understanding of Kubernetes, here are some suggestions for furthering your knowledge:
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!