⎈ k8s knowledge compiler

Extend the Kubernetes API with CustomResourceDefinitions [page]deterministic

tasks

This page shows how to install a [custom resource](/docs/concepts/extend-kubernetes/api-extension/custom-resources/) into the Kubernetes API by creating a [CustomResourceDefinition](/docs/reference/generated/kubernetes-api//#customresourcedefinition-v1-apiextensions-k8s-io).

##

If you are using an older version of Kubernetes that is still supported, switch to the documentation for that version to see advice that is relevant for your cluster.

## Create a CustomResourceDefinition

When you create a new CustomResourceDefinition (CRD), the Kubernetes API Server creates a new RESTful resource path for each version you specify. The custom resource created from a CRD object can be either namespaced or cluster-scoped, as specified in the CRD's `spec.scope` field. As with existing built-in objects, deleting a namespace deletes all custom objects in that namespace. CustomResourceDefinitions themselves are non-namespaced and are available to all namespaces.

For example, if you save the following CustomResourceDefinition to `resourcedefinition.yaml`:

```yaml apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: # name must match the spec fields below, and be in the form: <plural>.<group> name: crontabs.stable.example.com spec: # group name to use for REST API: /apis/<group>/<version> group: stable.example.com # list of versions supported by this CustomResourceDefinition versions: - name: v1 # Each version can be enabled/disabled by Served flag. served: true # One and only one version must be marked as the storage version. storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: cronSpec: type: string image: type: string replicas: type: integer # either Namespaced or Cluster scope: Namespaced names: # plural name to be used in the URL: /apis/<group>/<version>/<plural> plural: crontabs # singular name to be used as an alias on the CLI and for display singular: crontab # kind is normally the CamelCased singular type. Your resource manifests use this. kind: CronTab # shortNames allow shorter string to match your resource on the CLI shortNames: - ct ```

and create it:

```shell kubectl apply -f resourcedefinition.yaml ```

Then a new namespaced RESTful API endpoint is created at:

``` /apis/stable.example.com/v1/namespaces/*/crontabs/... ```

This endpoint URL can then be used to create and manage custom objects. The `kind` of these objects will be `CronTab` from the spec of the CustomResourceDefinition object you created above.

It might take a few seconds for the endpoint to be created. You can watch the `Established` condition of your CustomResourceDefinition to be true or watch the discovery information of the API server for your resource to show up.

## Create custom objects

After the CustomResourceDefinition object has been created, you can create custom objects. Custom objects can contain custom fields. These fields can contain arbitrary JSON. In the following example, the `cronSpec` and `image` custom fields are set in a custom object of kind `CronTab`. The kind `CronTab` comes from the spec of the CustomResourceDefinition object you created above.

If you save the following YAML to `my-crontab.yaml`:

```yaml apiVersion: "stable.example.com/v1" kind: CronTab metadata: name: my-new-cron-object spec: cronSpec: "* * * * */5" image: my-awesome-cron-image ```

and create it:

```shell kubectl apply -f my-crontab.yaml ```

You can then manage your CronTab objects using kubectl. For example:

```shell kubectl get crontab ```

Should print a list like this:

```none NAME AGE my-new-cron-object 6s ```

Resource names are not case-sensitive when using kubectl, and you can use either the singula …(trimmed)

Sources

tasks/extend-kubernetes/custom-resources/custom-resource-definitions.md · docExtend the Kubernetes API with CustomResourceDefinitions

Related (25)

references etcdetcd conf=1
references ConfigMapConfigMap conf=1
references CustomResourceDefinitionCustomResourceDefinition conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of Create a CustomResourceDefinitiondescribes conf=1
part_of Create custom objectsdescribes conf=1
part_of Delete a CustomResourceDefinitiondescribes conf=1
part_of Specifying a structural schemadescribes conf=1
part_of Serving multiple versions of a CRDdescribes conf=1
part_of Advanced topicsdescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of Field pruningdescribes conf=1
part_of IntOrStringdescribes conf=1
part_of RawExtensiondescribes conf=1
part_of Finalizersdescribes conf=1
part_of Validationdescribes conf=1
part_of Validation ratchetingdescribes conf=1
part_of Validation rulesdescribes conf=1
part_of Defaultingdescribes conf=1
part_of Publish Validation Schema in OpenAPIdescribes conf=1
part_of Additional printer columnsdescribes conf=1
part_of Field selectorsdescribes conf=1
part_of Subresourcesdescribes conf=1
part_of Categoriesdescribes conf=1
api_for CustomResourceDefinitiondocuments API object conf=1

← all Docs