Kubernetes Fundamentals
What is Kubernetes?
Kubernetes is an open-source container orchestration platform that automates deployment, scaling, and management of containerized applications across clusters of machines. It abstracts the complexity of managing containers in production environments, providing a declarative way to define and manage applications.
Why Kubernetes matters: In production, manually managing containers across multiple machines becomes complex and error-prone. Kubernetes handles scheduling containers on appropriate nodes, networking, storage, restarts on failures, and scaling—allowing developers to focus on application logic rather than infrastructure management.
Core Concepts
graph TD
A["Kubernetes Cluster"] -->B["Master Node
Control Plane"]
A -->C["Worker Nodes"]
B -->B1["API Server
Scheduler
Controller Manager"]
C -->C1["Kubelet
Container Runtime"]
C -->C2["Pods
Services
Storage"]
style B fill:#e1f5ff
style C fill:#fff3e0
style C2 fill:#e8f5e9
Cluster
A Kubernetes cluster consists of a control plane (master node) and worker nodes. The control plane manages cluster state and decisions. Worker nodes run containerized applications.
Pod
The smallest deployable unit in Kubernetes. A pod wraps one or more containers (usually one), sharing network namespace and storage. Containers in a pod communicate via localhost.
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.24
ports:
- containerPort: 80
Why pods exist: Pods enable tight coupling of containers that must work together, sharing network and storage while maintaining isolation from other pods.
Node
A physical or virtual machine in the cluster running the container runtime (Docker, containerd). Kubelet (Kubernetes agent) on each node manages pods.
Namespace
A logical cluster subdivision. Namespaces provide isolation and resource quotas, allowing multiple teams or projects to share a cluster.
# Create namespace
kubectl create namespace production
# or shorter: kubectl create ns production
# Deploy to specific namespace
kubectl apply -f app.yaml -n production
Key Objects
Deployment
Declares desired application state (replicas, container image, etc.). Kubernetes reconciles actual state to match desired state automatically.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: myapp:v1.0
ports:
- containerPort: 8080
resources:
requests:
memory: "256Mi"
cpu: "250m"
Why Deployments: They provide rolling updates, automatic rollbacks, and self-healing. If a pod crashes, Deployment recreates it automatically.
Service
Exposes pods to network traffic. Services provide stable DNS names and load balancing across pod replicas.
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080
type: ClusterIP
ConfigMap
Stores non-sensitive configuration data. Decouples configuration from container images.
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
database_url: "postgres://db:5432/myapp"
log_level: "INFO"
Secret
Stores sensitive data (passwords, API keys). Kubernetes encrypts secrets at rest.
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
data:
password: c2VjcmV0MTIz # base64 encoded
Common kubectl Commands
| Command | Purpose |
|---|---|
kubectl cluster-info |
Display cluster information |
kubectl get pods |
List all pods |
kubectl describe pod POD_NAME |
Show pod details |
kubectl logs POD_NAME |
View pod logs |
kubectl exec -it POD_NAME -- bash |
Execute command in pod |
kubectl apply -f file.yaml |
Create/update resources |
kubectl delete pod POD_NAME |
Delete pod |
kubectl scale deployment APP --replicas=5 |
Scale deployment |
Declarative vs Imperative
Imperative (not recommended for production):
kubectl run nginx --image=nginx
kubectl scale deployment nginx --replicas=3
# or shorter: kubectl scale deploy nginx --replicas=3
Declarative (recommended):
# 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.24
Apply declaratively:
kubectl apply -f deployment.yaml
Why declarative: Version control, repeatability, and easy rollbacks. YAML files serve as infrastructure-as-code.
Self-Healing and Automatic Recovery
Kubernetes continuously monitors pod health and automatically takes corrective action.
graph LR
A["Pod Crashes"] -->B["Kubelet Detects
Failure"]
B -->C["Deployment Controller
Notices Missing Pod"]
C -->D["New Pod Scheduled
& Created"]
D -->E["Pod Running
Service Updated"]
style A fill:#FFB6C6
style E fill:#90EE90
Real-World Example: Complete Application
# Complete application manifest
apiVersion: v1
kind: Namespace
metadata:
name: myapp
---
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: myapp
data:
database_url: "postgres://postgres:5432/myapp"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: myapp
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: myregistry.azurecr.io/api:v1.0
ports:
- containerPort: 5000
envFrom:
- configMapRef:
name: app-config
livenessProbe:
httpGet:
path: /health
port: 5000
initialDelaySeconds: 10
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: api-service
namespace: myapp
spec:
selector:
app: api
ports:
- port: 80
targetPort: 5000
type: LoadBalancer
Deploy:
kubectl apply -f application.yaml
Common Pitfalls
- No resource limits - Containers consume unlimited resources, crashing others
- No liveness probes - Dead containers keep running because Kubernetes doesn't detect failure
- Hardcoded configuration - Use ConfigMaps and Secrets instead
- Using latest image tags - Unpredictable versions; use specific version tags
- No namespace isolation - Everything in default namespace; use namespaces for security and organization
Key Takeaways
- Kubernetes automates container deployment, scaling, and management across clusters
- Pods are the smallest deployable unit; Deployments manage pod replicas with self-healing
- Services provide stable networking and load balancing across pods
- ConfigMaps and Secrets manage configuration and sensitive data separately from containers
- Declarative YAML files enable version control and reproducible deployments
- Kubernetes automatically restarts failed pods and maintains desired state
- Namespaces provide logical cluster isolation for multi-team environments
Next Steps: Install kubectl and Minikube locally, deploy a simple application, scale pods, and observe automatic recovery from pod failures.