Understanding Standard Kubernetes Resources and Controllers
Kubernetes relies on built-in controllers to manage standard resources. For instance, when you create a Deployment, Kubernetes stores the desired state in its etcd data store and automatically manages related ReplicaSets and Pods. The deployment controller continuously monitors the Deployment and ensures that the cluster state matches the desired configuration. Creating a Deployment with three replicas will result in three Pods being deployed. Below is an example YAML file defining a Deployment:deployment.yml, run the following commands to create, view, and delete the Deployment:
Custom Resources and Controllers: The Flight Ticket Example
Building on the standard resource management, you can extend Kubernetes by defining custom resources. Imagine a scenario where you want to manage flight ticket bookings directly in Kubernetes. With a custom resource called FlightTicket, you can create objects representing flight ticket bookings, list them, and delete them as needed.FlightTicket Object Definition
Below is an example YAML file that defines a FlightTicket object:Custom Controller for FlightTicket
A custom controller, typically written in Go, monitors FlightTicket objects. When a FlightTicket is created, updated, or deleted, the controller calls an external API to perform actions such as booking or canceling a flight. Below is a streamlined Go snippet to illustrate the controller’s logic:Without this custom controller, FlightTicket objects remain static data in etcd, and no automated flight booking actions are performed.
Handling Resource Creation Errors
If you create a FlightTicket object before Kubernetes is aware of its type, you will encounter an error similar to:Defining a Custom Resource with a CRD
A Custom Resource Definition (CRD) informs Kubernetes about a new resource type, detailing its metadata, scope (namespaced or cluster-scoped), API group, naming conventions (singular, plural, and short names), supported versions, and a validation schema using OpenAPI v3. Below is a sample CRD for the FlightTicket resource:flightticket-custom-definition.yml and apply it to your Kubernetes cluster using:
Summary
In this guide, we: • Explored how standard Kubernetes resources, such as Deployments, are managed using built-in controllers.• Demonstrated the creation, listing, and deletion of a Deployment using a YAML file and kubectl commands.
• Introduced custom resources with a FlightTicket example, emphasizing the need for a Custom Resource Definition (CRD) and a custom controller to automate actions. Future articles will cover the implementation of a custom controller to automatically process FlightTicket events and integrate with external APIs. For more details on Kubernetes resources and controllers, check out the Kubernetes Documentation.