This lesson covers developing custom controllers for Kubernetes, focusing on monitoring FlightTicket objects and integrating with a flight booking API.
In this lesson, we delve into developing custom controllers for Kubernetes. Building on our previous work, we have already defined a custom resource and created FlightTicket objects with data stored in etcd. The next step is to continuously monitor these objects and perform corresponding actions—such as calling a flight booking API to book, modify, or cancel flight tickets. This process is the core function of a custom controller.A controller is a process running in a loop that monitors the Kubernetes cluster and reacts to changes in specific objects (in this case, FlightTicket resources). Consider the following FlightTicket definition as a starting point:
Copy
Ask AI
apiVersion: flights.com/v1kind: FlightTicketmetadata: name: my-flight-ticketspec: from: Mumbai to: London number: 2
You can create the resource using:
Copy
Ask AI
kubectl create -f flightticket.yml
The command output confirms the creation:
Copy
Ask AI
flightticket "my-flight-ticket" created
To check the status of your FlightTicket, run:
Copy
Ask AI
kubectl get flightticket
Expected output:
Copy
Ask AI
NAME STATUSMy-flight-ticket Pending
While it is possible to write a controller in a language like Python, challenges such as managing expensive API calls, and building your own queuing and caching mechanisms might arise.
Developing the controller in Go using the Kubernetes Go client offers a more efficient approach. It provides libraries like shared informers that include built-in queuing and caching support.
To begin, clone the SampleController repository from GitHub. Ensure that the Go programming language is installed on your machine, then execute the following command in your terminal:
Cloning into 'sample-controller'...Resolving deltas: 100% (15787/15787), done.
Change into the cloned directory:
Copy
Ask AI
cd sample-controller
Customize the file controller.go with your specific business logic. One important function within your controller might involve making a call to the flight booking API after detecting changes in FlightTicket objects.Once you’ve incorporated your custom logic, build the controller with the command:
Copy
Ask AI
go build -o sample-controller .
During the build process, you may encounter messages such as:
Your controller code might include sections like the following:
Copy
Ask AI
package flightticketvar controllerKind = apps.SchemeGroupVersion.WithKind("Flightticket")// Code hidden for brevity// Run begins watching and syncing.func (dc *FlightTicketController) Run(workers int, stopCh <-chan struct{}) {}// Code hidden for brevity// callBookFlightAPI triggers the flight booking process.func (dc *FlightTicketController) callBookFlightAPI(obj interface{}) {}
The controller leverages the specified kubeconfig file to authenticate with the Kubernetes API server. It watches for changes to FlightTicket objects and, upon detecting a creation or modification, it executes your custom logic—such as interfacing with the flight booking API—to reconcile the desired state.
Ensure your API calls are handled efficiently to avoid timeouts and performance issues in production environments.
Once your custom controller is fully verified and functional, you may want to package it as a Docker image and deploy it within your Kubernetes cluster as a pod or Deployment. This approach streamlines updates by avoiding the need for manual rebuilding and restarting of the controller with every change.
This lesson provided a comprehensive overview of building a custom controller in Kubernetes. It discussed:
How to define custom resources for FlightTickets.
Monitoring and reacting to changes using controllers.
Building the controller in Go with the built-in queuing and caching support provided by the Kubernetes Go client.
Deploying your controller in a production environment.
Understanding how Custom Resource Definitions (CRDs) and controllers work together is fundamental, especially since exam questions may cover creating CRDs, managing CRD files, or interacting with existing controllers. Operators, which extend controller functionality further, are another exciting area to explore.For more details on Kubernetes concepts, check out these resources: