[1/24] K is for Kubernetes: Understanding the Basics and Architecture
π This is Post #1 in the Kubernetes A-to-Z Series
Reading Order: Start here β Next: Docker Fundamentals β
Series Progress: 1/24 complete | Difficulty: Beginner | Time: 15 min | Part 1/6: Foundation the first post in our comprehensive Kubernetes A-to-Z Series! In this post, weβll explore the fundamentals of Kubernetes, understand its architecture, and learn why it has become the de facto standard for container orchestration.
What is Kubernetes?
Kubernetes (often abbreviated as K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Originally developed by Google based on their internal Borg system, Kubernetes is now maintained by the Cloud Native Computing Foundation (CNCF).
Why Kubernetes Matters
In the world of microservices and cloud-native applications, Kubernetes solves several critical problems:
Traditional Deployment β Virtualized Deployment β Container Deployment β Kubernetes Orchestration
Single Server β Multiple VMs β Multiple Containers β Managed Container Clusters
Key Benefits:
- Automatic scaling based on demand
- Self-healing capabilities with automatic restarts
- Service discovery and load balancing
- Rolling updates with zero downtime
- Resource optimization across clusters
Kubernetes Architecture Overview
Kubernetes follows a master-worker architecture with a clear separation of concerns:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Control Plane (Master) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β API Server β Scheduler β Controller β etcd β
β β β Manager β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββ΄ββββββββββ
βΌ βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Worker Nodes β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β kubelet β kube-proxy β Container Runtime β
β β β (Docker/containerd/CRI-O) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Control Plane Components
The Control Plane (also called the Master Node) is the brain of your Kubernetes cluster:
1. API Server (kube-apiserver)
The API Server is the front door to your cluster. All communication between components goes through the API server.
# Check API server status
kubectl get componentstatuses
# View API server logs (if you have access)
kubectl logs -n kube-system kube-apiserver-<master-node-name>
Key Functions:
- Validates and processes REST requests
- Updates corresponding objects in etcd
- Serves as the clusterβs central communication hub
2. Scheduler (kube-scheduler)
The Scheduler decides where to run your pods based on resource requirements and constraints.
# View scheduler logs
kubectl logs -n kube-system kube-scheduler-<master-node-name>
# See scheduling events
kubectl get events --sort-by='.lastTimestamp' | grep Scheduled
Scheduling Process:
- Filters nodes that donβt meet pod requirements
- Ranks remaining nodes based on resource availability
- Assigns pod to the best-suited node
3. Controller Manager (kube-controller-manager)
Controllers are control loops that watch the cluster state and make changes to move the current state toward the desired state.
# List all controllers
kubectl get deployment -n kube-system
# View controller manager logs
kubectl logs -n kube-system kube-controller-manager-<master-node-name>
Major Controllers:
- Node Controller: Manages node lifecycle
- Replication Controller: Maintains correct pod count
- Endpoints Controller: Manages service endpoints
- Service Account Controller: Manages service accounts
4. etcd
etcd is a consistent and highly-available key-value store that holds all cluster data.
# Check etcd health (if external etcd)
etcdctl endpoint health
# Backup etcd data
etcdctl snapshot save /tmp/etcd-backup.db
etcd Characteristics:
- Distributed key-value store
- Strong consistency (CP in CAP theorem)
- Stores all cluster state and configuration
Worker Node Components
Worker Nodes are where your applications actually run:
1. kubelet
The kubelet is the primary node agent that ensures containers are running in pods.
# Check kubelet status on a node
systemctl status kubelet
# View kubelet logs
journalctl -u kubelet -f
# Get node information
kubectl get nodes -o wide
kubelet Responsibilities:
- Pod lifecycle management
- Container health checking
- Node status reporting
- Resource usage monitoring
2. kube-proxy
kube-proxy maintains network rules and enables network communication to pods.
# View kube-proxy logs
kubectl logs -n kube-system kube-proxy-<node-name>
# Check iptables rules (Linux)
iptables -t nat -L KUBE-SERVICES
Network Functions:
- Service load balancing
- Network rule management
- Pod-to-pod communication
- Service discovery
3. Container Runtime
The Container Runtime is responsible for running containers. Popular options include:
# Check container runtime
kubectl get nodes -o wide
# View running containers
crictl ps # For containerd
docker ps # For Docker
Supported Runtimes:
- containerd (recommended)
- CRI-O (Red Hat focused)
- Docker (deprecated in newer versions)
Kubernetes Objects and Abstractions
Kubernetes uses various objects to represent the state of your cluster:
Basic Objects
# Pod - Smallest deployable unit
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
# Service - Stable network endpoint
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
Controller Objects
# Deployment - Manages replica sets
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:latest
Setting Up Your First Kubernetes Cluster
Option 1: Minikube (Local Development)
# Install minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Start minikube
minikube start --driver=docker
# Check status
minikube status
# Get cluster info
kubectl cluster-info
Option 2: Kind (Kubernetes in Docker)
# Install kind
go install sigs.k8s.io/[email protected]
# Create cluster
kind create cluster --name my-cluster
# Get clusters
kind get clusters
# Delete cluster
kind delete cluster --name my-cluster
Option 3: Cloud Managed Services
# Google Kubernetes Engine (GKE)
gcloud container clusters create my-cluster --zone us-central1-a
# Amazon EKS
eksctl create cluster --name my-cluster --region us-west-2
# Azure AKS
az aks create --resource-group myResourceGroup --name my-cluster
Basic Kubernetes Commands
Cluster Information
# Check cluster version
kubectl version --short
# Get cluster info
kubectl cluster-info
# View all nodes
kubectl get nodes
# Describe a node
kubectl describe node <node-name>
Namespace Operations
# List namespaces
kubectl get namespaces
# Create namespace
kubectl create namespace my-namespace
# Set default namespace
kubectl config set-context --current --namespace=my-namespace
# Delete namespace
kubectl delete namespace my-namespace
Pod Operations
# List pods
kubectl get pods
kubectl get pods --all-namespaces
# Create pod
kubectl run nginx --image=nginx:latest
# Get pod details
kubectl describe pod nginx
# View logs
kubectl logs nginx
kubectl logs -f nginx # Follow logs
# Execute commands in pod
kubectl exec -it nginx -- /bin/bash
Kubernetes Design Principles
Understanding these principles will help you work effectively with Kubernetes:
1. Declarative Configuration
Define what you want, not how to achieve it:
# Declarative: What I want
spec:
replicas: 3
selector:
matchLabels:
app: nginx
2. Desired State Management
Kubernetes continuously works to maintain the desired state you specify.
3. Self-Healing Systems
Kubernetes automatically recovers from failures:
- Restart failed containers
- Reschedule pods on healthy nodes
- Replace failed nodes
4. Horizontal Scaling
Scale applications by adding more instances rather than making instances larger.
Common Kubernetes Patterns
1. Sidecar Pattern
A helper container that extends the main containerβs functionality:
apiVersion: v1
kind: Pod
metadata:
name: webapp
spec:
containers:
- name: app
image: myapp:latest
- name: logging-agent
image: fluentd:latest
2. Ambassador Pattern
A proxy container that handles external communication:
apiVersion: v1
kind: Pod
metadata:
name: database-client
spec:
containers:
- name: app
image: myapp:latest
- name: database-proxy
image: cloudsql-proxy:latest
3. Adapter Pattern
A container that transforms the main containerβs interface:
apiVersion: v1
kind: Pod
metadata:
name: metrics-adapter
spec:
containers:
- name: app
image: myapp:latest
- name: metrics-exporter
image: prometheus-exporter:latest
Best Practices for Beginners
1. Start Small
# Begin with simple pods
kubectl run hello --image=busybox -- echo "Hello World"
# Progress to deployments
kubectl create deployment nginx --image=nginx:latest
2. Use Namespaces
# Create development namespace
kubectl create namespace development
# Work in specific namespaces
kubectl config set-context --current --namespace=development
3. Label Everything
metadata:
labels:
app: myapp
environment: production
team: backend
4. Resource Limits
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Troubleshooting Basics
Check Pod Status
# Get pod status
kubectl get pods
# Describe pod for events
kubectl describe pod <pod-name>
# View logs
kubectl logs <pod-name>
Debug Common Issues
# Pod stuck in Pending
kubectl describe pod <pod-name> # Check events
# Pod crashing
kubectl logs <pod-name> --previous # Check previous container logs
# Service not working
kubectl get endpoints <service-name> # Check endpoints
Next Steps
Now that you understand Kubernetes basics and architecture, youβre ready to dive deeper into specific components. In the next post, weβll explore Containers and Docker fundamentals before moving on to Pods, the basic building blocks of Kubernetes.
Key Takeaways
- Kubernetes is a container orchestration platform that automates deployment, scaling, and management
- Control Plane components (API Server, Scheduler, Controller Manager, etcd) manage the cluster
- Worker Nodes run your applications using kubelet, kube-proxy, and container runtime
- Declarative configuration allows you to define desired state rather than imperative commands
- Self-healing capabilities automatically recover from failures
- Start with Minikube or Kind for local development
Resources for Further Learning
- Official Kubernetes Documentation
- Kubernetes Interactive Tutorial
- Kubernetes by Example
- CKAD Certification Guide
Command Reference Cheatsheet
# Cluster Management
kubectl version --short # Check version
kubectl cluster-info # Cluster information
kubectl get nodes # List nodes
kubectl describe node <name> # Node details
# Namespaces
kubectl get namespaces # List namespaces
kubectl create namespace <name> # Create namespace
kubectl config set-context --current --namespace=<name>
# Basic Operations
kubectl get pods # List pods
kubectl describe pod <name> # Pod details
kubectl logs <pod-name> # View logs
kubectl exec -it <pod-name> -- /bin/bash # Execute commands
kubectl create deployment <name> --image=<image> # Create deployment
kubectl scale deployment <name> --replicas=<n> # Scale deployment
Next in Series: C is for Containers: Docker Fundamentals Before Kubernetes - Understanding container basics before diving deeper into Kubernetes.
Series Navigation: