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:
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.
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.
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.
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
.
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.
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
.
Exercise: Create a CRD for a resource called MyWidget
, with a string field called description
and an integer field called quantity
.
Exercise: Create an instance of MyWidget
with description
set to "This is my widget" and quantity
set to 10.
Exercise: Use kubectl
to get a list of all MyWidget
objects.
Solutions:
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
apiVersion: mycompany.com/v1
kind: MyWidget
metadata:
name: example-widget
spec:
description: "This is my widget"
quantity: 10
Run kubectl get mywidgets
.
Remember to keep practicing and experimenting with different CRDs and their configurations!