Minikube cluster: Connecting to an outside database (MySQL, PostgreSQL, etc.)

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

When developing applications within a Minikube cluster, it is often necessary to connect to external resources such as databases. Depending on your setup, you might need to access a MySQL, PostgreSQL, or another type of database that isn’t hosted within your local Kubernetes cluster. This tutorial will guide you through the process of connecting a Minikube cluster to an outside database safely and securely.

Prerequisites

Before moving to the next section, make sure you have the following:

  • A running Minikube cluster
  • kubectl installed and configured to communicate with your cluster
  • Access information for the outside database (hostname, port, credentials)
  • Basic knowledge of Kubernetes objects (Pods, Services, Secrets, ConfigMaps)

Understanding Minikube’s Networking

Before we can connect to an external database, it’s essential to understand how networking works in Minikube. Minikube runs a Virtual Machine (VM) that hosts the Kubernetes components, creating a layer of abstraction between your local environment and the Kubernetes network. Any communication between Minikube and the outside world must go through this layer.

Setting up the Database Access

Assuming the outside database is reachable from your local machine, we’ll set up access to it from within Minikube.

Method 1: Direct Connection

kubectl run my-app --image=my-app-image --env="DB_HOST=192.168.99.1" --env="DB_PORT=3306" --env="DB_USER=user" --env="DB_PASSWORD=password"

This will create a new Pod named my-app which has environment variables set to point to an outside MySQL database. Replace 192.168.99.1 with the IP address of your database server, and change the other environment variables to match your database credentials.

Method 2: Using Kubernetes Secrets

To keep credentials secure, it is a better practice to store sensitive data in Kubernetes Secrets.

Creating the Secret:

kubectl create secret generic db-credentials --from-literal=username=user --from-literal=password=password

Using the Secret in a Pod:

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-app
    image: my-app-image
    env:
      - name: DB_HOST
        value: "192.168.99.1"
      - name: DB_PORT
        value: "3306"
      - name: DB_USER
        valueFrom:
          secretKeyRef:
            name: db-credentials
            key: username
      - name: DB_PASSWORD
        valueFrom:
          secretKeyRef:
            name: db-credentials
            key: password

This Pod will reference the Secrets to secure the database credentials.

Advanced Networking

If the database is not reachable from your machine or if you require an extra layer of security, you may have to use additional networking features such as port-forwarding, proxies, or VPNs. Here’s how you can use port-forwarding to connect to a database.

ssh -L local_port:db_hostname:db_port user@remote_host

This creates an SSH tunnel from your local machine to the remote host where the database is located.

Accessing the Database from an Application

Now that we have established how to set the environment and secure credentials, let’s consider a simple Python application that connects to the database.

import os
import psycopg2

# Database connection parameters
host = os.getenv('DB_HOST')
port = os.getenv('DB_PORT')
user = os.getenv('DB_USER')
password = os.getenv('DB_PASSWORD')
dbname = os.getenv('DB_NAME')

# Connect to the database
conn = psycopg2.connect(
    host=host,
    port=port,
    user=user,
    password=password,
    dbname=dbname
)

# ... Your database operations here ...

# Close the connection
conn.close()

Replace psycopg2 with the corresponding database adapter if you’re not using PostgreSQL.

Troubleshooting

When connecting to external databases, you might face certain issues such as timeouts, connection refusals, or credential mismanagement. The first steps in troubleshooting are to ensure that:

  • The outside database allows connections from your Minikube cluster’s IP address.
  • The credentials are correctly configured both in your Kubernetes Secrets and your application.
  • The network policies and firewalls are not blocking the traffic.

If you encounter SSL errors, you may need to configure your application or connection string to accept self-signed certificates, or properly set up a trusted chain of certificates on your client.

Conclusion

By following the steps in this tutorial, you should now have a secure method of connecting your Minikube cluster’s applications to an outside database. Always prioritize security by making use of Kubernetes Secrets and consider enhanced network strategies for complex scenarios.