Configuring MySql Database in Kubernetes
Creating Relevant Configuration Files
Before creating relevant configuration files, it is recommended to create a folder to centrally store these files. Additionally, create a namespace to store these resources to avoid conflicts with other resources.
In this example, I am creating a folder named 'database' and a namespace named 'database,' and I will create the relevant configuration files in this folder.
# Create a folder
mkdir database
# Create a namespace
kubectl create namespace database
You can also label nodes and specify nodeSelector in the deployment to deploy the application only on specified nodes.
In this example, I am labeling a node with the 'database' label and specifying nodeSelector in the deployment.
# Label the node
# <node-name> is the node server name.
kubectl label node <node-name> worker=database
Create the storage.yaml file with the following content:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: database
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Retain
Create the volume.yaml file with the following content:
# <path> is the path to the database storage
# Example: path: "/mnt/disks/data/database"
apiVersion: v1
kind: PersistentVolume
metadata:
name: database
labels:
app: database
spec:
storageClassName: database
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
hostPath:
path: <path>
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: database
namespace: database
labels:
app: database
spec:
storageClassName: database
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Gi
Create the config.yaml file with the following content:
# <password> Set your password.
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
namespace: database
labels:
app: mysql
data:
MYSQL_ROOT_PASSWORD: <password>
Create the deployment.yaml file with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: database
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
nodeSelector:
worker: database
containers:
- image: mysql:latest
name: mysql
ports:
- containerPort: 3306
envFrom:
- configMapRef:
name: mysql
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql
subPath: mysql
volumes:
- name: mysql
persistentVolumeClaim:
claimName: database
Create the service.yaml file with the following content:
# For external connectivity, NodePort is used here. If internal connectivity is sufficient, ClusterIP can be used.
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: database
labels:
app: mysql
spec:
type: NodePort
ports:
- name: mysql
port: 3306
nodePort: 3306
targetPort: 3306
selector:
app: mysql
# Deploy storage
kubectl apply -f storage.yaml
# Deploy volume
kubectl apply -f volume.yaml
# Deploy config
kubectl apply -f config.yaml
# Deploy deployment
kubectl apply -f deployment.yaml
# Deploy service
kubectl apply -f service.yaml
GCP Firewall Configuration
If you are using GCP and opening connections to the external world, you need to add a rule in the firewall to allow connections on port 3306.
After logging into the GCP platform, navigate to the left-hand menu:
Virtual Private Cloud > Firewall
Click the 'Create Firewall Rule' button at the top and add a rule to allow TCP port 3306 connections.
Test the Connection
Since NodePort is used in this example, you can connect directly from outside using the node's IP. You can test the connection using commonly used database tools such as DataGrip, Navicat, etc.
The default account is 'root', and the password is the one you set in config.yaml.