DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Auditing Tools for Kubernetes
  • Kubernetes Cluster Setup on Ubuntu, Explained
  • Manage Microservices With Docker Compose
  • Upgrading Kubernetes Clusters With Cluster API on Oracle Cloud

Trending

  • Event Driven Architecture (EDA) - Optimizer or Complicator
  • Scaling Microservices With Docker and Kubernetes on Production
  • Rust, WASM, and Edge: Next-Level Performance
  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Create Customer Resource Definitions in Kubernetes

How to Create Customer Resource Definitions in Kubernetes

Know the steps to create CRDs in Kubernetes.

By 
Aditya Bhuyan user avatar
Aditya Bhuyan
·
Aug. 19, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free

Kubernetes is a popular open-source platform used for automating the deployment, scaling, and management of containerized applications. It provides a powerful API for managing resources, but sometimes its built-in resources are not sufficient for your use case. That's where Kubernetes Custom Resource Definitions (CRDs) come in. CRDs allow you to define your own custom resources, which can be managed in the same way as built-in resources like pods and services.

In this tutorial, we'll go through the steps to implement a Kubernetes CRD.

Prerequisites

To follow along with this tutorial, you'll need:

  • A Kubernetes cluster with kubectl installed and configured.
  • The Kubernetes API Server running with RBAC (Role-Based Access Control) enabled.
  • Basic understanding of Kubernetes resource manifests and YAML.

Step 1: Define the CRD

First, we'll define the YAML file that describes our CRD. This file specifies the name, version, and schema of the custom resource. For example, let's create a CRD for a fictional application called "myapp" with a version of "v1beta1":

YAML
 
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: myapps.example.com
spec:
  group: example.com
  versions:
    - name: v1beta1
      served: true
      storage: true
  scope: Namespaced
  names:
    plural: myapps
    singular: myapp
    kind: Myapp

YAML to Create CRD Definition

In this YAML file:

  • apiVersion: The version of the Kubernetes API to use for this object.
  • kind: The kind of object this is (in this case, a CustomResourceDefinition).
  • metadata: Metadata for the object, including its name.
  • spec: The specification for the CRD.
    • group: The API group for the custom resource.
    • versions: A list of supported versions for the custom resource.
      • name: The name of the version.
      • served: Whether the version is served by the API server.
      • storage: Whether the version should be persisted.
    • scope: The scope of the custom resource (Cluster or Namespaced).
    • names: The names used to refer to the custom resource.
      • plural: The plural name of the resource.
      • singular: The singular name of the resource.
      • kind: The kind of the resource.
      • shortNames: A list of short names for the resource.

Save this YAML file as myapp-crd.yaml.

Step 2: Create the CRD

Next, we'll use kubectl to create the CRD in our Kubernetes cluster:

$ kubectl create -f myapp-crd.yaml 
Create CRD Definition

This will create the CRD myapps.example.com in the Kubernetes cluster.

Step 3: Define the Custom Resource

Now that we've defined the CRD, we can define the custom resource that will use this CRD. In this example, we'll create a YAML file that defines a custom resource for myapp:

YAML
 
piVersion: example.com/v1beta1 
kind: Myapp 
metadata: 
 name: myapp-sample 
spec: 
 replicas: 3 
 image: nginx:latest
YAML to create CRD

In this YAML file, we define the following:

  • apiVersion: The API group and version of the custom resource. Here, it's example.com/v1beta1.
  • kind: The kind of the custom resource. Here, it's Myapp.
  • metadata: Metadata associated with the custom resource. Here, we set the name to myapp-sample.
  • spec: The specification of the custom resource. Here, we specify the number of replicas and the image to use.

Save this YAML file as myapp-sample.yaml.

Step 4: Create the Custom Resource

Next, we'll use kubectl to create the custom resource in our Kubernetes cluster:

$ kubectl create -f myapp-sample.yaml

This will create the custom resource myapp-sample in the Kubernetes cluster.

Step 5: View the Custom Resource

To view the custom resource we just created, run the following command:

kubectl get crd
Command to check CRD

You should see the list of CRDs created in the cluster and validate that the resource you have created exists.

Conclusion

Kubernetes Custom Resource Definitions (CRDs) are a powerful feature that allows you to extend Kubernetes with your own custom resources. With CRDs, you can create your own Kubernetes API resources, which can be used just like any other native Kubernetes resource. With these simple steps, you can easily create CRDs in your cluster.

API Kubernetes YAML cluster

Published at DZone with permission of Aditya Bhuyan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Auditing Tools for Kubernetes
  • Kubernetes Cluster Setup on Ubuntu, Explained
  • Manage Microservices With Docker Compose
  • Upgrading Kubernetes Clusters With Cluster API on Oracle Cloud

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!