Kubernetes Operator to scale applications based on custom CRD
Kubernetes Operators allow you to extend the native functionality of Kubernetes by integrating custom logic into the control plane. You can codify application-specific operational knowledge directly into Kubernetes. Using Operarators you can automate application specific operational tasks.
In this project we have created a custom operator from scratch using operator-sdk to create the boilerplate code, defining you own CRD’s and then providing the custom logic for reconciling the required objects in the cluster. This opearor allows you to scale up or down the no of replicas for a list of deployments based on the time and count in the inputs from crd.
We start by using init and create api command to create basic structure and “Appscale” kind the using the operator-sdk commands.
operator-sdk init --domain vikasbadoni.com --owner "Vikas Badoni" --repo github.com/Projects-vb/Kube-operator/scaling
operator-sdk create api --kind Appscale --group api --version v1alpha1
Next, we finalize the structure of the CRD, which will define the inputs that the controller will read and match against.
Then we define custom structs in the appscale_types.go that will represent the custom resource data during execution. We have embedded the DeploymentList struct inside the AppscaleSpec as that will contain the list of deployments which are to be managed in crd.
type Appscale struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec AppscaleSpec `json:"spec,omitempty"`
Status AppscaleStatus `json:"status,omitempty"`
}
type AppscaleSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
Start int `json:"start"`
End int `json:"end"`
Replicas int `json:"replicas"`
Deployments []DeploymentList `json:"deployments"`
}
type DeploymentList struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}
You need to run “make manifest” to generate the new crds every time you make changes to the structs fields.
Next, We provide our custom logic inside Reconcile() function in /internal/controller/appscale_controller.go
Here, we create a pointer to appscale{} type and then get the required object from the cluster and retrieve the required values in different vars.
s := &apiv1alpha1.Appscale{}
err := r.Get(ctx, req.NamespacedName, s)
Based on some conditions we call the scaleApp function to get the required resource from the cluster and then update it.
r.Get(ctx, types.NamespacedName{
Namespace: deployment.Namespace,
Name: deployment.Name,
}, dep)
err := r.Update(ctx, dep)
Reconciliation loop is the core logic that ensures the current state of the cluster matches the desired state of the resource. It runs periodically and on any changes (e.g., creation, deletion, update) to its custom resources CRs.
Use “Make run” to run the operator locally and then submit a new crd file.
Thank You!
Github Code Link