Daemon Sets & Static Pods ☸☸
Everything about Daemon Sets & Static Pods in Kubernetes
Introduction 🚩🚩
Hi fellow Readers 👋 :))
I have been writing about the basics of Kubernetes and it’s objects for a while now. I feel the basics are covered well and it is time to move to the more advanced Kubernetes topics now.
One of such topic is regarding Daemon Sets and Static Pods in Kubernetes.
So In this article, we will try to learn about :
- What is a Daemon Set and Why should we use it ??
- How can we create a Daemon Set ??
- What is a Static Pod and Why should we use it ??
- How can we create a Static Pod ??
- Difference between Daemon Set & Static Pod.
So without any further delay, lets get started with it :))
What is a Daemon Set and Why should we use it ?? 🤔 🤔
A DaemonSet ensures that all Nodes must run a copy of a Pod on them.
As nodes are added to the cluster, Pods are added to them. As nodes are removed from the cluster, those Pods are garbage collected. Deleting a DaemonSet will clean up all the Pods it created.
In simple terms, a DaemonSet is a Kubernetes Object which make sure that each and every node present in the cluster should have a specific node running on them.
Let us try to understand what happens when DaemonSet is in the picture :
- Whenever a new node is added to the cluster, the DaemonSet will make the copy of Pod and schedule that Pod on that newly added node.
- Whenever a node is removed from the cluster, the DaemonSet will remove the copy of Pod from that Node.
- Deleting a DaemonSet will probably kill all the Pods which are scheduled by it on different nodes in the cluster.
DaemonSets are kind of like ReplicaSets, as it helps in to deploy multiple instances of pod.
But DaemonSets makes sure to runs one copy of your pod on each node of the cluster.
Use Cases of Using DaemonSets :
- It can be used to run a cluster storage pod on every node
- It can be used to run a logs collection pod on every node
- It can be used to run a node monitoring pod on every node
I guess now it might be clear that What is a DaemonSet and Why should we use it ? Let us see how can we create one in the next section :))
How can we create DaemonSets ? 🤓🤓
We can create DaemonSets using YAML files. To create a Kubernetes DaemonSet with YAML, you first create an empty file, assign it the necessary access permissions, and then define the necessary key-value pairs.
Below is an example of a DaemonSet definition file (daemon-set-definition.yaml
)
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: monitoring-daemon
labels:
app: nginx
spec:
selector:
matchLabels:
app: monitoring-agent
template:
metadata:
labels:
app: monitoring-agent
spec:
containers:
- name: monitoring-agent
image: monitoring-agent
There are many aspects and components in this file. Lets break down each one of them 😀
- Let’s start with the
apiVersion
(key-value pair). This is used to clarify what API server and version you will be running in the background when creating the DaemonSet. - Next is
kind
which signifies the kind of definition file this is. In our case, it is a “DaemonSet”. - Next is
metadata
, which is a dictionary including the item name and labels. The metadata stores values that are assigned to the DaemonSet which is being created. - Finally there is
spec
which is actually an array/list. Following are the values which are present in it and what they mean.selector
: It basically has the labels which help the DaemonSet to identify the PODs it has to watch and keep the guard of while it is up.template
: It is the section which basically contains the POD specifications. This section comes handy when DaemonSet needs to spin up new PODs during different scenarios.
We are done with the DaemonSet definition file. Now we can save and exit the file.
Use this command to create the DaemonSet based on the above YAML file :
kubectl create -f daemon-set-definition.yaml
Use this command to list DaemonSets
kubectl get daemonsets
Use this command to get more details of the DaemonSets
kubectl describe daemonsets monitoring-daemon
What is a Static Pod and Why should we use it ?? 🤔 🤔
Static Pods are managed directly by the kubelet daemon on a specific node, without the API server observing them. Unlike Pods that are managed by the control plane, instead the kubelet watches each static Pod.
Static Pods are always bound to one Kubelet on a specific node.
In simple terms, Static Pods are just simple Pods but they are not being controlled by the master node.
Rather the kubelet present on the node spins them up and watches them during their whole lifecycle. Even restarts them whenever they fail.
The kubelet automatically tries to create a mirror Pod on the Kubernetes API server for each static Pod. This means that the Pods running on a node are visible on the API server, but cannot be controlled from there.
Use Cases of Using Static Pods :
Static pods are usually used by different software for bootstrapping Kubernetes itself. For example, kubeadm
uses static pods to bring up Kubernetes control plane components like api-server, controller-manager as static pods on the Master Node.
Then these components will be looked by Kubelet present on the Master Node.
I guess now it might be clear that What is a Static Pod and Why should we use it ? Let us see how can we create one in the next section :))
How can we create Static Pods ?? 🤓🤓
Static pod can be created by using standard POD configuration files (yaml files) and then placing those files in specific directories on the Node where you want to create the Pod.
Use kubelet --pod-manifest-path=<the directory>
to start kubelet daemon, which periodically scans the directory and creates/deletes static pods as yaml/json files appear/disappear there.
For example, this is how to start a simple web server as a static pod :
- Choose a node where we want to run the static pod. In this example, it’s
my-node1
ssh my-node1
- Choose a directory, say
/etc/kubelet.d
and place a web server pod definition file there, e.g./etc/kubelet.d/static-web.yaml
- Configure your kubelet daemon on the node to use this directory by running it with
--pod-manifest-path=/etc/kubelet.d/
argument. - Restart kubelet
$ systemctl restart kubelet
Difference between Daemon Set & Static Pod
What next ? 👀 👀
Thanks a lot for reaching till here! This is the end of this article.
But we have only scratched the surface of the K8s ecosystem :))
Much more to go, it will be a fun journey where we will learn a lot of cool stuff together.Do clap and follow me 🙈 if you like my writings and want to read more from me in the future :))
In case of any doubts around this article or for some general chit chat, feel free to reach out to me on my social media handles
Twitter — https://twitter.com/ChindaVibhor
LinkedIn — https://www.linkedin.com/in/vibhor-chinda-465927169/
Related Articles
I will still keep on coming with new articles covering a bunch of topics I am exploring.
That’s All folks !! Doodles :))