TechLead
Lesson 11 of 18
5 min read
Docker & DevOps

Container Orchestration & Kubernetes Basics

Introduction to container orchestration with Docker Swarm and Kubernetes for scaling production applications

Why Container Orchestration?

When you run dozens or hundreds of containers in production, you need a system to manage deployment, scaling, networking, and health monitoring. This is what container orchestrators do.

What Orchestrators Handle

  • Scheduling: Decide which server runs each container
  • Scaling: Add or remove container replicas based on demand
  • Load Balancing: Distribute traffic across containers
  • Self-Healing: Restart failed containers automatically
  • Rolling Updates: Deploy new versions with zero downtime
  • Service Discovery: Containers find each other by name

Kubernetes (K8s) Overview

Kubernetes is the industry standard for container orchestration. Originally built by Google and now maintained by the CNCF, it provides a powerful platform for deploying and managing containerized applications at scale.

Core Kubernetes Concepts

  • Pod: The smallest deployable unit — one or more containers that share networking and storage
  • Deployment: Manages a set of identical pods, handles scaling and updates
  • Service: A stable network endpoint that routes traffic to pods
  • Namespace: Logical isolation for resources within a cluster
  • ConfigMap/Secret: Manage configuration and sensitive data separately from code

Kubernetes Deployment Example

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-api
  labels:
    app: my-api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-api
  template:
    metadata:
      labels:
        app: my-api
    spec:
      containers:
        - name: api
          image: myapp:v1.0.0
          ports:
            - containerPort: 3000
          env:
            - name: NODE_ENV
              value: "production"
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secrets
                  key: password
          resources:
            limits:
              memory: "512Mi"
              cpu: "500m"
            requests:
              memory: "256Mi"
              cpu: "250m"
          livenessProbe:
            httpGet:
              path: /health
              port: 3000
            initialDelaySeconds: 15
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /ready
              port: 3000
            initialDelaySeconds: 5
            periodSeconds: 5

Kubernetes Service

# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-api-service
spec:
  selector:
    app: my-api
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer

Essential kubectl Commands

# Apply configuration
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

# Get resources
kubectl get pods
kubectl get deployments
kubectl get services
kubectl get all

# Describe a resource (detailed info)
kubectl describe pod my-api-xyz

# View logs
kubectl logs my-api-xyz
kubectl logs -f my-api-xyz  # Follow

# Scale a deployment
kubectl scale deployment my-api --replicas=5

# Rolling update
kubectl set image deployment/my-api api=myapp:v2.0.0

# Rollback
kubectl rollout undo deployment/my-api

# Execute command in a pod
kubectl exec -it my-api-xyz -- sh

# Port forward for local testing
kubectl port-forward service/my-api-service 8080:80

Local Kubernetes Development

# Docker Desktop includes Kubernetes
# Enable it in Docker Desktop Settings > Kubernetes

# Or use Minikube
brew install minikube
minikube start

# Or use Kind (Kubernetes in Docker)
brew install kind
kind create cluster

Docker Compose vs Kubernetes

  • 🐳 Docker Compose: Simple multi-container setups, development, small deployments
  • ☸️ Kubernetes: Production-grade orchestration, auto-scaling, large distributed systems
  • 💡 Start with Compose for development, move to K8s when you need production orchestration

Continue Learning