In the Kubernetes API a resource is an endpoint that stores a collection of API objects of a certain kind. For example, the built-in pods resource contains a collection of Pod objects.

A custom resource is an object that extends the Kubernetes API or allows you to introduce your own API into a project or a cluster.

A custom resource definition (CRD) file defines your own object kinds and lets the API Server handle the entire lifecycle.

While only cluster admins can create CRDs, you can create an object from a CRD if you have read and write permission to it.

Creating a custom objects from a CRD

Custom objects can contain custom fields that contain arbitrary JSON code.

  1. Create a YAML definition for the custom object. In the following example definition, the cronSpec and image custom fields are set in a custom object of kind CronTab. The kind CronTab comes from the spec.kind field of the custom resource definition object in the previous example.

    Example YAML file for a custom object
    apiVersion: "stable.example.com/v1" (1)
    kind: CronTab (2)
    metadata:
      name: my-new-cron-object (3)
      finalizers: (4)
      - finalizer.stable.example.com
    spec: (5)
      cronSpec: "* * * * /5"
      image: my-awesome-cron-image
    1 Specify the group name and API version (name/version) from the custom resource definition.
    2 Specify the type in the custom resource definition.
    3 Specify a name for the object.
    4 Specify the finalizers for the object, if any. Finalizers allow controllers to implement conditions that must be completed before the object can be deleted.
    5 Specify conditions specific to the type of object.
  2. After you create the object file, create the object:

    oc create -f <file-name>.yaml

Managing custom objects

After you create objects, you can manage your custom resources.

  • To get information on a specific kind of custom resource, enter:

    oc get <kind>

    For example:

    oc get crontab
    
    NAME                 KIND
    my-new-cron-object   CronTab.v1.stable.example.com

    Note that resource names are not case-sensitive, and you can use either the singular or plural forms defined in the CRD, as well as any short name. For example:

    oc get crontabs
    oc get crontab
    oc get ct
  • You can also view the raw JSON data for a custom resource:

    oc get <kind> -o yaml
    oc get ct -o yaml
    
    apiVersion: v1
    items:
    - apiVersion: stable.example.com/v1
      kind: CronTab
      metadata:
        clusterName: ""
        creationTimestamp: 2017-05-31T12:56:35Z
        deletionGracePeriodSeconds: null
        deletionTimestamp: null
        name: my-new-cron-object
        namespace: default
        resourceVersion: "285"
        selfLink: /apis/stable.example.com/v1/namespaces/default/crontabs/my-new-cron-object
        uid: 9423255b-4600-11e7-af6a-28d2447dc82b
      spec:
        cronSpec: '* * * * /5' (1)
        image: my-awesome-cron-image (1)
    1 Custom data from the YAML that you used to create the object displays.