21. Custom Resource Definitions (CRDs) in Kubernetes

"Hello, I'm Kiran Pawar, a passionate Cloud and Devops Engineer with a strong background in cloud automation, configuration, and deployment. My journey in the world of technology has been a thrilling adventure, where I've had the privilege to work with cutting-edge tools and practices.
🚀 As a DevOps Engineer:
I specialize in automating, configuring, and deploying instances in cloud environments and data centers. My expertise extends to DevOps, GitOps, CI/CD pipeline management, HashiCorp Terraform, and containerization. I'm proficient in AWS and Linux/Unix administration, ensuring robust infrastructure and application performance.
🔧 My Tech Stack:
Front-end skills: HTML, CSS, SCSS, Tailwind CSS, Bootstrap, React, Material-UI, JavaScript DevOps toolbox: GIT, OWASP,Nexus,Trivy, Github, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus, Grafana, Argo CD, AWS EKS.
🌐 My Cloud Expertise:
I have hands-on experience managing AWS services, including EC2, S3, EBS, VPC, ELB, RDS, IAM, Route53, and more.
🔒 Networking and Security:
My skills include managing networking concepts such as TCP/IP protocols, security policies, and subnet interfacing. I have a strong understanding of infrastructure and networking, covering topics like firewalls, IP addressing, DNS, and more.
💡 What Sets Me Apart:
I bring a positive attitude, a strong work ethic, and a collaborative spirit to every project. I'm a self-starter, a fast learner, and an effective team player with strong interpersonal skills. In addition to my DevOps skills, I've developed shell scripts (Bash) for automating tasks and have proficiency in Python scripting. My ability to communicate and manage projects, along with a track record of resolving client issues, adds value to every team I work with. If you're looking for a DevOps engineer who is also well-versed in front-end technologies, feel free to connect with me. Let's explore new possibilities and create exceptional technical solutions together!"
Custom Resource Definitions (CRDs) extend Kubernetes' functionality by allowing you to define your own custom resources. With CRDs, you can manage application-specific configurations as first-class Kubernetes objects, seamlessly integrating them into the Kubernetes ecosystem.
What Are CRDs?
CRDs are part of Kubernetes' Custom Resource and Custom Controller architecture. A Custom Resource (CR) is a user-defined Kubernetes API object, while a Custom Controller handles the lifecycle of these resources. Together, they enable you to automate application-specific logic.
Why Use CRDs?
Flexibility: Define custom APIs for your applications without modifying Kubernetes itself.
Automation: Pair CRDs with custom controllers to manage resources programmatically.
Scalability: Use Kubernetes' native mechanisms (e.g.,
kubectl, RBAC) with your custom resources.
Creating and Using CRDs
1. Define a CRD
A CRD is defined as a Kubernetes resource in a YAML manifest. Here’s an example of a CustomResourceDefinition for managing databases:
database-crd.yml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: databases.example.com
spec:
group: example.com
names:
kind: Database
listKind: DatabaseList
plural: databases
singular: database
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
databaseName:
type: string
size:
type: string
Apply the CRD:
kubectl apply -f database-crd.yml

2. Create Custom Resources (CRs)
Once the CRD is registered, you can create instances of the Database resource.
my-database.yml
apiVersion: example.com/v1
kind: Database
metadata:
name: my-database
spec:
databaseName: "test-db"
size: "20Gi"
Apply the custom resource:
kubectl apply -f my-database.yml

3. View and Manage CRs
To view the custom resource:
kubectl get databases

To describe a specific custom resource:
kubectl describe database my-database

To delete a custom resource:
kubectl delete database my-database

Using CRDs with Controllers
To fully automate custom resources, pair the CRD with a controller. Controllers can be implemented using:
Kubernetes Operators: Advanced controllers for complex applications.
Frameworks: Tools like Kubebuilder or Operator SDK simplify controller development.
Best Practices for CRDs
Validation: Use OpenAPI schema validation to enforce rules on CRs.
Versioning: Define multiple versions of your CRD for backward compatibility.
Namespaces: Use Namespaced scope for most CRDs to avoid conflicts.
Conclusion
Custom Resource Definitions empower you to extend Kubernetes to suit your application’s unique requirements. By combining CRDs with controllers, you can automate complex workflows and integrate application-specific logic into your Kubernetes environment.
