intro
Multitenant database containers offer unique ways to handle multiple users, databases, or applications within a single infrastructure. Here’s what you need to know.
In today’s increasingly digital world, containerization has already became the cornerstone for many real-world applications. One solution gaining significant traction are multitenant database containers that offer a unique way to handle multiple users, databases, or applications within a single infrastructure while ensuring isolation, security, and scalability.
Let’s learn more about them!
What is Multitenancy and Why is It Important?
On a high level, multitenancy is a single instance of software serving multiple “tenants,” while keeping each the data associated with each tenant separate from other tenants. In other words, it is a single software application that provides solutions to problems to multiple customers while keeping each customer’s data isolated from another customer.
Multitenancy can be thought of as an apartment building: one building may be shared with many tenants, but at the same time, each tenant’s apartment is separate for enhanced privacy.
Benefits of Database Multitenancy
As far as databases are concerned, multitenancy refers to a single database instance being used to store data associated with multiple clients with each client having access to their own, isolated, set of data.
In the database world, multitenancy is generally achieved by using containers. Containers are standalone units of software that bundle an application together with its dependencies and as a result, offer several benefits including portability, scalability, and resource efficiency.
Containers are typically synonymous with Kubernetes and that is not without a reason… These days, Kubernetes has already became the de-facto standard orchestrator for containerized applications helping with deployment, scaling, and management.
In fact, containers are so popular that entire data breach search engines having thousands of data breaches were said to have been built on top of them. That is because containers provide:
Multitenant Database Containers
As such, we come to multitenant database containers. Some developers imagine that multitenant database containers are very hard to provision or maintain but actually that is not the case.
As you can already imagine, with the capabilities of containers we can easily work with databases inside of them because containers provide a portable, consistent, and isolated environment that allows databases to be deployed across any infrastructure with minimal configuration.
To provision a database, you need three things:
All of those things accept YAML-based (.yaml) files as configuration options, so what you need to do is to define options inside of YAML files, then tell Kubernetes to use them.
Here is a simple PV configuration:
1
apiVersion: v1
2
kind: PersistentVolume
3
metadata:
4
name: mysql-pv # meaning mysql-persistentvolume
5
spec: # specifications of your server
6
capacity: # we specify the storage underneath
7
storage: 1Gi # 1GB, change this based on your requirements
8
accessModes: # define how a Persistent Volume can be accessed by the pods that use it
9
- ReadWriteOnce
10
# What happens to the PV once a PVC (Persistent Volume Claim) is deleted?
11
persistentVolumeReclaimPolicy: Retain # Options: Retain, Delete
12
hostPath:
13
path: /.../data/mysql # Path to your MySQL data directory
After, ensure you have a Persistent Volume Claim (a PVC for short). This allows your database deployment to request storage from a PV (defined above.) The PVC configuration (also a YAML file) should match the requested size and access modes of the PV. It does not include anything fancy, you would just need to confirm the amount of storage you already defined:
1
apiVersion: v1
2
kind: PersistentVolumeClaim
3
metadata:
4
name: mysql-pvc
5
spec:
6
accessModes:
7
- ReadWriteOnce
8
resources:
9
requests:
10
storage: 1Gi # 1GB. This MUST match the size of your PV
Now, create a database deployment like so:
1
apiVersion: apps/v1
2
kind: Deployment
3
metadata:
4
name: mysql-deployment
5
spec:
6
replicas: 1 # You can scale to multiple replicas if needed
7
selector:
8
matchLabels:
9
app: mysql
10
template:
11
metadata:
12
labels:
13
app: mysql
14
spec:
15
containers:
16
- name: mysql
17
image: mysql:8.0 # Adjust as necessary
18
env:
19
- name: MYSQL_ROOT_PASSWORD
20
value: "password" # You can store passwords via Secrets. If you do, you don't need this
21
- name: MYSQL_DATABASE
22
value: "demo_db" # what database are you connecting to?
23
ports:
24
- containerPort: 3306 # the port of the DBMS. MySQL => 3306
25
volumeMounts:
26
- name: mysql-storage
27
mountPath: /var/lib/mysql # usually /var/lib/mysql unless you defined another dir in my.cnf
28
volumes:
29
- name: mysql-storage
30
persistentVolumeClaim:
31
claimName: mysql-pvc # this must match the name of your PVC (above)
Finally, you need to make your deployment able to communicate with applications inside of Kubernetes by creating a Service. This is also a YAML file (what a surprise, right?) and can be defined like so:
1
apiVersion: v1
2
kind: Service
3
metadata:
4
name: mysql-service
5
spec:
6
selector:
7
app: mysql # This should match the label in your deployment
8
ports:
9
- protocol: TCP
10
port: 3306 # The database port
11
targetPort: 3306 # The port "exposed" by your container. Usually the same
Now that you have all of the necessary configurations, apply them using Kubernetes. Run four commands using kubectl making sure to replace the file names according to the files you’ve created:
1
kubectl apply -f mysql-pv.yaml
2
kubectl apply -f mysql-pvc.yaml
3
kubectl apply -f mysql-deployment.yaml
4
kubectl apply -f mysql-service.yaml
Now you should be good to go. To be sure, verify everything by:
If everything is okay, you now know your way around multitenant database containers. Mission complete!
Conclusion
Multitenant database containers may sound scary, but in reality, everything is more simple than it seems. You do need to know your way around Kubernetes and may need to learn how to create configuration files in order to apply and deploy them, but otherwise, everything’s pretty straightforward.
We hope you’ve enjoyed this blog and that you will stay around for more. Stay tuned to the TheTable blog for more database content, and until next time!
FAQ
What are multitenant database containers?
Multitenant database containers are usually deployments of Kubernetes that make a single instance of an application or a database serve multiple distinct “tenants.” Since each tenant has its own isolated environment, the name is appropriate.
How to configure multitenant database containers in Kubernetes?
To configure multitenant database containers in Kubernetes, you will need three things: a Deployment Resource, a Persistent Volume, and a Service. These things should be YAML (.yaml) files and contain the configurations inside each one of them.
Why should I use DbVisualizer?
Consider using SQL clients like the one provided by DbVisualizer because they do not only come with powerful SQL clients facilitating autocomplete operations together with drag-and-drop abilities and the like, but also provide support for the most popular data sources, allow you to export data in a variety of formats, and have the capability to let you edit data as you view it.

