kube green - An operator to reduce CO2 footprint of your clusters
Shutdown your pods during non working hours to optimize resources.
It is custom resource definition a.k.a. CRD of k8s, that monitors the pods. It will bring up and down the pods based on the spec details.
An example confiuration. Source: kube-green website.
apiVersion: kube-green.com/v1alpha1
kind: SleepInfo
metadata:
name: working-hours
spec:
weekdays: "1-5"
sleepAt: "20:00"
wakeUpAt: "08:00"
timeZone: "Europe/Rome"
suspendCronJobs: true
excludeRef:
- apiVersion: "apps/v1"
kind: Deployment
name: my-deployment
learngo
on your machine.go install sigs.k8s.io/kind@v0.22.0 && kind create cluster --name learngo
kind
automatically switches to context and can be verified by running below command.$ kubectl config get-contexts | grep -i ^\* | awk '{print $1, $2}'
* kind-learngo
NOTE: After system reboot, we cannβt bring the cluster back. Hence, we need to delete and create it again.
$ kind get clusters
learngo
$ kind delete cluster --name=learngo
Deleting cluster "learngo" ...
Deleted nodes: ["learngo-control-plane"]
$ kind create cluster --name learngo
Creating cluster "learngo" ...
β Ensuring node image (kindest/node:v1.29.2) πΌ
β Preparing nodes π¦
β Writing configuration π
β Starting control-plane πΉοΈ
β Installing CNI π
β Installing StorageClass πΎ
Set kubectl context to "kind-learngo"
You can now use your cluster with:
kubectl cluster-info --context kind-learngo
Thanks for using kind! π
Once we have the running cluster, we need to install certmanager for authentication purpose.
The cert manager can be installed by running below command.
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.4/cert-manager.yaml
There is a file with resources to create issuer and certificates is present in file named green-pods/test-resources.yaml. Use the yaml file for deployment.
kubectl apply -f green-pods/test-resources.yaml
Verify the running pods.
$ kubectl get pods --namespace cert-manager
NAME READY STATUS RESTARTS AGE
cert-manager-67c98b89c8-ct56n 1/1 Running 0 40s
cert-manager-cainjector-5c5695d979-np99w 1/1 Running 0 40s
cert-manager-webhook-7f9f8648b9-lbdcb 1/1 Running 0 40s
You can also see the issuing of certificates once you describe the pod.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Issuing 29m cert-manager-certificates-trigger Issuing certificate as Secret does not exist
Normal Generated 29m cert-manager-certificates-key-manager Stored new private key in temporary Secret resource "selfsigned-cert-x7vcx"
Normal Requested 29m cert-manager-certificates-request-manager Created new CertificateRequest resource "selfsigned-cert-1"
Normal Issuing 29m cert-manager-certificates-issuing The certificate has been successfully issued
kubectl apply -f https://github.com/kube-green/kube-green/releases/download/v0.5.2/kube-green.yaml
sleepme
to configure with kube-green.kubectl create ns sleepme
kubectl -n sleepme create deploy echo-service-replica-1 --image=davidebianchi/echo-service
kubectl -n sleepme create deploy do-not-sleep --image=davidebianchi/echo-service
kubectl -n sleepme create deploy echo-service-replica-4 --image=davidebianchi/echo-service --replicas 4
kubectl -n sleepme get pods
As a final step, configure the kube green for above resources. To setup kube-green, the SleepInfo resource must be created in sleepme namespace. Update the green-pods/kube-green-test.yaml based on your requirements for testing purpose. The time and timezone needs to be modified.
kubectl -n sleepme apply -f kube-green-test.yaml
Verify the pods getting down and brought up by kube-green using k9s in real time environment.
kubectl delete -f green-pods/test-resources.yaml
kubectl delete -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.4/cert-manager.yaml
Thank you for contributing towards the green development π.