Kubernetes Container Orchestration Mastery
Scale Applications with Professional Container Management
Kubernetes has revolutionized how we deploy, scale, and manage containerized applications. This comprehensive guide will take you from Kubernetes basics to advanced orchestration techniques used in production environments worldwide.
🎯 Understanding Kubernetes Architecture
Kubernetes follows a master-worker architecture designed for scalability and reliability.
Control Plane Components
- API Server: Central management hub for all operations
- etcd: Distributed key-value store for cluster data
- Scheduler: Assigns pods to appropriate nodes
- Controller Manager: Runs controller processes
Worker Node Components
- kubelet: Primary node agent
- kube-proxy: Network proxy and load balancer
- Container Runtime: Docker, containerd, or CRI-O
🏗️ Core Kubernetes Objects
Pods - The Smallest Deployable Unit
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Deployments - Managing Pod Replicas
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:1.21
ports:
- containerPort: 80
Services - Network Abstraction
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
🚀 Essential kubectl Commands
Cluster Management
# Get cluster information
kubectl cluster-info
# View cluster nodes
kubectl get nodes
# Check cluster health
kubectl get componentstatuses
# View cluster events
kubectl get events --sort-by=.metadata.creationTimestamp
Resource Management
# Apply configuration from file
kubectl apply -f deployment.yaml
# Get all resources in current namespace
kubectl get all
# Describe detailed information
kubectl describe pod nginx-pod
# Get logs from a pod
kubectl logs nginx-pod -f
# Execute commands in a pod
kubectl exec -it nginx-pod -- /bin/bash
Scaling and Updates
# Scale deployment
kubectl scale deployment nginx-deployment --replicas=5
# Rolling update
kubectl set image deployment/nginx-deployment nginx=nginx:1.22
# Check rollout status
kubectl rollout status deployment/nginx-deployment
# Rollback to previous version
kubectl rollout undo deployment/nginx-deployment
🔧 Advanced Kubernetes Concepts
ConfigMaps and Secrets
# Create ConfigMap from file
kubectl create configmap app-config --from-file=config.properties
# Create Secret
kubectl create secret generic app-secret \
--from-literal=username=admin \
--from-literal=password=secret123
# ConfigMap YAML
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
database_url: "postgresql://localhost:5432/mydb"
log_level: "info"
Persistent Volumes
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-storage
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: fast-ssd
hostPath:
path: /data/storage
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-storage
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: fast-ssd
🌐 Networking and Ingress
Ingress Controller Setup
# Install NGINX Ingress Controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml
# Ingress Resource
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
Network Policies
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
🔐 Security Best Practices
RBAC (Role-Based Access Control)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
Pod Security Standards
apiVersion: v1
kind: Pod
metadata:
name: secure-pod
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000
containers:
- name: secure-container
image: nginx:1.21
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
📊 Monitoring and Observability
Health Checks
apiVersion: v1
kind: Pod
metadata:
name: healthy-pod
spec:
containers:
- name: app
image: nginx:1.21
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
Resource Monitoring
# Install metrics server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# View resource usage
kubectl top nodes
kubectl top pods
# Get resource quotas
kubectl get resourcequota
kubectl describe resourcequota
🔄 CI/CD Integration
GitOps with ArgoCD
# Install ArgoCD
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# Application definition
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/myorg/myapp
targetRevision: HEAD
path: k8s
destination:
server: https://kubernetes.default.svc
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: true
🛠️ Troubleshooting Guide
Common Issues and Solutions
Pod Stuck in Pending State
# Check events
kubectl describe pod <pod-name>
# Check node resources
kubectl describe nodes
# Check resource quotas
kubectl get resourcequota
Service Not Accessible
# Check service endpoints
kubectl get endpoints <service-name>
# Test service connectivity
kubectl run test-pod --image=busybox --rm -it -- wget -qO- <service-name>:<port>
# Check network policies
kubectl get networkpolicies
🎯 Conclusion
Kubernetes provides a powerful platform for container orchestration, but mastering it requires understanding its core concepts, networking model, and security practices. Start with basic deployments and gradually incorporate advanced features like RBAC, network policies, and monitoring.
Remember: Kubernetes is complex, but its power lies in its declarative nature and self-healing capabilities. Always use version control for your manifests and implement proper CI/CD pipelines.