Kubernetes has revolutionized container orchestration, enabling organizations to manage scalable, resilient applications in dynamic environments. This guide provides a structured path from fundamental concepts to production-grade deployments, leveraging Kubernetes' full potential while avoiding common pitfalls.
While Docker Compose simplifies local container management, Kubernetes introduces enterprise-grade orchestration capabilities. Unlike Compose's static single-node approach, Kubernetes:
# Docker Compose vs Kubernetes equivalent
# docker-compose.yml
services:
web:
image: nginx:alpine
ports:
- '80:80'
# Kubernetes Deployment + Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancerThis declarative approach enables zero-downtime updates and cross-cloud portability.
Kubernetes Architecture Kubernetes master-worker architecture (Source: Kubernetes.io)
The control plane comprises:
Worker nodes execute workloads using:
# Install prerequisites
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Start cluster with 4 CPUs and 8GB RAM
minikube start --cpus=4 --memory=8192 --driver=docker
# Verify cluster status
kubectl cluster-info
kubectl get nodesMinikube creates a single-node cluster ideal for development.
# Initialize control plane
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
# Configure kubectl
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Install network plugin (Calico)
kubectl create -f https://docs.projectcalico.org/manifests/calico.yaml
# Join worker nodes
kubeadm token create --print-join-commandkubeadm automates TLS certificate management and control plane setup.
# mysql-deployment.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: 'mysql'
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secrets
key: root_password
ports:
- containerPort: 3306
---
# wordpress-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
spec:
replicas: 3
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress:php8.0-apache
env:
- name: WORDPRESS_DB_HOST
value: mysql
- name: WORDPRESS_DB_USER
value: root
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secrets
key: root_password
ports:
- containerPort: 80
---
# wordpress-service.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
type: LoadBalancer
selector:
app: wordpress
ports:
- protocol: TCP
port: 80
targetPort: 80Apply with kubectl apply -f mysql-deployment.yaml -f wordpress-deployment.yaml -f wordpress-service.yaml.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app
annotations:
nginx.ingress.kubernetes.io/canary: 'true'
nginx.ingress.kubernetes.io/canary-weight: '10'
spec:
rules:
- host: app.example.com
http:
paths:
- backend:
service:
name: my-app-v2
port:
number: 80
path: /
pathType: PrefixGradually shift traffic between versions using service mesh or ingress controllers.
flux bootstrap github \
--owner=my-org \
--repository=my-repo \
--branch=main \
--path=./clusters/productionFluxCD synchronizes cluster state with Git repositories, enabling auditable infrastructure changes.
Kubernetes implements a flat network model where:
Traffic flow through Kubernetes network components
Implement network policies for microsegmentation:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: frontend-policy
spec:
podSelector:
matchLabels:
role: frontend
ingress:
- from:
- podSelector:
matchLabels:
role: backend
ports:
- protocol: TCP
port: 80This restricts frontend pods to only receive traffic from backend pods on port 80.
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install kube-prometheus prometheus-community/kube-prometheus-stackMonitor key metrics:
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: my-app-vpa
spec:
targetRef:
apiVersion: 'apps/v1'
kind: Deployment
name: my-app
updatePolicy:
updateMode: 'Auto'VPA automatically adjusts CPU/memory requests based on usage patterns.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: ['']
resources: ['pods']
verbs: ['get', 'watch', 'list']
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioPrinciple of Least Privilege (PoLP) implementation.
Enforce security contexts:
securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
seccompProfile:
type: RuntimeDefaultAdopt PSA (Pod Security Admission) to restrict privileged pods.
Kubernetes has evolved beyond container orchestration into a platform for:
As you scale, consider:
The Kubernetes ecosystem continues to grow, with 154 Certified Service Providers and 100+ SIGs (Special Interest Groups) driving innovation[^16]. By mastering its core concepts and embracing its extensibility, teams can build future-proof infrastructure that adapts to evolving business needs.
# Get involved in Kubernetes development
git clone https://github.com/kubernetes/kubernetes
cd kubernetes
./hack/install-etcd.sh
make