CRD is just a table in Kubernetes

Tried the simplest explanation of CRD ever.

Roy Hiroyuki OSAKI
ITNEXT

--

CRD sounds difficult.. For what? And How to create?

CRD, Custom Resource Definition is a special resource in Kubernetes. When you use Kubernetes in regular way, you don’t have to create that kind of resource. So this is not so important for many users. But sometimes, CRD appears in cutting edge blog posts, documents in kubernetes.io and discussion. Then many users are upset and have to take some time to understand it, such like me!

What is that? When do we use it? Once we need it, how do we create it?

No worry. I already went through the path. I would like to try an explanation about CRD as simple as I can.

For WHAT?

CRD is just a table of database.
For example, we can create a table named “Fruit” in database. This table can contain many records like “apple”, “banana” and “orange”. These records have columns like “sweetness”, “flavor” and “weight”, which show features of the fruits.

CRD is like a table “Fruit”.

CR (Custom Resource) is each record like “apple”.

After you create CRD (table), you can add or delete CRs (records).

Why CRD is needed? It is because more and more users got used to Kubernetes, knew the usefulness of Kubernetes, and they want to use it more widely. They want to input more data into Kubernetes for their own usage. Data format is different each other. These are not defined in Kubernetes out of the box. So they have to create kind of table in Kubernetes and they have to set their own column name or type to define the table like in database. This is the moment the CRD comes alive.

For example, IT manager wants to organize user list with CRD. In other cases, they want to organize a department list, planned schedule and so on. They might take care of fruits list but I guess it is rare.

HOW can we create CRD?

As mentioned, CRD is a table. When we create a table, we need to define the table’s format like column names and types. These elements are described in CRD file in YAML or JSON format.

Also CR describes each value for a record in YAML or JSON format.

Here is the details of CRD inside.

CRD format is divided into three parts. (But I don’t remember every details all the time. I look into the official doc every time I need.)

  • General part
    The same as other Kubernetes resource. Metadata includes CRD’s name itself (name: “fruit-crd” etc). apiVersion and kind are also required.
  • Table level info
    Table name (kind: “Fruit”), easy lower case name for command lines (simpler: “fruit”), plural form (plural: “fruits”)
  • Column level info
    Column name (“sweetness”), column type (“boolean”, “string”, “integer”, “object”), nested object (props: <child object name and columns>). These follows the format of OpenAPISpecification version 3.
  • More details is in the following official document.
    https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#create-a-customresourcedefinition

Check the CRD functionality

Is the CRD really a table? Let’s make sure that.

I cannot remember the format of CRD. So I would like to introduce simpler way to create CRD. Even though I don’t write CRD, I only write a “Table” definition then I have an Operator (automatic CRD creation tool) generate CRD.

Table is very simple. columns only includes pairs of column name and type.

Simpler definition “Table” example

After installing this Table into Kubernetes, my Operator generates the following CRD automatically. Of course, you don’t need this Operator. You can generate it by yourself. If you want to install my Operator, please run this command.

kubectl apply -f https://raw.githubusercontent.com/onelittlenightmusic/k8sasdb/master/install.yaml

After the sucessful installation of the Operator, check the pod is running with this command.

kubectl get pod -n k8sasdb-system
NAME READY STATUS RESTARTS AGE
k8sasdb-controller-manager-9dbf54b4f-hzrt9 2/2 Running 0 8s

You can download sample Table and each CR files from here.

Generated CRD

CRD (left: general part and table level info, right: column level info)

Then I added the following CR (YAML for one fruit).

CR “Fruit” example

Let’s check the results of table and record operations.

Create a table

kubectl create -f fruit.yaml

→ Success (CRD was installed = a table was created)

This is equivalent with SQL CREATE TABLE fruits;

Create a record

kubectl create -f apple.yaml

→ Success (CR was installed = a record was created)

Just like INSERT INTO fruits values('apple', ...);

Get list of records

kubectl get fruits

→ Success (2 CRs were listed and every columns were shown)

If you are familiar to database, does this result look like a result of SQL querySELECT * FROM fruits; ?

Get a record

kubectl get fruit apple

→ Success. Of course, other names like “banana” applies too. This is equivalent to SELECT * FROM fruits WHERE name = 'apple';

Delete a record

kubectl delete fruit apple

Check command of whole fruit list shows no result of “apple” after deletion.

→ Success. This means DELETE FROM fruits WHERE name = 'banana';

Summary

CRD is just a table in Kubernetes.

You can create new tables with CRDs.

You can add records with CRs.

You can define table schema (column name, type etc) in CRDs.

When you master CRD, CR and kubectl command, you can input every kind of data into Kubernetes.

Don’t need to be afraid. Even if you don’t remember CRD format, some generator like Operator will help you to manage CRDs. (My operator implementation is open source. Published here. If you have any, please give feedback.) Thank you!

Photo by Aline de Nadai on Unsplash

--

--

Writer for

@Hiroyuki_OSAKI Sr. research engineer (Hitachi America/Hitachi), CKA/CKAD/CKS, 大崎裕之(Japan) The opinions expressed here are my own, not those of Company.