7. Advanced Concepts with Deployments, ReplicaSets, DaemonSets, Jobs, and CronJobs
Kubernetes isn’t just about deploying applications—it’s about efficiently managing them across dynamic environments. In this blog, we’ll explore advanced Kubernetes concepts like rolling updates, ReplicaSets, DaemonSets, and Jobs, along with hands-on manifest examples to help you deploy confidently.
🔄 Rolling Update in Deployments
A rolling update ensures zero downtime by incrementally updating pods in a deployment to match a new version. This approach allows users to keep accessing the application while the update is in progress.
Key Features of Rolling Updates:
Updates pods one at a time (or based on a defined strategy).
Automatically rolls back if the new version fails.
Ensures high availability during updates.
Example: Applying a Rolling Update
Update the deployment image using the kubectl set
command:
kubectl set image deployment/nginx-deployment nginx-container=nginx:1.19 -n nginx
Monitor the rollout:
kubectl rollout status deployment/nginx-deployment -n nginx
Rollback in case of failure:
kubectl rollout undo deployment/nginx-deployment -n nginx
📋 ReplicaSets
A ReplicaSet ensures a specified number of pod replicas are running at all times. While it works similarly to Deployments, it lacks the ability to perform rolling updates or rollbacks.
Example: Nginx ReplicaSet Manifest
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
namespace: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginx-container
image: nginx:latest
Deploy the ReplicaSet:
kubectl apply -f nginx-replicaset.yml -n nginx
verify the ReplicaSet:
kubectl get rs -n nginx
🛠️ DaemonSets
What is a DaemonSet?
A DaemonSet ensures that a specific pod runs on all (or some) nodes in the cluster. It is ideal for workloads that need to run on every node, such as logging agents, monitoring tools, or network proxies.
Key Features:
Automatically creates a pod on every node.
Monitors for new or deleted nodes and adjusts pods accordingly.
No manual replica management required.
Use Cases for DaemonSets:
Log collection (e.g., FluentD or Filebeat).
Node monitoring (e.g., Prometheus Node Exporter).
Networking (e.g., Weave-net, Kube-proxy).
Example: Nginx DaemonSet Manifest
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-daemonset
labels:
tier: frontend
spec:
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Deploy the DaemonSet:
kubectl apply -f daemonset-deploy.yml -n nginx
Verify the DaemonSet:
kubectl get daemonsets -n nginx
🎯 Jobs
Jobs are used to run one-off tasks or batch processes in Kubernetes. Unlike Deployments or DaemonSets, Jobs run to completion.
Example Use Cases:
Data migration.
Batch processing tasks.
Database backups.
Example: Simple Job Manifest
apiVersion: batch/v1
kind: Job
metadata:
name: sample-job
spec:
template:
spec:
containers:
- name: sample-container
image: busybox
command: ["echo", "Hello, Kubernetes!"]
restartPolicy: Never
Deploy the Job:
kubectl apply -f job.yml -n nginx
View the Job status:
kubectl get jobs -n nginx
⏰ CronJobs
CronJobs schedule jobs to run periodically at specified times, much like Linux cron jobs.
Example Use Cases:
Running database cleanup tasks daily.
Sending email reports at regular intervals.
Example: CronJob Manifest
apiVersion: batch/v1
kind: CronJob
metadata:
name: sample-cronjob
spec:
schedule: "* * * * *" # Runs every minute
jobTemplate:
spec:
template:
spec:
containers:
- name: sample-container
image: busybox
command: ["echo", "Scheduled task executed!"]
restartPolicy: OnFailure
Deploy the CronJob:
kubectl apply -f cronjob.yml -n nginx
List active CronJobs:
kubectl get cronjobs -n nginx
📊 Conclusion
Kubernetes’ advanced features like rolling updates, ReplicaSets, DaemonSets, Jobs, and CronJobs provide unparalleled flexibility in managing workloads. By mastering these, you can build resilient, scalable, and automated systems.
Do you have additional insights or use cases? Let’s discuss in the comments below! 😊