Introduction to Kubernetes
As organizations move towards microservices architectures, managing thousands of containers across dozens of servers becomes impossible to do manually. Kubernetes (often abbreviated as K8s) is an open-source container orchestration system designed to automate the deployment, scaling, and management of containerized applications.
Why do we need Kubernetes?
While Docker helps you package and run an application in a container, Kubernetes solves the problem of running those containers in production. If a container crashes, Kubernetes restarts it. If traffic spikes, Kubernetes scales the application across multiple servers. It abstracts away the underlying hardware and allows you to treat a cluster of machines as a single computing resource.
Core Kubernetes Components
1. Pods
A Pod is the smallest deployable computing unit in Kubernetes. Unlike Docker, where you deploy containers directly, in Kubernetes you deploy Pods. A Pod usually encapsulates one container, but it can contain multiple tightly-coupled containers that share storage and a local network.
2. Deployments
While you can deploy a Pod manually, it's rarely done in practice because Pods are mortal—they don't self-heal. Instead, you use a Deployment. A Deployment instructs Kubernetes on how many replicas of a Pod should be running. If a node fails and takes down three of your Pods, the Deployment controller automatically spins up three new Pods on healthy nodes.
Here is an example of a simple Nginx Deployment in YAML:
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:1.14.2
ports:
- containerPort: 80
3. Services
Since Pods are constantly being created and destroyed, their IP addresses change constantly. A Service provides a stable IP address and DNS name that routes traffic to a specific set of Pods (usually selected via labels). This allows the frontend of your application to always know how to talk to the backend, regardless of which specific Pods are currently alive.
Basic kubectl Commands
kubectl is the command-line tool used to interact with a Kubernetes cluster.
kubectl apply -f deployment.yaml: Creates or updates resources defined in a YAML file.kubectl get pods: Lists all running Pods in the current namespace.kubectl describe pod [pod-name]: Provides detailed information and event logs for a specific Pod, useful for debugging CrashLoopBackOff errors.kubectl logs [pod-name]: Fetches the standard output logs of the container inside the Pod.
Kubernetes has a steep learning curve, but mastering its declarative configuration model is essential for modern DevOps and site reliability engineering.
Written by A.M. Rinas