Skip to content
tezvyn:
Menu

Custom Resource Definitions (CRDs): Teach Kubernetes New Tricks

Source: kubernetes.ioEasyHow cards are made

Custom Resource Definitions (CRDs): Teach Kubernetes New Tricks

CRDs let you define your own resource types, teaching Kubernetes new nouns like Database or Backup. This is how operators manage complex apps declaratively. The footgun is that a CRD only defines the API; you still need a controller to act on the objects.

Why it exists

Kubernetes ships with core resources like Pods and Deployments, but real-world systems need to manage more, like databases, message queues, or backup jobs. CRDs were created to extend the Kubernetes API to manage these application-specific components as native objects.

The mental model

A CRD is like adding a new noun to the Kubernetes vocabulary. You're teaching the API server a new concept, like a PostgresCluster or a ScheduledBackup. Once defined, you can interact with these new objects using standard tools like kubectl get mycustomresource.

How it works

You apply a YAML file that defines the CRD itself. This file specifies the new resource's name (e.g., crontabs.stable.example.com), its scope (namespaced or cluster-wide), and a schema for its spec and status fields. Once the CRD is registered, the Kubernetes API server automatically creates a new RESTful endpoint for your resource type. You can then create, read, update, and delete instances of this new object.

When to use it

Use CRDs when you want to manage a resource declaratively using Kubernetes-native tools. This is the foundation of the Operator Pattern, where a custom controller automates the lifecycle of a complex application defined by one or more CRDs. For example, the Prometheus Operator uses a ServiceMonitor CRD to automatically configure scraping targets.

When not to use it

If you only need to store configuration data, a ConfigMap or Secret is much simpler. If your desired API doesn't follow the declarative, CRUD-style model of Kubernetes resources, a CRD might be a poor fit. For very complex extensions that need custom validation or subresources, a more advanced (and much harder) pattern called an Aggregated API Server might be necessary.

One canonical example

A classic example is creating a CronTab resource. The CRD defines what a CronTab object looks like: it has a spec with a schedule (e.g., "/1 *") and a job template. A separate, custom controller watches for CronTab objects. When a new one is created, the controller creates Kubernetes Job resources on the specified schedule. The CRD itself does nothing but define the schema; the controller provides the behavior.

Interview question

After successfully applying a Custom Resource Definition (CRD) for a new "MyApplication" resource to a Kubernetes cluster, what is the immediate outcome?

  • a.All existing Kubernetes controllers are automatically updated to understand and orchestrate "MyApplication" resources.
  • b.The Kubernetes API server will immediately start validating and enforcing custom business logic defined within the CRD for "MyApplication" objects.
  • c.Kubernetes automatically gains the ability to manage instances of "MyApplication" by deploying necessary pods.
  • d.Users can now create, read, update, and delete "MyApplication" objects using kubectl, but no operational logic is yet associated with them.Correct
Why?

A CRD extends the Kubernetes API, allowing the API server to recognize and store new resource types, making them manageable via kubectl. However, the CRD only defines the API and schema; a separate custom controller is needed to provide the actual operational logic and act upon these new objects, which is why option C is incorrect.

Just read this? Test yourself on what you have been reading.

Read the original → kubernetes.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kubernetes — each one lists the topics its interview covers.

See open roles