This feature is an Enterprise feature. See our pricing plans or contact our sales team for more information.
Native sleep mode
Native sleep mode is intended for pre-production use cases only, and comes with some limitations and caveats and should not be enabled on virtual clusters that are using the current sleep mode configuration found in external.platform.
Not all workloads need to run all the time, and scaling them down saves time and money. With vCluster-native sleep mode you can scale down workloads based on a set schedule, or activity from users and ingress.
Enable native sleep mode​
To enable vCluster-native sleep mode, use the following configuration inside your vcluster.yaml:
experimental:
sleepMode:
enabled: true
autoSleep:
afterInactivity: 1h
How it works​
Sleep mode operations​
Sleep mode performs two main operations:
Sleeping Deletes bare pods and scales down the following resources:
- Deployments
- ReplicaSets
- ReplicationControllers
- DaemonSets
Waking
- Scales resources back to their original state
- Cannot restore previously deleted bare pods
Resource exemption​
Resources can be exempted from sleep mode using either:
- Adding the annotation
sleepmode.loft.sh/exclude: true - Configuring
sleepModewith specific labels - Adding configured labels to workloads that should keep running
Detecting activity​
To wake a sleeping cluster or to update the last active time, native sleep mode captures the following:
- Access of cluster resources through API calls (e.g.,
kubectl get <resource>) - Attempts to contact Ingress endpoints (NGINX only)
Ingress activity detection is only available for NGINX ingress controllers, making use of the mirror-target annotation. This has the effect of overwriting any previously set mirror-target annotation.
Ingress configuration​
Sync to host​
If you install your ingress controllers in the vCluster you'll need to exempt the controller from sleeping so it remains
active for requests that would wake the vCluster. If you install the Ingress controller in the host cluster, you'll need to
enable syncing ingresses to the host.
sync:
toHost:
ingresses:
enabled: true
Reachability​
For proper ingress activity detection:
- The vCluster pod must be discoverable by the ingress controller
- DNS lookup for
<vcluster-namespace>.<vcluster-svc-name>.svc.cluster.localmust resolve - Proper
dnsPolicyconfiguration in the ingress controller Helm installation
Examples​
The below examples demonstrate how to configure sleep mode in a virtual cluster. Please make sure to install the necessary prerequisites before proceeding.
Prerequisites​
-
Administrator access to a Kubernetes cluster: See Accessing Clusters with kubectl for more information. Run the command
kubectl auth can-i create clusterrole -Ato verify that your current kube-context has administrative privileges.infoTo obtain a kube-context with admin access, ensure you have the necessary credentials and permissions for your Kubernetes cluster. This typically involves using
kubectl configcommands or authenticating through your cloud provider's CLI tools. -
helm: Helm v3.10 is required for deploying the platform. Refer to the Helm Installation Guide if you need to install it. -
kubectl: Kubernetes command-line tool for interacting with the cluster. See Install and Set Up kubectl for installation instructions.
vCluster: vCluster command-line tool to provision and manage virtual clusters.- Homebrew
- Mac (Intel/AMD)
- Mac (Silicon/ARM)
- Linux (AMD)
- Linux (ARM)
- Download Binary
- Windows Powershell
brew install loft-sh/tap/vclusterThe binaries in the tap are signed using the Sigstore framework for enhanced security.
curl -L -o vcluster "https://github.com/loft-sh/vcluster/releases/latest/download/vcluster-darwin-amd64" && sudo install -c -m 0755 vcluster /usr/local/bin && rm -f vclustercurl -L -o vcluster "https://github.com/loft-sh/vcluster/releases/latest/download/vcluster-darwin-arm64" && sudo install -c -m 0755 vcluster /usr/local/bin && rm -f vclustercurl -L -o vcluster "https://github.com/loft-sh/vcluster/releases/latest/download/vcluster-linux-amd64" && sudo install -c -m 0755 vcluster /usr/local/bin && rm -f vclustercurl -L -o vcluster "https://github.com/loft-sh/vcluster/releases/latest/download/vcluster-linux-arm64" && sudo install -c -m 0755 vcluster /usr/local/bin && rm -f vclusterDownload the binary for your platform from the GitHub Releases page and add this binary to your $PATH.
md -Force "$Env:APPDATA\vcluster"; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12';
Invoke-WebRequest -URI "https://github.com/loft-sh/vcluster/releases/latest/download/vcluster-windows-amd64.exe" -o $Env:APPDATA\vcluster\vcluster.exe;
$env:Path += ";" + $Env:APPDATA + "\vcluster";
[Environment]::SetEnvironmentVariable("Path", $env:Path, [System.EnvironmentVariableTarget]::User);Reboot RequiredYou may need to reboot your computer to use the CLI due to changes to the PATH variable (see below).
Check Environment Variable $PATHLine 4 of this install script adds the install directory
%APPDATA%\vclusterto the$PATHenvironment variable. This is only effective for the current Powershell session, i.e. when opening a new terminal window,vclustermay not be found.Make sure to add the folder
%APPDATA%\vclusterto thePATHenvironment variable after installing vcluster CLI via Powershell. Afterward, a reboot might be necessary.Confirm that you've installed the correct version of the vCluster CLI.
vcluster --versiondocker: Container runtime installation guide.kind: Kubernetes in Docker installation guide.curl: Command-line tool for transferring data (pre-installed on most systems, see installation guide if needed)
Deployment​
Sleep mode with deployment resource.
Configure sleep mode 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
sleepMode:
enabled: true
autoSleep:
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 sleep mode 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 sleep mode. 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 theDeployments. For exampleVerify
Deploymentssleep 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 sleep mode 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​
Sleep mode with ingress resource.
Configure sleep mode for an ingress controller​
Create the
kindcluster.Create a kind clusterkind 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
EOFInstall the NGINX
IngressController.install ingress controllerhelm install ingress-nginx ingress-nginx/ingress-nginx \
--namespace ingress-nginx \
--create-namespace \
--set controller.dnsPolicy=ClusterFirstWithHostNet \
--set controller.hostNetwork=true \
--set controller.service.type=ClusterIPCreate the vCluster.
Use the following
vcluster.yamlto create a virtual cluster on your host. Save this file asvcluster.yamlvCluster config for auto sleeppro: true
sync:
toHost:
ingresses:
enabled: true
sleepMode:
enabled: true
autoSleep:
afterInactivity: 30sAnd run the following command:
Create vCluster with autoSleep configvcluster create my-vcluster -f vcluster.yamlEnable local DNS resolution for the virtual cluster.
Add
127.0.0.1 backend.localto your/etc/hostsfile to match the host configured in theIngressrules of the next step.Create resources for the
Ingresssuch as aDeploymentandService.Use the following manifest to create:
NamespacenamedbarDeploymentfor the pods backing theServiceServicefor theIngressIngressresource
Example deploymentsapiVersion: 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
rules:
- http:
paths:
- pathType: Prefix
path: /bar
backend:
service:
name: bar-service
port:
number: 8080
host: backend.localVerify the ingress is working properly with
curl.Test the
Ingressendpoint within the 30-second activity window by runningcurl --silent backend.local/bar. The pod name from theDeploymentthat responds is displayed.Allow the virtual cluster to go to sleep.
Wait 30 seconds for the cluster to sleep, then run the
curlcommand again. For convenience, runwatch -d curl --silent backend.local/barto repeatedly test the endpoint. On the first attempt, you’ll see the messageClient 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.
Additional examples​
Sleep mode label selectors and schedule.
experimental:
sleepMode:
enabled: true
autoSleep:
afterInactivity: 3h # Uses Go's Duration with a max unit of hour
exclude:
selector:
labels:
dont: sleep
experimental:
sleepMode:
enabled: true
timezone: America/Denver
autoSleep:
schedule: 30 17 * * 5
wakeup:
schedule: 0 7 * * 1
Config reference​
sleepMode required object pro​
SleepMode holds the native sleep mode configuration for Pro clusters
sleepMode required object pro​enabled required boolean pro​
Enabled toggles the sleep mode functionality, allowing for disabling sleep mode without removing other config
enabled required boolean pro​timeZone required string pro​
Timezone represents the timezone a sleep schedule should run against, defaulting to UTC if unset
timeZone required string pro​autoSleep required object pro​
AutoSleep holds autoSleep details
autoSleep required object pro​afterInactivity required string pro​
AfterInactivity represents how long a vCluster can be idle before workloads are automaticaly put to sleep
afterInactivity required string pro​schedule required string pro​
Schedule represents a cron schedule for when to sleep workloads
schedule required string pro​