In this post, we are going to talk about Custom Resource Definitions (CRDs). We will go over:
To follow along, I assume you have prior knowledge of Kubernetes, kubectl, and have minikube running.
(This article is part of our Kubernetes Guide. Use the right-hand menu to navigate.)
To begin to understand what CRD is, we must go over a couple of concepts in Kubernetes:
Custom Resource allows you to extend Kubernetes capabilities by adding any kind of API object useful for your application. Custom Resource Definition is what you use to define a Custom Resource. This is a powerful way to extend Kubernetes capabilities beyond the default installation.
The manifest below shows an example CRD crd.yaml
apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: name: appconfigs.stable.example.com spec: group: stable.example.com versions: - name: v1 served: true storage: true scope: Namespaced names: plural: appconfigs singular: appconfig kind: AppConfig shortNames: - ac
Let’s explain what the CRD above will create:
So we are probably wondering ok this is good but how is this useful? To use, the CRD above, we create a manifest using the kind we created with the CRD. For example we have my-kind.yaml
apiVersion: "stable.example.com/v1" kind: AppConfig metadata: name: demo-appconfig spec: uri: "some uri" Command: "some command" image: my-image
As you can see, we are using the kind we defined in our CRD, then we defined the fields we want our kind object to have. Here we want to define, the uri, command and image for our app. Now if we run “kubectl create -f my-kind.yaml” our application can consume the data we created with the manifest above. To see what is going on we can run “kubectl get ac -o yaml”, we can see detailed info on what we just created. Notice how we are using the short name (ac) we defined in our CRD.
To delete the CRD and resources we created, simply run kubectl delete just like with any other resources. It is important to know that the above CRD is just data which can be stored and retrieved therefore, it doesn’t give us a fully declarative API. The way to make this resource fully declarative is to add a custom controller, whose job is to make sure that the current state and the desired state are always in sync. An example is something like a replication controller.
CRD is a way to extend kubernetes allowing us to create a custom resource of our choice and making it declarative with the help of a custom controller.
For more on Kubernetes, explore these resources: