AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Automating SSL/TLS Certificate Rotation in AKS

By Luca Berton · Published 2024-01-01 · Category: containers-kubernetes

Learn how to automate SSL/TLS certificate rotation in Azure Kubernetes Service (AKS) using Cert-Manager and custom scripts for seamless security.

Automating SSL/TLS Certificate Rotation in AKS

Introduction

A Custom Resource Definition (CRD) in Kubernetes allows you to extend the Kubernetes API by defining your own custom resources. These custom resources can represent any kind of domain-specific entity, and you can manage them using standard Kubernetes tools like kubectl.

Overview of Custom Resource Definitions (CRDs)

Custom Resources: These are extensions of the Kubernetes API. They allow you to create your own custom resource types that can be managed like built-in resources (e.g., Pods, Services). • Custom Resource Definitions (CRDs): These are used to define the schema and behavior of custom resources. Once a CRD is created, you can create instances of the custom resource it defines.

Steps to Create and Use a Custom Resource Definition

Step 1: Define the Custom Resource Definition (CRD)

The CRD defines the structure and behavior of your custom resource. Here is an example of a simple CRD for a custom resource called MyApp.

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: myapps.example.com
spec:
  group: example.com
  versions:
  - name: v1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              replicas:
                type: integer
              image:
                type: string
              port:
                type: integer
    subresources:
      status: {}
  scope: Namespaced
  names:
    plural: myapps
    singular: myapp
    kind: MyApp
    shortNames:
    - ma

Explanation:

apiVersion: The API version for CRDs is apiextensions.k8s.io/v1. • kind: This is CustomResourceDefinition since you are defining a new custom resource. • metadata.name: The name of the CRD should be in the form of plural.group. In this case, it's myapps.example.com. • spec.group: The API group your custom resource belongs to. In this case, it's example.com. • spec.versions: The different versions of your custom resource. Each version can have its own schema. • spec.scope: Specifies whether the custom resource is namespaced or cluster-scoped. In this case, it's Namespaced. • spec.names: Specifies the names used for the custom resource: • plural: The plural name (myapps). • singular: The singular name (myapp). • kind: The kind of the resource (MyApp). • shortNames: Optional shorthand names for the resource.

Step 2: Apply the CRD to Your Kubernetes Cluster

To create the CRD in your cluster, apply the YAML file using kubectl:

kubectl apply -f myapp-crd.yaml

This command registers the MyApp custom resource with your Kubernetes API server.

Step 3: Create Instances of Your Custom Resource

After the CRD is applied, you can create instances of your custom resource. Here’s an example:

apiVersion: example.com/v1
kind: MyApp
metadata:
  name: myapp-sample
spec:
  replicas: 3
  image: nginx:latest
  port: 80

This YAML file creates an instance of the MyApp custom resource with the specified spec fields.

Apply this file using kubectl:

kubectl apply -f myapp-instance.yaml

Step 4: Interact with Your Custom Resource

You can interact with your custom resource using kubectl just like any other Kubernetes resource.

# List all instances of MyApp
kubectl get myapps

# Get details of a specific MyApp instance kubectl get myapp myapp-sample -o yaml

# Delete a MyApp instance kubectl delete myapp myapp-sample

Advanced Features

CRDs support various advanced features, including: • Validation: You can define schemas to enforce validation rules on your custom resources. • Subresources: You can add status subresources for tracking status information or scale subresources for managing replica counts. • Conversion Webhooks: These allow you to implement custom conversion logic between different versions of your custom resource.

Conclusion

Custom Resource Definitions (CRDs) are a powerful way to extend Kubernetes with domain-specific abstractions, making it possible to manage custom application-specific resources as first-class citizens in your Kubernetes environment. By defining a CRD, you gain the flexibility to tailor Kubernetes to fit the unique needs of your applications.

See also: Essential DNS Tools for Kubernetes CoreDNS and ExternalDNS

Related Articles

Nginx vhost provisioning with AnsibleAnsible Cron Module Guide

Category: containers-kubernetes

Browse all Ansible tutorials · AnsiblePilot Home