Sleep Mode
Loft provides two powerful features to reduce Kubernetes cost:
- Sleep Mode which puts spaces to sleep when nobody is using them, i.e. purging all pods while keeping all resources inside the spaces during periods of inactivity
- Auto-Delete which deletes spaces that have been idle for a while
Both of these feature typically rely on Loft's inactivity detection.
Working with Sleep Mode
With sleep mode, you can put spaces to sleep which means that Loft will set replicas: 0
for all replica-controlled resources such as Deployments
and StatefulSets
inside the space. This means that Kubernetes will delete all pods but the entire configuration of resources within the space is still there.
Sleep mode can be:
- Invoked manually
- Triggered by an inactivity timeout (no one has ran a
kubectl
command or accessed an ingress in this space for X minutes) - Scheduled using a CRON syntax
Manual + Automatic Sleep / Wake-up
Start Sleep (manual)
Automatic Sleep Mode (individual space)
Wake up space
Scheduled Sleep / Wake-up
Scheduled Sleep & Wake-Up (individual space)
Working with Auto-Delete
Loft lets you configure an auto-delete for spaces that have not been used for a certain period of time (inactivity).
Configure Auto-Delete Timeout
Inactivity Detection
All requests that are made through Loft count as activity in the space.If your kube-context points to Loft's API server as a proxy before the actual connected cluster's API server, every kubectl
request will be an activity and reset the inactivity timeout.
Ingress Requests
For ingress-nginx based ingresses, activity detection also works automatically. Other ingress controllers are currently not supported. For nginx based ingresses, Loft will add a special annotation to each ingress that will track activity and reset the timer as soon as a request is made to that ingress.
Advanced Configuration
Exclude Resources From Sleep
Loft allows you to specify resources that should not sleep within a space by providing the annotation sleepmode.loft.sh/exclude: 'true'
on either a Deployment
, StatefulSet
, ReplicaSet
or Pod
. For example the following Deployment
would not sleep if the space is sleeping:
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
annotations:
sleepmode.loft.sh/exclude: 'true'
spec:
replicas: 2
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: busybox
image: busybox
command:
- sleep
- '3600'
Long-Living Connections
It's possible that certain requests such as kubectl exec
or kubectl port-forward
keep an active connection open to the space that prevents it from sleeping. This is in most cases wanted since the space shouldn't start sleeping when someone is still clearly using it, however there are cases where the connection might be idle (someone left the laptop open, but is not using it anymore) in which you want to terminate such connections and put the space to sleep.
There are multiple ways to approach this problem of active connections preventing a space from sleeping:
- You can tell loft to timeout idle streaming connections (such as
kubectl exec
,kubectl port-forward
etc.) after a certain time period with the annotationloft.sh/streaming-connection-idle-timeout: '3600'
on a **cluster**. With this annotation set, loft will close connections automatically that are idle after the given seconds. By default, Loft will not timeout any connections. This will only apply to new opened connections to that cluster and not affect already running connections. - Tell loft to ignore all active connections for determining space activity via the annotation
sleepmode.loft.sh/ignore-active-conntections: 'true'
. This will put a space to sleep even though there still might be open connections such askubectl exec
orkubectl port-forward
. - Configure your kubelets with the flag
--streaming-connection-idle-timeout duration
(see docs). This behaves essentially as option 1, however this configuration is Loft independent.
