How to put a database in Kubernetes
Kubernetes is an open-source platform that lets us deploy and manage containerized applications. In Kubernetes, resources are defined as objects that can be managed later.
Steps to add the database in Kubernetes
1. First, we’ll choose the database to put in Kubernetes.
2. Then, we’ll create the Docker container image of the chosen database.
3. In Kubernetes, we’ll use YAML files to define and manage resources.
Putting database in Kubernetes
We can use several types of databases to put in Kubernetes. Some of the most popular databases include, but are not limited to:
MySQL
PostgreSQL
MongoDB
Other cloud-native databases
For this Answer, we'll use the PostgreSQL database.
Example
Click the "Run" button on the following widget to create a kind cluster.
apiVersion: apps/v1
kind: Deployment
metadata:
name: database-example-deployment
spec:
selector:
matchLabels:
app: database
template:
metadata:
labels:
app: database
spec:
containers:
- image: postgres
name: postgres-container
ports:
- containerPort: 5432
imagePullPolicy: Always
env:
- name: POSTGRES_PASSWORD
value: "secret"
---
apiVersion: v1
kind: Service
metadata:
name: database-example-svc
spec:
type: ClusterIP
ports:
- name: "database"
port: 5432
protocol: TCP
selector:
app: databaseExplanation
In lines 1 and 26, we define and use the
v1version of Kubernetes API to create theDeploymentand theServiceobjects.In lines 2 and 27, we define and create the
DeploymentandServicetype objects.In lines 4 and 29, we name the
database-example-deploymentanddatabase-example-svcdatabases to the objects that will be created.In lines 6–12 and 36–37, we define the
app: databaselabels for these objects.In lines 15–22, we define and create a container named
postgres-container. This container will use thepostgresimage and will run on port5432.In lines 20–22, we define environment variables that will be injected into this container. This environment variable is named
POSTGRES_PASSWORDand the value for this variable issecret.In lines 30–35, we define the service type
ClusterIP, the port5432, and the name that this service will use.
To create the Deployment and the Service, use the following command:
kubectl apply -f database.yaml
To check the status of resources, execute the following command:
kubectl get pods,services,deployment
The pod's status should be running, and the service should also be up.
Wrapping Up
We can create and experiment with databases and use ConfigMaps and secrets to inject the environment variables into the container. We can also create PersistentVolumesClaims and use them with our databases.
Unlock your potential: Kubernetes Deployment and Advanced Operations series, all in one place!
To deepen your understanding of Kubernetes deployment and management, explore our series of Answers below:
How to create a Kubernetes cluster locally with Minikube
Learn how to set up a Kubernetes cluster on your local machine using Minikube for development and testing.How to create deployment on a Kubernetes cluster via YAML files
Master the process of defining and managing deployments in Kubernetes using YAML configuration files.What is an Ingress controller in Kubernetes?
Understand the role of an Ingress controller in managing external access and routing traffic within a Kubernetes cluster.How to implement namespace quotas in Kubernetes
Discover how to apply resource limits to specific namespaces to optimize resource allocation and prevent overuse.How to implement Kubernetes resource quotas
Learn how to enforce limits on CPU, memory, and storage consumption across your Kubernetes cluster.Add users using certificates in a Kubernetes cluster
Explore the process of adding and authenticating users in Kubernetes by issuing and managing certificates.What is service discovery in Kubernetes?
Understand how Kubernetes enables seamless service discovery, allowing applications to communicate efficiently.How to put a database in Kubernetes
Learn the best practices for deploying and managing databases within a Kubernetes environment.How to run WordPress on Kubernetes
Follow a step-by-step guide to deploying a scalable and resilient WordPress site on Kubernetes.
Free Resources