The other workloads

When to use a StatefulSet, a DaemonSet, a Job or a CronJob instead of a Deployment.

GazeekKUBERNETES FUNDAMENTALS·LESSON 11
LESSON 11

The other workloads

When to use a StatefulSet, a DaemonSet, a Job or a CronJob instead of a Deployment.

0:00 / 3:54

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: Four workloads, side by side (new tab)

Now try it on a real cluster

The videos are free. Gazeek's hands-on labs give you a real Kubernetes cluster in your browser for every lab, with checks that tell you when you've got it right: the practice the CKA and CKAD exams need.

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Explain what a StatefulSet gives each Pod that a Deployment does not
  • Describe when a DaemonSet runs a Pod on every node
  • Distinguish a Job, which runs to completion, from a CronJob, which runs on a schedule
  • Choose the right workload resource for a given task
Transcript

The other workloads

Space Llama here. Deployments are great. They are not the only tool in the toolbox. Today, four more workloads, and when to pick each one. StatefulSets, DaemonSets, Jobs and CronJobs. Each one solves a problem that a Deployment does not.

StatefulSet

A Deployment's Pods are interchangeable. Any one can replace any other. Some apps don't work like that. Databases, for example, need each copy to keep its own identity and data. That's what a StatefulSet is for. Its Pods get stable names, like web zero, web one, web two. A replacement keeps the same name. Each Pod also gets a stable network name, so other Pods can always find the same copy. Each Pod gets its own persistent storage, which stays with that name. And by default, Pods are created in order, and removed in reverse order.

DaemonSet

A DaemonSet makes sure that every node runs a copy of a Pod. Or just some nodes, if you choose. When a node joins the cluster, it gets the Pod. When the node leaves, the Pod is cleaned up. It suits agents that belong to the node itself. Think log collectors, monitoring agents, or the cluster's network plugin. Kubernetes uses them itself. kube-proxy, which you met inside the node, often runs as a DaemonSet.

Job vs CronJob

A Deployment keeps Pods running forever. Sometimes you want the opposite. A Job runs Pods until a task finishes successfully. If a Pod fails, the Job retries it. When the work is done, the Job is complete, and nothing keeps running. A Job can also run several Pods in parallel, to get through a big batch faster. A CronJob creates Jobs on a repeating schedule. It uses the cron format from Linux, so you can say every night at two, or every Monday. Good for backups, reports and clean-ups. The chores nobody wants to remember. Each run is its own Job, so you can check whether last night's backup worked.

Four workloads, side by side

Here are all four, side by side. On the left, a StatefulSet. Pods db zero and db one each get their own PersistentVolumeClaim, and keep it. Next, a DaemonSet. One Pod on each of three nodes. Add a fourth node, get a fourth Pod. Then a Job. It runs its Pod until the task completes, and then it stops. Last, a CronJob. Every night at two, it creates a new Job, which runs its own Pod. Same Pods underneath. Different rules about how many, where, and for how long.

Choosing a workload

So how do you choose? A stateless app, like a website or an API, is a Deployment. If each copy needs its own name and its own storage, use a StatefulSet. If you need one copy on every node, use a DaemonSet. A task that runs once and finishes is a Job. The same task on a timetable is a CronJob.

Recap

Let's recap. StatefulSets give each Pod a stable name and its own storage. DaemonSets run one Pod on every node, or on the nodes you pick. Jobs run a task until it completes. And CronJobs create Jobs on a schedule.

Practice and what's next

That's the whole workload family. Big family. Well behaved. Practice choosing the right one at Gazeek Labs, with free Kubernetes practice questions and hands-on labs. Next up, Services, and how Pods find each other. See you there. That's the part. There's more on the channel. Alpaca Mango. Go on, then. Mate.

Hands-on labs

Now try it on a real cluster

Batch work and storage that lasts: a real cluster built for you, checked as you go.

Open the lab

Gazeek Flash · free

Exam questions on your phone

Free questions on Workloads: quick-fire batches and timed exams, on the web or Android.