Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Customize how the resource CRD uses

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to use the custom resource CRD. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Introduction

Custom Resource Define, referred to as CRD, is a way for Kubernetes (v1.7 +) to allow developers to customize resources in order to improve scalability.

CRD resources can be dynamically registered with the cluster. After registration, users can create and access this custom resource object through kubectl, similar to operating Pod.

However, it is important to note that CRD is only the definition of resources, and a corresponding controller is needed to listen for various events of CRD to add custom business logic.

Define

If you only CRUD the CRD resource itself, it can be implemented without Controller, which means that only the data is stored in the etcd, and there is no related operation on the data.

For example, we can define a CRD resource manifest file as follows:

ApiVersion: apiextensions.k8s.io/v1kind: CustomResourceDefinitionmetadata: # name must match the following spec field:. Name: foos.crd.example.com # for more information on the below annotation, please see # https://github.com/kubernetes/enhancements/blob/master/keps/sig-api-machinery/2337-k8s.io-group-protection/README.md annotations: "api-approved.kubernetes.io": "unapproved, experimental-only" Please get an approval from Kubernetes API reviewers if you're trying to develop a CRD in the * .k8s.io or * .kubernetes.io groups "spec: # group name for definition in REST API: / apis// group: crd.example.com # lists all API versions of the custom resource versions:-name: v1 # version name, such as v1 V1beta1 served: true # whether to enable access to `/ apis///..` storage: true # through REST APIs must mark one and only one version as the storage version schema: # define the declaration specification for custom objects # schema used for validation openAPIV3Schema: type: object properties: spec: type: Object properties: deploymentName: type: string replicas: type: integer minimum: 1 maximum: 10 status: type: object properties: availableReplicas: type: Integer names: # kind is a definition of hump form of sigular The kind: Foo # plural name is used in the resource list for the definition in REST API: / apis/// plural: foos # singular name is an alias for CLI operation or display singular: foo # shortNames is equivalent to the abbreviated form shortNames:-fo scope: Namespaced

The definition of this place is similar to our definition of ordinary resource objects, we can define a custom resource object at will, but when creating resources, we are certainly not free to write YAML files, when we submit the above CRD file to Kubernetes, Kubernetes will verify the declaration file we submitted, from the definition we can see that CRD is based on OpenAPI v3 schem specification.

Of course, this kind of check is only a check for the type of the field, which is relatively elementary. If you want a more complex check, you need to use Kubernetes's admission webhook to achieve it. For more information about the use of validation, you can check out the official documentation.

Now we can use kubectl directly to create this CRD resource list:

$kubectl apply-f crd.example.com_foos.yaml customresourcedefinition.apiextensions.k8s.io/foos.crd.example.com created

At this point, we can see that the CRD resource object we defined already exists in the cluster:

$kubectl get crd | grep examplefoos.crd.example.com 2022-05-11T05:28:51Z

At this point, a new namespace-level RESTful API is created:

/ apis/crd/example.com/v1/namespaces/*/foos/...

We can then use this API endpoint to create and manage custom objects of the type foo in the CRD object specification created above.

Now that we have a new resource called foos.crd.example.com in the Kubernetes cluster, we can use it to define a Foo resource object. The fields that can be contained in this custom resource object are defined by schema. For example, let's create a resource list as follows:

ApiVersion: crd.example.com/v1kind: Foometadata: name: example-foospec: deploymentName: example-foo replicas: 1

After the creation is completed, we can use kubectl to manage the Foo object we created here, such as:

Kubectl get fooNAME AGEexample-foo 20m

When using kubectl, resource names are case-insensitive, and we can use singular or plural forms defined in CRD as well as any abbreviations.

Kubectl get foo example-foo-o yamlapiVersion: crd.example.com/v1kind: Foometadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion": "crd.example.com/v1", "kind": "Foo", "metadata": {"annotations": {}, "name": "example-foo", "namespace": "default"}, "spec": {"deploymentName": "example-foo" "replicas": 1}} creationTimestamp: "2022-05-11T05:40:38Z" generation: 1 name: example-foo namespace: default resourceVersion: "37605212" uid: 56d5b1d3-f6f9-4106-90c4-a0e3c7d130c0spec: deploymentName: example-foo replicas: 1

As we said above, now our custom resource creation is complete, but we simply store the resource list data in etcd, which is of no other use, because we do not define a corresponding controller to handle the relevant business logic.

These are all the contents of the article "how to use Custom Resource CRD". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report