自定义资源定义(CRD)
通过定义自己的自定义资源来扩展 Kubernetes 功能,以管理特定领域的对象。
自定义资源定义(CRD) 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Extend Kubernetes API
Welcome! In this lesson, we'll explore Custom Resource Definitions (CRDs). CRDs allow you to extend Kubernetes by defining your own custom resource types.
Think of it as teaching Kubernetes new words and concepts, so it can manage application-specific objects just like it manages built-in ones like Pods or Deployments.
Why Custom Resources?
Kubernetes provides powerful primitives like Pods, Deployments, and Services. But what if your application needs to manage a unique kind of object, like a database cluster, a serverless function, or a custom build pipeline?
- Domain-Specific Objects: Define resources tailored to your application's unique needs.
- Abstraction: Simplify complex application deployments into a single, manageable resource.
- Kubernetes Native: Leverage Kubernetes's declarative API, tooling (
kubectl), and watch mechanisms for your custom objects.
Core vs. Custom Resources
Kubernetes comes with many built-in or 'core' resources you already know:
- Core Resources: Pod, Deployment, Service, ConfigMap. These are part of the standard Kubernetes API.
- Custom Resources: These are resources you define using CRDs. They behave just like core resources but represent your own application-specific concepts.
Once a CRD is created, you can create and manage instances of your custom resource using kubectl, just like any other Kubernetes object.
Anatomy of a CRD: Basics
A CRD is itself a Kubernetes resource defined in YAML. Here's what some key fields mean:
apiVersion: apiextensions.k8s.io/v1: Specifies the API version for the CRD itself.kind: CustomResourceDefinition: Identifies this YAML as a CRD definition.metadata.name: The unique name for your CRD, typically in the format<plural>.<group>.
Let's look at defining the core specification next.
CRD Anatomy: Group & Versions
The spec section of a CRD defines the custom resource's properties:
spec.group: The API group for your custom resource (e.g.,example.com). This helps organize your custom resources.spec.versions: A list of supported versions for your custom resource (e.g.,v1alpha1,v1). Each version can have its own schema.spec.scope: Defines if your custom resource isNamespaced(like Pods) orCluster(like Nodes).
CRD Anatomy: Names & Schema
More important fields in the spec:
spec.names: Defines how your custom resource will be referred to:plural: The plural name used inkubectl get(e.g.,webservers).singular: The singular name (e.g.,webserver).kind: The CamelCase name for the resource object (e.g.,WebServer).shortNames: Optional, short aliases (e.g.,ws).
spec.versions[].schema.openAPIV3Schema: This is crucial! It defines the structure and validation rules for your custom resource's data.
Defining a Simple WebServer CRD
Here's a basic CRD for a WebServer resource. Notice how it defines the group, versions, names, and a simple schema for its properties (like image and replicas).
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: webservers.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
image:
type: string
replicas:
type: integer
required:
- image
scope: Namespaced
names:
plural: webservers
singular: webserver
kind: WebServer
shortNames:
- wsApplying Your CRD
Once you have your CRD YAML file (e.g., webserver-crd.yaml), you can apply it to your Kubernetes cluster using kubectl. This registers your new resource type with Kubernetes.
It might take a few seconds for the API server to fully acknowledge the new type.
kubectl apply -f webserver-crd.yamlCreating a Custom Resource
After the CRD is applied, you can create instances of your WebServer custom resource. This YAML defines a specific WebServer object with its own image and replicas values, conforming to the schema you defined.
You can manage this object just like any other Kubernetes resource.
apiVersion: example.com/v1
kind: WebServer
metadata:
name: my-first-webserver
spec:
image: nginx:latest
replicas: 3Interacting with Custom Resources
Now that you've defined a CRD and created an instance, you can use standard kubectl commands to interact with your custom resources:
kubectl get webservers: List allWebServerobjects.kubectl get ws: Use the short name.kubectl describe webserver my-first-webserver: Get detailed information about a specificWebServerinstance.
This seamless integration is a key benefit of CRDs!
Check Your CRD Knowledge
Which of the following are benefits of using Custom Resource Definitions (CRDs) in Kubernetes?
Recap: Extending Kubernetes
Great job! You've learned about Custom Resource Definitions!
- CRDs allow you to extend the Kubernetes API with your own custom resource types.
- They enable Kubernetes to manage domain-specific objects using its native API and tooling.
- A CRD defines the schema and properties of your custom resource.
- Once defined, you can create and interact with instances of your custom resource using
kubectl.
Next, we'll see how Operators leverage CRDs to automate complex application management!
常见问题解答
「自定义资源定义(CRD)」课时是免费的吗?
是的 — 「自定义资源定义(CRD)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。
「自定义资源定义(CRD)」这节课中我会学到什么?
通过定义自己的自定义资源来扩展 Kubernetes 功能,以管理特定领域的对象。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Docker & Kubernetes for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「自定义资源定义(CRD)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?
能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。