Skip to main content

Connect to and use vcluster

Now that we deployed a vcluster, let's connect to it, run a couple of kubectl commands inside of it and then understand what happens behind the scenes inside our vcluster's host namespace that is part of the underlying host cluster.

Connection to the vcluster#

By default, vcluster CLI will connect to the virtual cluster either directly (on local Kubernetes distributions) or via port-forwarding for remote clusters.

If you want to use vcluster without port-forwarding, you can take a look at other supported exposing methods.

Run kubectl commands#

A virtual cluster behaves the same way as a regular Kubernetes cluster. That means you can run any kubectl command and since you are admin of this vcluster, you can even run commands like these:

kubectl get namespace
kubectl get pods -n kube-system

Let's create a namespace and a demo nginx deployment to understand how vclusters work:

kubectl create namespace demo-nginx
kubectl create deployment nginx-deployment -n demo-nginx --image=nginx

You can check that this demo deployment will create pods inside the vcluster:

kubectl get pods -n demo-nginx

What happens in the host cluster?#

The first thing to understand is that most resources inside your vcluster will only exist in your vcluster and not make it to the underlying host cluster / host namespace.

1. Use Host Cluster Kube-Context#

Let's verify this and switch our kube-context back to the host cluster:

# Switch back to the host cluster
vcluster disconnect

2. Check Namespaces#

Now, let's check the namespaces in our host cluster:

kubectl get namespaces
NAME STATUS AGE
default Active 11d
vcluster-my-vcluster Active 9m17s
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d

You will notice that there is no namespace demo-nginx because this namespace only exists inside the vcluster. Everything that belongs to the vcluster will always remain inside the vcluster's host namespace vcluster-my-vcluster.

3. Check Deployments#

So, let's check to see if our deployment nginx-deployment has made it to the underlying host cluster:

kubectl get deployments -n vcluster-my-vcluster
No resources found in host-namespace-1 namespace.

You will see that there is no deployment nginx-deployment because it also just lives inside the virtual cluster.

4. Check Pods#

The last thing to check is pods:

kubectl get pods -n vcluster-my-vcluster
NAME READY STATUS RESTARTS AGE
coredns-66c464876b-p275l-x-kube-system-x-my-vcluster 1/1 Running 0 14m
nginx-deployment-84cd76b964-mnvzz-x-demo-nginx-x-my-vcluster 1/1 Running 0 10m
my-vcluster-0 2/2 Running 0 14m

And there it is! The pod that has been scheduled for our nginx-deployment has actually made it to the underlying host cluster.

The reason for this is that vclusters do not have separate nodes*. Instead, they have a syncer which synchronizes resources from the vcluster to the underlying host namespace to actually get the pods of the vcluster running on the host cluster's nodes and the containers started inside the underlying host namespace.

Renaming

As you can see above in line 3, the names of pods get rewritten during the sync process since we are mapping pods from X namespaces inside the vcluster into one single host namespace in the underlying host cluster.

Benefits of Virtual Clusters#

Virtual clusters provide immense benefits for large-scale Kubernetes deployments and multi-tenancy:

  • Full Admin Access:
    • Deploy operators with CRDs, create namespaces and other cluster scoped resources that's usually not possible in namespaces.
    • Taint and label nodes without any influence on the host cluster.
    • Reuse and share services across multiple virtual clusters with ease.
  • Cost Savings:
    • You can create lightweight vclusters that share the underlying host cluster instead of creating separate "real" clusters.
    • vclusters are just deployments, so they can be easily auto-scaled, purged, snapshotted and moved.
  • Low Overhead:
    • vclusters are super lightweight and only reside in a single namespace.
    • vclusters run with k3s, a super low-footprint k8s distribution, but they can also run with "real" k8s.
    • The control plane of a vcluster runs inside a single pod (+1 CoreDNS pod for vcluster-internal DNS capabilities).
  • No Network Degradation:
    • Since the pods and services inside a vcluster are actually being synchronized down to the host cluster*, they are effectively using the underlying cluster's pod and service networking and are therefore not a bit slower than any other pods in the underlying host cluster.
  • API Server Compatibility:
    • vclusters run with the k3s API server which is certified k8s distro which ensures 100% Kubernetes API server compliance.
    • vcluster have their own API server, controller-manager and a separate, isolated data store (sqlite for easiest option but this is configurable, you can also deploy a full-blown etcd if needed).
  • Security:
    • vcluster users need much fewer permissions in the underlying host cluster / host namespace.
    • vcluster users can manage their own CRDs independently and can even mess with RBAC inside their own vclusters.
    • vclusters provide an extra layer of isolation because each vcluster has its own API server and control plane (much fewer requests to the underlying cluster that need to be secured*).
  • Scalability:
    • Less pressure / fewer requests on the k8s API server in large-scale cluster*
    • Higher scalability of cluster via cluster sharding / API server sharding into smaller vclusters
    • No need for cluster admins to worry about conflicting CRDs or CRD versions with growing number of users and deployments

* Only very few resources and API server requests actually reach the underlying Kubernetes API server. Only workload-related resources (e.g. Pod) and networking-related resources (e.g. Service) need to be synchronized down to the host cluster since the vcluster does not have any nodes or network itself.