A simple explanation of CRD in Kubernetes and how to use it

Space by maryanion



CRD (Custom Resource Definition) is a special resource in Kubernetes that allows you to enter any data. The Kubernetes aaS team from Mail.ru has translated an article on what it is, how to create and use it.



What is CRD



CRD is just a table in the database. Let's say we have created a table "Fruits", there are many records in it: "apple", "banana", "orange". Each record has columns with fruit characteristics such as sweetness, flavor, and weight.



So, CRD is similar to the "Fruit" table.







CR (Custom resource) - a separate record in the table, for example, "apple" or "banana". After creating a CRD (table), you can add or remove CR (records).







CRD is needed as more and more users want to contribute data to Kubernetes. The data format can be different, and CRDs are not defined in Kubernetes out of the box. As a result, you need to create your own tables, setting the names and types of columns, as in a database.



For example, an IT manager wants to use a CRD to organize a list of users, a list of departments, a work schedule, or something else.



How to create a CRD



As mentioned, CRD is a table. When we create a table, we need to set its format: column names and types. In CRD, these elements are described in YAML or JSON format. CR also describes each value to be written in YAML or JSON format.







Let's see what the CRD looks like from the inside:







CRD is divided into three parts: general part, information at the table level and information at the column level:



  1. A common part. As with other Kubernetes resources, metadata includes the name CRD ( name: "fruit-crd"), apiVersionand kind.
  2. Table-level information. Table name ( kind: "fruit"), command line simple lowercase name ( simpler: "fruits"), plural ( plural: "fruits")
  3. Column level information. Column name ( "sweetness"), column type ( "boolean", "string", "integer", "object"), nested objects ( : < >). They conform to the OpenAPISpecification version 3 format.


More information about CRD is in the official manual .



CRD check



Is CRD really just a table? Let's check.



I don't want to remember the CRD format and I need an easier way to create it. So I am only writing a table definition and I have a statement - an automatic CRD generation tool.



The description is very simple: you need to list the pairs of column names and types.







After applying this YML, my operator in Kubernetes automatically generates the corresponding CRD. You can create your own operator, but if you want to try mine, run the following command:



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


After successfully installing the operator, check that the Pod has started:



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 tables and CR records from here .



This is what the generated CRD looks like. Left: general information and table level, right: column level information.







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





Example CR "Fruit"



Now let's check the results in the table and the operations on the records.



Creating a table



kubectl create -f fruit.yaml






Success: CRD installed = table created.







This is the equivalent of an SQL command CREATE TABLE fruits;.



Create a record



kubectl create -f apple.yaml






Success: CR set = record created. This is the equivalent of an SQL command INSERT INTO fruits values('apple', ...);.



Getting a list of records



kubectl get fruits






Success: Displays two CRs and all columns for them. Equivalent to SQL command SELECT * FROM fruits;.



Receiving a record



kubectl get fruit apple






Successfully. Of course, other names can be used, such as "banana". The command is equivalent to a SQL query SELECT * FROM fruits WHERE name = 'apple';.



Delete entry



kubectl delete fruit apple






After deleting, the command to check the entire list of fruits does not show the result "apple".







Successfully. Analog in the SQL: DELETE FROM fruits WHERE name = 'banana';.



Conclusion



CRD is just a table in Kubernetes. You can create new tables with CRD and add records with CR. Also in CRD, you can define the table schema by defining column names and types. Once you have mastered the CRD, CR, and kubectl commands, you can upload any data to Kubernetes.



There is no need to be afraid: even if you do not remember the CRD format, the operator will help you. My open source version of the operator is posted here .



What else to read:



  1. How to use kubectl more efficiently: a comprehensive guide .
  2. Kubernetes: .
  3. Kubernetes .



All Articles