Skip to content

Commit

Permalink
Kubernetes and Docker files for Server and Client (#231)
Browse files Browse the repository at this point in the history
* Added Docker and Kubernetes support for running Portals Server and Client.
  • Loading branch information
MKLepium authored Aug 10, 2023
1 parent ccb285f commit fbbc109
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
35 changes: 35 additions & 0 deletions scripts/deployment/Kubernetes/Deployment.yaml
Original file line number Diff line number Diff line change
@@ -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

55 changes: 55 additions & 0 deletions scripts/deployment/README.md
Original file line number Diff line number Diff line change
@@ -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 <image-name> 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 <image name>`


- For WSL/Minikube users, connecting to the pod can be a little tricky. The easiest workaround is to use port forwarding:
`kubectl port-forward <pod-name> 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`
18 changes: 18 additions & 0 deletions scripts/deployment/docker/Dockerfile-Client
Original file line number Diff line number Diff line change
@@ -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$"
16 changes: 16 additions & 0 deletions scripts/deployment/docker/Dockerfile-Server
Original file line number Diff line number Diff line change
@@ -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"

25 changes: 25 additions & 0 deletions scripts/deployment/docker/Dockerfile-ShoppingCartClient
Original file line number Diff line number Diff line change
@@ -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$ "

0 comments on commit fbbc109

Please sign in to comment.