Connecting to a DB - PART 2: Kubernetes
In this guide, you will learn HOW-TO run the Docker Image we built in Connecting to a DB - PART 1: Docker Image on Kubernetes.
Prerequisites
To complete this guide, you need:
Database
PostgreSQL
We basically will repeat everything we did in WildFly Java Microservice - PART 2: Kubernetes but, before, we will deploy PostgreSQL on Kubernetes.
PostgreSQL configmap
Create the file named postgres-configmap.yaml
with the following content:
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-secret
labels:
app: postgres
data:
POSTGRESQL_USER: postgres
POSTGRESQL_PASSWORD: admin
POSTGRESQL_HOST: postgres-service
POSTGRESQL_PORT: "5432"
POSTGRESQL_DATABASE: postgres
POSTGRESQL_JNDI: java:jboss/datasources/PostgreSQLDS
apply the ConfigMap configuration to Kubernetes:
kubectl apply -f postgres-configmap.yaml
PostgreSQL deployment
Create the file named postgres-deployment.yaml
with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: 'docker.io/library/postgres'
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5432
env:
- name: POSTGRES_USER
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_USER
- name: POSTGRES_PASSWORD
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_PASSWORD
- name: POSTGRES_DB
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_DATABASE
apply the Deployment configuration to Kubernetes:
kubectl apply -f postgres-deployment.yaml
PostgreSQL service
Create a file named postgres-service.yaml
with the following content:
apiVersion: v1
kind: Service
metadata:
name: postgres-service
labels:
app: postgres
spec:
ports:
- protocol: TCP
port: 5432
targetPort: 5432
selector:
app: postgres
apply the Service configuration to Kubernetes:
kubectl apply -f postgres-service.yaml
Note
|
if you are using minikube, and you want to test if PostgreSQL is working, run kubectl port-forward svc/postgres-service 5432:5432 and then connect via JDBC using URL jdbc:postgresql://localhost:5432/postgres
|
Image Registry
To make the my-jaxrs-app-db:latest
Docker Image available to Kubernetes, you need to push it to some Image Registry that is accessible by the Kubernetes cluster you want to use.
Quay.io
There are many Image Registries you can use: in this guide, we will push the my-jaxrs-app-db:latest
Docker Image, to the quay.io Image Registry.
Create a public repository named my-jaxrs-app-db
on quay.io (e.g. https://quay.io/repository/tborgato/my-jaxrs-app-db).
Note
|
replace tborgato with the name of your account in all the commands that will follow
|
Tag the Docker image:
podman tag my-jaxrs-app-db quay.io/tborgato/my-jaxrs-app-db
Push the my-jaxrs-app-db
Docker Image to it:
podman push quay.io/tborgato/my-jaxrs-app-db
At this point, the my-jaxrs-app-db:latest
Docker Image should be publicly available and free to be consumed by any Kubernetes Cluster; you can verify this by running:
podman pull quay.io/tborgato/my-jaxrs-app-db
Deploy to Kubernetes
To deploy our my-jaxrs-app-db
Docker Image on minikube, create a file named deployment-my-jaxrs-app-db.yaml
(see kubernetes deployment) in the same directory as the Dockerfile
and the pom.xml
file, with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-jaxrs-app-db-deployment
labels:
app: my-jaxrs-app-db
spec:
replicas: 1
selector:
matchLabels:
app: my-jaxrs-app-db
template:
metadata:
labels:
app: my-jaxrs-app-db
spec:
containers:
- name: my-jaxrs-app-db
image: quay.io/tborgato/my-jaxrs-app-db
ports:
- containerPort: 8080
- containerPort: 9990
livenessProbe:
httpGet:
path: /health/live
port: 9990
readinessProbe:
httpGet:
path: /health/ready
port: 9990
startupProbe:
httpGet:
path: /health/started
port: 9990
env:
- name: POSTGRESQL_USER
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_USER
- name: POSTGRESQL_PASSWORD
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_PASSWORD
- name: POSTGRESQL_HOST
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_HOST
- name: POSTGRESQL_PORT
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_PORT
- name: POSTGRESQL_DATABASE
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_DATABASE
- name: POSTGRESQL_JNDI
valueFrom:
configMapKeyRef:
name: postgres-secret
key: POSTGRESQL_JNDI
apply the Deployment configuration to Kubernetes:
kubectl apply -f deployment-my-jaxrs-app-db.yaml
We used minikube as Kubernetes Cluster, hence we expose the deployment as NodePort
:
kubectl expose deployment.apps/my-jaxrs-app-db-deployment --type=NodePort --port=8080
Check the application
Find out on what IP address/port, minikube is exposing your service:
$ minikube service my-jaxrs-app-db-deployment --url
http://192.168.39.143:30433
Just like we did before, hit the following URLs, using a utility like curl
:
$ curl -X POST http://192.168.39.143:30433/hello/test-table/somedata1/somedata2
{"field1":"somedata1","field2":"somedata2","id":1}
$ curl http://192.168.39.143:30433/hello/test-table
[{"field1":"somedata1","field2":"somedata2","id":1}]
What’s next?
References
Back to Guides