Multi-Tenancy in Kubernetes using Loft’s Vcluster

Pavan Kumar
Nerd For Tech
Published in
6 min readApr 30, 2022

--

Multi-Tenancy in Kubernetes using Loft Vcluster

Kubernetes is almost everywhere. If you want to deploy a web application, you’d need Kubernetes. You’d need to train an ML algorithm ( Kubeflow ) you’d need Kubernetes. Run Data analytics you'd need Kubernetes. So ideally Kubernetes is being used in every possible way. But are you using it the right way? Are you saving costs? Are you making use of the entire compute resources? Are you also sharing it the right way? Ahh, this is the actual point of the article, Multi-Tenancy. Multitenancy is a reference to the mode of operation of software where multiple independent instances of one or multiple applications operate in a shared environment. The instances (tenants) are logically isolated, but physically integrated.“Tenants” is a term for a group of users or software applications that all share access to the hardware through the underlying software. Multiple tenants on a server all share the memory, which is dynamically allocated and cleaned up as needed. They also share access to system resources, such as the network controller.

Image Credits: Google

What is the entire story all about? (TLDR)

  1. Multi-Tenancy in Kubernetes.
  2. Using loft.sh’s Vcluster solution.
  3. Exploring various scenarios in multitenancy using loft.sh vcluster.
Loft Vcluster comparison

Prerequisites

  1. A Kubernetes Cluster ( EKS, AKS, Kind, etc ).
  2. helm, loft.sh’s vcluster binary.

Story Resources

  1. GitHub Link: https://github.com/pavan-kumar-99/medium-manifests
  2. GitHub Branch: multitenancy

Scenarios for Multi-Tenancy ( Problem Statement )

Earlier I have explained the definitions of multitenancy and the tools that are used to enable this. But first, let us understand the problem statement for this use case. Assume you are a company that provides Kubernetes clusters to customers to deploy their workloads. Let us suppose you have 100 customers. Provisioning one cluster per customer is a nightmare. Managing those clusters is not an easy task, as the number of customers grows the number of clusters also increases. Drawbacks of having such architecture?

  1. Increase in the overall cost.
  2. Redundancy of the components to be installed ( For example bootstrap components like istio, vault, consul, etc to be installed on all the clusters ).
  3. Management of the clusters will be a nightmare.
  4. Lots of duplicate work to be done.
  5. Heavy spike in the costs ( You end up paying money for both the control plane and worker nodes ).
  6. No Isolation.
  7. Each customer would end up using a lot more than allocated resources. We cannot control the number of resources that the customer can use.

What if? What if there is a solution to this. How would you react if you can create a Kubernetes cluster inside a Kubernetes cluster?

Surprised

Yes, you have read that right. Here comes loft.sh’s vcluster ( virtual cluster ) into the picture. Virtual clusters are fully working Kubernetes clusters that run on top of other Kubernetes clusters. Compared to fully separate “real” clusters, virtual clusters do not have their own node pools. Instead, they are scheduling workloads inside the underlying cluster while having their own separate control plane. The virtual cluster itself only consists of the core Kubernetes components: API server, controller manager, and storage backend (such as etcd, SQLite, MySQL, etc.).

vcluster Architecture

Advantages of using loft.sh vcluster

  1. Fine-grained Isolation per tenant.
  2. Reduction in the Overall Cost.
  3. No Redundancy of components.
  4. Easy management of tenants/clusters.
  5. Resource allocation per tenant can be controlled easily.

Well, I hope you now have a clear picture of multitenancy and Virtual clusters. Let us now get started with the Demo. :)

Installing Vcluster using helm

Make sure you have vcluster and helm binary installed

I am already having a Kubernetes cluster up and running. You can use any Kubernetes distribution for this demo. Let us now create two namespaces in our physical cluster. These namespaces are nothing but our customers. Let us name the customers as

  1. customer-1 ( Trial Customer )
  2. customer-2 ( Paid Customer )
$ git clone https://github.com/pavan-kumar-99/medium-manifests.git \
-b multitenancy
$ cd medium-manifests

Trial Customer

Alright, we have now created two namespaces for two new customers. So let us assume that customer-1 is a free trial customer and he should only be given very few compute resources. For example CPU: 5cores, Memory: 10Gi, Pods 3 etc.

Free tier customer

Let us now design a Virtual Cluster ( Vcluster ) for the free tier customer.

$ kubectl create ns customer-1$ kubectl config set-context --current --namespace=customer-1$ helm upgrade --install customer-1 vcluster \--values customer1-values.yaml \--repo https://charts.loft.sh \--namespace customer-1

The vcluster should now be created. You can check the pod in the customer-1 namespace. Things would now get interesting. Open another terminal tab and execute the following commands to connect to your virtual cluster.

$ vcluster connect customer-1 --namespace customer-1$ export KUBECONFIG="./kubeconfig.yaml"
Connected to Vcluster :)

Let me create a namespace in the Virtual cluster.

$ kubectl create ns test

The namespace should now be visible in the vcluster but not in the actual host cluster. Let us now create a deployment and scale the replicas to 2 ( As the hard limit is 3, no more than 2 pods will be able to run on the virtual cluster. There is already one pod by a system component ). The same is also the case with the other Kubernetes components.

Ahhh, There you see !!!!

Paid Customer

Now let’s switch to the new customer ( Customer-2 ). The Paid customer should be having a huge quantity of resources including CPU, memory, services, etc.

Paid Customer
$ kubectl create ns customer-2$ kubectl config set-context --current --namespace=customer-2$ helm upgrade --install customer-2 vcluster \--values customer2-values.yaml \--repo https://charts.loft.sh \--namespace customer-2
Customer 2 Virtual Cluster

A virtual cluster is now created for customer-2 as well.

Open a new terminal tab.

$ vcluster connect customer-2 --namespace customer-2$ export KUBECONFIG="./kubeconfig.yaml"

You should now be able to create a huge number of computing resources including pods, volumes, etc.

Virtual Cluster for customer 2

This is how loft.sh’s Vcluster can be used to achieve multitenancy. The aforementioned example is one of the use-cases of multitenancy. In the Kubernetes Era, there are many more scenarios where multitenancy could be applied. Please feel free to share your experiences/thoughts/ideas to implement multitenancy in Kubernetes. Also, feel free to get in touch for any queries/consultation on Kubernetes here.

Cleanup

$ helm delete customer-1 -n customer-1$ helm delete customer-2 -n customer-2

Here are some of my other articles that may interest you

Until next time…..

Recommended

Further References

--

--

Pavan Kumar
Nerd For Tech

Senior Cloud DevOps Engineer || CKA | CKS | CSA | CRO | AWS | ISTIO | AZURE | GCP | DEVOPS Linkedin:https://www.linkedin.com/in/pavankumar1999/