Auto Sleep
| Enterprise | ||||
|---|---|---|---|---|
| Available in these plans | Free | Dev | Prod | Scale |
| Auto Sleep | ||||
This feature is an Enterprise feature. See our pricing plans or contact our sales team for more information.
Not all workloads need to run continuously. Scaling them down reduces costs. With auto sleep, you can scale workloads based on a set schedule or user activity and ingress.
Auto sleep is intended for pre-production use cases only, and has limitations when used on a virtual cluster instance not connected to the platform.
Configure​
- vcluster.yaml
- vCluster CLI
- Platform UI
- Platform CRDs
Enable auto sleep​
To enable auto sleep, add the following configuration to your vcluster.yaml:
sleep:
auto:
afterInactivity: 1h
Deployment example​
A deployment resource in Kubernetes manages a set of identical pods. Configuring auto sleep for a deployment scales down the pods while keeping the control plane active. This setup allows the virtual cluster to reduce resource usage while still being able to monitor activity and trigger wake-up actions when needed.
- Without agent: The control plane remains active, allowing the virtual cluster instance to monitor activity and wake up automatically.
- With agent: The platform agent can shut down the control plane completely for maximum resource savings.
Configure auto sleep with a deployment resource
Configure auto sleep for a deployment resource​
Create the
kindcluster.create kind clusterkind create cluster --name sleep-mode-demoDeploy a virtual cluster.
Use the following
vcluster.yamlto create a virtual cluster on your host. Save this file asvcluster.yamlvCluster config for auto sleeppro: true
sleep:
auto:
afterInactivity: 30s
exclude:
selector:
labels:
sleep: no-thanksAnd run the following command:
Create vCluster with autoSleep configvcluster create my-vcluster -f vcluster.yamlWorkloads with the label
sleep: no-thanksdon't enter auto sleep after 30 seconds.Create demo deployments in your virtual cluster.
Use the following deployment YAML to create two deployments.
Example deploymentsapiVersion: apps/v1
kind: Deployment
metadata:
name: sleepy-deployment
labels:
app: sleepy-dep
spec:
replicas: 2
selector:
matchLabels:
app: demo-dep-1
template:
metadata:
labels:
app: demo-dep-1
spec:
containers:
- command:
- /agnhost
- serve-hostname
- --http=true
- --port=8080
image: registry.k8s.io/e2e-test-images/agnhost:2.39
name: sleepy-demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: no-sleep-deployment
labels:
sleep: no-thanks
spec:
replicas: 2
selector:
matchLabels:
app: demo-dep-2
template:
metadata:
labels:
app: demo-dep-2
spec:
containers:
- command:
- /agnhost
- serve-hostname
- --http=true
- --port=8080
image: registry.k8s.io/e2e-test-images/agnhost:2.39
name: not-sleepy-demoThe first deployment does not have any special configurations for auto sleep. You can replace it with another deployment if needed. The second deployment includes a special label on the Deployment, preventing it from scaling down after 30 seconds.
You can verify this by waiting
30 secondsand then getting information about the Deployments. For exampleVerify Deployments sleep status.
deployment sleep check> sleep 30; kubectl get deployments
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
default no-sleep-deployment 2/2 2 2 1m
default sleepy-deployment 0/2 0 0 1mThe
sleepy-deploymentreports0/2replicas after 30 seconds. Runningkubectlcounts as cluster activity, which is why it reports0/2instead of0/0. Thekubectlcommand triggersvClusterto update the replica count back to the original value of 2, but the replicas haven't become ready by the timekubectl get ...returns.
Next steps​
Experiment with the auto sleep feature by trying the following:
- Add the
sleep: no-thankslabel to the first deployment and verify neither sleeps. - Remove the
sleep: no-thankslabel from both the deployments and verify that both go to sleep.
Ingress controller example​
An ingress controller, such as NGINX, manages external HTTP/S traffic to services in a Kubernetes cluster.
To configure auto sleep for the ingress controller, make sure it stays responsive to incoming traffic and can wake up the cluster if needed. This setup keeps the ingress controller active, even when the virtual cluster instance is in auto sleep and allows the controller to handle requests that trigger the virtual cluster to wake up. Create the [Deprecated]: Install the NGINX IngressController. Create the vCluster. Use the following And run the following command: Enable local DNS resolution for the virtual cluster. Add Create resources for the Ingress such as a Deployment and Service. Use the following manifest to create: Verify the ingress is working properly with Test the Ingress endpoint within the 30-second activity window by running Allow the virtual cluster to go to sleep. Wait 30 seconds for the cluster to sleep, then run the Configure auto sleep with an ingress resource
Configure auto sleep for an ingress controller​
kind cluster.kind create cluster --name ingress-demo --config - <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
apiServerAddress: "0.0.0.0"
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
EOFhelm install ingress-nginx ingress-nginx/ingress-nginx \
--namespace ingress-nginx \
--create-namespace \
--set controller.dnsPolicy=ClusterFirstWithHostNet \
--set controller.hostNetwork=true \
--set controller.service.type=ClusterIPvcluster.yaml to create a virtual cluster on your host. Save this file as vcluster.yamlpro: true
sync:
toHost:
ingresses:
enabled: true
sleep:
auto:
afterInactivity: 30svcluster create my-vcluster -f vcluster.yaml127.0.0.1 backend.local to your /etc/hosts file to match the host configured in the Ingress rules of the next
step.
barapiVersion: v1
kind: Namespace
metadata:
name: bar
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: bar-deployment
namespace: bar
labels:
app: bar-dep
spec:
replicas: 2
selector:
matchLabels:
app: bar
template:
metadata:
labels:
app: bar
spec:
containers:
- command:
- /agnhost
- serve-hostname
- --http=true
- --port=8080
image: registry.k8s.io/e2e-test-images/agnhost:2.39
name: bar-app
---
kind: Service
apiVersion: v1
metadata:
name: bar-service
namespace: bar
spec:
selector:
app: bar
ports:
# Default port used by the image
- port: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: bar
spec:
ingressClassName: nginx # the ingress-nginx project has been deprecated, we recommend using a different ingress class
rules:
- http:
paths:
- pathType: Prefix
path: /bar
backend:
service:
name: bar-service
port:
number: 8080
host: backend.localcurl.curl --silent backend.local/bar. The pod name from the Deployment that responds is displayed.curl command again. For convenience, run watch -d curl --silent backend.local/bar to repeatedly test the endpoint. On the first attempt, you’ll see the message Client sent an HTTP request to an HTTPS server. because an HTTP request was sent to the HTTPS wake endpoint. Subsequent requests display new pod names.
Istio gateway example​
Enable Istio integration for auto sleep to work with Istio resources.
When Istio is installed on the host cluster and the Istio integration is enabled, the virtual cluster instance syncs Gateway and VirtualService resources to the host cluster.
With auto sleep enabled, any traffic routed to a Service in the virtual cluster instance is tracked as activity and keeps the virtual cluster instance awake. If the vCluster is asleep, incoming traffic wakes it up. This example continues from the Istio integration example. Complete the Istio integration example. Install the Istio ingress gateway controller to allow routing from outside the cluster. Save the following manifest as Create the Create a Gateway resource that uses a selector matching the newly installed ingress gateway in your vCluster. Update the vCluster with auto sleep enabled. Use the following Run the following command: Enable local DNS resolution for the virtual cluster. Add Add the Gateway you created and the Use Test the Gateway endpoint within the 30-second activity window by running either Allow the virtual cluster to go to sleep. Wait 30 seconds for the cluster to sleep. Then run the Configure an ingress gateway for cluster external access
ingress.yaml:apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
name: ingress
spec:
profile: empty # Do not install CRDs or the control plane
components:
ingressGateways:
- name: istio-ingressgateway
namespace: istio-ingress
enabled: true
label:
istio: ingressgateway
values:
gateways:
istio-ingressgateway:
injectionTemplate: gatewayistio-ingress namespace and install the ingress gateway:kubectl create namespace istio-ingress
istioctl install -f ingress.yamlapiVersion: networking.istio.io/v1
kind: Gateway
metadata:
name: istio-sm-gateway
namespace: test
spec:
# The selector matches the ingress gateway pod labels.
# If you installed Istio using Helm following the documentation, this is "istio=ingress"
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "smdemo.local"vcluster.yaml to update your virtual cluster on your host cluster with --upgrade.
Save this file as vcluster.yamlpro: true
sleep:
auto:
afterInactivity: 30s
integrations:
istio:
enabled: truevcluster create <your-vcluster-name> -f vcluster.yaml --upgrade127.0.0.1 smdemo.local to your /etc/hosts file to match the host configured in the Gateway configuration.smdemo.local host to the VirtualService from the [Istio integration example](../vcluster- yaml/integrations/istio). The updated VirtualService should look like the following:apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
name: nginx-service
namespace: test
spec:
gateways:
- istio-sm-gateway
hosts:
- smdemo.local
http:
- match:
- uri:
prefix: /v2
name: nginx-v2
rewrite:
uri: /
route:
- destination:
host: nginx-service.test.svc.cluster.local
subset: v2
- name: nginx-v1
route:
- destination:
host: nginx-service.test.svc.cluster.local
subset: v1curl to verify that the Gateway is working correctly.curl --silent smdemo.local/v1 or curl --silent smdemo.local/v2.curl command again. To repeatedly test the endpoint, run watch -d curl --silent smdemo.local/v2. While the cluster is asleep or waking up, a 503 error is displayed.
Enable auto sleep with label selectors and schedules​
Use label selectors and schedules to configure auto sleep based on inactivity or specific timing:Auto sleep label selectors and schedule
sleep:
auto:
afterInactivity: 3h # Uses Go duration with a max unit of hour
exclude:
selector:
labels:
dont: sleepsleep:
auto:
timezone: America/Denver
schedule: 30 17 * * 5
wakeup:
schedule: 0 7 * * 1
Manual sleep​
Click on Virtual Clusters to find the virtual cluster that you want to put to sleep.
Hover over the row of the virtual cluster that you want to put to sleep.
Click on the vertical ellipsis button and select Sleep to put the virtual cluster to sleep.
Notice how the Status column shows that the virtual cluster is now sleeping.
Note that the virtual cluster will automatically wake up again, once you run a kubectl command or send a Kubernetes request via another tool to the virtual cluster.
Configure auto sleep​
This only works for virtual clusters without a template, if you want to change the auto sleep configuration for a virtual cluster created by a template, it must be configured in the template.
Click on Virtual Clusters to find the virtual cluster that you want to configure auto sleep for.
Hover over the row of the virtual cluster that you want to configure.
Click on Edit on the virtual cluster that you want to edit.
Use the Sleep After Inactivity (minutes) field to specify the time to wait before putting the virtual cluster to sleep if there is no more user activity in this virtual cluster.
Click on the button to save the changes.
Configure scheduled auto sleep and wake-up​
This only works for virtual clusters without a template, if you want to change the auto sleep configuration for a virtual cluster created by a template, it must be configured in the template.
In the Virtual Clusters view, hover over the virtual cluster that you want to configure auto sleep for.
Click on the button.
In the Definition section, under the Sleep Mode section, use the Sleep Schedule field and/or the Wake-Up Schedule field to specify the Cronjob Times when the respective virtual cluster should be put to sleep or woken up.
[Optional] Input a Schedule Timezone for the timezone to apply the schedule.
Click on the button to save the changes.
Wake up virtual cluster​
In the Virtual Clusters view, hover over the virtual cluster that you want to wake up.
Click on the vertical ellipsis button and select Wake Up to wake up the virtual cluster.
Notice how the Status column shows that the virtual cluster is now running again after a Pending state.
Configure auto-delete​
The platform lets you configure auto-delete for virtual clusters that have not been used for a certain period of time.
This only works for virtual clusters without a template, if you want to change the auto sleep configuration for a virtual cluster created by a template, it must be configured in the template.
In the Virtual Clusters view, hover over the virtual cluster that you want to configure auto-delete for.
Click on the button.
Under the Sleep Mode section, use the Delete After Inactivity (minutes) field to specify the time to wait before deleting the virtual cluster if there was no more user activity within this virtual cluster.
Click on the button to save the changes.
You can configure auto sleep for a virtual cluster by setting the sleep configuration in the VirtualClusterInstance manifest.
apiVersion: management.loft.sh/v1
kind: VirtualClusterInstance
metadata:
name: my-vcluster
namespace: loft-p-my-project
spec:
clusterRef:
cluster: my-connected-cluster
sleepModeConfig:
afterInactivity: 3600 # seconds
autoDelete:
afterInactivity: 86400 # seconds
templateRef:
name: my-vcluster-template
For scheduled sleep and wake-up, you can use the sleepSchedule and wakeupSchedule fields:
apiVersion: management.loft.sh/v1
kind: VirtualClusterInstance
metadata:
name: my-vcluster
namespace: loft-p-my-project
spec:
clusterRef:
cluster: my-connected-cluster
sleepModeConfig:
sleepSchedule: "30 17 * * 5" # Sleep every Friday at 17:30
wakeupSchedule: "0 7 * * 1" # Wake every Monday at 7:00
timezone: America/Denver
templateRef:
name: my-vcluster-template
Pause a virtual cluster (Helm driver)​
For virtual clusters deployed with the Helm driver (without platform):
vcluster pause VCLUSTER_NAME --namespace NAMESPACE
This will scale down the virtual cluster and delete all workloads created through the virtual cluster. Upon resume, all workloads will be recreated. Other resources such as persistent volume claims, services etc. will not be affected.
Resume a paused virtual cluster​
vcluster resume VCLUSTER_NAME --namespace NAMESPACE
Sleep a virtual cluster (Platform)​
For virtual clusters managed by the vCluster Platform:
vcluster platform sleep vcluster VCLUSTER_NAME --project PROJECT_NAME
You can optionally prevent the virtual cluster from waking up for a certain period:
vcluster platform sleep vcluster VCLUSTER_NAME --project PROJECT_NAME --prevent-wakeup 3600
The --prevent-wakeup flag specifies the number of seconds the virtual cluster should remain asleep (use 0 for infinite sleeping).
Wake up a sleeping virtual cluster (Platform)​
vcluster platform wakeup vcluster VCLUSTER_NAME --project PROJECT_NAME
Note that the virtual cluster will automatically wake up again, once you run a kubectl command or send a Kubernetes request via another tool to the virtual cluster.
Config reference​
sleep required object ​
Sleep holds configuration for automatically putting the virtual cluster to sleep.
sleep required object ​auto required object ​
Auto holds automatic sleep configuration
auto required object ​afterInactivity required string ​
AfterInactivity represents how long a vCluster can be idle before workloads are automatically put to sleep
afterInactivity required string ​schedule required string ​
Schedule represents a cron schedule for when to sleep workloads
schedule required string ​exclude required object ​
Exclude holds configuration for labels that, if present, will prevent a workload from going to sleep
exclude required object ​wakeup required object ​
Wakeup holds configuration for waking the vCluster on a schedule
wakeup required object ​schedule required string ​
schedule required string ​timezone required string ​
Timezone specifies time zone used for scheduled sleep operations. Defaults to UTC.
Accepts the same format as time.LoadLocation() in Go (https://pkg.go.dev/time#LoadLocation).
The value should be a location name corresponding to a file in the IANA Time Zone database, such as "America/New_York".
timezone required string ​