Cyberithub

How to Install kube-downscaler in Kubernetes cluster using Kustomize and Kubectl

Advertisements

In this article, we will see how to install kube-downscaler in kubernetes cluster using kustomize and kubectl tool. kube-downscaler is a free and open source kubernetes controller designed to scale down the workloads such as deployments, statefulsets, cronjobs etc. during non office hours to save resources and costs. It is well suited to be used for non-production clusters such as uat, dev, sit and biz environment.

It eliminates the effort of scaling down resources manually every day. It also provides the annotations of excluding critical workloads from scaling down. This is the perfect tool to automate resource usage in a Kubernetes cluster. Here we will see the steps to deploy and use this tool in a kubernetes cluster,

 

How to install kube-downscaler in Kubernetes cluster using Kustomize and Kubectl

How to install kube-downscaler in Kubernetes cluster using Kustomize and Kubectl

Also Read: How to delete all kubernetes objects deployed by kustomize tool?

Step 1: Prerequisites

a) You should have a running Kubernetes Cluster.

b) You should have kubectl and git installed and available to use. To install kubectl on ubuntu 22.04 based systems, check How to Install kubectl on Ubuntu 22.04 LTS (Jammy Jellyfish)

c) You should have sudo or root access to run kubectl commands.

d) You should have access to kubernetes control node to run all the required commands.

 

Step 2: Install Kustomize

You can visit kustomize official website and take the latest step to download the precompiled binary package in your system. Below script will automatically detect your system os and download the right package to be used in the system.

cyberithub@cluster:~$ curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
v5.6.0
kustomize installed to //root/kustomize

You can then copy the binary to /usr/local/bin directory from /root by using cp /root/kustomize /usr/local/bin/ command as shown below.

cyberithub@cluster:~$ cp /root/kustomize /usr/local/bin/

 

Step 3: Clone git repo

In the next step, you have to clone kube-downscaler repo from official website in your local system using git clone https://codeberg.org/hjacobs/kube-downscaler.git command as shown below. This will create a copy of all repo contents in the current working path of your local system.

cyberithub@cluster:~$ git clone https://codeberg.org/hjacobs/kube-downscaler.git
Cloning into 'kube-downscaler'...
remote: Enumerating objects: 1203, done.
remote: Counting objects: 100% (461/461), done.
remote: Compressing objects: 100% (131/131), done.
remote: Total 1203 (delta 429), reused 330 (delta 330), pack-reused 742 (from 1)
Receiving objects: 100% (1203/1203), 392.08 KiB | 427.00 KiB/s, done.
Resolving deltas: 100% (809/809), done.

 

Step 4: Deploy using kustomize

You can now use kustomize tool to check the build and then deploy the k8s objects in your cluster default namespace using kubectl utility. To do both build and deploy together, run kustomize build kube-downscaler/deploy/ | kubectl apply -f- as shown below. This will deploy all the objects as shown below.

cyberithub@cluster:~$ kustomize build kube-downscaler/deploy/ | kubectl apply -f- 
serviceaccount/kube-downscaler created
clusterrole.rbac.authorization.k8s.io/kube-downscaler created
clusterrolebinding.rbac.authorization.k8s.io/kube-downscaler created
configmap/kube-downscaler created
deployment.apps/kube-downscaler created

 

Step 5: Verify Installation

After successfully deploying all objects, you can verify the creation or updation of all the deployed objects using kubectl get all -n default command as shown below. This confirms successful installation of kube-downscaler tool in your cluster. Now you can use different annotations provided by this tool to scale down the resources.

cyberithub@cluster:~$ kubectl get all -n default
NAME                                 READY  STATUS   RESTARTS  AGE
pod/kube-downscaler-bc8888bf4-htmhv  1/1    Running  0         26s

NAME                TYPE       CLUSTER-IP  EXTERNAL-IP  PORT(S)  AGE
service/kubernetes  ClusterIP  10.96.0.1   <none>       443/TCP  3d17h

NAME                             READY  UP-TO-DATE  AVAILABLE  AGE
deployment.apps/kube-downscaler  1/1    1           1          27s

NAME                                       DESIRED  CURRENT READY  AGE
replicaset.apps/kube-downscaler-bc8888bf4  1        1       1      27s

 

Step 6: Scale down workload

Let's see an example where we will scale down the replica of an application called cyberithub-app to 1 from original deployment replica of 3 on Saturday and Sunday from 7:00 PM to midnight (New York time). Apart from these hours, application deployment will run at full capacity of 3 replicas as shown below.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cyberithub-app
  annotations:
    downscaler/downtime: "Sat-Sun 19:00-23:59 America/New_York"
    downscaler/downtime-replicas: "1"
spec:
  replicas: 3
  selector:
    matchLabels:
      app: cyberithub-app
  template:
    metadata:
      labels:
        app: cyberithub-app
    spec:
      containers:
      - name: cyberithub-container
        image: nginx:latest

 

Step 7: Delete all objects

Once you are done using this tool, you can also choose to uninstall it from your cluster by simply removing all the created objects using kustomize build kube-downscaler/deploy/ | kubectl delete -f- command as shown below.

cyberithub@cluster:~$ kustomize build kube-downscaler/deploy/ | kubectl delete -f-
serviceaccount "kube-downscaler" deleted
clusterrole.rbac.authorization.k8s.io "kube-downscaler" deleted
clusterrolebinding.rbac.authorization.k8s.io "kube-downscaler" deleted
configmap "kube-downscaler" deleted
deployment.apps "kube-downscaler" deleted

Leave a Comment