0Pricing
Docker & Kubernetes for Developers · Lektion

Benutzerdefinierte Ressourcendefinitionen (CRDs)

Erweitern Sie die Funktionalität von Kubernetes, indem Sie eigene benutzerdefinierte Ressourcen zur Verwaltung domänenspezifischer Objekte definieren.

Benutzerdefinierte Ressourcendefinitionen (CRDs) ist eine kostenlose Docker & Kubernetes for Developers-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Docker & Kubernetes for Developers-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Docker & Kubernetes for Developers-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 is Namespaced (like Pods) or Cluster (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 in kubectl 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:
      - ws

Applying 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.yaml

Creating 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: 3

Interacting 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 all WebServer objects.
  • kubectl get ws: Use the short name.
  • kubectl describe webserver my-first-webserver: Get detailed information about a specific WebServer instance.

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!

Häufig gestellte Fragen

Ist die Lektion „Benutzerdefinierte Ressourcendefinitionen (CRDs)“ kostenlos?

Ja — der vollständige Text von „Benutzerdefinierte Ressourcendefinitionen (CRDs)“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Docker & Kubernetes for Developers-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Docker & Kubernetes for Developers-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Benutzerdefinierte Ressourcendefinitionen (CRDs)“?

Erweitern Sie die Funktionalität von Kubernetes, indem Sie eigene benutzerdefinierte Ressourcen zur Verwaltung domänenspezifischer Objekte definieren. Du übst Docker & Kubernetes for Developers mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Docker & Kubernetes for Developers zu starten?

Keine Vorkenntnisse erforderlich. Docker & Kubernetes for Developers auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Benutzerdefinierte Ressourcendefinitionen (CRDs)“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Docker & Kubernetes for Developers-Lektion Code schreiben und ausführen?

Ja. Jede Docker & Kubernetes for Developers-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Benutzerdefinierte Ressourcendefinitionen (CRDs)
  2. Das Operator-Pattern in Kubernetes
  3. Serverless mit Kubernetes (Knative)
  4. Den API-Server mit Admission-Webhooks erweitern
← Zurück zu Docker & Kubernetes for Developers