Creating and Managing Custom Resource Definitions

Tutorial 1 of 5

Introduction

In this tutorial, we will dive into creating and managing Custom Resource Definitions (CRDs) in Kubernetes. CRDs essentially allow you to create a new resource type in Kubernetes, which functions just like a built-in resource, allowing you to extend the functionality of your Kubernetes cluster.

By the end of this guide, you will understand:

  • What a Custom Resource Definition (CRD) is
  • How to create a CRD
  • How to manage and interact with your CRD

Before we begin, you should have a basic understanding of Kubernetes and YAML. Familiarity with command-line interfaces and Linux would be beneficial, though not mandatory.

Step-by-Step Guide

Understanding CRDs

In Kubernetes, a CRD is a built-in API that allows you to create your own custom resources (like Pods, Services, etc.). Once a CRD is created, you can use it like any other native Kubernetes Object.

Creating a CRD

To create a CRD, you need to define it in a YAML file. Here's a simple example:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: myresources.mycompany.com
spec:
  group: mycompany.com
  versions:
  - name: v1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              field1:
                type: string
              field2:
                type: integer
  scope: Namespaced
  names:
    plural: myresources
    singular: myresource
    kind: MyResource

This YAML file creates a new CRD called MyResource under the group mycompany.com. The spec properties define the structure of the resources.

Managing CRDs

Once your CRD is created, you can manage it with kubectl commands. For example, to get a list of your MyResource objects, you could run kubectl get myresources.

Code Examples

Let's create an instance of our MyResource:

apiVersion: mycompany.com/v1
kind: MyResource
metadata:
  name: example-resource
spec:
  field1: "Hello"
  field2: 42

This creates an instance of MyResource with field1 set to "Hello" and field2 set to 42. You can interact with it like any other Kubernetes resource.

Summary

In this tutorial, we learned how to create and manage Custom Resource Definitions (CRDs) in Kubernetes, allowing us to extend the Kubernetes API with our own resources.

For further exploration, try creating your own CRDs with different field types and properties, and experiment with managing them with kubectl.

Practice Exercises

  1. Exercise: Create a CRD for a resource called MyWidget, with a string field called description and an integer field called quantity.

  2. Exercise: Create an instance of MyWidget with description set to "This is my widget" and quantity set to 10.

  3. Exercise: Use kubectl to get a list of all MyWidget objects.

Solutions:

  1. Solution:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: mywidgets.mycompany.com
spec:
  group: mycompany.com
  versions:
  - name: v1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              description:
                type: string
              quantity:
                type: integer
  scope: Namespaced
  names:
    plural: mywidgets
    singular: mywidget
    kind: MyWidget
  1. Solution:
apiVersion: mycompany.com/v1
kind: MyWidget
metadata:
  name: example-widget
spec:
  description: "This is my widget"
  quantity: 10
  1. Solution:

Run kubectl get mywidgets.

Remember to keep practicing and experimenting with different CRDs and their configurations!