Skip to content

Commit

Permalink
Multi-cluster, Multi-tenant Kustomize Example
Browse files Browse the repository at this point in the history
  • Loading branch information
seifrajhi committed Jun 3, 2024
1 parent d37202c commit c480d2b
Show file tree
Hide file tree
Showing 17 changed files with 167 additions and 0 deletions.
21 changes: 21 additions & 0 deletions multi-cluster-multi-tenant-kustomize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Multi-cluster, Multi-tenant Kustomize Example

This repository shows an example of how to use Kustomize's bases and overlays to maintain manifests for an application that requires one instance of the application to be deployed per tenant and per environment.

Bases are configurations that inherit nothing. Overlays are configurations that inherit from somewhere. Overlays can inherit from bases or from other overlays.

Our example has just one base, the example app represented by a single nginx deployment.

In overlays, we have `clusters`, `plans` and `tenant-envs`.

1. `clusters`: We have one directory per region. If a tenant-env should be in the us, you add it as a base to the `us/kustomization.yaml`. If a tenant-env should be in the eu, you add it to the `eu/kustomization.yaml` bases.

1. `plans`: The plans overlay is where you'd put configuration that is different per plan. In our example trial tenants get less replicas then paying tenants.

1. `tenant-envs`: Our example has a `test` and a `prod` environment per client. Both tenant environments go onto the same cluster. The tenant-env overlays are where you put configuration that is specific to an env. E.g. the database connection should be unique per tenant per env. The tenant envs would also be a good place to give a certain tenant a specific version of the app (e.g. a hotfix) by overwriting the image tags for that tenant and possibly in the tenants test env first.

Adopting a repository structure like this to manage multiple tenants makes it intuitive to understand where certain changes should be made while at the same time reducing the amount of duplicate manifests to a minimum.

Applying a configuration to a cluster ist just one `kustomize build overlays/clusters/eu | kubectl apply -f -` command.

Kustomize has recently been included into kubectl. Once that's released a simple `kubectl apply -f overlays/clusters/eu` is good enough.
24 changes: 24 additions & 0 deletions multi-cluster-multi-tenant-kustomize/bases/app/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: app
name: app
spec:
replicas: 2
selector:
matchLabels:
app: app
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: app
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
commonLabels:
app.kubernetes.io/name: app
app.kubernetes.io/component: frontend
app.kubernetes.io/part-of: app
app.kubernetes.io/managed-by: kustomize

resources:
- deployment.yaml
- service.yaml
18 changes: 18 additions & 0 deletions multi-cluster-multi-tenant-kustomize/bases/app/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: app
name: app
spec:
ports:
- name: 80-8080
port: 80
protocol: TCP
targetPort: 8080
selector:
app: app
type: ClusterIP
status:
loadBalancer: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bases:
- ../../tenant-envs/one/prod
- ../../tenant-envs/one/test

commonLabels:
example.com/region: eu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bases:
- ../../tenant-envs/two/prod
- ../../tenant-envs/two/test

commonLabels:
example.com/region: us
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bases:
- ../../../bases/app

commonLabels:
example.com/plan: paid
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
bases:
- ../../../bases/app

commonLabels:
example.com/plan: trial

patches:
- patch-deployment-replicas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
spec:
replicas: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace: one-prod

bases:
- ../../../plans/paid

commonLabels:
app.kubernetes.io/instance: tenant-one
example.com/stage: prod

resources:
- namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: one-prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace: one-test

bases:
- ../../../plans/paid

commonLabels:
app.kubernetes.io/instance: tenant-one
example.com/stage: test

resources:
- namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: one-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace: two-prod

bases:
- ../../../plans/trial

commonLabels:
app.kubernetes.io/instance: tenant-two
example.com/stage: prod

resources:
- namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: two-prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace: two-test

bases:
- ../../../plans/trial

commonLabels:
app.kubernetes.io/instance: tenant-two
example.com/stage: test

resources:
- namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: two-test

0 comments on commit c480d2b

Please sign in to comment.