How to install Database MySQL and PostgreSQL on Kubernetes, the better way

How to install Database MySQL and PostgreSQL on Kubernetes using NFS Storage.

How to install Database MySQL and PostgreSQL on Kubernetes, the better way
PostgreSQL & MySQL

Introduction

People often have doubts about installing databases like MySQL or PostgreSQL in Kubernetes due to a variety of reasons. While Kubernetes is an excellent platform for managing containerized applications and automating deployment, scaling, and management of applications, it is not without its challenges when it comes to managing stateful applications like databases.

Stateful vs Stateless

Kubernetes is designed primarily for stateless applications. Stateless applications can be scaled horizontally easily, while stateful applications like databases require persistent storage, which adds complexity when running in a Kubernetes environment.

Persistent Storage

Managing persistent storage is more complicated in Kubernetes. Although Kubernetes has features like StatefulSets and Persistent Volumes (PVs) to help manage stateful applications, the management of these resources can be challenging and less flexible than traditional database management solutions.

Backup and Recovery

While Kubernetes offers some built-in features for backup and recovery, they may not be as robust as those provided by traditional database management systems. Additionally, configuring backup and recovery solutions for databases running in Kubernetes can be more complex than with traditional systems.

Despite these challenges, some organizations successfully run databases like MySQL and PostgreSQL in Kubernetes by carefully managing stateful resources, leveraging the right tools and techniques, and investing time in learning and understanding Kubernetes-native solutions.

Database on Kubernetes Cluster

It is okay to install a database on Kubernetes, as long as we understand the challenges and are prepared to address them. Many organizations have successfully deployed and managed databases like MySQL and PostgreSQL on Kubernetes. There are several reasons why you might choose to do this:

  1. Unified Infrastructure Management: Deploying your database alongside other components of your application stack in Kubernetes allows you to manage your entire infrastructure in a consistent and unified manner. This can simplify operations, reduce overhead, and streamline the deployment process.
  2. Scalability: Kubernetes can help scale your database horizontally by adding more replicas when needed. This can lead to improved performance and better resource utilization.
  3. High Availability: Kubernetes can automatically manage the deployment and availability of database instances, ensuring that they are distributed across different nodes and zones to provide high availability and fault tolerance.
  4. Self-healing: Kubernetes can detect when a database instance fails and automatically reschedule it on a healthy node, which can help maintain the overall health of your application.
  5. Automation: Kubernetes allows you to automate many aspects of database management, including deployment, scaling, and updates, which can help reduce the operational burden on your team.

Installing Database on Kubernetes Cluster

As we elaborated in this article, NFS can be a good choice when comes to persistent storage on Kubernetes Cluster, and this tutorial is based on that. Assume you have created a NFS Storages (nfs-postgresql - nfs-mysql)  and use them as persistent storage for MySQL and PostgreSQL.

Dedicated Namespace

We recommend to create dedicated namespace for Database instance, let's say database .

~$ kubectl create namespace database

PVs and PVCs

Next step is creating PVs and PVCs that can be done through this script below

Install to Kubernetes Cluster

~$ kubectl -n database apply -f nfs-db.yaml

Install MySQL and PostgreSQL via Helm Chart

The most highly recommended method for installing MySQL or PostgreSQL on Kubernetes is by utilizing Helm Charts. Bitnami offers excellent Helm Charts for both MySQL and PostgreSQL, which we will employ in this process. Bitnami is a reputable company specializing in providing a comprehensive library of pre-packaged, easy-to-deploy applications and development stacks tailored for a range of platforms.

To begin, we'll add Bitnami to the Helm repository with the following command:

~$ helm repo add bitnami https://charts.bitnami.com/bitnami
~$ helm update

Next, we'll proceed to install our Database instance. First, create two files named mysql-values.yaml and postgresql-values.yaml, which will contain the scripts provided below:

The configurations for both MySQL and PostgreSQL appear similar, containing some essential details:

  1. existingClaim: This setting is required for our database instance to utilize the PVCs we created earlier, which employ NFS Storage as their persistent storage.
  2. volumePermissions: It is crucial to set enabled as true. This allows the database instance to modify filesystem permissions, ensuring proper access and functionality.

By incorporating these configurations, we can effectively leverage NFS Storage and maintain the necessary permissions for our database instances.

Please note that in this example, we are installing a single primary database instance. In upcoming articles, we will cover how to create MySQL and PostgreSQL clusters on Kubernetes, providing more advanced and scalable solutions for managing databases within the cluster environment.

Install with Helm:

~$ helm install postgresql -f pg-values.yml bitnami/postgresql -n database
~$ helm install mysql -f mysql-values.yml bitnami/mysql -n database

Done! Now you have MySQL and PostgreSQL DB instances running on your Kubernetes Cluster.


About 8grams

We are a small DevOps Consulting Firm that has a mission to empower businesses with modern DevOps practices and technologies, enabling them to achieve digital transformation, improve efficiency, and drive growth.

Ready to transform your IT Operations and Software Development processes? Let's join forces and create innovative solutions that drive your business forward.

Subscribe to our newsletter for cutting-edge DevOps practices, tips, and insights delivered straight to your inbox!