K8S/MariaDB: How to deploy MariaDB on Kubernetes Using secrets and configMaps
MariaDB is a very popular open-source Relational Database Server . It is successor of the Mysql and almost same thing. Because of it is very mature, well documented and have broad range of support, many organization and individuals prefers MariaDB in their solutions.
In this article, we will deploy a MariaDB server as a container instance on Kubernetes. So, how to install mariadb on kubernetes using configmap and secret?
- First of all create a configmap that contains the mariadb configuration file. You can use the following command:
kubectl create configmap mariadb-config --from-file=my.cnf
where my.cnf is a file that contains the mariadb settings, like below:
[mysqld]
bind-address = 0.0.0.0
You can write your own my.cnf file or browse the net for the examples. I am assuming you have already a great one :)
2. Create two secrets that store the mariadb root password and the user credentials. You can use the following commands:
kubectl create secret generic mariadb-root-password --from-literal=password=secret
kubectl create secret generic mariadb-user-creds --from-literal=username=user --from-literal=password=pass
3. Create a deployment that runs the mariadb image and mounts the configmap and the secrets as volumes and environment variables. You can use the following yaml file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mariadb-deployment
spec:
replicas: 1
selector:
matchLabels:
app: mariadb
template:
metadata:
labels:
app: mariadb
spec:
containers:
- name: mariadb
image: mariadb:10.4
ports:
- containerPort: 3306
volumeMounts:
- name: config-volume
mountPath: /etc/mysql/conf.d/
env:
- name: MARIADB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mariadb-root-password
key: password
- name: MARIADB_USER
valueFrom:
secretKeyRef:
name: mariadb-user-creds
key: username
- name: MARIADB_PASSWORD
valueFrom:
secretKeyRef:
name: mariadb-user-creds
key: password
volumes:
- name: config-volume
configMap:
name: mariadb-config
As you see above, mariadb configmap (configuration values) and secrets (username and password) defined with configMap and secrtKeyRef sections and key values under them.
4. Apply the deployment using the following command:
kubectl apply -f mariadb-deployment.yaml
The command that provided above will create a MariaDB deployment and pods. You need to expose your mariadb deployment to allow access through network if necessary.
You can use the kubectl expose
command to create a Service that exposes a MariaDB deployment on a specific port using the NodePort
type. Here is an example command:
kubectl expose deployment mariadb-deployment --type=NodePort --name=mariadb-service --port=3306
This command creates a Service named mariadb-service
that exposes the mariadb
deployment on port 3306
using the NodePort
type. The NodePort
type makes the Service accessible on a static port on each node in the cluster.
You can change — type=NodePort to ClusterIP if everything remains internal and not expose to external network or LoadBalancer if you have a LoadBalancer and place the service behind a LoadBalancer.
5. Verify that the deployment is running and that you can connect to the MariaDB server using the secrets.
In this article we explained how to deploy a MariaDB instance using secrets and configmaps and optionally exposed. There are long checklists for the MariaDB and Kubernetes, I will write them in future articles.
Enjoy your life!