Skip to main content

Connecting Kubernetes Cluster to E2E DBaaS

This article will guide you on how to integrate Kubernetes with Database as a Service (E2E DBaaS).

Using an external Database as a Service (E2E DBaaS) with Kubernetes offers several benefits. This reduces the administrative burden on your Kubernetes team, allowing them to focus on application development and deployment.

Benefits of Using E2E DBaaS with Kubernetes

  • High Availability: E2E DBaaS, especially when provided as managed services, often come with built-in high availability features. This means your database can continue to operate even if a pod in your Kubernetes cluster fails, contributing to improved application uptime and reliability.

  • Scalability: Kubernetes allows you to scale your application pods independently of the database. When using an E2E DBaaS as an external database, you can scale your application horizontally to meet increased demand without impacting database performance. This scalability is crucial for handling variable workloads.

  • Data Persistence: Placing the database outside of the Kubernetes cluster ensures data persistence, a fundamental requirement for safeguarding crucial application data. Even when your application pods are temporary and subject to rescheduling or replacement, your data remains securely preserved within the external database.

In this article, we will implement this integration using the MySQL DB engine.

Prerequisites

  1. Provision a DBaaS Instance: Begin by provisioning a DBaaS instance using MySQL, choosing the desired version that suits your project requirements. This will serve as your managed database backend.

  2. Create a Virtual Private Cloud (VPC): Establish network isolation by creating a VPC. Within this VPC, you will connect your DBaaS and create your Kubernetes cluster. Proper VPC configuration ensures network security and segmentation.

  3. Deploy a Kubernetes Cluster: Deploy a Kubernetes cluster within the VPC you've created. This cluster will be the foundation for orchestrating your application containers.

Step 1: Establishing the Configuration for Your Kubernetes Cluster

To allow Kubernetes to connect with DBaaS, you need to select the Allowed Host IP as the VPC CIDR Range and attach the VPC to the DBaaS where Kubernetes has been deployed.

Kubernetes DBaaS Configuration

Step 2: Create a ConfigMap for Endpoint Configuration

Create a ConfigMap that contains the endpoint information (database host and port):

apiVersion: v1
kind: ConfigMap
metadata:
name: database-config
data:
DB_HOST: "10.12.162.11" # Attached VPC IP address
DB_PORT: "3306" # MySQL Standard Database Port

To create the ConfigMap please run the below mentioned command after successful creation of above mentioned file.

 kubectl apply -f database-configmap.yaml

Step 3: Create a Secret for Username and Password

Create a Secret file to securely store the DBaaS username and password in encoded format.

Please refer to the screenshot below on how to encode the DBaaS username and password.

Encode DBaaS Credentials

Create a SecretKey file that contains the key information (database username and password):

apiVersion: v1
kind: Secret
metadata:
name: external-db-credentials
type: Opaque
data:
MYSQL_USERNAME: a3ViZWRiZC5jb20= # DBaaS Username in encoded format
MYSQL_ROOT_PASSWORD: Y1gzVWpXRXRRejRmVDRTIQo= # DBaaS Password in encoded format

To create the SecretKey please run the below mentioned command after successful creation of above mentioned file.

  kubectl apply -f database-secret.yaml

Step 4 : Create a DB client Deployment


apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-client-deployment
spec:
replicas: 1
selector:
matchLabels:
app: mysql-client
template:
metadata:
labels:
app: mysql-client
spec:
containers:
- name: mysql-client
image: mariadb
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: database-config
key: DB_HOST
- name: DB_PORT
valueFrom:
configMapKeyRef:
name: database-config
key: DB_PORT
- name: MYSQL_USERNAME
valueFrom:
secretKeyRef:
name: external-db-credentials
key: MYSQL_USERNAME
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: external-db-credentials
key: MYSQL_ROOT_PASSWORD

Check the DB Client Pod Status :

To check the Pod status please run the below mntioned command.

   kubectl get pods

Check DBaaS Connectivity

To Check the Connectivity from the Pod to DBaaS

Execute the following command in the MySQL pod shell:

 kubectl exec -it mysql-client-deployment-76dfb78bc9-mmtzk -- /bin/bash

Check DBaaS Connectivity