Tenants and owners with Capsule
This is the free lesson
Next: What an owner can and cannot do
The rest of this course is part of Pro, together with every other course and the ones added later.
$25 a month billed yearly, or $29 month to month
Until 30 November: $210 for the first year, or $14.50 for the first month, with code LAUNCH.
A free account keeps your progress across devices. The paid lessons need Pro.
Code Snippets
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: tenancy
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: ClusterConfiguration
apiServer:
extraArgs:
- name: enable-admission-plugins
value: NodeRestriction,PodNodeSelector
- role: worker
labels:
pool: search
- role: worker
labels:
pool: billingapiVersion: capsule.clastix.io/v1beta2
kind: Tenant
metadata:
name: search
spec:
owners:
- kind: User
name: alice
nodeSelector:
pool: search
---
apiVersion: capsule.clastix.io/v1beta2
kind: Tenant
metadata:
name: billing
spec:
owners:
- kind: User
name: bob
nodeSelector:
pool: billingdocker info --format '{{.NCPU}} CPUs' && kind version
kind create cluster --config cluster.yaml
kubectl wait --for=condition=Ready nodes --all --timeout=120s
kubectl get nodes -L pool
kubectl get pod -n kube-system kube-apiserver-tenancy-control-plane -o yaml | grep admission
helm install cert-manager oci://quay.io/jetstack/charts/cert-manager --version v1.21.3 \
-n cert-manager --create-namespace --set crds.enabled=true --wait
helm repo add projectcapsule https://projectcapsule.github.io/charts
helm install capsule projectcapsule/capsule --version 0.15.2 \
-n capsule-system --create-namespace --wait
kubectl get capsuleconfiguration default -o jsonpath='{.spec.users}{"\n"}'
kubectl apply -f tenants.yaml
kubectl get tenants
kubectl create namespace search-dev --as alice --as-group projectcapsule.dev
kubectl create namespace billing-dev --as bob --as-group projectcapsule.dev
kubectl get namespaces -l capsule.clastix.io/tenant -L capsule.clastix.io/tenant
kubectl get namespace search-dev -o jsonpath='{.metadata.annotations}{"\n"}'
kubectl create deployment web --image nginx:1.29 -n billing-dev --as alice --as-group projectcapsule.dev
kubectl create namespace carol-dev --as carol
kubectl get tenantsIn this video you'll share one Kubernetes cluster between two teams with Capsule, and let each team's owner create namespaces, only for her own team. Capsule adds a Tenant object to Kubernetes. When an owner creates a namespace, Capsule's webhook, called by the API server, the application programming interface of the cluster, puts it into the owner's tenant, with the tenant's node selector. cert-manager issues the webhook's certificate. alice owns search, and bob owns billing. In this lesson you will build a three-node cluster with the PodNodeSelector plugin on, install cert-manager and Capsule with two tenants, and create namespaces as a tenant owner. Before you start, you need Docker running, and kind, kubectl and Helm. The first command checks them. Docker answers with four CPUs, and kind is version 0.33. The cluster comes from a file in my project folder, k8s-tenancy. One control plane and two workers. Each worker carries a pool label: search for one team, billing for the other. The patch turns on an admission plugin of the API server, PodNodeSelector. It reads a node selector from an annotation on the namespace, and adds it to every pod created there. kind leaves it off by default. kind starts three containers, one for each node. It takes about twenty seconds, so I speed up the video. Three nodes, and the pool column shows search and billing on the two workers. Is the plugin on? The API server runs as a pod, so its flags are in the pod spec. NodeRestriction, kind's default, and PodNodeSelector. Now Capsule. Its webhook needs a certificate, which comes from cert-manager: without cert-manager, the Capsule chart fails. The cert-manager chart is in an OCI registry, a container registry that also stores charts, so there is no repository to add. The crds flag also installs its custom resource definitions, the new object types that cert-manager adds to the API. This takes about twenty seconds, so I speed up the video. The udienza.com episode with Dario Tranchitella, chief technology officer of Clastix, sums up the project: Capsule shares one Kubernetes cluster across tenants by grouping namespaces; it was donated to the CNCF, the Cloud Native Computing Foundation. Capsule's configuration names one group, projectcapsule.dev. Only users in this group can own tenants and create namespaces. Everyone else gets the plain Kubernetes rules. A Tenant is a cluster-wide object. search has one owner, the user alice, and billing has bob. Each tenant also gets a node selector: the pool label of its worker. Two tenants, active, with no namespaces yet. alice has no credentials of her own, so I act as her with impersonation: the as flag sets the user, and as-group the group. As the cluster admin, I am allowed to do that. In plain Kubernetes, only an admin creates namespaces. Here each owner created one, and Capsule labelled it with the tenant. Capsule also wrote the tenant's node selector into an annotation, pool equals search. The admission plugin copies it into every pod of the namespace. Now alice tries to start a Deployment in bob's namespace. Forbidden. Capsule gave alice the admin role in her own namespaces only, and billing-dev belongs to bob. Last, carol, a user outside the Capsule group. Forbidden too. The right to create namespaces is bound to the Capsule group, and carol is not in it. Each tenant now counts one namespace. Owners create their own namespaces, and each new namespace lands in its owner's tenant. In this lesson you learned that a Capsule tenant groups namespaces and names their owners, that owners in the Capsule group create namespaces for their own tenant only, and that a tenant's node selector needs the PodNodeSelector admission plugin.