Advanced Workloads & Scheduling
Control exactly where and how your workloads run using DaemonSets, Jobs, and Affinities.
Theory: Beyond Standard Deployments
While Deployments are great for standard web servers, Kubernetes provides controllers for specialized workloads.
Workload Types
- DaemonSet: Ensures a copy of a Pod runs on *every* node (e.g., logging agents).
- Job: Runs a task to completion (e.g., a DB migration).
- CronJob: Runs a Job on a specific schedule.
Advanced Scheduling
Normally, the scheduler places pods wherever there is room. You can constrain this using:
- Node Affinity: "Prefer to run on nodes with SSDs."
- Taints & Tolerations: Nodes push pods away (Taint) unless the pod explicitly has the antidote (Toleration).
Deep Dive: Autoscaling Flows
The Horizontal Pod Autoscaler (HPA) scales the number of Pod replicas based on observed CPU utilization or custom metrics. The Vertical Pod Autoscaler (VPA) increases/decreases the CPU and Memory resource requests/limits for existing Pods.
3. InitContainers and Troubleshooting
An InitContainer runs to completion before the main app containers start (e.g.,
waiting for a DB to be ready, or running database migrations). If a main container crashes
immediately on start, it enters a CrashLoopBackOff state, often requiring
kubectl logs --previous to debug.
Hands-on Labs
Interview Prep: Advanced Workloads
Node Affinity is a property of Pods that attracts them to a set of nodes (e.g., "schedule this pod only on nodes with GPU=true"). Taints are a property of Nodes that repel Pods unless the Pod explicitly has a matching Toleration. They are often used together to create dedicated nodes.
It means a pod repeatedly fails to start or crashes immediately after starting. Common causes: missing dependencies, bad configuration/secrets leading to an application panic, or OOM (Out of Memory) kills. You debug it by running `kubectl logs my-pod --previous` to see the logs from the crashed container before it was restarted.
InitContainers securely separate setup logic (like fetching secrets from Vault or running DB migrations) from the main app. They can contain tools that you don't want in the app image for security reasons. Also, if an InitContainer fails, K8s will repeatedly restart the Pod until it succeeds, blocking the app container from starting prematurely.