liveBook · Manning

Custom resource definition

As explained in Acing the Certified Kubernetes Administrator Exam, Second Edition

A custom resource definition is a Kubernetes API object that defines a new custom resource type and its schema.

A custom resource definition (CRD) extends the Kubernetes API with a new resource type. It defines the type’s name, API group, scope, and schema, including permitted fields and values. A namespaced resource belongs to a namespace; a cluster-scoped resource belongs to the whole cluster. Installing a CRD makes the type available through the Kubernetes API, but does not by itself define how to manage instances. An operator typically supplies that behavior through a controller. section 5.4.1 section 5.4

Keep the definition separate from the object you create: one CRD can have many custom resources (CRs). For example, CloudNativePG installs a CRD for PostgreSQL Cluster resources; an individual Cluster object requests a particular PostgreSQL cluster. Here, “PostgreSQL cluster” does not mean the Kubernetes cluster. section 5.4.3 section 5.4.1

To install and inspect an operator’s resource types:

  • Apply the operator manifest, for example with `kubectl apply --server-side -f https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.26/releases/cnpg-1.26.1.yaml`. This installation also creates its CRDs. Helm installs an operator’s CRDs by default unless given `--skip-crds`. section 5.4.2
  • Run `kubectl get crds` to confirm the definitions exist, then `kubectl api-resources --api-group=postgresql.cnpg.io` to find their API resources. Use `kubectl explain clusters.postgresql.cnpg.io` to inspect the Cluster type and its fields without reading the full generated CRD schema. section 5.4.2 section 5.4.1
  • Check that the operator controller is running with `kubectl get deployment -n cnpg-system cnpg-controller-manager` before expecting it to act on a new Cluster object. section 5.4.3
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: my-postgres-cluster
  namespace: default
spec:
  instances: 1
  storage:
    size: 1Gi

This YAML is a custom resource, not its CRD. It selects the installed `postgresql.cnpg.io/v1` Cluster type and requests one PostgreSQL instance with 1Gi of storage in the default namespace. section 5.4.3

Apply that CR with `kubectl apply -f postgres-cluster.yaml`. Verify the result with `kubectl get cluster my-postgres-cluster`, `kubectl describe cluster my-postgres-cluster`, and `kubectl get pods`; the example shows a healthy Cluster and a running pod. CRDs also underpin the Gateway API, whose GatewayClass, Gateway, and HTTPRoute objects may already be available when configuring routes. section 5.4.3 section 6.7

How Acing the Certified Kubernetes Administrator Exam, Second Edition covers it

How deep it goes
Explains CRD fields, scope, installation, discovery, and CR creation; directs field inspection to kubectl explain rather than unpacking generated schemas.
Examples
Uses CloudNativePG installation, a PostgreSQL Cluster manifest, kubectl discovery commands, and cluster-status checks.
What it assumes
Assumes familiarity with built-in Kubernetes resources and kubectl while explaining the CRD–custom resource distinction explicitly.
What you can do after
Operate/configure it by installing an operator’s CRDs, inspecting available fields, creating a custom resource, and verifying its resulting workload.
How it connects
Distinguishes custom resources from their definitions and connects CRDs to operators, controllers, API groups, and the Gateway API.

Also explained in

Compare how 7 books explain custom resource definition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Certified Kubernetes Administrator Exam, Second Edition. The section links point to the parts of the book it draws on.