Creating and Deploying the Kubernetes Operator

A client wants to automate the operations of the weather report collection. They are currently connecting to third-party data providers and retrieving the results. In addition, they want to use cloud-native Kubernetes solutions in their clusters.
We are expected to automate the operations of weather report data collection by implementing a Kubernetes operator.
We'll create a Kubernetes operator by using the Operator Framework SDK and utilize it by creating a custom resource, custom controller logic, and finally, deploying into the cluster. Let's begin by implementing the following steps:

  1. Create the operator project using the Operator Framework SDK tools with the following command:
operator-sdk new k8s-operator-example --api-version=k8s.
packt.com/v1 --kind=WeatherReport

This command creates a completely new Kubernetes operator project with the name k8s-operator-example and watches for the changes of the WeatherReport custom resource, which is defined under k8s.packt.com/v1. The generated operator project is available under the k8s-operator-example folder.

  1. A custom resource definition has already been generated in the deploy/crd.yaml file. However, the specification of the custom resource is left empty so that it can be filled by the developer. Specifications and statuses of the custom resources are coded in Go, as shown in pkg/apis/k8s/v1/types.go:
type WeatherReport struct {
metav1.TypeMeta 'json:",inline"'
metav1.ObjectMeta 'json:"metadata"'
Spec WeatherReportSpec
'json:"spec"'
Status WeatherReportStatus
'json:"status,omitempty"'
}
type WeatherReportSpec struct {
City string 'json:"city"'
Days int 'json:"days"'
}

You can refer to the complete code at: https://goo.gl/PSyf25.

In the preceding code snippet, WeatherReport consists of metadata, spec, and status, just like any built-in Kubernetes resource. WeatherReportSpec includes the configuration, which is City and Days in our example.WeatherReportStatus includes State and Pod to keep track of the status and the created pod for the weather report collection.

  1. One of the most critical parts of the operator is the handler logic, where domain expertise and knowledge is converted into code. In this example activity, when a new WeatherReport object is created, we will publish a pod that queries the weather service and writes the result to the console output. All of these steps are coded in the pkg/stub/handler.go file as follows:
func (h *Handler) Handle(ctx types.Context, event types.Event) error {
switch o := event.Object.(type) {
case *apiv1.WeatherReport:
if o.Status.State == "" {
weatherPod := weatherReportPod(o)
err := action.Create(weatherPod)
if err != nil && !errors.IsAlreadyExists(err) {
logrus.Errorf("Failed to create weather report pod : %v", err)

You can refer the complete code at: https://goo.gl/uxW4jv.

In the Handle function, events carrying objects are processed. This handler function is called from the informers watching for the changes on the registered objects. If the object is WeatherReport and its status is empty, a new weather report pod is created, and the status is updated with the results.

  1. Build the complete project as a Docker container with the Operator SDK and toolset:
operator-sdk build <DOCKER_IMAGE:DOCKER_TAG>

The resulting Docker container is pushed to Docker Hub as onuryilmaz/k8s-operator-example for further usage in the cluster.

  1. Deploy the operator into the cluster with the following commands:
kubectl create -f deploy/crd.yaml
kubectl create -f deploy/operator.yaml

With the successful deployment of the operator, logs could be checked as follows:

kubectl logs -l name=k8s-operator-example

The output is as follows:

  1. After deploying the custom resource definition and the custom controller, it is time to create some resources and collect the results. Create a new WeatherReport instance as follows:
kubectl create -f deploy/cr.yaml

With its successful creation, the status of the WeatherReport can be checked:

 kubectl describe weatherreport amsterdam-daily

You will see the following output:

  1. Since the operator created a pod for the new weather report, we should see it in action and collect the results:
kubectl get pods

You'll see the following result:

  1. Get the result of the weather report with the following command:
kubectl logs $(kubectl get weatherreport amsterdam-daily -o jsonpath={.status.pod})

You'll see the following output:

  1. Clean up with the following command:
kubectl delete -f deploy/cr.yaml
kubectl delete -f deploy/operator.yaml
kubectl delete -f deploy/crd.yaml
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.35.58