Part 1: How to Create a Kubernetes Custom Resource Definition

Understanding How Kubernetes Works with Resources

Pre-requisites

  • A working Kubernetes cluster. (local or remote)
    If you don’t have one, you can always run a K3s cluster locally or you can use a Kind Kubernetes cluster. You can read about starting a Kind Kubernetes cluster here.
  • Basic knowledge of working with “Kubectl” commands to create, get or delete a resource.

Creating a Kubernetes Custom Resource Definition (CRD)

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: my-crds.com.amrut.prabhu
spec:
group: com.amrut.prabhu
names:
kind: my-crd
plural: my-crds
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
type: object
properties:
my-own-property:
type: string

Understanding The Kubernetes Custom Resource Definition Format

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: my-crds.com.amrut.prabhu
spec:
group: com.amrut.prabhu
names:
kind: my-crd
plural: my-crds
scope: Namespaced
versions:
- name: v1
served: true
storage: true

Understanding CRD Schema

schema:
openAPIV3Schema:
type: object
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
properties:
my-own-property:
type: string
type: object
  • apiVersion: To define the version of the CRD we will be using.
  • kind: The type of the CRD
  • metadata: The metadata which will be added such as the name, annotations, etc. This will be of the type Object.
  • spec: This defines the custom specifications properties you want to provide.

Adding Kubernetes CRD to the Kubernetes Cluster

kubectl apply -f my-crd.yaml
apiVersion: com.amrut.prabhu/v1
kind: my-crd
metadata:
name: my-custom-resource-instance
spec:
my-own-property: "My first CRD instance"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amrut Prabhu

Amrut Prabhu

Software Craftsman, Tech Enthusiast. I run https://refactorfirst.com to post all my articles