Skip to content
Visual explainer 3h to learn 4 steps

DaemonSets, static Pods, Jobs & init/sidecar containers

Choose the right workload type and debug static Pods on a node.

By Invicly Tech, , in Kubernetes Administrator (CKA). About 10 minutes to read.

Topic 17 of 33

In short

Different controllers run Pods in different ways. DaemonSets run one Pod per node, static Pods are run directly by a kubelet from a local file, and Jobs and CronJobs run tasks to completion. Init containers run before app containers, and native sidecars keep running alongside them.

Explained simply

A Deployment is great for web apps that run forever with any number of copies. But some jobs have a different shape, and Kubernetes has a different controller for each one.

A DaemonSet runs exactly one Pod on every node, which suits node agents like log shippers and network plugins. A Job runs a task until it finishes, like a database migration. A CronJob starts a Job on a schedule, like a nightly backup.

Static Pods are special. They are not created through the API at all. The kubelet on a node reads YAML files from a folder and runs them directly. This is how the control plane itself (API server, etcd, scheduler) runs on kubeadm clusters, before any API exists.

Inside one Pod you can also have helper containers. Init containers run first and must finish, for example to wait for a database. Sidecar containers start before the app and keep running next to it, for example to ship logs or proxy traffic.

Press Play on a diagram to walk through it one step at a time.

ComparePick a workload type
DaemonSet
  • One Pod per matching node
  • Log agents, CNI, kube-proxy
  • New nodes get a Pod automatically
Static Pod
  • Manifest file on one node
  • Run by kubelet, no API needed
  • Control plane components
Job and CronJob
  • Run to completion
  • completions, parallelism, backoffLimit
  • CronJob runs Jobs on a schedule
FlowPod container startup
  1. Step 1
    Init containers

    Run one by one; each must succeed.

  2. Step 2
    Sidecar containers

    Init containers with restartPolicy Always; keep running.

  3. Step 3
    App containers

    Start after inits finish and sidecars start.

MapWhich workload type should I use?
What does the Pod do?
  • Serves forever, any copies
    • Deployment
  • Serves forever, stable identity and disk
    • StatefulSet
  • One per node
    • DaemonSet
  • Runs once to completion
    • Job
  • Runs on a schedule
    • CronJob
  • Must exist before the API
    • Static Pod

In depth

DaemonSets: one per node, and how to limit them

The DaemonSet controller creates one Pod per eligible node and pins it there with node affinity on the node's name, so the normal scheduler still binds it. New nodes get a Pod automatically; removed nodes lose theirs. To run on only some nodes, add a nodeSelector or affinity. To include control plane nodes, add a toleration for node-role.kubernetes.io/control-plane:NoSchedule.

DaemonSet Pods automatically tolerate several node condition taints, such as not-ready, unreachable, disk-pressure and memory-pressure, so node agents keep running on unhealthy nodes. updateStrategy RollingUpdate (default, maxUnavailable 1) replaces Pods node by node; OnDelete waits until you delete each Pod yourself. kubectl cannot create a DaemonSet directly, so generate a Deployment and edit it.

  • Change kind to DaemonSet
  • Delete replicas, strategy and status
  • kubectl get ds -A shows DESIRED versus READY per DaemonSet

Static Pods: how the kubelet finds them

The kubelet reads its configuration file, on kubeadm /var/lib/kubelet/config.yaml, and the staticPodPath field names the folder it watches, normally /etc/kubernetes/manifests. Any valid Pod YAML dropped there starts within seconds; removing the file stops the Pod; editing it recreates the Pod.

The kubelet also creates a mirror Pod in the API so you can see it with kubectl, named after the Pod plus the node name, like kube-apiserver-cp1. You cannot change a static Pod through the mirror; deleting the mirror only makes it reappear. If a manifest has a YAML error, nothing appears in kubectl at all, and you must read the kubelet logs with journalctl -u kubelet.

FlowFinding and fixing a static Pod
  1. 1
    Find the config

    ps aux | grep kubelet shows --config

  2. 2
    Read staticPodPath

    grep staticPodPath in that file

  3. 3
    Edit the manifest

    Save the file in that folder

  4. 4
    kubelet restarts it

    Watch with crictl ps

  5. 5
    Mirror Pod updates

    kubectl get pods -n kube-system

Jobs and CronJobs: the knobs that matter

A Job runs Pods until completions of them succeed (default 1), running up to parallelism at once. backoffLimit (default 6) caps retries before the Job is marked Failed; activeDeadlineSeconds fails it after a time limit; ttlSecondsAfterFinished deletes it automatically later. Pods must use restartPolicy OnFailure (restart in place) or Never (a new Pod each try).

A CronJob creates Jobs on a five-field cron schedule in the controller's time zone unless you set spec.timeZone. concurrencyPolicy is Allow (default), Forbid (skip if the last run is still going) or Replace. successfulJobsHistoryLimit defaults to 3 and failedJobsHistoryLimit to 1. suspend: true pauses it. To test right now, run kubectl create job manual --from=cronjob/backup.

  • kubectl create job x --image=busybox -- echo hi
  • kubectl create cronjob y --image=busybox --schedule='*/5 * * * *' -- date
  • kubectl get jobs shows COMPLETIONS like 3/3

Init containers and native sidecars

Init containers run in order, each to completion, before any app container starts. If one fails, the kubelet retries it (unless the Pod's restartPolicy is Never) and the Pod shows Init:Error or Init:CrashLoopBackOff. Read its log with kubectl logs POD -c INITNAME.

Since Kubernetes 1.33, native sidecars are stable: an init container with restartPolicy: Always starts in order like an init container but keeps running, and the next container starts once it has started (or passed its startup probe). Sidecars stop after the app containers on shutdown, and they do not block a Job from completing. Before this, sidecars were plain extra containers in containers, with no start order and a habit of keeping Jobs alive forever.

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Run a DaemonSet on every node~45m
  2. 2
    Create and debug static Pods~45m
  3. 3
    Run Jobs and CronJobs~45m
  4. 4
    Add init and sidecar containers~45m

Key points

  • Edit or delete the file in the staticPodPath to change a static Pod.
  • Static Pods appear in the API as mirror Pods with the node name suffix.
  • Native sidecars are declared in initContainers with restartPolicy: Always.
  • Jobs need restartPolicy OnFailure or Never.

Key terms

Tap a card to flip it. 0/6 known

Examples

bash
kubectl create job pi --image=perl:5.40 -- perl -Mbignum=bpi -wle "print bpi(100)"
kubectl create cronjob backup --image=busybox \
  --schedule="0 2 * * *" -- sh -c "echo backup"

# Static Pod on a node
kubectl run static-web --image=nginx --dry-run=client -o yaml \
  | sudo tee /etc/kubernetes/manifests/static-web.yaml
kubectl get pods   # static-web-<nodename>

A DaemonSet that also runs on the control plane

You need a node agent on every node, including the control plane. Step 1: generate Deployment YAML. Step 2: change kind to DaemonSet and remove replicas and strategy. Step 3: add the control plane toleration. Step 4: apply and check that DESIRED equals the number of nodes.

yaml
apiVersion: apps/v1
kind: DaemonSet
metadata: { name: node-agent, namespace: kube-system }
spec:
  selector: { matchLabels: { app: node-agent } }
  template:
    metadata: { labels: { app: node-agent } }
    spec:
      tolerations:
        - { key: node-role.kubernetes.io/control-plane, operator: Exists, effect: NoSchedule }
      containers:
        - { name: agent, image: busybox:1.36, command: [sh, -c, "sleep infinity"] }
# kubectl get ds node-agent -n kube-system

Wait for a database with an init container

An app crashes if it starts before its database Service exists. Step 1: add an init container that loops until DNS for the Service resolves. Step 2: apply; the Pod shows Init:0/1 while waiting. Step 3: create the db Service; the init container exits 0 and the app starts. Step 4: if it hangs, read the init container log by name.

yaml
spec:
  initContainers:
    - name: wait-db
      image: busybox:1.36
      command: [sh, -c, "until nslookup db; do echo waiting; sleep 2; done"]
  containers:
    - { name: app, image: nginx:1.27 }
# kubectl get pod app        STATUS Init:0/1
# kubectl logs app -c wait-db

Quick reference

  • staticPodPath lives in /var/lib/kubelet/config.yaml; kubeadm uses /etc/kubernetes/manifests
  • Static Pod mirror name = Pod name + - + node name
  • kubectl create job manual --from=cronjob/backup runs a CronJob now
  • Job backoffLimit defaults to 6; restartPolicy must be OnFailure or Never
  • CronJob concurrencyPolicy: Allow, Forbid or Replace
  • Native sidecar = initContainers entry with restartPolicy: Always (stable since 1.33)
  • kubectl logs POD -c INITNAME reads an init container's output

Common mistakes

  • Deleting a mirror Pod with kubectl; the kubelet recreates it from the file.
  • Using restartPolicy Always in a Job, which the API rejects.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You run kubectl delete pod kube-scheduler-cp1 -n kube-system. What happens?

Question 2

2A CronJob's previous run is still going when the next one is due. Which setting skips the new run?

Question 3

3How do you declare a native sidecar container?

Question 4

4A DaemonSet shows DESIRED 2 on a 3-node cluster. What is the most likely cause?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create a Job named count that runs busybox, prints the numbers 1 to 5, and must succeed 3 times with 2 Pods in parallel.

  2. Exercise 2medium

    On node01, create a static Pod named static-busy running busybox with sleep 3600, then remove it again.

  3. Exercise 3hard

    A Pod is stuck in Init:CrashLoopBackOff. Find which init container fails and why, and explain why the app container never started.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between a static Pod and a DaemonSet?
A static Pod is run by one kubelet from a local file and works even without an API server. A DaemonSet is a normal API object whose controller schedules one Pod on every matching node.
How do I trigger a CronJob manually?
Run kubectl create job NAME --from=cronjob/CRONJOB. It creates a Job from the CronJob's template right away.
Why does my Job never finish when it has a sidecar?
A sidecar declared as a regular container keeps running, so the Pod never completes. Declare it as a native sidecar in initContainers with restartPolicy: Always, which Kubernetes stops when the main containers finish.

Go deeper

ShareXLinkedInWhatsApp