DEV Community

Cover image for Deploying PostgreSQL, MySQL, and MongoDB on Kubernetes: A Step-by-Step Guide
Dmitry Romanoff
Dmitry Romanoff

Posted on

Deploying PostgreSQL, MySQL, and MongoDB on Kubernetes: A Step-by-Step Guide

Kubernetes has become the go-to platform for managing containerized applications at scale. If you're looking to run popular databases like PostgreSQL, MySQL, and MongoDB in a Kubernetes environment, this guide will show you how to deploy, connect, and interact with these databases. We will walk through deploying each database in its own Pod, exposing them using Kubernetes Services, and performing basic SQL operations.

Prerequisites

Before diving in, ensure you have the following:

  • A running Kubernetes cluster.
  • kubectl installed and configured to interact with the cluster.
  • Helm installed (optional but recommended for installing the databases easily).

Step 1: Create the my-lab Namespace

We’ll create a dedicated namespace for deploying all the databases. This will keep things organized and separate from other workloads in your cluster.

kubectl create namespace my-lab
Enter fullscreen mode Exit fullscreen mode

Step 2: Deploy PostgreSQL in the my-lab Namespace

We’ll use Helm to deploy PostgreSQL from the Bitnami repository. Helm makes it easier to manage Kubernetes applications.

2.1. Add the Bitnami Helm repository:

helm repo add bitnami https://charts.bitnami.com/bitnami
Enter fullscreen mode Exit fullscreen mode

2.2. Install PostgreSQL:

helm install my-postgres bitnami/postgresql --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

2.3. Check the status of the PostgreSQL Pod:

kubectl get pods --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

2.4. Retrieve the PostgreSQL password (we will use this to authenticate when connecting to PostgreSQL):

kubectl get secret --namespace my-lab my-postgres-postgresql -o jsonpath="{.data.postgresql-password}" | base64 --decode
Enter fullscreen mode Exit fullscreen mode

Save this password for the next steps. It will be used for connecting to PostgreSQL.

Step 3: Deploy MySQL in the my-lab Namespace

Next, we'll deploy MySQL using a similar process.

3.1. Install MySQL using Helm:

helm install my-mysql bitnami/mysql --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

3.2. Check the MySQL Pod’s status:

kubectl get pods --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

3.3. Retrieve the MySQL root password:

kubectl get secret --namespace my-lab my-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy MongoDB in the my-lab Namespace

We’ll now deploy MongoDB using Helm:

4.1. Install MongoDB with Helm:

helm install my-mongo bitnami/mongodb --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

4.2. Check the MongoDB Pod status:

kubectl get pods --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

4.3. Retrieve the MongoDB root password:

kubectl get secret --namespace my-lab my-mongo-mongodb -o jsonpath="{.data.mongodb-root-password}" | base64 --decode
Enter fullscreen mode Exit fullscreen mode

Step 5: Expose Databases for Local Access Using kubectl port-forward

To interact with the databases from your local machine, we'll use kubectl port-forward. This allows you to expose the database services locally without modifying the Kubernetes cluster's networking.

5.1 Port Forward PostgreSQL

Run the following command to expose PostgreSQL locally on port 5732:

kubectl port-forward svc/my-postgres-postgresql 5732:5432 --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

PostgreSQL will now be accessible at localhost:5732.

5.2 Port Forward MySQL

Similarly, expose MySQL locally on port 3706:

kubectl port-forward svc/my-mysql 3706:3306 --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

MySQL will now be accessible at localhost:3706.

5.3 Port Forward MongoDB

Expose MongoDB locally on port 27717:

kubectl port-forward svc/my-mongo-mongodb 27717:27017 --namespace my-lab
Enter fullscreen mode Exit fullscreen mode

MongoDB will now be accessible at localhost:27717.

Step 6: Connect to the Databases and Perform SQL Operations

Now that the databases are deployed and exposed, let’s connect to each one and perform basic database operations like creating tables, inserting data, and querying data.

6.1 PostgreSQL

To connect to PostgreSQL, use the psql command-line tool:

psql -h localhost -p 5732 -U postgres -d postgres
Enter fullscreen mode Exit fullscreen mode

Enter the password you retrieved earlier.

Once connected, you can create a table, insert data, and query the database:

-- Create a table
CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    position VARCHAR(100)
);

-- Insert some data
INSERT INTO employees (name, position) VALUES ('Alice', 'Manager');
INSERT INTO employees (name, position) VALUES ('Bob', 'Developer');

-- Retrieve data
SELECT * FROM employees;
Enter fullscreen mode Exit fullscreen mode

6.2 MySQL

To connect to MySQL, use the mysql client:

Start a bash shell in the MySQL Pod:

kubectl exec -it my-mysql-0 -n my-lab -- bash
Enter fullscreen mode Exit fullscreen mode

Connect to MySQL:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Enter the root password you retrieved earlier.

Once inside MySQL, you can create a database, create a table, insert data, and query:

-- Create a database
CREATE DATABASE d_test;

-- Use the database
USE d_test;

-- Create a table
CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    position VARCHAR(100)
);

-- Insert data
INSERT INTO employees (name, position) VALUES ('Alice', 'Manager');
INSERT INTO employees (name, position) VALUES ('Bob', 'Developer');

-- Retrieve data
SELECT * FROM employees;
Enter fullscreen mode Exit fullscreen mode

6.3 MongoDB

To connect to MongoDB, use the mongosh shell:

kubectl exec -it <mongo-pod-name> -n my-lab -- mongosh -u root -p
Enter fullscreen mode Exit fullscreen mode

Once connected, perform basic CRUD operations:

// Switch to the database
use mydb;

// Create a collection
db.createCollection('employees');

// Insert some data
db.employees.insert({ name: 'Alice', position: 'Manager' });
db.employees.insert({ name: 'Bob', position: 'Developer' });

// Retrieve data
db.employees.find();
Enter fullscreen mode Exit fullscreen mode

Final Notes

The kubectl port-forward method is great for local development and testing, but for production environments, you’ll want to expose your databases using proper Kubernetes Services and Ingress controllers.

The default configuration provided by Helm charts works for basic deployments. If you need persistence, high availability, or custom configurations, you should customize the Helm charts or use Kubernetes manifests.

While this guide covers PostgreSQL, MySQL, and MongoDB, Kubernetes is a flexible platform, and you can easily swap these databases for other types like MariaDB, Redis, or even more complex setups.

Deploying and managing databases on Kubernetes is a great way to leverage the scalability and resiliency of Kubernetes, while still using familiar relational and NoSQL databases.

Top comments (0)