Custom Resource Definition
Custom Resource Definition is the feature of kubernetes where it allows us to create custom resources which do not exist in kubernetes. Every resource is in kubernetes such as pods, deployments, replicasets etc are backed by their controllers. These controllers are responsible for managing these resources.
For instance, deployment controller is responsible to create and manage pods replicas. These resources get added to the etcd database, but their behavior is controlled by their controllers.
So, creating a custom resource requires two parts:
- Register the custom resource to kubernetes using
CustomResourceDefinition. - Creating controller to manage that resource.
CustomResourceDefinition
Creating a custom resource requires resource yaml file as shown below.
person-crd.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: persons.people.com
spec:
group: people.com
scope: Namespaced
names:
kind: Person
singular: person
plural: persons
shortNames:
- pp
versions:
- name: v1
served: true
storage: true
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
name:
type: string
age:
type: integerwhere
spec.groupis api groupspec.scopedefines this custom resource is namespacedspec.namesdefines the names this resource can be searched with such ask get personork get personsor using short namek get pp.spec.versionslist all the available versions out of which only one can bestorageequal totruerefer note.servedtells if the version can be used.spec.versions[].openAPIV3Schemadefines thespecstructure of custom resource.
Create this custom resource definition using command,
k create -f person-crd.yaml
Creating custom resource
Once the custom resource definition is created using persons-crd.yaml resource file, we can create custom resource of kind Person.
apiVersion: people.com/v1
kind: Person
metadata:
name: nitin
namespace: learning
spec:
name: nitin sharma
age: 28It is to be noted that this will only create the resource in the etcd server, and that’s it. There is no controller defined to handle this custom resource.