Showing posts with label Custom Resource Definitions. Show all posts
Showing posts with label Custom Resource Definitions. Show all posts

Friday, February 2, 2024

Custom Resource Definitions (CRDs) in Kubernetes

To install Custom Resource Definitions (CRDs) in Kubernetes, you typically define them in a YAML file and then apply that file using kubectl. CRDs allow you to extend the Kubernetes API by defining your own resources.

Here’s a step-by-step guide to install CRDs:

Step 1: Create a CRD YAML File

Create a YAML file that defines your CRD. Below is an example of a CRD YAML definition for a MyCustomResource:

 

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: mycustomresources.mygroup.example.com
spec:
  group: mygroup.example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                someField:
                  type: string
  scope: Namespaced
  names:
    plural: mycustomresources
    singular: mycustomresource
    kind: MyCustomResource
    shortNames:
      - mcr

This defines a CRD called MyCustomResource, which is scoped to a namespace and has a field someField.

Step 2: Install the CRD using kubectl

Once you’ve defined your CRD YAML file, you can install it using the kubectl command. Assuming the file is called mycustomresource-crd.yaml, you can install it like this:

kubectl apply -f mycustomresource-crd.yaml
 

Step 3: Verify the CRD is Installed

To verify the CRD has been installed successfully, you can use:

kubectl get crds

 You should see mycustomresources.mygroup.example.com in the list of CRDs.

Step 4: Create Custom Resources

Once the CRD is installed, you can now create Custom Resources (CRs) based on that CRD. Here’s an example of a custom resource using the CRD:

apiVersion: mygroup.example.com/v1
kind: MyCustomResource
metadata:
  name: my-custom-resource-instance
spec:
  someField: "Some value"


To create this custom resource, save the YAML to a file (e.g., my-custom-resource.yaml) and apply it:

 kubectl apply -f my-custom-resource.yaml