Why Kubernetes?
Docker runs containers on one machine. Real systems run hundreds of containers across many machines and need: automatic restarts, scaling, rolling updates, service discovery, load balancing and secret handling. Kubernetes (K8s) is the open-source system that does this. You describe the desired state (for example 3 copies of my app) and Kubernetes works continuously to make reality match.
Core idea: Kubernetes is a reconciliation loop. You declare what you want in YAML. Controllers compare desired state with actual state and fix the difference, again and again.
Architecture
text
CONTROL PLANE WORKER NODES
+--------------------------------------------+ +------------------------------+
| kube-apiserver <-- everything talks to it | | kubelet (runs Pods) |
| etcd (cluster database) | ---> | kube-proxy (Service routing) |
| kube-scheduler (chooses a node for a Pod) | | container runtime (containerd)|
| controller-manager (keeps desired state) | | Pod Pod Pod |
+--------------------------------------------+ +------------------------------+
| Component | Job |
| kube-apiserver | The front door. Validates and stores objects, serves kubectl and all components |
| etcd | Consistent key-value database holding all cluster state. Back it up |
| kube-scheduler | Places new Pods on nodes based on resources, affinity and taints |
| controller-manager | Runs controllers (Deployment, ReplicaSet, Node, Job...) that fix differences |
| kubelet | Agent on each node. Starts containers and reports status |
| kube-proxy | Implements Service networking rules on each node |
| Container runtime | containerd or CRI-O runs the containers |
Managed services (Amazon EKS, Azure AKS, Google GKE) run the control plane for you. You manage worker nodes and your applications.
Main Objects
| Object | Meaning |
| Pod | Smallest unit: one or more containers sharing network and storage |
| ReplicaSet / Deployment | Keeps N Pods running and does rolling updates |
| Service | Stable name and IP that load balances to Pods |
| Ingress / Gateway | HTTP routing from outside the cluster |
| ConfigMap / Secret | Configuration and sensitive data |
| Volume / PVC | Storage for Pods |
| Namespace | Virtual cluster inside a cluster for isolation |
| StatefulSet, DaemonSet, Job, CronJob | Special workload types |
Create a Local Cluster
Pick one tool. Both run Kubernetes inside Docker.
bash
# Option A: kind (Kubernetes in Docker)
kind create cluster --name lab
# multi-node cluster with a config file:
cat <<'EOF' > kind.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
EOF
kind create cluster --name lab --config kind.yaml
# Option B: minikube
minikube start --driver=docker --nodes 2
bash
# Install kubectl (Linux example)
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install kubectl /usr/local/bin/
kubectl version --client
kubectl cluster-info
kubectl get nodes
Result
kubectl get nodes should list your nodes with STATUS Ready.
kubectl Basics
bash
kubectl get pods # list Pods in the current namespace
kubectl get pods -A # all namespaces
kubectl get pods -o wide # more columns (node, IP)
kubectl get all # common objects
kubectl describe pod mypod # events and details: first stop for troubleshooting
kubectl logs mypod # container logs
kubectl logs mypod -c sidecar -f # a specific container, follow
kubectl exec -it mypod -- sh # shell in a Pod
kubectl apply -f app.yaml # create or update from a file (declarative)
kubectl delete -f app.yaml
kubectl explain pod.spec.containers # built-in documentation of fields
kubectl api-resources # all resource types
Exam and daily speed-ups: alias k=kubectl, enable shell completion (source <(kubectl completion bash)), and use kubectl explain instead of searching the web.
Run Something Now
bash
kubectl run web --image=nginx:1.27 --port=80 # a single Pod
kubectl get pods
kubectl port-forward pod/web 8080:80 # open http://localhost:8080
kubectl delete pod web
# Generate YAML without creating anything
kubectl run web --image=nginx:1.27 --dry-run=client -o yaml > pod.yaml
Anatomy of a Kubernetes YAML
yaml
apiVersion: v1 # API group and version of this kind
kind: Pod # type of object
metadata: # identity
name: web
namespace: default
labels:
app: web
tier: frontend
spec: # desired state
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80
Every object follows the same pattern: apiVersion, kind, metadata, spec (and Kubernetes fills status).
Namespaces
bash
kubectl get namespaces
kubectl create namespace dev
kubectl apply -f app.yaml -n dev
kubectl get pods -n dev
# make dev the default for your current context
kubectl config set-context --current --namespace=dev
Default namespaces: default, kube-system (cluster components), kube-public, kube-node-lease.
Labels and Selectors
Labels are key-value tags on objects. Selectors choose objects by label. Services, Deployments and many other objects find their Pods this way.
bash
kubectl get pods --show-labels
kubectl get pods -l app=web
kubectl get pods -l 'tier in (frontend,backend)'
kubectl label pod web env=dev
kubectl annotate pod web owner=team-a
Contexts and kubeconfig
bash
kubectl config get-contexts # clusters you can talk to
kubectl config use-context kind-lab # switch cluster
kubectl config current-context
# Config lives in ~/.kube/config (keep it private!)
Production safety: Always check your current context before running apply or delete. Many outages start with the right command in the wrong cluster. Tools like kubectx and prompt indicators help.
Practice Tasks
- Create a kind cluster with one control plane and two workers
- Run an nginx Pod, port-forward to it and delete it
- Create namespaces
dev and prod and run a Pod in each
- Use
kubectl describe on a Pod stuck with a wrong image name and read the Events
- Generate a Pod YAML with
--dry-run=client -o yaml and add a label
Conclusion
Kubernetes keeps your declared desired state true using a control plane (API server, etcd, scheduler, controllers) and nodes running kubelet. kubectl, YAML, namespaces and labels are the daily tools.
Quick Summary: desired state loop; apiserver/etcd/scheduler/controllers; kubelet; kind; kubectl get/describe/logs/apply; labels + selectors.