Kubernetes Fundamentals
⚠️ Recommended Prerequisites: You should only take this lesson if you find yourself in the following situations:
- A single Docker instance can no longer handle the traffic
- Requires automatic scaling, self-healing, and multi-node high availability
- The team has decided to migrate to K8s
If Docker + Compose is sufficient for your project, skip this lesson and go straight to Lesson 24.
1. What You'll Learn
- K8s Core Architecture and Concepts
- Pod / Deployment / Service Three Core Objects
- Common kubectl Commands
- A Approach to Migrating from Compose to K8s
- Setting Up a Local Minikube Cluster
2. The Story of a Cluster Expansion
(1) Pain Point: Swarm is no longer sufficient
The Swarm cluster Alice had deployed was no longer sufficient—five replicas couldn’t handle the traffic, rolling updates weren’t flexible enough, there was no automatic scaling, and configuration management was inadequate. The team decided to migrate to Kubernetes, but Alice was completely lost when it came to K8s Pods, Deployments, and Services.
(2) Solution for Learning minikube Locally
Bob set up a K8s development environment using minikube on Alice's laptop.
BASH
# Start local K8s cluster
minikube start --driver=docker
# Deploy an application
kubectl create deployment nginx --image=nginx:1.25-alpine --replicas=3
kubectl expose deployment nginx --port=80 --type=NodePort
(3) Benefits: Understand the core concepts of K8s in 5 minutes
Alice discovered that K8s Pods and Docker containers have many similarities: a Deployment corresponds to Compose’s replicas, and a Service corresponds to Docker’s network and port mapping.
3. K8s Core Architecture
(1) Architecture Overview
graph TB
K["kubectl CLI"] --> API["API Server<br/>REST API Entrance"]
API --> SCHED["Scheduler<br/>Pod Scheduling"]
API --> ETCD["etcd<br/>Cluster State Store"]
API --> CTRL["Controller Manager<br/>Deployment/ReplicaSet"]
API --> KUBELET["Kubelet<br/>Node Proxy"]
KUBELET --> CRI["Container Runtime<br/>containerd/Docker"]
CRI --> POD["Pod<br/>Container Group"]
(2) K8s Core Components
| Component | Function | Analogy with Docker |
|---|---|---|
| API Server | Cluster API Entry Point | Docker Daemon API |
| etcd | State Store | Docker Internal State |
| Scheduler | Pod Scheduling | Swarm Scheduler |
| Kubelet | Node Agent | Docker Daemon (Single Node) |
| Kube-proxy | Network Proxy | Docker Routing Mesh |
4. Core Concepts of K8s
(1) Pod / Deployment / Service
| K8s Concept | Purpose | Docker Analogy |
|---|---|---|
| Pod | Smallest scheduling unit (1+ container) | Container (but may contain multiple) |
| Deployment | Declarative Replica Mgmt | Compose replicas + restart |
| Service | Stable Network Identifiers + Load Balancing | Docker DNS + Routing Mesh |
| Ingress | HTTP Routing Entry | Nginx reverse proxy |
| ConfigMap | Configuration Management | Environment Variables / env_file |
| Secret | Sensitive Information | Docker Secret |
| PV/PVC | Persistent Storage | Named Volume |
(2) Docker Compose → K8s Concept Map
| Docker Compose | Kubernetes | Description |
|---|---|---|
service |
Deployment + Service |
Service Definition |
replicas |
Deployment.spec.replicas |
Number of copies |
ports |
Service.spec.ports |
Port mapping |
| Persistent storage | ||
environment ConfigMap + Secret Configuration Management |
||
depends_on |
initContainers / Sequential Startup |
Dependency Control |
healthcheck |
livenessProbe + readinessProbe |
Health Checkup |
5. Common kubectl Commands
▶ Example: Starting a cluster with minikube (Difficulty: ⭐⭐)
BASH
# Start a local K8s cluster
minikube start --driver=docker --kubernetes-version=v1.28.0
# Check cluster status
kubectl cluster-info
kubectl get nodes
💻 Output:
TEXT
Kubernetes control plane is running at https://127.0.0.1:32768
CoreDNS is running at https://127.0.0.1:32768/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane 60s v1.28.0
▶ Example:kubectl create deployment(Difficulty⭐⭐)
BASH
# Create a deployment with 3 replicas
kubectl create deployment nginx --image=nginx:1.25-alpine --replicas=3
# Check deployment status
kubectl get deployments
kubectl get pods
💻 Output:
TEXT
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 30s
NAME READY STATUS RESTARTS AGE
nginx-6c8b5f4d8f-abc12 1/1 Running 0 30s
nginx-6c8b5f4d8f-def34 1/1 Running 0 30s
nginx-6c8b5f4d8f-ghi56 1/1 Running 0 30s
▶ Example:kubectl expose service(Difficulty⭐⭐)
BASH
# Expose deployment as a service
kubectl expose deployment nginx --port=80 --type=NodePort
# Get the service URL
minikube service nginx --url
# Or use port-forward for local access
kubectl port-forward svc/nginx 8080:80
▶ Example: Scaling with kubectl scale (Difficulty: ⭐⭐)
BASH
# Scale to 5 replicas
kubectl scale deployment nginx --replicas=5
# Verify
kubectl get pods
▶ Example: kubectl rollout (Difficulty: ⭐⭐⭐)
BASH
# Update the image
kubectl set image deployment/nginx nginx=nginx:1.26-alpine
# Check rollout status
kubectl rollout status deployment/nginx
# Rollback if something goes wrong
kubectl rollout undo deployment/nginx
# View rollout history
kubectl rollout history deployment/nginx
6. YAML Declarative Configuration
(1) Deployment + Service YAML
YAML
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25-alpine
ports:
- containerPort: 80
resources:
limits:
cpu: "0.5"
memory: "128Mi"
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: NodePort
BASH
# Apply the YAML
kubectl apply -f nginx-deployment.yaml
# Verify
kubectl get all
7. Single-Node vs. Multi-Node K8s
| Dimension | minikube | kind | Production Cluster |
|---|---|---|---|
| Number of nodes | 1 | 1+ | 3+ |
| Purpose | Learning/Development | CI Testing | Production |
| Installation | minikube start | kind create cluster | kubeadm / Cloud Service |
| High Availability | ❌ | ❌ | ✅ |
| Automatic Scaling | ❌ | ❌ | ✅ (HPA) |
8. Complete Example: Deploying an Application with minikube
BASH
# ============================================
# Complete walkthrough: K8s on minikube
# Covers: cluster, deploy, service, scale, update
# ============================================
# 1. Start minikube cluster
minikube start --driver=docker
# 2. Create deployment
kubectl create deployment web --image=nginx:1.25-alpine --replicas=2
# 3. Expose as service
kubectl expose deployment web --port=80 --type=NodePort
# 4. Access the application
minikube service web --url
# Or: kubectl port-forward svc/web 8080:80
# 5. Scale to 5 replicas
kubectl scale deployment web --replicas=5
kubectl get pods -w
# 6. Rolling update
kubectl set image deployment/web nginx=nginx:1.26-alpine
kubectl rollout status deployment/web
# 7. Rollback
kubectl rollout undo deployment/web
# 8. Clean up
kubectl delete deployment web
kubectl delete service web
minikube stop
❓ FAQ
Q Which should I choose, K8s or Docker Swarm?
A Swarm is simple and easy to learn, making it suitable for small teams (<20 people) and straightforward use cases. K8s is powerful but complex, making it suitable for large teams, scenarios requiring automatic scaling, and a robust ecosystem. Rule of thumb: Use Swarm for 3–5 nodes on a single machine; use K8s for 5 or more nodes or when advanced features are needed.
Q What is the difference between a Pod and a Container?
A A Pod is the smallest scheduling unit in K8s and can contain one or more containers. Containers within a Pod share networking and storage. Most Pods contain only one container—in this case, a Pod is roughly equivalent to a Container. Multi-container Pods are used in tightly coupled scenarios (such as an application and a sidecar proxy).
Q How many servers does K8s require?
A For learning: 1 server (Minikube). For development: 1–3 servers. For production: 3+ Master nodes + N Worker nodes. With managed K8s services from cloud providers (EKS/GKE/AKS), you don’t need to manage the Master nodes; a minimum of 1 Worker node is sufficient.
Q Should I learn K8s directly from Compose?
A I recommend mastering Docker + Compose first (Lessons 1–22 of this course) before moving on to K8s. Compose covers 80% of use cases; K8s is only needed when you require a cluster. If you jump straight into K8s, you’re likely to get overwhelmed by the concepts—first understand the basics of containers and orchestration, then learn the abstractions of K8s.
Q Which is better for learning—minikube or kind?
A minikube offers more features (Dashboard, add-ons, multiple drivers) and is better suited for beginners. kind (Kubernetes in Docker) is lighter and starts up faster, making it ideal for CI and multi-cluster testing. We recommend minikube for learning.
📖 Summary
- K8s Core Architecture: API Server + etcd + Scheduler + Kubelet + Kube-proxy
- Pod (smallest scheduling unit) ≈ Container, Deployment (replica management) ≈ Compose replicas, Service (networking) ≈ Docker DNS
- kubectl is the K8s CLI, equivalent to the docker command
- minikube: Start a local K8s cluster with a single command—perfect for learning
- K8s' declarative YAML configuration corresponds to Compose's
docker-compose.yml - from Docker Migrate to K8s:Compose service → Deployment + Service,Volume → PV/PVC,env → ConfigMap + Secret
📝 Exercises
- Basic Exercise (Difficulty: ⭐): Use minikube to start a local K8s cluster, deploy an Nginx instance, and access it via port forwarding.
- Advanced Exercise (Difficulty ⭐⭐): Create a Deployment and a Service using a YAML file,
kubectl apply -fdeploy them, and expose the ports. - Challenge (Difficulty: ⭐⭐⭐): Perform a rolling update (nginx: 1.25 → 1.26), observe
kubectl rollout status, and then roll back usingkubectl rollout undo.



