Master Docker containers and Kubernetes orchestration for scalable, production-ready applications
Containers package your application with all its dependencies into a single, portable unit. They solve the "it works on my machine" problem by ensuring consistency across development, testing, and production.
Docker is the most popular container platform. It uses images (blueprints) to create containers (running instances).
# Dockerfile - defines how to build an image
FROM node:20-alpine
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy application code
COPY . .
# Build application
RUN npm run build
# Expose port
EXPOSE 3000
# Start application
CMD ["npm", "start"]Common Docker Commands:
docker build -t myapp:1.0 . - Build image
docker run -p 3000:3000 myapp:1.0 - Run container
docker ps - List running containers
docker logs container-id - View logs
docker stop container-id - Stop container
Kubernetes (K8s) is a container orchestration platform that automates deployment, scaling, and management of containerized applications across clusters of machines.
API Server: Frontend for K8s control plane, handles all REST requests
etcd: Distributed key-value store for cluster data
Scheduler: Assigns pods to nodes based on resources
Controller Manager: Runs controllers (replication, endpoints, etc.)
Kubelet: Agent that ensures containers are running
Kube-proxy: Network proxy for service communication
Container Runtime: Runs containers (Docker, containerd, CRI-O)
Smallest deployable unit. One or more containers that share network and storage.
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"Manages ReplicaSets and provides declarative updates for Pods. Handles rolling updates and rollbacks.
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: myapp/api:1.0
ports:
- containerPort: 8080
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0Exposes Pods to network traffic. Provides stable IP and DNS name for dynamic Pod IPs.
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
app: api
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer # ClusterIP, NodePort, or LoadBalancerConfigMaps store configuration data. Secrets store sensitive data (base64 encoded).
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DATABASE_HOST: "postgres.default.svc.cluster.local"
LOG_LEVEL: "info"
---
# Secret
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
DATABASE_PASSWORD: cGFzc3dvcmQxMjM= # base64 encoded
---
# Using in Pod
spec:
containers:
- name: app
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: app-secretsHelm is the package manager for Kubernetes. It uses "charts" (packages) to define, install, and upgrade complex Kubernetes applications.
mychart/
βββ Chart.yaml # Chart metadata
βββ values.yaml # Default configuration values
βββ templates/ # Kubernetes manifest templates
β βββ deployment.yaml
β βββ service.yaml
β βββ ingress.yaml
β βββ _helpers.tpl # Template helpers
βββ charts/ # Dependent chartsreplicaCount: 3
image:
repository: myapp/api
tag: "1.0"
pullPolicy: IfNotPresent
service:
type: LoadBalancer
port: 80
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
autoscaling:
enabled: true
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 80helm install myapp ./mychart - Install chart
helm upgrade myapp ./mychart - Upgrade release
helm rollback myapp 1 - Rollback to revision
helm list - List releases
helm uninstall myapp - Uninstall release
You've learned container orchestration with Docker and Kubernetes:
Now that you can orchestrate containers, let's learn Infrastructure as Code to manage your entire infrastructure declaratively.
Continue to Module 4: Infrastructure as Code β