diff --git a/guides/get-started-microservices-on-kubernetes/_includes/_constants.adoc b/guides/get-started-microservices-on-kubernetes/_includes/_constants.adoc index 88bb6a33..60055e5d 100644 --- a/guides/get-started-microservices-on-kubernetes/_includes/_constants.adoc +++ b/guides/get-started-microservices-on-kubernetes/_includes/_constants.adoc @@ -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 @@ -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 diff --git a/guides/get-started-microservices-on-kubernetes/simple-microservice-database-part1.adoc b/guides/get-started-microservices-on-kubernetes/simple-microservice-database-part1.adoc index f1df616d..cae90f4e 100644 --- a/guides/get-started-microservices-on-kubernetes/simple-microservice-database-part1.adoc +++ b/guides/get-started-microservices-on-kubernetes/simple-microservice-database-part1.adoc @@ -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 @@ -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 @@ -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"] ---- @@ -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 diff --git a/guides/get-started-microservices-on-kubernetes/simple-microservice-database-part2.adoc b/guides/get-started-microservices-on-kubernetes/simple-microservice-database-part2.adoc index 314e30d5..f099cbf0 100644 --- a/guides/get-started-microservices-on-kubernetes/simple-microservice-database-part2.adoc +++ b/guides/get-started-microservices-on-kubernetes/simple-microservice-database-part2.adoc @@ -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: @@ -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"] ---- @@ -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] \ No newline at end of file diff --git a/guides/get-started-microservices-on-kubernetes/simple-microservice-part1.adoc b/guides/get-started-microservices-on-kubernetes/simple-microservice-part1.adoc index 394288ed..f85b3adb 100644 --- a/guides/get-started-microservices-on-kubernetes/simple-microservice-part1.adoc +++ b/guides/get-started-microservices-on-kubernetes/simple-microservice-part1.adoc @@ -159,14 +159,14 @@ 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 ---- @@ -174,11 +174,11 @@ NOTE: As you can see, we used link:https://podman.io/[Podman], but you can use l === 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 ... @@ -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 diff --git a/guides/get-started-microservices-on-kubernetes/simple-microservice-part2.adoc b/guides/get-started-microservices-on-kubernetes/simple-microservice-part2.adoc index 1fd16684..815bf085 100644 --- a/guides/get-started-microservices-on-kubernetes/simple-microservice-part2.adoc +++ b/guides/get-started-microservices-on-kubernetes/simple-microservice-part2.adoc @@ -16,15 +16,15 @@ include::_includes/_constants.adoc[] == Pushing the Docker Image to an Image Registry -To make the `{docker-image-name}:latest` Docker Image available to Kubernetes, we need to push it to some Image Registry that is accessible by the Kubernetes cluster we want to use; +To make the `{my-jaxrs-app-docker-image-name}:latest` Docker Image available to Kubernetes, we need to push it to some Image Registry that is accessible by the Kubernetes cluster we want to use; -There are many options to achieve this; in this guide, we will push the `{docker-image-name}:latest` Docker Image, to the link:quay.io[quay.io] Image Registry; +There are many options to achieve this; in this guide, we will push the `{my-jaxrs-app-docker-image-name}:latest` Docker Image, to the link:quay.io[quay.io] Image Registry; === Quay.io -You can skip this step in case you prefer to re-use the `quay.io/{quay-io-account-name}/{docker-image-name}` Docker Image, that we already pushed to link:quay.io[quay.io] in the `{quay-io-account-name}` account; +You can skip this step in case you prefer to re-use the `quay.io/{quay-io-account-name}/{my-jaxrs-app-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 `{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]); +Create a public repository named `{my-jaxrs-app-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 @@ -32,48 +32,49 @@ Tag the Docker image: [source,bash,subs="normal"] ---- -podman tag {docker-image-name} quay.io/{quay-io-account-name}/{docker-image-name} +podman tag {my-jaxrs-app-docker-image-name} quay.io/{quay-io-account-name}/{my-jaxrs-app-docker-image-name} ---- -Push the `{docker-image-name}` Docker Image to it: +Push the `{my-jaxrs-app-docker-image-name}` Docker Image to it: [source,bash,subs="normal"] ---- -podman push quay.io/{quay-io-account-name}/{docker-image-name} +podman push quay.io/{quay-io-account-name}/{my-jaxrs-app-docker-image-name} ---- -At this point, the `{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: +At this point, the `{my-jaxrs-app-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}/{docker-image-name} +podman pull quay.io/{quay-io-account-name}/{my-jaxrs-app-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-{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: +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-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-docker-image-name}.yaml [source,yaml,subs="normal"] ---- apiVersion: apps/v1 kind: Deployment metadata: - name: {docker-image-name}-deployment + name: {my-jaxrs-app-docker-image-name}-deployment labels: - app: {docker-image-name} + app: {my-jaxrs-app-docker-image-name} spec: replicas: 1 selector: matchLabels: - app: {docker-image-name} + app: {my-jaxrs-app-docker-image-name} template: metadata: labels: - app: {docker-image-name} + app: {my-jaxrs-app-docker-image-name} spec: containers: - - name: {docker-image-name} - image: quay.io/{quay-io-account-name}/{docker-image-name} + - name: {my-jaxrs-app-docker-image-name} + image: quay.io/{quay-io-account-name}/{my-jaxrs-app-docker-image-name} ports: - containerPort: 8080 - containerPort: 9990 @@ -95,16 +96,16 @@ Deploy to your Kubernetes Cluster: [source,bash,subs="normal"] ---- -$ kubectl apply -f deployment-{docker-image-name}.yaml -deployment.apps/{docker-image-name}-deployment configured +$ kubectl apply -f deployment-{my-jaxrs-app-docker-image-name}.yaml +deployment.apps/{my-jaxrs-app-docker-image-name}-deployment created ---- 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/{docker-image-name}-deployment --type=NodePort --port=8080 -service/{docker-image-name}-deployment exposed +$ kubectl expose deployment.apps/{my-jaxrs-app-docker-image-name}-deployment --type=NodePort --port=8080 +service/{my-jaxrs-app-docker-image-name}-deployment exposed ---- === Check your application @@ -113,7 +114,7 @@ Find out on what IP, link:https://minikube.sigs.k8s.io/docs/[minikube] is exposi [source,bash,subs="normal"] ---- -$ minikube service {docker-image-name}-deployment --url +$ minikube service {my-jaxrs-app-docker-image-name}-deployment --url http://192.168.39.139:30782 ----