Kubernetes · Chapter 27 of 28

CRDs and operators

By the end of this chapter you can teach the API server a kind of object of your own with a CustomResourceDefinition, give it a schema that refuses bad data, choose the columns kubectl shows for it, read it with kubectl explain, and run a small controller that turns each such object into running pods: an operator. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Packaging and operating
  • 4 tasks
  • Kubernetes
  • In the all labs plan

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.

Why you need this

Jhatpat has 14 dark stores in Lucknow and opens 6 in Kanpur next year. Each one needs a store agent running in the cluster, the program that reads that store's stock scanners, with the store's area in its settings. Today Bhola opens a store, then sends Tushar a message, and Tushar copies a Deployment, changes two words and applies it. When the Chinhat store moved in September, its old agent ran for a week, reading nobody's scanners.

Everything you know so far describes how to run things: Deployments, Services, ConfigMaps. None of them can say what Bhola means, "Jhatpat has a dark store in Aliganj that holds 1,200 crates", and nothing in the cluster acts on a store opening or closing. Ammar wants the stores themselves in the API, as objects Bhola's team can list, with kubectl refusing a store with no area, and a controller that keeps exactly one agent per store, the way the ReplicaSet controller keeps pods.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    darkstore-crd.yaml: install the CRD and two DarkStores

    Teach the API server a new kind, DarkStore, then make two of them.

    1. Open darkstore-crd.yaml. Add spec.capacity to its schema: an integer, with minimum 50 and maximum 2000.

    2. Put city, area and capacity in spec's required list.

    3. Add three printer columns to the version v1: City, Area and Capacity, from .spec.city, .spec.area and .spec.capacity.

    4. Apply darkstore-crd.yaml. Then apply stores.yaml.

    To pass: the CRD darkstores.jhatpat.in is Established, with kind DarkStore and a served, stored version v1. Its schema and columns are as above. The DarkStores gomti-nagar and hazratganj are in ch27 with exactly the values in stores.yaml.

  2. Task 2

    aliganj.yaml: a validation error and its fix

    See the API server refuse bad data, then fix the data.

    1. Do task 1 first, so the schema has its maximum.

    2. Apply aliganj.yaml as Bhola wrote it, with capacity 5000. Read the error.

    3. Change the capacity in aliganj.yaml to 1200.

    4. Apply aliganj.yaml again.

    To pass: an apply of aliganj.yaml was refused in the terminal. The DarkStore aliganj is in ch27 with capacity 1200. The CRD still has its maximum of 2000: do not change the schema to let 5000 in. If aliganj went in with 5000 before the schema had its maximum, delete aliganj and start this task again.

  3. Task 3

    operator.sh: run the operator and see it work

    Run the operator, so every DarkStore gets its own agent.

    1. Read operator.sh. See what it lists, what it makes for each store, and what it writes back.

    2. Make a fourth DarkStore named chinhat in ch27: city lucknow, area Chinhat, capacity 300. Write a small file like stores.yaml and apply it.

    3. Run bash operator.sh --once. Wait a few seconds, then run it once more.

    4. List the Deployments and the DarkStores.

    To pass: chinhat has exactly those values. Every DarkStore in ch27 (all four) has a Deployment store-agent-<name>, owned by that DarkStore, with a ready pod. Each DarkStore's status.agent names its agent.

  4. Task 4 · interview question

    Interview: what makes a CRD plus a controller an operator

    Interview question: what is the difference between a CRD alone and a CRD with a controller?

    1. Ammar will ask: "I created a DarkStore and nothing happened. Then you ran a bash loop and an agent appeared. What makes a CRD plus a controller an operator?"

    2. Write your answer in two or three sentences of your own.

    3. Save it in answer.txt, below the notes.

    To pass: answer.txt says what a CRD alone does, names the controller, and says what the controller does. Lines that start with # do not count.

What you will learn

  • CustomResourceDefinitions: a new kind, its group, versions and names.
  • The schema: types, required fields, minimum and maximum, and the API server's real validation errors.
  • Printer columns and kubectl explain for your kind.
  • Custom resources are just data: stored and validated, and nothing more, until a controller acts.
  • An operator: a controller that watches your kind and reconciles, with owner references, status, and finalizers in one paragraph.

More in Packaging and operating

Build "CRDs and operators" yourself

Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.