Kubernetes has a lot of features and deployment options for running containers. One of these is the DaemonSet. In this blog post, we’ll discuss what DaemonSets are, what they can be used for, and how to create and update them.
In this blog:
A Kubernetes DaemonSet is a container tool that ensures that all nodes (or a specific subset of them, but we’ll get to that later) are running exactly one copy of a pod. DaemonSets will even create the pod on new nodes that are added to your cluster!
When using Kubernetes, most of the time you don’t care where your pods are running, but sometimes you want to run a single pod on all your nodes. For example, you might want to run fluentd on all your nodes to collect logs. In this case, using a DaemonSet tells Kubernetes to make sure there is one instance of the pod on nodes in your cluster.
Like everything else in Kubernetes, DaemonSets can be configured using a YAML file:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: my-daemonset
namespace: my-namespace
Labels:
key: value
spec:
template:
metadata:
labels:
name: my-daemonset-container
...
selector:
matchLabels:
name: my-daemonset-container
The YAML file consists of the following parts:
Once you’ve got the configuration complete, create the DaemonSet in your cluster by doing the following:
kubectl apply -f daemonset.yaml
You should see the specified pod running on each node!
Sometimes you’ll need to send data from your other pods to the DaemonSet pod running on the node. For example, you may want to send metrics data to a monitoring pod. In these cases, you have two options:
There are a couple of ways that you can update your DaemonSet. The first is to edit the DaemonSet directly with the following command:
kubectl edit ds/NAME
This command will allow you to edit the DaemonSet’s configuration in the command line, and will apply the changes when you are done. However, we don’t recommend this method, as it doesn’t lend itself well to version control.
Instead, we recommend that you just update the original configuration file and then use the same command you used for creation. This allows you to keep track of changes in version control:
kubectl apply -f daemonset.yaml
Once you’ve sent updates to Kubernetes using either of the two methods, the DaemonSet is updated according to your update strategy, which is set with the spec.updateStrategy.type option. Here are the possible options and how they apply your updates:
Kubernetes will create new pods that match your updated DaemonSet only after you manually delete the old pods.
RollingUpdate is the default update strategy in Kubernetes 1.12+.
Kubernetes automatically deletes old pods and then creates new pods that match your updated DaemonSet. By default, DaemonSet pods are replaced one at a time so you don’t have any unavailability, but you can change spec.updateStrategy.rollingUpdate.maxUnavailable if you want to update more than one node at a time. You can also change spec.minReadySeconds to give your pod some time to initialize before it receives requests.
As an example, the following config performs a rolling update where two nodes are updated at a time and wait for 30 seconds after they enter the READY state before receiving requests:
spec:
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 2
minReadySeconds: 30
When you apply the update, you can watch its status by using the following command:
kubectl rollout status ds/<daemonset-name>
Click here for more on Kubernetes RollingUpdate.
DaemonSets provide a lot of value if you need a specific pod on every node. They are easy to create and update, and are a great feature in Kubernetes.
However, like the rest of Kubernetes, they can be hard to monitor with traditional tools. If you are looking for a monitoring solution for Kubernetes, consider Blue Matador. Blue Matador automatically checks for over 25 Kubernetes events out-of-the-box. We also monitor over 20 AWS services in conjunction with Kubernetes, providing full coverage for your entire production environment with no alert configuration or tuning required.