Skip to content
tezvyn:
Menu

Creating an instance of a custom resource

Source: interviewEasyHow cards are made

Summary

using a CRD.

Key points

write a manifest with apiVersion (group/version), kind, metadata.name, and a spec matching the CRD schema, then kubectl apply -f it.

Watch out for

omitting the group in apiVersion or applying before the CRD is registered.

What's really being asked

Whether you understand that a custom resource is created exactly like a built-in object once its CRD is registered, and that apiVersion must reference the custom group.

The full answer

Once the CRD is applied and established, you create an instance by writing a normal Kubernetes manifest and running kubectl apply -f manifest.yaml or kubectl create -f. The manifest needs four things: apiVersion set to the CRD's group and version, such as batch.example.com/v1 rather than just v1; kind matching the CRD's declared kind, here CronJob; a metadata block with at least a name and optionally a namespace if the CRD is namespaced; and a spec whose fields conform to the schema the CRD defines. A minimal manifest might read: apiVersion batch.example.com/v1, kind CronJob, metadata name my-job, spec with schedule set to a cron expression and an image. The API server validates the spec against the CRD's OpenAPI schema and rejects unknown or wrongly typed fields. You can then list instances with kubectl get cronjob.batch.example.com.

The mistakes people make

Using apiVersion v1 without the custom group, applying before the CRD is registered (which fails with no matches for kind), or assuming a controller exists to act on it when only the type is defined.

What usually comes next

How does the API server validate the spec? What happens if no controller watches it? How do short names and categories get defined?

A concrete example

With a CronJob CRD in group batch.example.com version v1 registered, you write a file containing apiVersion batch.example.com/v1, kind CronJob, metadata name nightly-report, and a spec with schedule "0 2 *" and image reporter:1.0. Running kubectl apply -f nightly.yaml creates the object, and kubectl get cronjob nightly-report confirms it, though it does nothing until a matching controller reconciles it.

Interview question

When writing the manifest for a custom resource defined by a CRD in group batch.example.com version v1, what must apiVersion be?

  • a.apps/v1, the standard workloads group
  • b.v1, since all resources share the core API version
  • c.The cluster's Kubernetes server version
  • d.batch.example.com/v1, including the CRD's API groupCorrect
Why?

Custom resources live under their CRD's API group, so apiVersion must be group/version like batch.example.com/v1. Using bare v1 or apps/v1 references the wrong group and the API server will not match the kind.

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