From fbbc109c226159cd20f302e34b33000092fdb25e Mon Sep 17 00:00:00 2001 From: Maximilian Georg Kurzawski <52952859+MKLepium@users.noreply.github.com> Date: Thu, 10 Aug 2023 23:20:41 +0200 Subject: [PATCH] Kubernetes and Docker files for Server and Client (#231) * Added Docker and Kubernetes support for running Portals Server and Client. --- scripts/deployment/Kubernetes/Deployment.yaml | 35 ++++++++++++ scripts/deployment/README.md | 55 +++++++++++++++++++ scripts/deployment/docker/Dockerfile-Client | 18 ++++++ scripts/deployment/docker/Dockerfile-Server | 16 ++++++ .../docker/Dockerfile-ShoppingCartClient | 25 +++++++++ 5 files changed, 149 insertions(+) create mode 100644 scripts/deployment/Kubernetes/Deployment.yaml create mode 100644 scripts/deployment/README.md create mode 100644 scripts/deployment/docker/Dockerfile-Client create mode 100644 scripts/deployment/docker/Dockerfile-Server create mode 100644 scripts/deployment/docker/Dockerfile-ShoppingCartClient diff --git a/scripts/deployment/Kubernetes/Deployment.yaml b/scripts/deployment/Kubernetes/Deployment.yaml new file mode 100644 index 00000000..c7d1c5c0 --- /dev/null +++ b/scripts/deployment/Kubernetes/Deployment.yaml @@ -0,0 +1,35 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: portals + template: + metadata: + labels: + app: portals + spec: + containers: + - name: portals-server + image: "test-server" + imagePullPolicy: Never + ports: + - containerPort: 8080 + +--- +apiVersion: v1 +kind: Service +metadata: + name: test-service +spec: + type: NodePort + selector: + app: portals + ports: + - protocol: TCP + port: 8080 + targetPort: 8080 + diff --git a/scripts/deployment/README.md b/scripts/deployment/README.md new file mode 100644 index 00000000..c4fafd8a --- /dev/null +++ b/scripts/deployment/README.md @@ -0,0 +1,55 @@ +# Portals + +## Build the Docker Container +The Dockerfile +To build the Docker container for the server, use the following command (replace `test-server` with a more meaningful container tag): + +`docker build -f scripts/deployment/docker/Dockerfile-Server . -t test-server` + +To run the container: + +`docker run -dp 127.0.0.1:8080:8080 test-server` + +## Kubernetes + +If you want to run the server on a kubernetes cluster (e.g. a local minikube setup) + +0. Make sure your cluster/minikube setup is running + +`minikube start` + +1. Deploy the container into the Kubernetes environment (Replace with the actual image name you have chosen): + +`kubectl apply -f scripts/deployment/Kubernetes/Deployment.yaml` + +2. Wait for a bit and check if the pods have started + +`kubectl get pods` + + +## Known Issues : +- If the status of the pod is `ImagePullBackOff`, and you are using Minikube, ensure that Minikube can use the built container. Some Minikube setups require you to push the built Docker image to Minikube with the following command: +`minikube image load ` + + +- For WSL/Minikube users, connecting to the pod can be a little tricky. The easiest workaround is to use port forwarding: +`kubectl port-forward 8080:8080` To force k8s to forward the port from the pod to localhost + + +# Full Example + +Before deploying the Server and the Client into the Kubernetes environment, make sure you have started Minikube and built the Docker images for the Client and Server: + +``` +minikube start +docker build -f scripts/deployment/docker/Dockerfile-ShoppingCartClient . -t test-client +docker build -f scripts/deployment/docker/Dockerfile-Server . -t test-server +``` + +1. Deploy the Server into the Kubernetes environment (Adjust the image name in the `deployment.yaml` file): + +`kubectl apply -f scripts/deployment/Kubernetes/Deployment.yaml` + +2. Once the Server has started, you can run the ShoppingCartClient: + +`docker run --network host test-client` \ No newline at end of file diff --git a/scripts/deployment/docker/Dockerfile-Client b/scripts/deployment/docker/Dockerfile-Client new file mode 100644 index 00000000..bd27e3fb --- /dev/null +++ b/scripts/deployment/docker/Dockerfile-Client @@ -0,0 +1,18 @@ +FROM sbtscala/scala-sbt:openjdk-oraclelinux8-11.0.16_1.8.1_3.2.1 + +# Copy Necessary Files +COPY ./portals-distributed/ /app/portals-distributed +COPY ./portals-core/ /app/portals-core +COPY ./project/ /app/project +COPY ./build.sbt /app/build.sbt +# Necessary because of imports in portals-distributed/examples +COPY ./portals-examples/ /app/portals-examples + + +WORKDIR /app +RUN sbt compile + +ENTRYPOINT sbt "distributed/runMain portals.distributed.ClientCLI submitDir \ + --directory portals-distributed/target/scala-3.3.0/classes" && \ + sbt "distributed/runMain portals.distributed.ClientCLI launch \ + --application portals.distributed.examples.HelloWorld$" \ No newline at end of file diff --git a/scripts/deployment/docker/Dockerfile-Server b/scripts/deployment/docker/Dockerfile-Server new file mode 100644 index 00000000..e9dd3cc1 --- /dev/null +++ b/scripts/deployment/docker/Dockerfile-Server @@ -0,0 +1,16 @@ +FROM sbtscala/scala-sbt:openjdk-oraclelinux8-11.0.16_1.8.1_3.2.1 + +# Copy Necessary Files +COPY ./portals-distributed/ /app/portals-distributed +COPY ./portals-core/ /app/portals-core +COPY ./project/ /app/project +COPY ./build.sbt /app/build.sbt +# Necessary because of imports in portals-distributed/examples +COPY ./portals-examples/ /app/portals-examples + + +WORKDIR /app +RUN sbt compile +EXPOSE 8080 +ENTRYPOINT sbt "distributed/runMain portals.distributed.SBTRunServer" + diff --git a/scripts/deployment/docker/Dockerfile-ShoppingCartClient b/scripts/deployment/docker/Dockerfile-ShoppingCartClient new file mode 100644 index 00000000..03919b86 --- /dev/null +++ b/scripts/deployment/docker/Dockerfile-ShoppingCartClient @@ -0,0 +1,25 @@ +FROM sbtscala/scala-sbt:openjdk-oraclelinux8-11.0.16_1.8.1_3.2.1 + +# Copy Necessary Files +COPY ./portals-distributed/ /app/portals-distributed +COPY ./portals-core/ /app/portals-core +COPY ./project/ /app/project +COPY ./build.sbt /app/build.sbt +# Necessary because of imports in portals-distributed/examples +COPY ./portals-examples/ /app/portals-examples + + +WORKDIR /app +RUN sbt compile + +ENTRYPOINT sbt "distributed/runMain portals.distributed.ClientCLI submitDir \ + --directory portals-distributed/target/scala-3.3.0/classes" && \ + sbt "distributed/runMain portals.distributed.ClientCLI launch \ + --application portals.distributed.examples.shoppingcart.Inventory$ " && \ + sbt "distributed/runMain portals.distributed.ClientCLI launch \ + --application portals.distributed.examples.shoppingcart.Cart$ " && \ + sbt "distributed/runMain portals.distributed.ClientCLI launch \ + --application portals.distributed.examples.shoppingcart.Orders$ " && \ + sbt "distributed/runMain portals.distributed.ClientCLI launch \ + --application portals.distributed.examples.shoppingcart.Analytics$ " + \ No newline at end of file