
Containers and Orchestration Refersher
/ 18 min read
Table of Contents
What are Containers?
Containers are lightweight, portable, executable packages that include everything needed to run an application: code, runtime, system tools, libraries, and settings. They provide consistent environments across development, testing, and production.
Containers vs Virtual Machines
| Aspect | Containers | Virtual Machines |
|---|---|---|
| OS | Share host OS kernel | Each has full OS |
| Size | Lightweight (MBs) | Heavy (GBs) |
| Startup | Seconds | Minutes |
| Resource Usage | Low overhead | High overhead |
| Isolation | Process-level | Hardware-level |
| Portability | High | Medium |
Key Benefits
- Consistency: “Works on my machine” problem solved
- Portability: Run anywhere containers are supported
- Efficiency: Better resource utilization than VMs
- Scalability: Quick startup and lightweight
- DevOps: Simplified CI/CD pipelines
Docker Fundamentals
Core Components
1. Docker Image
Read-only template used to create containers. Built using layers.
# Example DockerfileFROM node:18-alpineWORKDIR /appCOPY package*.json ./RUN npm installCOPY . .EXPOSE 3000CMD ["npm", "start"]2. Docker Container
Running instance of an image.
# Create and run containerdocker run -d -p 3000:3000 --name myapp my-node-app
# List running containersdocker ps
# Stop containerdocker stop myapp
# Remove containerdocker rm myapp3. Dockerfile
Text file with instructions to build an image.
# Multi-stage build exampleFROM node:18-alpine AS builderWORKDIR /appCOPY package*.json ./RUN npm ci --only=production
FROM node:18-alpine AS runtimeWORKDIR /appCOPY --from=builder /app/node_modules ./node_modulesCOPY . .EXPOSE 3000USER nodeCMD ["npm", "start"]4. Docker Registry
Storage and distribution system for Docker images.
# Pull image from registrydocker pull nginx:latest
# Tag imagedocker tag myapp:latest myregistry.com/myapp:v1.0
# Push image to registrydocker push myregistry.com/myapp:v1.0Docker Architecture
Docker Client → Docker Daemon → Containers ↓ Images Docker Registry Volumes NetworksDocker Commands Reference
Image Management
# Build imagedocker build -t myapp:latest .
# List imagesdocker images
# Remove imagedocker rmi myapp:latest
# Image historydocker history myapp:latest
# Inspect imagedocker inspect myapp:latestContainer Management
# Run containerdocker run -d --name myapp -p 8080:80 nginx
# Execute command in running containerdocker exec -it myapp /bin/bash
# View container logsdocker logs myapp
# Copy files to/from containerdocker cp file.txt myapp:/path/to/destination
# Container statsdocker stats myappVolume Management
# Create volumedocker volume create myvolume
# List volumesdocker volume ls
# Mount volumedocker run -v myvolume:/data myapp
# Remove volumedocker volume rm myvolumeNetwork Management
# Create networkdocker network create mynetwork
# List networksdocker network ls
# Connect container to networkdocker network connect mynetwork myapp
# Inspect networkdocker network inspect mynetworkDocker Compose
Multi-Container Applications
Define and run multi-container applications using YAML.
version: '3.8'
services: web: build: . ports: - "3000:3000" environment: - NODE_ENV=production depends_on: - db - redis volumes: - ./logs:/app/logs networks: - app-network
db: image: postgres:13 environment: - POSTGRES_DB=myapp - POSTGRES_USER=user - POSTGRES_PASSWORD=password volumes: - postgres_data:/var/lib/postgresql/data networks: - app-network
redis: image: redis:alpine ports: - "6379:6379" networks: - app-network
volumes: postgres_data:
networks: app-network: driver: bridgeCompose Commands
# Start servicesdocker-compose up -d
# Stop servicesdocker-compose down
# View logsdocker-compose logs -f
# Scale servicedocker-compose scale web=3
# Build servicesdocker-compose build
# Execute commanddocker-compose exec web bashContainer Best Practices
Dockerfile Optimization
1. Use Multi-Stage Builds
# Build stageFROM node:18 AS builderWORKDIR /appCOPY package*.json ./RUN npm ci --only=production
# Runtime stageFROM node:18-alpineWORKDIR /appCOPY --from=builder /app/node_modules ./node_modulesCOPY . .USER nodeCMD ["npm", "start"]2. Minimize Layers
# Bad - Multiple layersRUN apt-get updateRUN apt-get install -y curlRUN apt-get install -y vim
# Good - Single layerRUN apt-get update && \ apt-get install -y curl vim && \ apt-get clean && \ rm -rf /var/lib/apt/lists/*3. Use .dockerignore
node_modulesnpm-debug.log.git.gitignoreREADME.md.envcoverage4. Run as Non-Root User
FROM node:18-alpineRUN addgroup -g 1001 -S nodejsRUN adduser -S nextjs -u 1001USER nextjsSecurity Best Practices
1. Use Official Base Images
# Prefer official imagesFROM node:18-alpine# Over custom or unknown imagesFROM some-random-user/node2. Keep Images Updated
# Use specific versions, not latestFROM node:18.17.0-alpine3. Scan for Vulnerabilities
# Docker vulnerability scanningdocker scan myapp:latest
# Trivy scanningtrivy image myapp:latest4. Limit Container Capabilities
# Run with limited capabilitiesdocker run --cap-drop=ALL --cap-add=NET_ADMIN myappContainer Orchestration
Why Orchestration?
- Service Discovery: Find and connect services
- Load Balancing: Distribute traffic across instances
- Auto-scaling: Scale based on demand
- Health Checks: Monitor and restart failed containers
- Rolling Updates: Deploy without downtime
- Resource Management: CPU and memory allocation
Kubernetes (K8s)
Core Concepts
Cluster Architecture
Master Node (Control Plane)├─ API Server├─ etcd (Key-Value Store)├─ Scheduler└─ Controller Manager
Worker Nodes├─ kubelet├─ kube-proxy└─ Container Runtime (Docker/containerd)Key Objects
1. Pod
Smallest deployable unit, contains one or more containers.
apiVersion: v1kind: Podmetadata: name: my-podspec: containers: - name: web image: nginx:1.20 ports: - containerPort: 80 - name: sidecar image: busybox command: ['sh', '-c', 'sleep 3600']2. Deployment
Manages ReplicaSets and provides declarative updates.
apiVersion: apps/v1kind: Deploymentmetadata: name: web-deploymentspec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: web image: nginx:1.20 ports: - containerPort: 80 resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"3. Service
Exposes pods to network traffic.
apiVersion: v1kind: Servicemetadata: name: web-servicespec: selector: app: web ports: - protocol: TCP port: 80 targetPort: 80 type: ClusterIP # ClusterIP, NodePort, LoadBalancer4. ConfigMap
Stores configuration data.
apiVersion: v1kind: ConfigMapmetadata: name: app-configdata: database_url: "postgresql://localhost:5432/mydb" debug: "true"5. Secret
Stores sensitive data.
apiVersion: v1kind: Secretmetadata: name: app-secrettype: Opaquedata: username: dXNlcm5hbWU= # base64 encoded password: cGFzc3dvcmQ= # base64 encoded6. Ingress
Manages external access to services.
apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: web-ingressspec: rules: - host: myapp.example.com http: paths: - path: / pathType: Prefix backend: service: name: web-service port: number: 80Kubernetes Commands
Cluster Management
# Cluster infokubectl cluster-info
# Node statuskubectl get nodes
# Cluster eventskubectl get eventsPod Management
# List podskubectl get pods
# Pod detailskubectl describe pod my-pod
# Pod logskubectl logs my-pod
# Execute command in podkubectl exec -it my-pod -- /bin/bash
# Port forwardingkubectl port-forward pod/my-pod 8080:80Deployment Management
# Create deploymentkubectl create deployment web --image=nginx
# Apply YAML filekubectl apply -f deployment.yaml
# Scale deploymentkubectl scale deployment web --replicas=5
# Rolling updatekubectl set image deployment/web web=nginx:1.21
# Rollbackkubectl rollout undo deployment/web
# Deployment statuskubectl rollout status deployment/webService Management
# Expose deploymentkubectl expose deployment web --port=80 --type=LoadBalancer
# List serviceskubectl get services
# Service endpointskubectl get endpointsAdvanced Kubernetes Concepts
1. Namespaces
Logical isolation within cluster.
apiVersion: v1kind: Namespacemetadata: name: production# Create namespacekubectl create namespace production
# List resources in namespacekubectl get pods -n production
# Set default namespacekubectl config set-context --current --namespace=production2. Resource Quotas
Limit resource consumption.
apiVersion: v1kind: ResourceQuotametadata: name: compute-quota namespace: productionspec: hard: requests.cpu: "4" requests.memory: 8Gi limits.cpu: "8" limits.memory: 16Gi pods: "10"3. Horizontal Pod Autoscaler (HPA)
Automatically scale pods based on metrics.
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: name: web-hpaspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: web-deployment minReplicas: 2 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 704. Persistent Volumes
Manage storage.
# Persistent VolumeapiVersion: v1kind: PersistentVolumemetadata: name: my-pvspec: capacity: storage: 10Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: standard hostPath: path: /data
---# Persistent Volume ClaimapiVersion: v1kind: PersistentVolumeClaimmetadata: name: my-pvcspec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi storageClassName: standard5. StatefulSets
For stateful applications.
apiVersion: apps/v1kind: StatefulSetmetadata: name: databasespec: serviceName: "database" replicas: 3 selector: matchLabels: app: database template: metadata: labels: app: database spec: containers: - name: postgres image: postgres:13 ports: - containerPort: 5432 volumeMounts: - name: data mountPath: /var/lib/postgresql/data volumeClaimTemplates: - metadata: name: data spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 10GiContainer Security
1. Image Security
# Scan image for vulnerabilitiesdocker scan nginx:latest
# Use distroless imagesFROM gcr.io/distroless/java:11
# Multi-stage builds to reduce attack surfaceFROM maven:3.8-openjdk-11 AS build# ... build steps ...FROM gcr.io/distroless/java:11COPY --from=build /app/target/app.jar /app.jar2. Runtime Security
# Security ContextapiVersion: v1kind: Podspec: securityContext: runAsNonRoot: true runAsUser: 1000 fsGroup: 2000 containers: - name: app securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: - ALL3. Network Policies
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: deny-allspec: podSelector: {} policyTypes: - Ingress - Egress4. Pod Security Standards
apiVersion: v1kind: Namespacemetadata: name: secure-namespace labels: pod-security.kubernetes.io/enforce: restricted pod-security.kubernetes.io/audit: restricted pod-security.kubernetes.io/warn: restrictedContainer Monitoring & Logging
Monitoring Stack
1. Prometheus + Grafana
# Prometheus ConfigMapapiVersion: v1kind: ConfigMapmetadata: name: prometheus-configdata: prometheus.yml: | global: scrape_interval: 15s scrape_configs: - job_name: 'kubernetes-pods' kubernetes_sd_configs: - role: pod2. Metrics Collection
# Install metrics-serverkubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# View resource usagekubectl top nodeskubectl top podsLogging
1. Centralized Logging (ELK Stack)
# Fluentd DaemonSet for log collectionapiVersion: apps/v1kind: DaemonSetmetadata: name: fluentdspec: selector: matchLabels: name: fluentd template: spec: containers: - name: fluentd image: fluent/fluentd-kubernetes-daemonset:v1-debian-elasticsearch volumeMounts: - name: varlog mountPath: /var/log - name: varlibdockercontainers mountPath: /var/lib/docker/containers readOnly: true2. Application Logging
# Structured logging in containerFROM node:18-alpineCOPY . .# Application logs to stdout/stderrCMD ["node", "app.js"]Alternative Orchestration Platforms
1. Docker Swarm
# Initialize swarmdocker swarm init
# Create servicedocker service create --name web --replicas 3 -p 80:80 nginx
# Scale servicedocker service scale web=5
# Update servicedocker service update --image nginx:1.21 web2. Amazon ECS
{ "family": "web-app", "networkMode": "awsvpc", "cpu": "256", "memory": "512", "containerDefinitions": [ { "name": "web", "image": "nginx:latest", "portMappings": [ { "containerPort": 80, "protocol": "tcp" } ] } ]}3. HashiCorp Nomad
job "web" { datacenters = ["dc1"] type = "service"
group "web" { count = 3
task "nginx" { driver = "docker" config { image = "nginx:latest" ports = ["http"] }
resources { cpu = 500 memory = 256 } } }}CI/CD with Containers
Build Pipeline
# GitHub Actions examplename: Build and Deployon: push: branches: [main]
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2
- name: Build Docker image run: docker build -t myapp:${{ github.sha }} .
- name: Run tests run: docker run --rm myapp:${{ github.sha }} npm test
- name: Push to registry run: | docker tag myapp:${{ github.sha }} myregistry.com/myapp:${{ github.sha }} docker push myregistry.com/myapp:${{ github.sha }}
- name: Deploy to Kubernetes run: | kubectl set image deployment/myapp app=myregistry.com/myapp:${{ github.sha }}GitOps with ArgoCD
apiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: myappspec: source: repoURL: https://github.com/myorg/myapp-config path: kubernetes targetRevision: HEAD destination: server: https://kubernetes.default.svc namespace: production syncPolicy: automated: prune: true selfHeal: trueInterview Questions and Answers
Q1: What is the difference between a container and a virtual machine?
A1: Containers share the host OS kernel and isolate at the process level, making them lightweight (MBs) with second-level startup times. Virtual machines include a full OS, requiring a hypervisor, making them heavy (GBs) with minute-level startup times. Containers provide process-level isolation while VMs provide hardware-level isolation. Containers are more portable and efficient but VMs offer stronger isolation.
Q2: Explain Docker architecture and its main components.
A2: Docker uses a client-server architecture:
- Docker Client: CLI that sends commands to Docker daemon
- Docker Daemon: Background service that manages containers, images, volumes, and networks
- Docker Registry: Storage for Docker images (e.g., Docker Hub)
- Docker Images: Read-only templates with layers
- Docker Containers: Running instances of images
The client communicates with the daemon via REST API, and the daemon pulls images from registries and manages container lifecycle.
Q3: What is a Dockerfile and how does layering work?
A3: A Dockerfile is a text file containing instructions to build a Docker image. Each instruction (FROM, RUN, COPY, etc.) creates a new layer. Layers are cached and reused, making builds faster. For example:
FROM node:18 # Layer 1WORKDIR /app # Layer 2COPY package.json # Layer 3RUN npm install # Layer 4COPY . . # Layer 5Only changed layers and subsequent layers are rebuilt. This makes Docker builds efficient.
Q4: What is the difference between CMD and ENTRYPOINT in Dockerfile?
A4:
- CMD: Provides default arguments that can be overridden at runtime. Used for default commands.
- ENTRYPOINT: Defines the executable that always runs. Arguments are appended to it.
Example:
ENTRYPOINT ["python"]CMD ["app.py"]Running docker run myapp test.py executes python test.py (CMD overridden), but the ENTRYPOINT remains. Best practice: use ENTRYPOINT for the executable and CMD for default arguments.
Q5: Explain multi-stage builds and their benefits.
A5: Multi-stage builds use multiple FROM statements in a Dockerfile, allowing you to separate build and runtime environments. Benefits:
- Smaller final images: Build tools aren’t included in final image
- Better security: Fewer attack vectors
- Cleaner separation: Build stage separate from runtime
Example: Use a full Node.js image to build, then copy only the build artifacts to a smaller Alpine image.
Q6: What are Docker volumes and why are they important?
A6: Docker volumes are persistent storage mechanisms that exist outside the container filesystem. They’re important because:
- Data persistence: Data survives container deletion
- Sharing data: Multiple containers can share volumes
- Performance: Better I/O performance than bind mounts
- Backup/restore: Easier to backup and migrate
Types: Named volumes (managed by Docker), bind mounts (host filesystem path), and tmpfs (memory-only).
Q7: What is Docker Compose and when would you use it?
A7: Docker Compose is a tool for defining and running multi-container applications using YAML files. Use it when:
- Running multiple related containers (web app + database + cache)
- Need to define service dependencies
- Want reproducible development environments
- Need to manage container configuration as code
It simplifies starting/stopping entire application stacks with single commands (docker-compose up/down).
Q8: Explain the concept of a Kubernetes Pod.
A8: A Pod is the smallest deployable unit in Kubernetes, containing one or more tightly coupled containers that share:
- Network namespace: Same IP address and port space
- Storage volumes: Shared volumes
- Lifecycle: Started/stopped together
Pods are ephemeral and designed to run a single instance of an application. Multiple containers in a pod typically include a main container and helper “sidecar” containers for logging, monitoring, or proxying.
Q9: What is a Kubernetes Deployment and what advantages does it provide?
A9: A Deployment is a Kubernetes object that manages ReplicaSets and provides declarative updates for Pods. Advantages:
- Desired state management: Maintains specified number of replicas
- Rolling updates: Zero-downtime deployments
- Rollback capability: Revert to previous versions
- Scaling: Easy horizontal scaling
- Self-healing: Automatically replaces failed pods
Deployments are ideal for stateless applications.
Q10: What is the difference between a Deployment and a StatefulSet?
A10: Deployment:
- For stateless applications
- Pods are interchangeable
- Random pod names (web-7d4f8-xyz)
- No guaranteed ordering
- Shared storage
StatefulSet:
- For stateful applications (databases)
- Pods have unique identities
- Predictable names (db-0, db-1, db-2)
- Ordered creation/deletion
- Dedicated storage per pod
Use StatefulSets when pods need stable network identities or persistent storage.
Q11: Explain Kubernetes Services and their types.
A11: A Service exposes Pods to network traffic and provides stable endpoints. Types:
- ClusterIP (default): Internal-only access within cluster
- NodePort: Exposes service on each node’s IP at a static port
- LoadBalancer: Creates external load balancer (cloud providers)
- ExternalName: Maps service to external DNS name
Services use label selectors to route traffic to matching Pods, providing load balancing and service discovery.
Q12: What are ConfigMaps and Secrets? When would you use each?
A12: ConfigMap: Stores non-sensitive configuration data (database URLs, feature flags, config files) Secret: Stores sensitive data (passwords, API keys, certificates) - base64 encoded
Use ConfigMap for environment-specific settings. Use Secrets for credentials and sensitive data. Both can be injected as environment variables or mounted as volumes. Secrets offer additional protection through encryption at rest (when configured).
Q13: What is a Kubernetes Namespace and why use it?
A13: Namespaces provide logical isolation within a cluster, creating virtual clusters. Benefits:
- Resource isolation: Separate dev, staging, production
- Access control: Apply RBAC policies per namespace
- Resource quotas: Limit CPU/memory per namespace
- Organization: Group related resources
Default namespaces: default, kube-system, kube-public, kube-node-lease. Create custom namespaces for multi-tenancy and environment separation.
Q14: Explain Kubernetes Ingress and how it differs from a Service.
A14: Service: Layer 4 (TCP/UDP) load balancing, internal routing Ingress: Layer 7 (HTTP/HTTPS) routing, external access management
Ingress provides:
- Path-based routing:
/api→ service-a,/web→ service-b - Host-based routing:
api.example.com→ service-a - TLS termination: SSL/TLS certificate management
- Single entry point: One load balancer for multiple services
Requires an Ingress Controller (nginx, traefik, etc.) to function.
Q15: What is a DaemonSet and when would you use it?
A15: A DaemonSet ensures a copy of a Pod runs on all (or selected) nodes. Use cases:
- Log collection: Fluentd/Filebeat on every node
- Monitoring agents: Prometheus node exporters
- Network plugins: CNI agents
- Storage daemons: Ceph, GlusterFS agents
When a node joins the cluster, the DaemonSet automatically schedules a Pod on it. Ideal for node-level services.
Q16: Explain Kubernetes resource requests and limits.
A16: Requests: Minimum guaranteed resources (used for scheduling) Limits: Maximum resources a container can use (enforced)
resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"- Container uses requests for scheduling decisions
- Exceeding memory limit → pod killed (OOMKilled)
- Exceeding CPU limit → throttled, not killed
- Set requests based on typical usage, limits for safety
Q17: What is a Horizontal Pod Autoscaler (HPA)?
A17: HPA automatically scales the number of Pods based on observed metrics (CPU, memory, or custom metrics). It:
- Monitors metrics via Metrics Server
- Compares current vs target utilization
- Adjusts replica count within min/max bounds
- Rechecks every 15 seconds (default)
Example: Scale from 2-10 replicas when CPU exceeds 70%. HPA prevents manual scaling and handles traffic spikes automatically. Requires resource requests to be defined.
Q18: What are liveness and readiness probes in Kubernetes?
A18: Liveness Probe: Checks if container is alive. If fails, kubelet kills and restarts container. Readiness Probe: Checks if container is ready to serve traffic. If fails, removes pod from service endpoints.
Types: HTTP GET, TCP Socket, Exec command
Example use: Liveness ensures hung app restarts; Readiness ensures traffic only goes to fully initialized pods. Don’t confuse them - liveness restarts, readiness gates traffic.
Q19: What is the difference between a .dockerignore and .gitignore?
A19: .dockerignore: Excludes files from Docker build context (sent to daemon) .gitignore: Excludes files from Git repository
.dockerignore improves build performance by reducing context size. Common entries:
node_modules.git*.md.envcoverage/Smaller build context = faster uploads to daemon, faster builds, smaller images (if files would be COPYied).
Q20: Explain Docker networking modes.
A20: Docker networking modes:
- Bridge (default): Private network, containers communicate via internal IPs
- Host: Container shares host network namespace, no isolation
- None: No networking, isolated container
- Overlay: Multi-host networking for Swarm/Kubernetes
- Macvlan: Assigns MAC address, container appears as physical device
Use bridge for most cases, host for performance (bypasses NAT), overlay for multi-host orchestration.
Q21: What is a Kubernetes PersistentVolume (PV) and PersistentVolumeClaim (PVC)?
A21: PersistentVolume (PV): Cluster resource representing storage (admin creates) PersistentVolumeClaim (PVC): Request for storage by user (pod uses)
Workflow:
- Admin creates PV with capacity/access modes
- User creates PVC requesting storage
- Kubernetes binds PVC to matching PV
- Pod references PVC in volume mount
Decouples storage provisioning from consumption. Supports dynamic provisioning via StorageClasses.
Q22: What are Init Containers in Kubernetes?
A22: Init Containers run before app containers in a Pod, completing before app containers start. Use cases:
- Wait for dependencies: Check if database is ready
- Setup tasks: Clone git repo, download config
- Security: Fetch secrets, set permissions
Multiple init containers run sequentially. If any fails, kubelet restarts the Pod. Main containers only start after all init containers succeed.
Q23: Explain Kubernetes RBAC (Role-Based Access Control).
A23: RBAC controls who can access which resources. Components:
- Role/ClusterRole: Defines permissions (verbs: get, list, create, delete)
- RoleBinding/ClusterRoleBinding: Grants role to users/groups/service accounts
- ServiceAccount: Identity for pods
Role is namespace-scoped; ClusterRole is cluster-wide. Example: Give developer read-only access to pods in dev namespace. RBAC follows principle of least privilege.
Q24: What is a sidecar container pattern?
A24: A sidecar container runs alongside the main container in the same Pod, sharing resources. Common patterns:
- Logging: Sidecar collects and forwards logs
- Monitoring: Exports metrics from main container
- Proxy: Envoy sidecar for service mesh
- Data synchronization: Syncs files between containers
Example: Web server (main) + log shipper (sidecar). Sidecars extend functionality without modifying main application.
Q25: What is container orchestration and why is it needed?
A25: Container orchestration automates deployment, scaling, networking, and management of containerized applications. Needed for:
- High availability: Automatic restarts and failover
- Scaling: Handle increased load automatically
- Load balancing: Distribute traffic across instances
- Service discovery: Containers find each other
- Rolling updates: Zero-downtime deployments
- Resource management: Optimal container placement
Without orchestration, managing hundreds of containers manually is impractical. Kubernetes, Docker Swarm, and ECS are popular orchestrators.
Q26: What are Kubernetes labels and selectors?
A26: Labels: Key-value pairs attached to objects for organization Selectors: Query labels to identify resources
labels: app: web environment: production version: v1.2Selectors filter objects: app=web,environment=production
Services use selectors to route traffic to pods. Deployments use them to manage pods. Labels enable loose coupling - change labels to redirect traffic without modifying pods.
Q27: Explain the concept of immutable infrastructure with containers.
A27: Immutable infrastructure means containers are never modified after deployment - replaced entirely with new versions. Benefits:
- Consistency: Eliminates configuration drift
- Reliability: Same image across environments
- Easy rollback: Redeploy previous image
- Security: No runtime patches, rebuild instead
Instead of SSH into container and patching, build new image with fix and deploy. Containers are cattle, not pets.
Q28: What is a Kubernetes Job and CronJob?
A28: Job: Runs pods to completion, ensures specified number of successful completions CronJob: Runs Jobs on a schedule (cron syntax)
Use cases:
- Job: Data migration, batch processing, one-time tasks
- CronJob: Scheduled backups, report generation, cleanup tasks
schedule: "0 2 * * *" # Daily at 2 AMJobs handle retries and parallelism. CronJobs create Jobs at scheduled times.
Q29: What are Kubernetes Taints and Tolerations?
A29: Mechanism to control pod scheduling on nodes:
Taint: Applied to nodes, repels pods without matching toleration Toleration: Applied to pods, allows (but doesn’t require) scheduling on tainted nodes
Use cases:
- Dedicated nodes for specific workloads (GPU nodes)
- Isolate production from dev
- Prevent scheduling on nodes with issues
Effects: NoSchedule, PreferNoSchedule, NoExecute
Example: Taint GPU nodes, only pods with GPU toleration can schedule there.
Q30: What are the benefits of using multi-stage builds in Docker?
A30: Multi-stage builds create smaller, more secure images by separating build and runtime:
Benefits:
- Smaller images: 500MB build image → 50MB final image (10x reduction)
- Improved security: No build tools in production image
- Single Dockerfile: No need for separate build/runtime Dockerfiles
- Faster deployments: Smaller images transfer faster
- Better layer caching: Build dependencies cached separately
Example: Compile Go app in builder stage (has compiler), copy binary to scratch image (no OS). Final image is ~10MB vs ~800MB with build tools.