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: database
Creating a database deployemnt and service in Kubernetes

Explanation

  • In lines 1 and 26, we define and use the v1 version of Kubernetes API to create the Deployment and the Service objects.

  • In lines 2 and 27, we define and create the Deployment and Service type objects.

  • In lines 4 and 29, we name the database-example-deployment and database-example-svc databases to the objects that will be created.

  • In lines 6–12 and 36–37, we define the app: database labels for these objects.

  • In lines 15–22, we define and create a container named postgres-container. This container will use the postgres image and will run on port 5432.

  • In lines 20–22, we define environment variables that will be injected into this container. This environment variable is named POSTGRES_PASSWORD and the value for this variable is secret.

  • In lines 30–35, we define the service type ClusterIP, the port 5432, and the name that this service will use.

To create the Deployment and the Service, use the following command:

kubectl apply -f database.yaml
Command to create all resources

To check the status of resources, execute the following command:

kubectl get pods,services,deployment
Command to get all resources

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:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved