Skip to content

Commit

Permalink
Connecting to a DB - PART 2: Kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
tommaso-borgato committed May 13, 2024
1 parent 3c1c5ab commit d57a544
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
:version-resteasy-client: 6.2.7.Final
:version-wildfly-cloud-galleon-pack: 6.0.0.Final
:version-wildfly-maven-plugin: 5.0.0.Final
:docker-image-name: my-jaxrs-app
:my-jaxrs-app-docker-image-name: my-jaxrs-app
:my-jaxrs-app-db-docker-image-name: my-jaxrs-app-db
:quay-io-account-name: tborgato
:version-wildfly-datasources-galleon-pack: 3.0.0.Final
:postgre-sql-user: postgres
Expand All @@ -20,3 +21,5 @@
:postgre-sql-port: 5432
:postgre-sql-database: postgres
:postgre-sql-jndi: java:jboss/datasources/PostgreSQLDS
:postgre-docker-image: docker.io/library/postgres
:postgre-sql-kubernetes-service-name: postgres-service
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Start PostgreSQL:

[source,bash,subs="normal"]
----
podman run --rm --name my-postgres -p {postgre-sql-port}:{postgre-sql-port} -e POSTGRES_PASSWORD={postgre-sql-password} -e POSTGRES_USER={postgre-sql-user} -e POSTGRES_DB={postgre-sql-database} postgres
podman run --rm --name my-postgres -p {postgre-sql-port}:{postgre-sql-port} -e POSTGRES_PASSWORD={postgre-sql-password} -e POSTGRES_USER={postgre-sql-user} -e POSTGRES_DB={postgre-sql-database} {postgre-docker-image}
----

== Maven Project
Expand Down Expand Up @@ -327,13 +327,13 @@ $ curl http://localhost:8080/hello/test-table/list

== Docker Image

=== Re-build the Docker Image
=== Build the Docker Image

Re-build the Docker Image with the following command:
Build the Docker Image with the following command:

[source,bash,subs="normal"]
----
$ podman build -t {docker-image-name}:latest .
$ podman build -t {my-jaxrs-app-db-docker-image-name}:latest .
STEP 1/3: FROM quay.io/wildfly/wildfly-runtime:latest
STEP 2/3: COPY --chown=jboss:root target/server $JBOSS_HOME
--> 4609f8ed0c7f
Expand All @@ -346,9 +346,9 @@ db4677f5bf4f471f5624bd63a21fce3d91b7b3b93e985d3e86a8a4b0682d85cd

=== Run the Docker Image locally

To check that our brand-new `{docker-image-name}:latest` Docker Image works as expected, run the following command:
To check that our brand-new `{my-jaxrs-app-db-docker-image-name}:latest` Docker Image works as expected, run the following command:

Note that, when running the `{docker-image-name}:latest` Docker Image, we specify some environment variables used by WildFly to connect to the PostgreSQL database:
Note that, when running the `{my-jaxrs-app-db-docker-image-name}:latest` Docker Image, we specify some environment variables used by WildFly to connect to the PostgreSQL database:

[source,bash,subs="normal"]
----
Expand All @@ -360,7 +360,7 @@ podman run --rm -p 8080:8080 -p 9990:9990 \
-e POSTGRESQL_DATABASE={postgre-sql-database} \
-e POSTGRESQL_JNDI={postgre-sql-jndi} \
--network slirp4netns:allow_host_loopback=true \
--name={docker-image-name} {docker-image-name}
--name={my-jaxrs-app-db-docker-image-name} {my-jaxrs-app-db-docker-image-name}:latest
----

NOTE: we added the `--network slirp4netns:allow_host_loopback=true` option to Podman which allows to reference the host as `host.containers.internal` from inside the container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ metadata:
labels:
app: postgres
data:
POSTGRES_DB: {postgre-sql-database}
POSTGRES_USER: {postgre-sql-user}
POSTGRES_PASSWORD: {postgre-sql-password}
POSTGRESQL_USER: {postgre-sql-user}
POSTGRESQL_PASSWORD: {postgre-sql-password}
POSTGRESQL_HOST: {postgre-sql-kubernetes-service-name}
POSTGRESQL_PORT: "{postgre-sql-port}"
POSTGRESQL_DATABASE: {postgre-sql-database}
POSTGRESQL_JNDI: {postgre-sql-jndi}
----

apply the ConfigMap configuration to the Kubernetes:
Expand Down Expand Up @@ -65,16 +68,29 @@ spec:
spec:
containers:
- name: postgres
image: 'postgres:latest'
image: '{postgre-docker-image}'
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: postgres-secret
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 ConfigMap configuration to the Kubernetes:
apply the Deployment configuration to the Kubernetes:

[source,bash,subs="normal"]
----
Expand All @@ -91,24 +107,174 @@ Create e fine named `postgres-service.yaml` with the following content:
apiVersion: v1
kind: Service
metadata:
name: postgres
name: {postgre-sql-kubernetes-service-name}
labels:
app: postgres
spec:
type: NodePort
ports:
- port: 5432
- protocol: TCP
port: 5432
targetPort: 5432
selector:
app: postgres
----

apply the ConfigMap configuration to the Kubernetes:
apply the Service configuration to the Kubernetes:

[source,bash,subs="normal"]
----
kubectl apply -f postgres-service.yaml
----

NOTE: if you are using link:https://minikube.sigs.k8s.io/docs/[minikube], and you want to test if PostgreSQL is working, run `kubectl port-forward svc/postgres 5432:5432` and then connect via JDBC using URL `jdbc:postgresql://{postgre-sql-host}:{postgre-sql-port}/{postgre-sql-database}`

=== Quay.io

You can skip this step in case you prefer to re-use the `quay.io/{quay-io-account-name}/{my-jaxrs-app-db-docker-image-name}` Docker Image, that we already pushed to link:quay.io[quay.io] in the `{quay-io-account-name}` account;

Create a public repository named `{my-jaxrs-app-db-docker-image-name}` on link:quay.io[quay.io] (e.g. link:https://quay.io/repository/{quay-io-account-name}/my-jaxrs-app[https://quay.io/repository/{quay-io-account-name}/my-jaxrs-app]);

NOTE: replace `{quay-io-account-name}` with the name of your account in all the commands that will follow

Tag the Docker image:

[source,bash,subs="normal"]
----
podman tag {my-jaxrs-app-db-docker-image-name} quay.io/{quay-io-account-name}/{my-jaxrs-app-db-docker-image-name}
----

Push the `{my-jaxrs-app-db-docker-image-name}` Docker Image to it:

[source,bash,subs="normal"]
----
podman push quay.io/{quay-io-account-name}/{my-jaxrs-app-db-docker-image-name}
----

At this point, the `{my-jaxrs-app-db-docker-image-name}:latest` Docker Image should be publicly available and free to be consumed by any Kubernetes Cluster; you can verify this by running:

[source,bash,subs="normal"]
----
podman pull quay.io/{quay-io-account-name}/{my-jaxrs-app-db-docker-image-name}
----

== Deploy to Kubernetes

To deploy our link:https://docs.docker.com/guides/docker-concepts/the-basics/what-is-an-image/[Docker image] on link:https://minikube.sigs.k8s.io/docs/[minikube], create a file named `deployment-{my-jaxrs-app-db-docker-image-name}.yaml` (see link:https://kubernetes.io/docs/concepts/workloads/controllers/deployment/[kubernetes deployment]) in the same directory as the `Dockerfile` and the `pom.xml` file, with the following content:

.deployment-{my-jaxrs-app-db-docker-image-name}.yaml
[source,yaml,subs="normal"]
----
apiVersion: apps/v1
kind: Deployment
metadata:
name: {my-jaxrs-app-db-docker-image-name}-deployment
labels:
app: {my-jaxrs-app-db-docker-image-name}
spec:
replicas: 1
selector:
matchLabels:
app: {my-jaxrs-app-db-docker-image-name}
template:
metadata:
labels:
app: {my-jaxrs-app-db-docker-image-name}
spec:
containers:
- name: {my-jaxrs-app-db-docker-image-name}
image: quay.io/{quay-io-account-name}/{my-jaxrs-app-db-docker-image-name}
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 the Kubernetes:

[source,bash,subs="normal"]
----
kubectl apply -f deployment-{my-jaxrs-app-db-docker-image-name}.yaml
----

We used to link:https://minikube.sigs.k8s.io/docs/[minikube] as Kubernetes Cluster, hence we expose the deployment as `NodePort`:

[source,bash,subs="normal"]
----
kubectl expose deployment.apps/{my-jaxrs-app-db-docker-image-name}-deployment --type=NodePort --port=8080
----

=== Check the application

Find out on what IP, link:https://minikube.sigs.k8s.io/docs/[minikube] is exposing your service:

[source,bash,subs="normal"]
----
$ minikube service {my-jaxrs-app-db-docker-image-name}-deployment --url
http://192.168.39.143:30433
----

Just like we did before, hit the following URLs, using a utility like `curl`:

.Insert some Data into the Database:
[source,bash]
----
$ curl -X POST http://192.168.39.143:30433/hello/test-table/insert/somedata1/somedata2
{"field1":"somedata1","field2":"somedata2","id":1}
----

.Query the database to show the inserted data:
[source,bash]
----
$ curl http://192.168.39.143:30433/hello/test-table/list
[{"field1":"somedata1","field2":"somedata2","id":1}]
----

== What's next?

link:simple-microservice-infinispan-part1[{simple-microservice-infinispan-part1}]

Back to Guides

< link:../get-started-microservices-on-kubernetes[Back to Getting Started with WildFly micro-services on Kubernetes]
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,26 @@ Build the Docker Image with the following command:

[source,bash,subs="normal"]
----
$ podman build -t {docker-image-name}:latest .
$ podman build -t {my-jaxrs-app-docker-image-name}:latest .
STEP 1/3: FROM quay.io/wildfly/wildfly-runtime:latest
STEP 2/3: COPY --chown=jboss:root target/server $JBOSS_HOME
--> cf1b99511a9b
STEP 3/3: RUN chmod -R ug+rwX $JBOSS_HOME
COMMIT {docker-image-name}:latest
COMMIT {my-jaxrs-app-docker-image-name}:latest
--> e1ab6e80ed20
Successfully tagged localhost/{docker-image-name}:latest
Successfully tagged localhost/{my-jaxrs-app-docker-image-name}:latest
e1ab6e80ed20c3619a7e859f03c71f33b79a4d292f971ed83e7484f4779121d8
----

NOTE: As you can see, we used link:https://podman.io/[Podman], but you can use link:https://www.docker.com/[Docker] as well, depending on your preference or what you have installed on your PC; to do that, replace *podman* with *docker* in the previous command (and in the ones that will follow);

=== Run the Docker Image locally

To check that our brand-new `{docker-image-name}:latest` Docker Image works as expected, run the following command:
To check that our brand-new `{my-jaxrs-app-docker-image-name}:latest` Docker Image works as expected, run the following command:

[source,bash,subs="normal"]
----
$ podman run --rm -p 8080:8080 -p 9990:9990 --name={docker-image-name} {docker-image-name}
$ podman run --rm -p 8080:8080 -p 9990:9990 --name={my-jaxrs-app-docker-image-name} {my-jaxrs-app-docker-image-name}
...
16:14:49,477 INFO [org.wildfly.extension.undertow] (MSC service thread 1-5) WFLYUT0006: Undertow HTTP listener default listening on 0.0.0.0:8080
...
Expand Down Expand Up @@ -211,7 +211,7 @@ $ curl http://127.0.0.1:8080/hello/pippo
Hello 'pippo'.
----

The only difference here, is that the response is served by the application running inside our `{docker-image-name}:latest` Docker Image;
The only difference here, is that the response is served by the application running inside our `{my-jaxrs-app-docker-image-name}:latest` Docker Image;

=== Check Liveness, Readiness and Startup Probes

Expand Down
Loading

0 comments on commit d57a544

Please sign in to comment.