Virtual Clusters in Loft
A virtual cluster is a fully functional Kubernetes cluster that runs inside the namespace of another Kubernetes cluster (host cluster). Virtual clusters are very useful if you are hitting the limits of namespaces and do not want to make special exceptions to the multi-tenancy configuration of the underlying cluster, e.g. a user needs their own CRD or user needs pods from 2 namespaces to communicate with each other but your standard NetworkPolicy does not allow this, then a virtual cluster may be perfect for this user.
# 1. Create virtual cluster
loft create vcluster dev-anna
✓ Creating namespace 'vcluster-dev-anna'
✓ Applying security templates to namespace
✓ Starting virtual cluster inside namespace
✓ Virtual cluster 'dev-anna' successfully started.
Loft has created a kube-context on your local machine to access this virtual cluster. It is your current kube-context now.
# 2. Work within virtual cluster
kubectl get namespaces
NAME STATUS AGE
default Active 5s
kube-system Active 5s
kube-public Active 5s
kube-node-lease Active 5s
kubectl create deployment nginx-deployment --image=nginx
deployment.apps/nginx-deployment created
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-mnvzz 1/1 Running 0 3s
Working with Virtual Clusters
Unlike "real" clusters, virtual clusters are much more lightweight and much faster to spin up and tear down again.
Create Virtual Clusters
- UI
- CLI
- kubectl
- Go to the Virtual Clusters view using the menu on the left
- Click on the button
- Use the field Display Name to define the name of this virtual cluster and optionally specify other settings
- Click on the button at the very bottom
Retrieve a kube-context for this virtual cluster using Loft CLI:
loft use vcluster [vcluster-name]
To create a virtual cluster using Loft CLI, run:
loft create vcluster [vcluster-name]
Running loft create vcluster
will automatically add a kube-context to your kube-config file, so you can immediately run kubectl
commands right after creating a virtual cluster.
If you do not have a kube-context for the cluster where this space should be created in, you can set up a kube-context using:
loft use cluster [cluster-name]
Create a file named
vcluster.yaml
with the following content:apiVersion: cluster.loft.sh/v1
kind: VirtualCluster
metadata:
name: vcluster-name # Set the name for your virtual cluster here
namespace: vc-host-namespace # Host namespace for the virtual clusterCreate the virtual cluster using kubectl:
kubectl apply -f vcluster.yaml
Retrieve a kube-context for this virtual cluster using Loft CLI:
loft use vcluster [vcluster-name]
Delete Virtual Clusters
- UI
- CLI
- kubectl
- In the Virtual Clusters view, hover over the row of the virtual cluster that you want to delete.
- While hovering over the row, you will see buttons appear on the right in the Actions column
- Click on the button to Delete the virtual cluster
loft delete vcluster [vcluster-name]
Deleting virtual clusters with Loft CLI has the advantage that Loft CLI will also delete the kube-context for this virtual cluster from your local kube-config file to keep everything cleaned up.
If you do not have a kube-context for the cluster where this virtual cluster is located in, you can set up a kube-context using:
loft use cluster [cluster-name]
Delete the virtual cluster using kubectl:
kubectl delete vcluster [vcluster-name]
# Alternatives:
# 1) kubectl delete space [host-namespace] # Delete the host namespace
# 2) kubectl delete -f vcluster.yaml
List Virtual Clusters
- UI
- CLI
- kubectl
To see a list of virtual clusters, go to the Virtual Clusters view using the main menu on the left.
Run this command using Loft CLI to get a list of all virtual clusters you have access to across all clusters:
loft list vclusters
If you want to retrieve a kube-context for any of your virtual clusters, run:
loft use vcluster [vcluster-name]
If you do not have a kube-context for the space (host namespace) where you want to list virtual clusters in, you can set up a kube-context using:
loft use space [space-name]
Run this kubectl command to get a list of virtual clusters in this namespace:
kubectl get virtualclusters
Virtual Cluster Templates
Loft allows you to create templates for virtual clusters. Virtual cluster templates are optional templates that a user can choose to apply when creating a virtual cluster.
Common use cases for virtual cluster templates may be:
- Adding development tooling to a virtual cluster
- Deploying pre-populated databases with test data
- Equipping new virtual clusters with optional credentials, CRDs, etc.
1. Create Virtual Cluster Template
- Go to the Virtual Clusters view using the menu on the left
- Switch to the Virtual Cluster Templates tab
- Click the button to create a new virtual cluster template
- In the drawer that appears on the right, use the field Display Name to specify a Name for your virtual cluster template
- Specify any of these options: labels, annotations, virtual cluster version, helm values and apps to deployed
- Expand the Space Template should be applied for the underlying host namespace if being created as part of the virtual clusters creation process section to specify which
- On the very bottom, click on the button to create this virtual cluster template
2. Create Virtual Cluster Based On Template
- UI
- CLI
- Go to the Virtual Clusters view using the menu on the left
- Click on the button
- Use the field Virtual Cluster Template to select a template to use for creating this virtual cluster
- Use the field Display Name to define the name of this space and optionally specify other settings
- Click on the button at the very bottom
Retrieve a kube-context for this virtual cluster using Loft CLI:
loft use vcluster [vcluster-name]
To use a virtual cluster template to create a virtual cluster using Loft CLI, run:
loft create vcluster [vcluster-name] --template [template-name]
Running loft create vcluster
will automatically add a kube-context to your kube-config file, so you can immediately run kubectl
commands right after creating a virtual cluster.
Sleep Mode
Loft's sleep allows you to configure an inactivity timeout for virtual clusters after which the virtual cluster will be put to sleep automatically. Technically, the sleep mode does not work directly on the virtual cluster itself but rather on the host namespace where the virtual cluster is running inside of.
Learn more about configuring sleep mode for spaces.
Auto-Delete
Loft's auto-delete feature allows you to configure an inactivity timeout for virtual clusters after which the virtual cluster will be deleted automatically. Technically, auto-delete does not work directly on the virtual cluster itself but rather on the host namespace where the virtual cluster is running inside of.
Learn more about configuring auto-delete for spaces.
Access Permissions
Loft makes it easy to give other users or even entire teams access to one of your virtual clusters.
- UI
- CLI
- In the Virtual Clusters view, hover over virtual cluster that you want to give someone access to
- While hovering over the row, you will see buttons appear on the right in the Actions column
- Click on the button to Edit the virtual cluster
- In the drawer that appears on the right, expand the section
- Use the Who should have access to this Virtual Cluster? field to select all Users/Teams who should get access to this virtual cluster
OPTIONAL: To change the access of any of the selected users or team to this virtual cluster, expand the vcluster Admin role to another appropriate role for each user or team if needed
section and use the selectors to change the- On the very bottom, click on the button to save the changes
To give someone access to a virtual cluster using Loft CLI, run:
loft share vcluster [optional:name]