Skip to content
ImagineWorld

Building Multitenant Clusters with vClusters

node_idnode-book_kubernetes-an-enterprise-guide_chapter-09-building-multitenant-clusters-with-vclusters
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num9
chapter_titleBuilding Multitenant Clusters with vClusters
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 9 — Building Multitenant Clusters with vClusters

Section titled “Chapter 9 — Building Multitenant Clusters with vClusters”

We’ve alluded to multitenancy in previous chapters, but this is the first chapter where we will focus on the challenges of multitenancy in Kubernetes and how to approach them with a relatively new technology called “virtual clusters.” In this chapter, we’ll explore the use cases for virtual clusters, how they’re implemented, how to deploy them in an automated way, and how to interact with external services with your Pod's identity. We’ll finish the chapter by building and deploying a self-service multitenant portal for Kubernetes.

In this chapter, we’ll cover:

  • The benefits and challenges of multitenancy
  • Using vClusters for tenants
  • Building a multitenant cluster with self service

This chapter will involve a larger workload than previous chapters, so a more powerful cluster will be needed. This chapter has the following technical requirements:

We do our best to test everything, but there are sometimes half a dozen systems or more in our integration labs. Given the fluid nature of technology, sometimes things that work in our environment don’t work in yours. Don’t worry, we’re here to help! Open an issue on our GitHub repo at https://github.com/PacktPublishing/Kubernetes-An-Enterprise-Guide-Third-Edition/issues and we’ll be happy to help you out!

The Benefits and Challenges of Multitenancy

Section titled “The Benefits and Challenges of Multitenancy”

Before we dive into virtual clusters and the vCluster project, let’s first explore what makes multitenant Kubernetes valuable and so difficult to implement. So far, we’ve alluded to challenges with multitenancy, but our focus has been on configuring and building a single cluster. This is the first chapter where we’re going to directly address multitenancy and how to implement it. The first topic we will explore is what multitenant Kubernetes is, and why you should consider using it.

Kubernetes orchestrates the allocation of resources for workloads through an API and a database. These workloads are typically comprised of Linux processes that require specific computing resources and memory. In the initial five to six years of Kubernetes’ evolution, a prevailing trend was to have a dedicated cluster for each “application.” It’s important to note that when we say “application,” it could refer to a single monolithic application, a collection of microservices, or several interrelated monolithic applications. This approach is notably inefficient and results in the proliferation of management complexities and potential wasted resources. To understand this better, let’s examine all of the elements within a single cluster:

  • etcd Database: You should always have an odd number of etcd instances;at least three instances of etcd are needed to maintain high availability.
  • Control Plane Nodes: Similar to etcd, you’ll want at least two control plane nodes, but more likely three for high availability.
  • Worker Nodes: You’ll want a minimum of two nodes, regardless of the load you’re putting on your infrastructure.

Your control plane requires resources, so even though most Kubernetes distributions don’t require dedicated control plane nodes anymore, it’s still additional components to manage. Looking at your worker nodes, how much utilization are the nodes using? If you have a heavily used system, you will get the most out of the hardware, but are all your applications always that heavily utilized? Allowing multiple “applications” to use a single infrastructure can vastly reduce your hardware utilization, requiring fewer clusters, whether you’re running on pre-paid infrastructure or pay-as-you-go infrastructure. Over-provisioning resources will increase power, cooling, rack space, etc., all of which will add additional costs. Fewer servers also means reducing the maintenance requirements for hardware, further reducing costs.

In addition to hardware costs, there’s a human cost to the cluster-per-application approach. Kubernetes skills are very expensive and difficult to find in the market. It’s probably why you’re reading this book! If each application group needs to maintain its own Kubernetes expertise, that means duplicating skills that are difficult to obtain, also increasing costs. By pooling infrastructure resources, it becomes possible to establish a centralized team responsible for overseeing Kubernetes deployments, eliminating the necessity for other teams, whose primary focus lies outside infrastructure development, to duplicate these skills.

There are major security benefits to multitenancy as well. With common infrastructure, it’s easier to centralize enforcement of security requirements. Taking authentication as an example, if every application gets its own cluster, how will you enforce common authentication requirements? How will you onboard new clusters in an automated way? If you’re using OIDC, are you going to integrate a provider for each cluster?

Another example of the benefits of centralized infrastructure is secret management. If you have a centralized Vault deployment, do you want to integrate a new cluster for each application? In Chapter 8, we integrated a cluster with Vault; if you have massive cluster sprawl, the same integration needs to be done to each individual cluster – how will that be automated and managed?

Moving to a multitenant architecture reduces your long-term costs by reducing the amount of infrastructure needed to run your workload. It also cuts down on the number of administrators needed to manage infrastructure and makes it easier to centralize security and policy enforcement.

While multitenancy provides a significant advantage, it does come with some challenges. Next, we’ll explore the challenges of implementing multitenant Kubernetes clusters.

We explored the benefits of multitenant Kubernetes, and while the adoption is growing for multitenancy, why isn’t it as common as the cluster-per-application approach?

Creating a multitenant Kubernetes cluster requires several considerations for security, usability, and management. Implementing solutions for these challenges is often very implementation-specific and requires integration with third-party tools, which are outside the scope of most distributions.

Most enterprises add an additional layer of complexity based on their management silos. Application owners are judged based on their own criteria and by their own management. Anyone whose pay is dependent on certain criteria will want to make sure they have as much control of those criteria as possible, even if it’s for the wrong reasons. This silo effect can have an adversely negative impact on any centralization effort that doesn’t afford appropriate control to application owners. Since these silos are unique to each enterprise, it’s impossible for a single distribution to account for them in a way that is easily marketable. Rather than deal with the additional complexities, it’s much easier for a vendor to market a cluster-per-application approach.

With the fact that there are few multitenant Kubernetes distributions on the market, the next question becomes “What are the challenges of making a generic Kubernetes cluster multitenant?”

We’re going to break down the answers to this question by impact:

  • Security: Most containers are just Linux processes. We’ll cover this in more detail in Chapter 12, Node Security with Gatekeeper. What’s important to understand for now is that there is very little security that separates processes on a host. If you’re running processes from multiple applications, you want to make sure that a breakout doesn’t impact other processes.
  • Container breakouts: While this is important for any cluster, it’s a necessity in multitenant clusters. We will cover securing our container runtimes in Chapter 13, KubeArmor Securing Your Runtime.
  • Impact: Any issues with centralized infrastructure will have adverse impacts on multiple applications. This is often referred to as “blast radius.” If there’s an upgrade that goes wrong or fails, who’s impacted? If there’s a container breakout, who’s impacted?
  • Resource Bottlenecks: While it’s true that a centralized infrastructure gets better utilization of resources, it can also create bottlenecks. How quickly can you onboard a new tenant? How much control do application owners have in their own tenants? How difficult is it to grant or revoke access? If your multitenant solution can’t keep up with application owners, application owners will take on the infrastructure themselves. This will lead to wasted resources, configuration drift, and difficulty reporting and auditing all of the clusters.
  • Restrictions: A centralized platform that is overly restrictive will result in application owners looking to either maintain their own infrastructure or outsource their infrastructure to third-party solutions. This is one of the most common issues with any centralized service that can be best illustrated by the continuous rise and fall of Platform as a Service (PaaS) implementations that fail to provide the flexibility needed for application workloads.

While these issues can be applied to any centralized service, they do have some unique impacts on Kubernetes. For instance, if each application gets its own namespace, how can an application properly subdivide that namespace for different logical functions? Should you grant multiple namespace per application?

Another major impact on Kubernetes cluster designs is the deployment and management of Custom Resource Definitions (CRDs). CRDs are cluster-level objects, and running multiple versions is nearly impossible in the same cluster; as we have pointed out in previous chapters, CRDs are growing in popularity as a way of storing configuration data. Multitenant clusters may have version conflicts that need to be managed.

Many of these challenges will be addressed in later chapters, but in this chapter, we’re going to focus on two aspects:

  • Tenant Boundaries: What is the scope of a tenant? How much control within the boundary does the tenant have?
  • Self-Service: How does a centralized Kubernetes service interact with users?

Both aspects will be addressed by adding two components to our clusters. OpenUnison has already been introduced to handle authentication and will be extended for its self-service capabilities with namespace as a Service. The other external system will be vCluster, from Loft Labs.

Since we have already used OpenUnison to demonstrate namespace as a Service, we can move on to the additional challenges, like CRD versioning issues in the next section using the vCluster project.

In the KinD chapter, we explained that KinD is nested in Docker to provide us with a full Kubernetes cluster. We compared this to nesting dolls, where components are embedded in other components, which can cause confusion to users who are newer to containers and Kubernetes. vCluster is a similar concept – it creates a virtual cluster in the main host clusters, and while it does appear to be a standard Kubernetes cluster, it is nested within the host clusters. Keep this in mind as you are reading the rest of the chapter.

In the previous section, we walked through the benefits and challenges of multitenancy and how those challenges impact Kubernetes. In this section, we’re going to introduce the vCluster project from Loft Labs, which allows you to run a Kubernetes control plane inside of an unprivileged namespace. This allows each tenant to get their own “virtual” Kubernetes infrastructure that they can have complete control over without impacting other tenants’ own implementation or other workloads in the “main cluster.”

Graphical user interface, application Description automatically generated

Figure 9.1: Logical layout of a vCluster

In the above diagram, each tenant gets their own namespace, which runs a vCluster. The vCluster is a combination of three components:

  • Database: Somewhere that can store the vCluster’s internal information. This may be etcd or a relational database, depending on which cluster type you deploy vCluster with.
  • API Server: The vCluster includes its own API server for its pods to interact with. This API server is backed by the database managed by the vCluster.
  • Synchronization Engine: While a vCluster has its own API server, the pods are all run in the host cluster. To achieve this, the vCluster synchronizes certain objects between the host and vCluster. We’ll cover this in greater detail next.

The benefit of the vCluster’s approach is that from the Pod's perspective, it’s working within a private cluster while it’s really running in a main host cluster. The tenant can divide its own cluster into whatever namespace suit it and deploy CRDs, or operators, as needed.

A picture containing text Description automatically generated

Figure 9.2: Pod’s perspective of a vCluster

In the above diagram, we see that the pod is deployed into our tenant’s namespace, but instead of communicating with the host cluster’s API server, it’s communicating with the vCluster’s API server. This is possible because the pod definition that gets synchronized from the vCluster into the host cluster has its environment variables and DNS overwritten to tell the pod that the host kubernetes.default.svc points to the vCluster, not the host cluster’s API server.

Since the pod runs in the host cluster, and the vCluster runs in the host cluster, all of the pods are subject to the ResourceQuotas put in place on the namespace. This means that any pod deployed to a vCluster is bound by the same rules as a pod deployed directly into a namespace including any restrictions created by quotas, policies, or other admission controllers. In Chapter 11, Extending Security Using Open Policy Agent, you’ll learn about using admission controllers to enforce policies across your cluster. Since the pod is running in the host cluster, you only need to apply those policies to your host. This vastly simplifies our security implementation because, now, tenants can be given cluster-admin access to their virtual clusters without compromising the security of the host cluster.

Another important note is that because the host cluster is responsible for running your pod, it’s also responsible for all of the vClusters Ingress traffic. You do not have to redeploy your Ingress controller on each vCluster – they share the host Ingress controller, reducing the need for maintaining additional Ingress deployments or creating multiple wildcard domains for each vCluster.

Now that we have a basic understanding of what a vCluster is and how it helps to address some of the challenges of multitenancy in Kubernetes, the next step is to deploy a vCluster and see how the internals work.

In the previous section, we focused on the theory behind how a vCluster works and is implemented. In this section, we’re going to deploy a vCluster and a simple workload so that we can see what changes have occurred between a pod that runs in a vCluster and what is deployed into the host cluster.

The first step is to create a new cluster – we will delete the existing KinD cluster and deploy a fresh one. We will then execute a script called deploy_vcluster_cli.sh in the chapter9 directory:

$ kind delete cluster -n cluster01
$ cd chapter2
$ ./create-cluster.sh
$ cd ../chapter9
$ ./deploy_vcluster_cli.sh

At this point, we have a fresh cluster and the CLI for deploying vClusters.

The next step is to create a vCluster. First, we will create a new namespace called tenant1, and then use the vCluster utility to create a new vCluster called myvcluster in the new namespace:

$ kubectl create ns tenant1
$ vcluster create myvcluster --distro k3s -n tenant1

Once the vcluster command has completed, we’ll have a running vCluster, built using k3s, running on the host cluster. Logically, it is its own cluster and we can “connect” directly to it using the vClusters kubeconfig or the vcluster utility.

vCluster is designed to support multiple Kubernetes cluster implementations. The default, and most common, is k3s, which is a Kubernetes implementation that replaces etcd with a relational database and replaces the multiple binaries with a single binary. It was originally developed for edge deployments but works well for single tenants too. We could use k0s from Mirantis or even a vanilla Kubernetes, but k3s does well for most situations.

Let’s disconnect and see what’s running in our host:

$ vcluster disconnect
info Successfully disconnected from vcluster: myvcluster and switched back to the original context: kind-cluster01
$ kubectl get pods -n tenant1
NAME READY STATUS RESTARTS AGE
coredns-864d4658cb-mdcx5-x-kube-system-x-myvcluster 1/1 Running 0 4m45s
myvcluster-0 2/2 Running 0 5m12s

As shown in the output above, there are two pods running in the tenant1 namespace: CoreDNS and our vCluster. If we look at the services in our namespace, you will see a list of services similar to the following:

$ kubectl get svc -n tenant1
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns-x-kube-system-x-myvcluster ClusterIP 10.96.142.24 <none> 53/UDP,53/TCP,9153/TCP 7m18s
myvcluster NodePort 10.96.237.247 <none> 443:32489/TCP,10250:31188/TCP 7m45s
myvcluster-headless ClusterIP None <none> 443/TCP 7m45s
myvcluster-node-cluster01-worker ClusterIP 10.96.209.122 <none> 10250/TCP 7m18s

There are several services set up to point to our vCluster’s API server and DNS server that provide access to the vCluster, making it logically appear as a “full” standard cluster.

Now let’s connect to our vCluster and deploy a pod. In the chapter9/simple directory, we have a pod manifest that we will use for our example. First, we will connect to the cluster and deploy the example pod using kubectl in the chapter9/simple directory:

$ vcluster connect myvcluster -n tenant1
$ cd chapter9/simple
$ kubectl create -f ./virtual-pod.yaml
$ kubectl logs -f virtual-pod
Wed Sep 20 17:50:03 UTC 2023
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=virtual-pod
PWD=/
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.96.237.247:443
SHLVL=1
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.96.237.247
KUBERNETES_SERVICE_HOST=10.96.237.247
KUBERNETES_PORT=tcp://10.96.237.247:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.142.24

Notice that the Pod's environment variables use 10.96.237.247 as the IP address for the API server; this is the ClusterIP from the mycluster Service that’s running on the host. Also, the nameserver is 10.96.142.24, which is the ClusterIP for our vCluster’s kube-dns Service in the host. As far as the pod is concerned, it thinks it is running inside of the vCluster. It doesn’t know anything about the host cluster. Next, disconnect from the vCluster and take a look at the pods in our tenant1 namespace on the host cluster:

$ vcluster disconnect
$ kubectl get pods -n tenant1
NAME READY STATUS RESTARTS AGE
coredns-864d4658cb-mdcx5-x-kube-system-x-myvcluster 1/1 Running 0 22m
myvcluster-0 2/2 Running 0 22m
virtual-pod-x-default-x-myvcluster 1/1 Running 0 7m11s

Notice that your vCluster’s pod is running in your host cluster. The Pod ‘s name on the host includes both the name of the pod and the namespace from the vCluster. Let’s take a look at the pod definition. We’re not going to put all of the output here because it would take up multiple pages. What we want to point out is that in addition to our original definition, the pod includes a hard-coded env section:

env:
- name: KUBERNETES_PORT
value: tcp://10.96.237.247:443
- name: KUBERNETES_PORT_443_TCP
value: tcp://10.96.237.247:443
- name: KUBERNETES_PORT_443_TCP_ADDR
value: 10.96.237.247
- name: KUBERNETES_PORT_443_TCP_PORT
value: "443"
- name: KUBERNETES_PORT_443_TCP_PROTO
value: tcp
- name: KUBERNETES_SERVICE_HOST
value: 10.96.237.247
- name: KUBERNETES_SERVICE_PORT
value: "443"
- name: KUBERNETES_SERVICE_PORT_HTTPS
value: "443"

It also includes its own hostAliases:

hostAliases:
- hostnames:
- kubernetes
- kubernetes.default
- kubernetes.default.svc
ip: 10.96.237.247

So, while the Pod is running in our host cluster, all of the things that tell the pod where it’s running are pointing to our vCluster.

In this section, we launched our first vCluster and a pod in that vCluster to see how it gets mutated to run in our host cluster. In the next section, we’re going to look at how we can access our vCluster with an eye on the same enterprise security we’re required to use in our host cluster.

In the previous section, we deployed a simple vCluster and accessed the vCluster using the vcluster connect command. This command first creates a port-forward to the vCluster’s API server Service and then adds a context with a master certificate to our kubectl configuration file, which is similar to our KinD cluster.

We spent much of Chapter 6, Integrating Authentication into Your Cluster, walking through why this is an anti-pattern, and those reasons still apply to vClusters. You’re still going to need to integrate enterprise authentication into your vCluster. Let’s look at two approaches:

  • Decentralized: You can leave authentication as an exercise to the cluster owner. This negates many of the advantages of multitenancy and will require that each cluster is treated as a new integration into your enterprise’s identity system.
  • Centralized: If you host an identity provider (IdP) in your host cluster, you can tie each vCluster to that IdP instead of directly to the centralized identity store. In addition to providing centralized authentication, this approach makes it easier to automate the onboarding of new vClusters and limits the amount of secret information, such as credentials, that needs to be stored in the vCluster.

The choice is clear when working in a multitenant environment; your host cluster should also host central authentication.

The next issue to understand regarding vCluster access is the network path to your vCluster. The vcluster command creates a local port-forward to your API server. This means that every time a user wants to use their API server, they’ll need to set up a port-forward to their API server. This isn’t a great user experience (UX) and can be error-prone. It would be better to set up a direct connection to our vCluster’s API server, as we would for any standard Kubernetes cluster. The challenge with setting up direct network access to our vCluster’s API server is that while it’s a NodePort, nodes are rarely exposed directly to the network. They usually sit behind a load balancer and rely on Ingress controllers to provide access to cluster resources.

The answer is to use the application infrastructure our host cluster already provides for our vClusters.

In Chapter 6, Integrating Authentication into Your Cluster, we talked about using impersonating proxies with cloud-managed clusters. The same scenario can be applied to vClusters. While you can configure k3s to use OIDC for authentication, using an impersonating proxy vastly simplifies the network management because we’re not creating new load balancers or infrastructure to support our vClusters.

Diagram Description automatically generated

Figure 9.3: vCluster with authentication

In the above diagram, we can see how the networking and authentication come together in our host cluster. The host cluster will have an OpenUnison that authenticates users to our Active Directory. Our vCluster will have its own OpenUnison with a trust established with our host cluster’s OpenUnison. The vCluster will use kube-oidc-proxy to translate the authentication tokens from OpenUnison into impersonation headers to our vCluster’s API server. This approach gives us a central authentication and networking system, while also making it easier for vCluster owners to incorporate their own management applications without having to get the host cluster team involved. Local cluster management applications such as ArgoCD and Grafana can all be integrated into the vCluster’s OpenUnison instead of the host cluster’s OpenUnison.

To show our setup in action, the first thing we need to do is update our vCluster so that it will synchronize Ingress objects from our vCluster into our host cluster, using the vcluster tool. In the chapter/host directory, we have an updated values file called vcluster-values.yaml; we will use this values file to upgrade the vCluster in the tenant1 namespace:

$ cd chapter9/host
$ vcluster create myvcluster --upgrade -f ./vcluster-values.yaml -n tenant1

This command will update our vCluster to synchronize the Ingress objects we create in our vCluster into our host cluster. Next, we’ll need OpenUnison running in our host cluster:

$ vcluster disconnect
$ ./deploy_openunison_imp_impersonation.sh

Before deploying anything, we want to make sure that we’re running against the host, not our vCluster. The script we just ran is similar to what we ran in Chapter 6, Integrating Authentication into Your Cluster; it will deploy our “Active Directory” and OpenUnison to the vCluster. Once OpenUnison has been deployed, the last step is to run the satellite deployment process for OpenUnison:

$ vcluster disconnect
$ ./deploy_openunison_vcluster.sh

This script is like the host’s deployment script with some key differences:

  • The values for our OpenUnison do not contain any authentication information.
  • The values for our OpenUnison have a different cluster name from our host cluster.
  • Instead of running ouctl install-auth-portal, the script runs ouctl install-satelite, which sets up OpenUnison to use OIDC between the satellite cluster and the host cluster. This command creates the oidc section of the values.yaml file for us.

Once the script has completed executing, you can log in to OpenUnison just as you did in Chapter 6. In your browser, go to https://k8sou.apps.X-X-X-X.nip.io, where X-X-X-X is the IP address of your cluster, but with dashes instead of dots. Since our cluster is at 192.168.2.82, we use https://k8sou.apps.192-168-2-82.nip.io/. To log in, use the user mmosley with the password start123.

Once you’re logged in, you’ll see that there is now a tree with options for Host Cluster and tenant1. You can click on tenant1, then click on the tenant1 Tokens badge.

Graphical user interface, application, website, Teams Description automatically generated

Figure 9.4: OpenUnison portal page

Once the new page loads, you can grab your kubectl configuration and paste it into your terminal:

Graphical user interface, text, application Description automatically generated

Figure 9.5: OpenUnison kubectl configuration generator

Depending on which client you’re running on, you can paste this command into a Windows or Linux/macOS terminal and start using your vCluster without having to distribute the vcluster CLI tool and while using your enterprise’s authentication requirements.

In this section, we looked at how to integrate enterprise authentication into our vClusters and how to provide consistent network access to our vClusters as well. In the next section, we’ll explore how to integrate our vClusters with external services, such as HashiCorp’s Vault.

Accessing External Services from a vCluster

Section titled “Accessing External Services from a vCluster”

In the previous chapter, we integrated a HashiCorp Vault instance into our cluster. Our pods communicated with Vault using the tokens projected into our pods, allowing us to authenticate to Vault without a pre-shared key or token and using short-lived tokens. Relying on short-lived tokens reduces the risk that a compromised token can be used against your cluster.

The pod-based identity used with Vault gets more complex with vClusters because the keys used to create the Pod's tokens are unique to the vCluster. Also, Vault needs to know about each vCluster in order to verify the projected tokens used in the vCluster.

If we are running our own Vault in our host cluster, we could automate the onboarding so that each new vCluster is registered with Vault as its own cluster. The challenge with this approach is that Vault is a complex system that’s often run by its own team with its own onboarding process. Adding a new vCluster in a way that works for the team that owns Vault may not be as simple as calling some APIs. Therefore, before we can implement a strategy for integrating our vClusters into Vault, we need to examine how vClusters handle identity.

A pod running in a vCluster has two distinct identities:

  • vCluster Identity: The token that is projected into our pod from a vCluster is scoped to the API server for the vCluster. It was signed by a unique key that the host cluster has no knowledge of. It is associated with the ServiceAccount the pod runs as inside of the vCluster’s API server.
  • Host Cluster Identity: While the pod is defined on the vCluster, it’s executed in the host cluster. This means that the security context of the pod will run and it requires a distinct identity from the vCluster. It will have its own name and its own signing keys.

If we inspect a pod from our vCluster as synchronized into our host cluster, we’ll see that there’s an annotation that contains a token in it:

vcluster.loft.sh/token-ejijuegk: >-
eyJhbGciOiJSUzI1NiIsImtpZCI6IkVOZDVhZnEzUzdtLXBSR2JUM3RJUkRHM0FqWkhzQV9KSkNZcm8yMHdNVUUifQ…

This token is injected into our pod via the fieldPath configuration later in the pod. This could be a security issue since anything that logs the Pod ‘s creation, such as an audit log, can now leak a token. The vCluster project has a configuration to generate Secret objects in the host cluster for project tokens so that they’re not in the pod manifest. Adding the following to our values.yaml file will fix this issue:

syncer:
extraArgs:
- --service-account-token-secrets=true

With that done, let’s update our cluster and redeploy all the Pods:

$ vcluster disconnect
$ vcluster create myvcluster --upgrade -f ./vcluster-values-secrets.yaml -n tenant1
$ kubectl delete pods --all --all-namespaces --force

In a moment, the pods inside of the vCluster will come back. Inspecting the Pod, we see that the token is no longer in the Pod ‘s manifest but is now mounted to a Secret in the host. This is certainly an improvement, as audit systems are generally more discreet about logging the contents of Secrets. Next, let’s inspect our token’s claims.

If we inspect this token, we’ll see some issues:

{
"aud": [
"https://kubernetes.default.svc.cluster.local"
],
"exp": 2010881742,
"iat": 1695521742,
"iss": "https://kubernetes.default.svc.cluster.local",
"kubernetes.io": {
"namespace": "openunison",
"pod": {
"name": "openunison-orchestra-d7bc468bc-qhpts",
"uid": "fb6874f7-c01c-4ede-9062-ce5409509200"
},
"serviceaccount": {
"name": "openunison-orchestra",
"uid": "3d9c9147-d0e0-43f6-9e4d-a0d7db0c6b8c"
}
},
"nbf": 1695521742,
"sub": "system:serviceaccount:openunison:openunison-orchestra"
}

The exp and iat claims are in bold because, when you translate this from Unix Epoch time to something a human can understand, this token is good from Sunday, September 24, 2023 2:15:42 AM until Wednesday, September 21, 2033 2:15:42 AM. That’s a ten-year token! This ignores the fact that the token was configured in the pod to only be good for ten minutes. This is a known issue in vCluster. The good news is that the tokens themselves are projected, so when the pod they’re projected into dies, the API server will no longer accept these tokens.

The issue of vCluster token length comes into play when accessing external services because this will be true of any token we generate, not just tokens for the vCluster API server. When we integrated our clusters into Vault in the previous chapter, we did so using our Pod's identity so that we could leverage shorter-lived tokens that aren’t static and have well-defined expirations. A ten-year token is effectively a token with no expiration. The main mitigation is that we configured Vault to verify the status of the token before accepting it, so a token bound to a destroyed pod will be rejected by Vault.

The alternative to using the vCluster’s injected identity is to leverage the host cluster’s injected identity. This identity will be governed by the same rules as any other identity generated by the TokenRequest API in the host cluster. There are two issues with this approach:

  • vCluster disables host tokens: In the host synced pod, automountServiceAccountToken is false. This is to prevent a collision between the vCluster and the host cluster because our pod shouldn’t know the host cluster exists! We can get around this by creating a mutating webhook that will add a TokenRequest API projection in the host cluster that can be accessed by our pod.
  • Host tokens don’t have vCluster namespaces: When we do generate a host token for our synced pod, the namespace will be embedded in the name of the ServiceAccount, not as a claim in the token. This means that most external services’ policy languages will not be able to accept policies based on the host token, but configured via a namespace without creating a new policy for each vCluster namespace.

These approaches both have benefits and drawbacks. The biggest benefit to using vCluster tokens is that you can easily create a policy that allows you to limit access to secrets based on namespaces inside of your vCluster without creating new policies for each namespace. The downside is the issues with vCluster tokens and the fact that you now need to onboard each individual vCluster into your Vault. Using host tokens better mitigates the issues with vCluster tokens, but you’re not able to easily create generic policies for each vCluster in Vault.

In this section, we spent time understanding how vClusters manage pod identities and how those identities can be used to interact with external services, such as Vault. In the next section, we will spend time on what’s needed to create a highly available vCluster and manage operations.

Creating and Operating High-Availability vClusters

Section titled “Creating and Operating High-Availability vClusters”

So far in this chapter, we’ve focused on the theory of how vClusters work, how to access a vCluster securely, and how vClusters handle pod identity to interact with external systems. In this section, we’ll focus on how to deploy and manage vClusters for high availability. Much of the documentation and examples for vCluster focuses on vCluster as a development or testing tool. For the use cases discussed earlier in this chapter, we want to focus on creating vClusters that can run production workloads. The first part of building production-ready vClusters is to understand how to run a vCluster in a way that allows for failures or downtime of individual components without hampering the vCluster’s ability to run.

Let’s define the goal of a highly available vCluster and the gap between our current deployments and that goal. When you have a highly available vCluster, you want to make sure that:

  • You can continue to interact with the API server during an upgrade or migration to another physical node in either the host cluster or the vCluster.
  • If there’s a catastrophic issue, you can restore from a backup.

When running a vCluster, the first point about being able to interact with an API server becomes clear while upgrading the host cluster’s nodes. During that upgrade process, you want your API server to be able to continue to run. You want to be able to continue syncing objects from your virtual API server into your host; you also want pods that interact with the API server to still be able to do so during a physical host upgrade. For instance, if you’re using OpenUnison, then you want it to be able to create session objects so users can interact with their vClusters while host cluster operations are happening.

The second point about disaster recovery is also important. We hope to never need it, but what happens if we’ve irrevocably broken our vCluster? Can we restore back to a point that we know was functional?

The first aspect of understanding how to run a highly available vCluster is that it will need multiple instances of pods that run the API server, syncer, and CoreDNS. If we look at our tenant1 namespace, we’ll see that our vCluster has one pod that is associated with a StatefulSet that hosts the vCluster’s API server and syncer. There is also a pod that is synced from inside the vCluster for CoreDNS. We’d want there to be at least two (better if three) instances of each of these pods so that we can tell our API server to use PodDisruptionBudget to make sure we have a minimum number of instances running so that one can be brought down for whatever event is occurring.

The second aspect to understand is how vCluster manages data. Our current deployment uses k3s, which uses a local SQLite database with data stored on a PersistentVolume. This works well for development, but for a production cluster, we want each component of our vCluster to be working off the same data. For k3s -based vClusters, this means using one of the supported relational databases or etcd. We could deploy etcd, but a relational database is generally easier to manage. We’re going to deploy our database in-cluster, but it wouldn’t be unusual to use an external database as well. In our exercises, we’ll use MySQL. We won’t worry about building a highly available database for our examples, since each database has its own mechanisms for high availability. If this were a production deployment though, you’d want to make sure that your database is built using the project’s recommended HA deployment and that you have a regular backup and recovery plan in place. With that said, let’s start by tearing down our current cluster and creating a new one:

$ kind delete cluster -n cluster01
$ cd chapter2/HAdemo
$ ./create-multinode.sh

Wait for the new multi-node cluster to finish launching. Once it’s running, deploy MySQL:

$ cd ../../chapter9/ha
$ ./deploy_mysql.sh

If the deploy_mysql.sh script fails with “Can’t connect to local MySQL server through socket,” wait a moment and rerun it. It’s safe to rerun. This script:

  1. Deploys the cert-manager project with self-signed ClusterIssuers.
  2. Creates TLS keypairs for MySQL.
  3. Installs MySQL as a StatefulSet and configures it to accept TLS authentication.
  4. Creates a database for our cluster and a user that’s configured to authenticate via TLS.

With MySQL deployed and configured for TLS authentication, we’ll next create the tenant namespace and a certificate that will map to our database user:

$ kubectl create -f ./vcluster-tenant1.yaml

Finally, we can deploy our vCluster:

$ vcluster create tenant1 --distro k3s --upgrade -f ./vcluster-ha-tenant1-vaules.yaml -n tenant1 --connect=false

It will take a few minutes, but you’ll have four pods in the tenant1 namespace:

NAME READY STATUS RESTARTS AGE
coredns-6ccdd78696-r5kmd-x-kube-system-x-tenant1 1/1 Running 0 88s
coredns-6ccdd78696-rw9lv-x-kube-system-x-tenant1 1/1 Running 0 88s
tenant1-0 2/2 Running 0 2m16s
tenant1-1 0 2m16s

We can now leverage PodDisruptionBudget to tell Kubernetes to keep one of the vCluster pods running during upgrades.

Speaking of upgrades, the next question is how to upgrade our vCluster. Now that we have a highly available vCluster, we can look to upgrade our vCluster to a new version.

It’s important that you know how to upgrade your vClusters. You’ll want to make sure that your vCluster and host cluster don’t drift too far apart. While the pods that are synced into your host cluster will communicate with your vCluster’s API server, any impact on the synchronized pods (and other synchronized objects) could impact your workloads.

Given the importance of staying up to date, it is great to report that upgrading a vCluster is incredibly easy. It’s important to remember that vCluster orchestrates the clusters and synchronizes objects, but the clusters themselves are managed by their own implementation. In our deployments, we’re using k3s, which will upgrade its data storage in the database when the new pods are deployed. Since the vcluster create command is a wrapper for Helm, all we need to do is update our values with the new image and redeploy:

$ kubectl get pod tenant1-0 -n tenant1 -o json | jq -r '.spec.initContainers[0].image'
rancher/k3s:v1.29.5-k3s1
$ vcluster create tenant1 --upgrade -f ./vcluster-ha-tenant1-vaules-upgrade.yaml -n tenant1 --connect=false

This command upgrades our vCluster to use a k3s 1.30 image, which is just running a Helm upgrade on our installed chart. You’re leveraging the power of Kubernetes to simplify upgrades! Once it’s done running, you can check that the pods are now running k3s 1.30:

$ kubectl get pod tenant1-0 -n tenant1 -o json | jq -r '.spec.initContainers[0].image'
rancher/k3s:v1.30.1-k3s1

We’ve covered creating highly available clusters and how to upgrade our vClusters. This is enough to embark on building a multitenant cluster. In the next section, we’ll integrate what we have learned to build out a multitenant cluster where each tenant gets their own vCluster.

Building a Multitenant Cluster with Self Service

Section titled “Building a Multitenant Cluster with Self Service”

In the previous sections, we explored how multitenancy works, how the vCluster project helps to address multitenancy challenges, and how to configure a vCluster with secure access and high availability. Each of these individual components was addressed as a separate component. The next question is how to integrate all these components into a single service. In this section, we’ll walk through creating a self-service platform for a multitenant cluster.

One of the most important aspects of multitenancy is repeatability. Can you create each tenant the same way consistently? In addition to making sure that your approach is repeatable, what’s the amount of work that a customer needs to go through to get a new tenant? Remember that this book has a focus on enterprise, and enterprises almost always have compliance requirements. You also need to consider how to integrate your compliance requirements into the onboarding process.

The combination of needing repeatability and compliance often leads to the need for a self-service portal for onboarding new tenants. Creating a self-service portal has become the focus of many projects, often as part of a “Platform Engineering” initiative. We’re going to build our self-service platform from OpenUnison’s namespace as a Service portal. Using OpenUnison as our starting point, let’s us focus on how the components will integrate, rather than diving into the specifics of writing the code for the integrations. This multitenant self-service onboarding portal will serve as a starting point that we’ll add to as we explore more aspects of multitenancy through this book.

We’re going to approach our multitenant cluster first by defining our requirements, then analyze how each of those requirements will be fulfilled, and finally, we’ll roll out our cluster and portal. Once we’re done with this section, you’ll have the start of a multitenant platform you can build from.

Our requirements for each individual tenant will be like requirements for a physical cluster. We’re going to want to:

  • Isolate tenants by authorization: Who should have access to make updates to each tenant? What drives access? So far, we’ve been mainly concerned with cluster administrators, but now we need to worry about tenant administrators.
  • Enforce enterprise authentication: When a developer or admin accesses a tenant, we’ll need to ensure that we’re doing so using our enterprise authentication.
  • Externalized Secrets: We want to make sure our source of truth for secret data is outside of our cluster. This will make it easier for our security team to audit usage.
  • High Availability and Disaster Recovery: There are going to be times that we need to impact if a tenant’s API server is running. We’ll need to rely on Kubernetes to make sure that, even during those times, there’s a way for tenants to do their work.
  • Encryption in Transit: All connections between components need to be encrypted.
  • No Secrets in Helm Charts: Keeping secret data in a chart would mean that it’s stored as a Secret in our namespace, violating the requirement to externalize secret data.

We’ve worked with most of these requirements already in this chapter. The key question is, “How do we pull everything together and automate it?” Having read through this chapter and looked through the scripts, you can probably see where this implementation is going. Just like any enterprise project, we need to understand how silos are going to impact our implementation. For our platform, we’re going to assume that:

  • Active Directory cannot be automatically updated: It’s not unusual that you won’t be given the ability to create your own groups via an API to Active Directory (AD). While interacting with AD only requires LDAP capabilities, compliance requirements often dictate that a formal process is followed for creating groups and adding members to those groups.
  • Vault can be automated: Since Vault is API enabled and we have a good relationship with the Vault team, they’ll let us automate the onboarding of new tenants directly.
  • Intra-Cluster Communication does not require the Enterprise CA: Enterprises often have their own certificate authorities (CAs) for generating TLS certificates. These CAs are generally not exposed to an external facing API and are not able to issue intermediate CAs that can be used by a local cert-manager instance. We’ll use a CA specific to our cluster for issuing all certificates for use within the cluster.
  • Host Cluster-Managed MySQL: We’re going to host our MySQL instance on the cluster, but we won’t dive into operations around MySQL. We’ll assume that it’s been deployed as highly available. Database administration is its own discipline, and we won’t pretend to be able to cover it in this section.

With these requirements and assumptions in hand, the next step is to plan out how to implement our multitenant platform.

In the previous section, we defined our requirements. Now, let’s construct a matrix of tools that will tell us what each component will be responsible for:

RequirementComponentNotes
Portal AuthenticationOpenUnison + Active DirectoryOpenUnison will capture credentials; Active Directory will verify them.
TenantKubernetes namespace + vClusterEach tenant will receive their own namespace in the host cluster, with a vCluster deployed to it.
Tenant AuthenticationOpenUnisonEach tenant will receive its own OpenUnison instance.
AuthorizationOpenUnison with Active Directory GroupsEach tenant will have a unique Active Directory group that will provide administrative capabilities.
Certificate Generationcert-manager Projectcert-manager will generate the keys needed to communicate between vCluster and MySQL.
Secrets ManagementCentralized VaultEach tenant will receive its own Vault database that will be enabled with Kubernetes authentication.
OrchestrationOpenUnisonWe’ll use OpenUnison’s workflow engine for onboarding new tenants.

Table 9.1: Implementation matrix

Given our requirements and implementation matrix, our multitenant platform will look like this:

Diagram Description automatically generated

Figure 9.6: Multitenant platform design

Using the above diagram, let’s walk through the steps that will need to occur to implement our platform:

  1. OpenUnison will create a namespace and RoleBinding to our Active Directory group to the admin ClusterRole.
  2. OpenUnison will generate a Certificate object in our tenant’s namespace, which will be used by our vCluster to communicate with MySQL.
  3. OpenUnison will create a database in MySQL for the vCluster and a user tied to the certificate generated in Step 2.
  4. OpenUnison will deploy a Job that will run the vcluster command and deploy the tenant’s vCluster.
  5. OpenUnison will deploy a Job that will deploy the Kubernetes Dashboard, deploy OpenUnison, and integrate the vCluster OpenUnison into the host cluster’s OpenUnison.
  6. OpenUnison will create an authentication policy in Vault that will allow tokens from our tenant’s vCluster to authenticate to Vault using local pod identities. It will also run a Job that will install the vault sidecar into our cluster.

We’re giving our vCluster capabilities in the centralized Vault for retrieving secrets. In an enterprise deployment, you’d also want to control who can log in to Vault using the CLI and web interface using the same authentication and authorization as our clusters to tailor access, but that’s beyond the scope of this chapter (and book).

Finally, you could use any automation engine you’d like to perform these tasks, such as Terraform or Pulumi. If you want to use one of these tools instead, the same concepts can be used and translated into the implementation-specific details. Now that we’ve designed our onboarding process, let’s deploy it.

The previous section focused on the requirements and design of our multitenant platform. In this section, we’re going to deploy the platform and walk through deploying a tenant. The first step is to start with a fresh cluster:

$ kind delete cluster -n cluster01
$ kind delete cluster -n multinode
$ cd chapter2
$ ./create-cluster.sh

Once your cluster is running, the next step is to deploy the portal. We scripted everything:

$ cd chapter9/multitenant/setup/
$ ./deploy_openunison.sh

This script does quite a bit:

  1. Deploys cert-manager with internal CAs for our cluster
  2. Deploys MySQL configured with our internal CA
  3. Deploys OpenUnison, using impersonation, and deploys our customizations for vCluster
  4. Deploys Vault
  5. Integrates Vault and our control plane cluster
  6. Enables OpenUnison to create new authentication mechanisms and policies in Vault

Depending on how much horsepower your infrastructure has, this script can take ten to fifteen minutes to run. Once deployed, the first step will be to log in to the portal at https://k8sou.apps.IP.nip.io/, where IP is your IP address with the dots changed to dashes. My cluster’s IP is 192.168.2.82, so the URL is https://k8sou.apps.192-168-2-102.nip.io/. Use the user mmosley with the password start123. You’ll notice a new badge called New Kubernetes Namespace. Click on that badge.

Graphical user interface, application Description automatically generated

Figure 9.7: OpenUnison front page with the New Kubernetes Namespace badge

On the next screen, you’ll be asked to provide some information for the new namespace (and tenant). We created two groups in our “Active Directory” for managing access to our tenant. While, out of the box, OpenUnison supports both the admin and view ClusterRole for mappings, we’re going to focus on the admin ClusterRole mapping. The admin group for our namespace will also be the cluster-admin for our tenant vCluster. This means any user that is added to this group in Active Directory will gain cluster-admin access to our vCluster for this tenant. Fill out the form as you see in Figure 9.8 and click SAVE.

Graphical user interface, text, application, email Description automatically generated

Figure 9.8: New Namespace

Once saved, close this tab to return to the main portal and hit Refresh. You’ll see that there is a new menu option on the left-hand side called Open Approvals. OpenUnison is designed around self-service, so the assumption is that the tenant owners will request that a new tenant be deployed. In this case, mmosley will be both the tenant owner and the approver. Click on Open Approvals and click Act on Request

Graphical user interface, application Description automatically generated

Figure 9.9: Approval screen

Fill in the Justification field and click on APPROVE REQUEST and CONFIRM APPROVAL. This will approve the user’s request and launch a workflow that implements the steps we designed in Figure 9.6. This workflow will take five to ten minutes, depending on the horsepower of your cluster. Usually, OpenUnison will send the requestor an email once a workflow is complete, but we’re using an SMTP blackhole here to pull in all emails that are generated to make the lab implementation easier. You’ll have to wait until the tenant1 namespace is created and the OpenUnison instance is running. If you look in the tenant1 namespace on the host cluster, you’ll see that the vault-agent-injector pod is running. This lets you know the rollout is complete.

To track your vCluster’s deployment, there are three pods to look at:

  • onboard-vcluster-openunison-tenant1 – The pod from this Job contains the logs for creating and deploying vCluster into your tenant’s namespace.
  • deploy-helm-vcluster-teant1 – The pod from this Job integrates Vault.
  • openunison-orchestra – The pod from this deployment runs OpenUnison’s onboarding workflows.

If there are any errors in the process, you’ll find them here.

Now that our tenant has been deployed, we can log in and deploy a pod. Log out of OpenUnison, and log back in using the user name jjackson and the password start123. The jjackson user is a member of our admin group in Active Directory, so they’ll immediately be able to access and administer the vCluster in the tenant1 namespace.

Graphical user interface, application, website, Teams Description automatically generated

Figure 9.10: Access to tenant1 vCluster

The jjackson user is able to interact with our vCluster the same way they would with the host cluster, using either the dashboard or directly via the CLI. We’re going to use jjackson ‘s session to log in to our tenant vCluster and deploy a pod that uses secret data from our Vault. First, ssh into your cluster’s host on a new session and create a secret in our Vault for our pod to consume:

$ cd chapter9/multitenant/setup/vault
$ . ./vault_cli.sh
$ vault kv put secret/data/vclusters/tenant1/ns/default/config some-password=mysupersecretp@ssw0rd

The last command creates our secret data in the Vault. Note that the path that we created specifies that we’re working with the tenant1 vCluster in the default namespace. The way our cluster is deployed, only pods with ServiceAccounts in the default namespace for our tenant1 vCluster will be able to access the some-password value.

Next, let’s log in to our vCluster using jjackson. First, set your KUBECONFIG variable to a temporary file and set jjackson ‘s session up using the tenant1 token:

$ export KUBECONFIG=$(mktemp)$ export TMP_CERT=$(mktemp)…Cluster "tenant1" set.
Context "tenant1" created.
User "jjackson@tenant1" set.
Switched to context "tenant1".
$ cd ../../examples
$ ./create-vault.sh
$ kubectl logs test-vault-vault-watch -n default
Defaulted container "test" out of: test, vault-agent, vault-agent-init (init)
Wed Oct 11 16:30:37 UTC 2023
MY_SECRET_PASSWORD="mysupersecretp@ssw0rd"

The pod was able to authenticate to our Vault using its own ServiceAccount ‘s identity to retrieve the secret! We did have to make two updates to our pod for our vCluster to connect to Vault:

annotations:
vault.hashicorp.com/service: "https://vault.apps.192-168-2-82.nip.io"
vault.hashicorp.com/auth-path: "auth/vcluster-tenant1"
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/log-level: trace
vault.hashicorp.com/role: cluster-read
vault.hashicorp.com/tls-skip-verify: "true"
vault.hashicorp.com/agent-inject-secret-myenv: 'secret/data/vclusters/tenant1/ns/default/config'
vault.hashicorp.com/secret-volume-path-myenv: '/etc/secrets'
vault.hashicorp.com/agent-inject-template-myenv: |
{{- with secret "secret/data/vclusters/tenant1/ns/default/config" -}}
MY_SECRET_PASSWORD="{{ index .Data "some-password" }}"
{{- end }}

We had to add a new annotation to tell the Vault sidecar where to authenticate. The auth/vcluster-tenant1 authentication path was created by our onboarding workflow. We also needed to set the requested role to cluster-read, which was also created by the onboarding workflow. Finally, we needed to tell the sidecar where to look up our secret data.

With that, we’ve now built the start of a self-service multitenant portal! We’re going to expand on this portal as we dive into more topics that are important to multitenancy. If you want to dive into the code for how we automated the vCluster onboarding, chapter9/multitenant/vlcluster-multitenant is the Helm chart that holds the custom workflows and templates/workflows/onboard-vcluster.yaml is the starting point for all the work that gets done. We broke up each major step in its own workflow to make it easier to read.

Multitenancy is an important topic in modern Kubernetes deployments. Providing a shared infrastructure for multiple tenants cuts down on resource utilization and can provide more flexibility while creating the isolation needed to maintain both security and compliance. In this chapter, we worked through the benefits and challenges of multitenancy in Kubernetes, introduced the vCluster project, and learned how to deploy vClusters to support multiple tenants. Finally, we walked through implementing a self-service multitenant portal and integrated our Vault deployment so tenants could have their own secrets management.

In the next chapter, we’ll dive into the security of the Kubernetes Dashboard. We’ve used it and deployed it in the last few chapters, and now we’re going to understand how its security works and how those lessons learned apply to other cluster management systems too.

  1. Kubernetes Custom Resource Definitions can support multiple versions.
    1. True 2. False
  2. What is the security boundary in Kubernetes?
    1. pods 2. Containers 3. NetworkPolicies 4. Namespaces
  3. Where do the pods in a vCluster run?
    1. In the vCluster 2. In the host cluster 3. There are no pods
  4. vClusters have their own Ingress controllers.
    1. True 2. False
  5. vClusters share keys with host clusters.
    1. True 2. False
  1. b – False – There’s some version management, but generally you can only have one version of a CRD.
  2. d – Namespaces are the security boundary in Kubernetes.
  3. b – When a pod is created in a vCluster, the syncer creates a matching pod in the host cluster for scheduling.
  4. b – False – Generally, the Ingress object is synced into the host cluster.
  5. b – False – Each vCluster gets its own unique keys to identify it.

Join the book’s Discord workspace for a monthly Ask Me Anything session with the authors: