Advanced Workload Controllers
Beyond Deployments
While Deployments are perfect for stateless applications, Kubernetes provides specialized controllers for different use cases:
Workload Controller Types
- Deployment: Stateless applications, replicated Pods
- DaemonSet: One Pod per node (system services)
- StatefulSet: Stateful applications with stable identity
- Job: One-off tasks
- CronJob: Scheduled tasks
When to Use Each Controller
| Controller | Use Case | Examples |
|---|---|---|
| Deployment | Stateless, replicated apps | Web servers, APIs, microservices |
| DaemonSet | One Pod per node | Monitoring agents, log collectors, network plugins |
| StatefulSet | Stateful apps with identity | Databases, message queues, distributed systems |
| Job | Run-to-completion tasks | Batch processing, data migration |
| CronJob | Scheduled tasks | Backups, reports, cleanup jobs |
This Course Focus
This lesson covers two critical controllers for production Kubernetes:
DaemonSets
Solve the per-node problem:
- Automatically launch one Pod on every node
- Centrally managed and configured
- Perfect for node-level system services
- Examples: monitoring agents, log collectors, network overlay components
StatefulSets
Manage stateful applications:
- Stable network identity: Predictable hostnames
- Ordered deployment: Sequential Pod creation/deletion
- Persistent storage: Stable volume attachments
- Examples: databases (MongoDB, PostgreSQL), message queues (Kafka), distributed systems
Course Outline
- DaemonSets: One Pod per node for system services
- StatefulSets: Managing databases and stateful applications
- Kubernetes Federation: Multi-cluster management (bonus topic)
Prerequisites
Before starting this course, you should understand:
- Pods and containers
- Deployments and ReplicaSets
- Services and networking
- Volumes and storage
DaemonSets: One Pod Per Node
The Per-Node Problem
Many system-level tasks require that a single instance of a Pod runs on every node in the cluster.
Common Scenarios
- Monitoring agents: Collect metrics from each node and send to central location
- Log collectors: Gather logs from all containers on a node
- Network overlay: CNI plugins that need to run on every node
- Storage daemons: Distributed storage agents
- Security agents: Intrusion detection, vulnerability scanning
Requirements
The solution must ensure the agent:
- Launches automatically on every node
- Centrally managed and configured from one point
- Scales with cluster: New nodes automatically get the Pod
- Updates easily: Change image/config in one place
Why Alternative Solutions Fail
1. Static Pods (Not Suitable)
Static Pods Limitations
Static Pods are launched by kubelet based on manifest files in /etc/kubernetes/manifests/
- ✓ Run automatically on each node
- ✗ Cannot be centrally managed through Kubernetes API
- ✗ Must manually place manifests on each node
- ✗ No central configuration updates
- ✗ Typically used for Control Plane components only
2. Pod Anti-Affinity (Not Ideal)
Pod Anti-Affinity Approach
Pod anti-affinity can prevent two Pods from running on the same node, but:
- ✗ Complex to configure for every node
- ✗ Doesn't guarantee one Pod per node
- ✗ Requires manual scaling to match node count
- ✗ Not automatic when nodes are added/removed
DaemonSet: The Dedicated Solution
DaemonSet Guarantees
A DaemonSet ensures that exactly one copy of a Pod runs on every node (or selected nodes):
- ✓ Automatically launches on all nodes
- ✓ Centrally managed via Kubernetes API
- ✓ Automatically deploys to new nodes
- ✓ Removes Pods when nodes are deleted
- ✓ Single point of configuration
- ✓ Easy to update across all nodes
DaemonSet Manifest
How DaemonSets Work
kubectl apply -f daemonset.yaml
Controller identifies all nodes in cluster
One Pod scheduled to each node automatically
DaemonSet controller detects new node
Pod automatically created on new node
Pod automatically deleted when node leaves
Visual Example
DaemonSet Deployment Across Cluster
Every node has exactly one monitoring-agent Pod
DaemonSet Operations
Create DaemonSet
View DaemonSet Pods
Update DaemonSet
Node Selectors and Tolerations
Run on Specific Nodes Only
Run on Master Nodes
Common DaemonSet Examples
1. Log Collector (Fluentd)
2. Network Plugin (Calico)
3. Storage Plugin (Ceph)
DaemonSet Best Practices
- Use for node-level system services only
- Keep DaemonSet Pods lightweight (low resource usage)
- Configure proper resource limits
- Use readiness probes to ensure Pod is ready
- Set appropriate tolerations for master nodes
- Use node selectors to target specific node types
- Monitor DaemonSet Pods across all nodes
StatefulSets: Managing Stateful Applications
The Stateful Application Challenge
Some applications require more than what Deployments provide:
Deployment Limitations for Stateful Apps
Deployments treat all Pods as identical and interchangeable:
- ✗ Pods get random names (webapp-abc123)
- ✗ No stable network identity
- ✗ No guaranteed order of creation/deletion
- ✗ No persistent storage association
- ✗ Pods can be replaced on any node
This doesn't work for databases, message queues, or distributed systems!
What StatefulSets Provide
StatefulSet Guarantees
StatefulSets provide three critical features for stateful applications:
- Stable Network Identity: Predictable, persistent hostnames
- Ordered Deployment & Scaling: Sequential Pod creation/deletion
- Persistent Storage: Stable volume attachments that persist across rescheduling
1. Stable Network Identity
Each Pod in a StatefulSet gets a stable, unique network identity that persists across rescheduling.
Naming Pattern
Stable Hostnames via Headless Service
Why Stable Identity Matters
For databases and distributed systems:
- Nodes need to discover each other by name
- Configuration files reference specific members
- Replication requires consistent identities
- Example: MongoDB replica set members are identified by hostname
2. Ordered Deployment and Scaling
StatefulSets maintain a strict, ordinal index and deploy/scale/update Pods in a predictable, ordered manner.
Deployment Order
kubectl apply -f statefulset.yaml
Wait until Running and Ready
Only after mongodb-0 is Ready
Wait until Running and Ready
Only after mongodb-1 is Ready
All Pods now running
Termination Order
Pods deleted in REVERSE order
Wait until fully terminated
Only after mongodb-2 is terminated
Wait until fully terminated
Only after mongodb-1 is terminated
Why Order Matters
For databases and distributed systems:
- Initialization: Primary node must start before replicas
- Configuration: Earlier nodes configure later nodes
- Graceful shutdown: Proper cleanup order prevents data loss
- Example: MongoDB primary (mongodb-0) must be ready before secondaries join
3. Persistent Storage
StatefulSets manage PersistentVolumeClaims (PVCs) for each ordinal, ensuring stable storage.
Volume Claim Templates
How Storage Works
Storage Persistence Example
Complete StatefulSet Example
StatefulSet Operations
Create StatefulSet
View StatefulSet Status
Scale StatefulSet
Update StatefulSet
StatefulSet vs Deployment
| Feature | Deployment | StatefulSet |
|---|---|---|
| Pod Names | Random (webapp-abc123) | Ordered (mongodb-0, mongodb-1) |
| Network Identity | None (pods interchangeable) | Stable DNS names |
| Creation Order | All at once (parallel) | Sequential (one by one) |
| Deletion Order | Random | Reverse sequential |
| Storage | Shared or ephemeral | Per-Pod persistent volumes |
| Use Case | Stateless applications | Stateful applications |
Common StatefulSet Use Cases
1. Databases
- MongoDB replica sets
- PostgreSQL with replication
- MySQL/MariaDB clusters
- Cassandra
- Redis with persistence
2. Message Queues
- Kafka (requires stable broker IDs)
- RabbitMQ clusters
- Pulsar
3. Distributed Systems
- Elasticsearch clusters
- ZooKeeper ensembles
- etcd clusters
- Consul
StatefulSet Best Practices
- Always create Headless Service first
- Use PodDisruptionBudgets to prevent disruptions
- Configure appropriate resource requests/limits
- Implement readiness probes (critical for ordered startup)
- Use init containers for initialization tasks
- Consider using Operators for complex stateful apps
- Test disaster recovery procedures
- Back up persistent volumes regularly
StatefulSet Considerations
- PVCs are NOT deleted with StatefulSet (manual cleanup needed)
- Slower deployments due to sequential ordering
- Requires more careful management than Deployments
- Storage class must support dynamic provisioning
- Deleting a Pod doesn't delete its PVC (prevents data loss)
Kubernetes Federation (Bonus Topic)
Multi-Cluster Management
As organizations scale, they often need to run applications across multiple Kubernetes clusters in different data centers or regions.
Multi-Cluster Scenarios
- Geographic distribution: Clusters in different regions for low latency
- High availability: Survive datacenter failures
- Compliance: Data residency requirements
- Environment separation: Dev, staging, prod in separate clusters
- Cloud diversity: Avoid vendor lock-in
What is Kubernetes Federation?
Federation Purpose
Kubernetes Federation is designed to unite multiple, separate Kubernetes clusters into a single, cohesive entity.
It provides a single point of control for managing resources across all member clusters.
Federation Capabilities
1. Unified Resource Management
2. Cross-Cluster View
View combined status of Pods running across all member clusters:
3. Intelligent Scheduling
Federation can distribute workloads based on:
- Cluster capacity and resources
- Geographic preferences
- Cluster policies
- Cost optimization
Federation Architecture
Federation Setup
Manages federated resources across all clusters
Full Kubernetes cluster
Full Kubernetes cluster
Full Kubernetes cluster
Federation Use Cases
1. Multi-Datacenter Deployment
2. Disaster Recovery
If one cluster fails, traffic automatically routes to healthy clusters:
- Applications continue running in other DCs
- Automatic failover
- Minimal downtime
3. Compliance and Data Residency
Federation Project Status
Federation v1: Deprecated
The first version of Kubernetes Federation was ultimately unsuccessful due to design issues:
- Complex to set up and maintain
- Limited adoption
- Architectural limitations
- Officially deprecated and removed
Federation v2 (KubeFed)
A second version is under development with improved design:
- Project name: KubeFed (Kubernetes Cluster Federation)
- Status: Beta/experimental
- Improved architecture: More flexible and extensible
- Production readiness: Still uncertain for critical workloads
- Active development: Ongoing improvements
Alternatives to Federation
While Federation v2 matures, consider these alternatives for multi-cluster management:
1. Multi-Cluster Service Mesh
- Istio multi-cluster: Connect multiple clusters at the service mesh level
- Linkerd: Multi-cluster service discovery
- Provides cross-cluster service communication
2. GitOps Approaches
- Flux/ArgoCD: Deploy same manifests to multiple clusters
- Infrastructure as code for all clusters
- Centralized configuration management
3. Third-Party Solutions
- Rancher: Multi-cluster Kubernetes management platform
- Google Anthos: Unified management across clouds
- Red Hat Advanced Cluster Management: Enterprise multi-cluster
When to Consider Federation
Good Use Cases
- Large organizations with multiple datacenters
- Global applications requiring regional deployment
- High availability across geographic regions
- Compliance with data residency requirements
- Disaster recovery strategies
Consider Carefully If:
- Single datacenter deployment
- Small team with limited expertise
- Simple application architecture
- Just starting with Kubernetes
- Need production-stable solution today
Summary: Advanced Workloads
Course Summary
- One Pod per node automatically
- Perfect for system services (monitoring, logging)
- Centrally managed and configured
- Stable network identity with predictable names
- Ordered deployment and scaling
- Persistent storage per Pod
- Essential for databases and stateful apps
- Multi-cluster management
- Single control point for multiple clusters
- v1 deprecated, v2 under development
- Consider alternatives for production