Kubernetes: Deployment Quarkus with Databases

Daniel S. Blanco
3 min readApr 22, 2024

--

We recently did a post where we explained how to set up a Kubernetes cluster on-premises, here. Where we also saw some basic Kubernetes concepts, which helped us to better understand this example.

Today we will see how to deploy two different containers and establish a connection between them. For this we will follow with an example of a Quarkus microservice and its connection to a MariaDB database. But we will also see new Kubernetes concepts that will expand our knowledge.

  • ConfigMap: It is another type of object available in Kubernetes. Used to store non-confidential data in key-value format. They can be used as environment variables, command line arguments or as configuration files in a Volume.
  • Secrets: Another object that allows storing and managing confidential information. Allowing flexibility to add keywords without the need to configure them in the pods.

We will start by setting up the MariaDB service. Associated to it, we will create a ConfigMap that allows us to add an initial .sql script. That will be executed when the database is initialized. Very useful for testing environments.

apiVersion: v1
kind: ConfigMap
metadata:
name: mariadb-initdb-config
data:
initdb.sql: |
CREATE DATABASE library;
GRANT ALL PRIVILEGES ON library.* TO 'username-default'@'%' IDENTIFIED BY 'my_cool_secret';
USE `library`;
CREATE TABLE `BOOK` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`NAME` varchar(45) DEFAULT NULL,
`AUTHOR` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
INSERT INTO BOOK (NAME, AUTHOR) VALUES('Ender Game', 'Orson S. Card');
INSERT INTO BOOK (NAME, AUTHOR) VALUES('The stars my destination', 'Alfred Bester');
INSERT INTO BOOK (NAME, AUTHOR) VALUES('Dune', 'Frank Herbert');

On the other hand, we will create the Secret to be able to securely store the user and administrator passwords of the database. The keys included in the secrets must be encoded in base 64.

apiVersion: v1
kind: Secret
metadata:
name: mariadb-secret
data:
user.pass: bXlfY29vbF9zZWNyZXQ=
root.pass: bXktc2VjcmV0LXB3

The next thing is the database configuration. We will create it as a deployment object where we will have all the configuration. As environment variables, which refer to the stored secrets, we will configure the values of the administrator and user passwords. And through the configuration of the volumes we will be able to indicate the script that must execute at the beginning.

apiVersion: apps/v1
kind: Deployment # what to create?
metadata:
name: mariadb
spec: # specification for deployment resource
replicas: 1 # how many replicas of pods we want to create
selector:
matchLabels:
app: mariadb
strategy:
type: Recreate
template: # blueprint for pods
metadata:
labels:
app: mariadb # service will look for this label
spec: # specification for pods
containers: # we can have one or more containers
- name: mariadb
image: mariadb
ports:
- containerPort: 3306 #port exposed in cluster
env: # allows to define environment variables
- name: MARIADB_ROOT_PASSWORD
valueFrom:
secretKeyRef: # allows to refer to a secret configured
name: mariadb-secret #name of the secret
key: root.pass #key of the secret that you want to use
- name: MARIADB_USER
value: username-default
- name: MARIADB_PASSWORD
valueFrom:
secretKeyRef:
name: mariadb-secret
key: user.pass
volumeMounts:
- name: mariadb-initdb #configuration of the volumes
mountPath: /docker-entrypoint-initdb.d
volumes:
- name: mariadb-initdb #volumen to configure
configMap:
name: mariadb-initdb-config #refer key of the config map

The next step will be to configure the Quarkus microservice. In a next post we will go more into the configuration of Quarkus with Kubernetes or Docker. But in this one we will only show how to configure the database to also see how we can make the connection between both containers.

## bbdd testing conf
quarkus.datasource.devservices.image-name=mariadb:10.3.6
quarkus.datasource.jdbc.driver=org.mariadb.jdbc.Driver

%prod.quarkus.datasource.db-kind=mariadb
%prod.quarkus.datasource.username=username-default
%prod.quarkus.datasource.password=my_cool_secret
%prod.quarkus.datasource.jdbc.url=jdbc:mariadb://mariadb:3306/library
%prod.quarkus.datasource.jdbc.max-size=5

As you can see, it is enough to indicate the label indicated in the deployment. And the data of the user of BBDD that we have indicated in the creation of the pod.

And this has been all. I hope you have learned a little more about Kubernetes, ConfigMap, Secrets and Quarkus.

--

--

No responses yet