CustomResourceDefinition
CustomResourceDefinition (CRD) is an ingenious mechanism introduced in v1.7 that allows you to extend the Kubernetes API without tinkering with code to manage custom objects. Practically speaking, it's an upgraded version of ThirdPartyResources (TPR), which was deprecated in v1.8.
API Version Comparison Table
v1.8+
apiextensions.k8s.io/v1beta1
CRD Example
The example below crafts a custom API: /apis/stable.example.com/v1/namespaces/<namespace>/crontabs/….
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: <plural>.<group>
name: crontabs.stable.example.com
spec:
# group name to use for REST API: /apis/<group>/<version>
group: stable.example.com
# versions to use for REST API: /apis/<group>/<version>
versions:
- name: v1beta1
# Each version can be enabled/disabled by the Served flag.
served: true
# One and only one version must be marked as the storage version.
storage: true
- name: v1
served: true
storage: false
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
plural: crontabs
# singular name to be used as an alias on the CLI and for display
singular: crontab
# kind is normally the CamelCased singular type. Your resource manifests use this.
kind: CronTab
# shortNames allow a shorter string to match your resource on the CLI
shortNames:
- ctOnce the API is set up, you can proceed to create specific CronTab objects.
Finalizer
Finalizers are used to implement asynchronous pre-deletion hooks for controllers, which can be specified via metadata.finalizers.
Once the finalizer is indicated, the operation to delete an object by a client will merely set metadata.deletionTimestamp instead of performing a direct deletion. This triggers controllers that are listening to the CRD to perform cleanup operations before deletion, remove its own finalizer from the list, and then initiate a new deletion operation. Only then is the object to be deleted truly eliminated.
Validation
Starting from v1.8, an experimental validation mechanism based on OpenAPI v3 schema was added to validate the conformity of resources submitted by users in advance. To use this function, you need to configure the kube-apiserver's --feature-gates=CustomResourceValidation=true.
For instance, the CRD below necessitates:
spec.cronSpecto be a string that matches a regular expressionspec.replicasto be an integer between 1 and 10
For example, when creating the following CronTab:
You'll encounter a validation failure error:
Subresources
From v1.10 onwards, CRD also supports two subresources /status and /scale in the beta version, and they are enabled by default from v1.11.
To use in v1.10, you need to enable
--feature-gates=CustomResourceSubresources=trueon thekube-apiserver.
Categories
Categories are used to group CRD objects, making it possible to query all objects belonging to that group with kubectl get <category-name>.
CRD Controllers
When extending the Kubernetes API using CRD, it's generally also necessary to implement a controller for the new resources to monitor their changes and make further processing.
https://github.com/kubernetes/sample-controller provides an example of a CRD controller, including
How to register
FooresourcesHow to create, delete, and query
FooobjectsHow to monitor changes of
Fooresources
Kubebuilder
As demonstrated above, building a CRD controller from scratch is quite challenging considering the level of understanding required for Kubernetes's API. Integrating RBAC, constructing images, and continuous integration and deployment all demand a large amount of work.
kubebuilder exists to solve this issue, providing an easy-to-use framework for creating CRD controllers and directly generating the resource files needed for image building, continuous integration, and deployment.
Installation
How to Use
Initialize the Project
Create API
Then, depending on your actual needs, modify pkg/apis/ship/v1beta1/sloop_types.go and pkg/controller/sloop/sloop_controller.go to add business logic.
Run Local Test
If you run into the error
ValidationError(CustomResourceDefinition.status): missing required field "storedVersions" in io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.CustomResourceDefinitionStatus], manually modifyconfig/crds/ships_v1beta1_sloop.yaml:```yaml status: acceptedNames: kind: "" plural: "" conditions: [] storedVersions: []
Then run
kubectl apply -f config/crdsto create the CRD.
You can then create resources with Kind as Sloop using ships.k8s.io/v1beta1, such as
Build Image and Deploy Controller
kustomize no longer supports wildcards, so the above
make deploymay encounter aLoad from path ../rbac/*.yaml failederror. The solution is to manually modifyconfig/default/kustomization.yaml:resources:
../rbac/rbac_role.yaml
../rbac/rbac_role_binding.yaml
../manager/manager.yaml
Then execute
kustomize build config/default | kubectl apply -f -to deploy. By default, it's deployed to thedemo-systemnamespace.
Documentation and Testing
References
最后更新于