Connecting Kubernetes cluster to E2E DBaaS
This article will guide how to integrate kubernetes with 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.
Using an E2E DBaaS with Kubernetes offers several benefits and advantages that can be compelling for various use cases:
High Availability: E2E DBaaS, especially 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. This contributes 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 the database performance. This scalability is crucial for handling variable workloads.
Data Persistence: Placing the database outside of the Kubernetes cluster offers a valuable advantage: it ensures data persistence, a fundamental requirement for safeguarding crucial application data. Even in scenarios where your application pods are temporary and subject to rescheduling or replacement, your data remains securely preserved within the external database.
In this article we have implemented this by using MySQL DB Engine.
Prerequisite :
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.
Create a Virtual Private Cloud (VPC) to establish network isolation. Within this VPC, you will connect your DBaaS and create your Kubernetes cluster. Proper VPC configuration ensures network security and segmentation.
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 we need to select Allowed Host IP as VPC CIDR Range and attach the VPC to DBaaS where kubernetes has deployed.
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 the screenshot below how to encode the DBaaS username and password.
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: a3ViZWRiCg== # DBaaS Username in encoded format
MYSQL_ROOT_PASSWORD: Y1gzVWpXRXRRejRmVDRTIQo= # DBaaS Password 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
To check the connectivity from the Pod to DBaaS :
Execute the below mentioned command into mysql pod shell
kubectl exec -it mysql-client-deployment-76dfb78bc9-mmtzk -- /bin/bash