How Do You Use Custom Resource Definitions in Kubernetes?

Problem scenario
You want to extend Kubernetes' functionality. You want to use CustomResourceDefinitions. You can view API resources by running "kubectl api-resources". You want to add to the list. How do you use a CRD?

Solution

  1. Create a file called contint.yaml with the following content:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: contintexamples.continualintegration.com
spec:
  group: continualintegration.com
  names:
    kind: ContintExample
    plural: contintexamples
    listKind: ContintExampleList
    singular: contintexample
  scope: Namespaced
  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:
              owner:
                type: string
                description: The name of the owner of this Contint Example franchise
              address:
                type: string
                description: The physical address of the Contint Example
              currency:
                type: string
                description: The currency which this Contint Example accepts payments in
              replicas:
                type: integer
                description: The number of instances of the Contint Example app to deploy
    additionalPrinterColumns:
      - name: Owner
        jsonPath: .spec.owner
        type: string
      - name: Currency
        jsonPath: .spec.currency
        type: string
      - name: Replicas
        jsonPath: .spec.replicas
        type: integer
      - name: Age
        jsonPath: .metadata.creationTimestamp
        type: date

(# The above file was adapted from https://www.tutorialworks.com/kubernetes-custom-resources/ )

2. Run this: kubectl apply -f contint.yaml

3. You have now created a CustomResourceDefinition. The below steps 3a, 3b, 3c, 3d, and 3e are optional.
3.a. Run kubectl api-resources to view what API resources are currently at your disposal. Run kubectl get crd to view what you already may exist.

3.b. Run kubectl get crd to view what you created among everything else that was already there.

3.c. Run kubectl describe crd contintexamples.continualintegration.com to view the details.

3.d. Run kubectl api-resources to view what API resources are currently at your disposal.

3.e. Test it by running this: kubectl api-resources --api-group=continualintegration.com

Leave a comment

Your email address will not be published. Required fields are marked *