Deploying and Updating DaemonSets
Deploying a DaemonSet in a Kubernetes cluster is similar to deploying other workloads, such as Deployments or ReplicaSets. You can create a DaemonSet by defining a YAML manifest and applying it to your cluster using the kubectl apply
command.
Here's an example of a DaemonSet manifest that deploys a simple "hello world" application:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: hello-world-daemonset
spec:
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: ubuntu:22.04
command: ["/bin/bash", "-c", "while true; do echo 'Hello, World!'; sleep 10; done"]
To deploy this DaemonSet, you can run the following command:
kubectl apply -f hello-world-daemonset.yaml
This will create the DaemonSet and schedule the "hello world" Pods on all the nodes in your Kubernetes cluster.
Updating a DaemonSet is similar to updating other workloads. You can modify the container image, resource requirements, or other configurations in the DaemonSet manifest and apply the changes using kubectl apply
. Kubernetes will then perform a rolling update, ensuring that new Pods are deployed and old Pods are terminated in a controlled manner.
Here's an example of how you can update the container image in the DaemonSet:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: hello-world-daemonset
spec:
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: ubuntu:20.04 ## Update the container image
command: ["/bin/bash", "-c", "while true; do echo 'Hello, World!'; sleep 10; done"]
After applying this updated manifest, Kubernetes will gradually roll out the new Pods with the updated container image, ensuring that the cluster maintains availability throughout the update process.