diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dbe3d935..a96aa2af 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -38,7 +38,7 @@ jobs: docker build --tag psi-todo . docker build --build-arg JAR_FILE=build/libs/\*.jar --tag psi-todo . - name: Build local stack - run: docker-compose --env-file=env/ci/Docker.env up -d + run: docker-compose --env-file=env/ci/Docker.env -f docker-compose.yaml -f env/local/docker-compose-override.yml up -d - name: Test local stack run: | sleep 120 diff --git a/todo-app/Dockerfile b/todo-app/Dockerfile index 301f3fd9..63e347db 100644 --- a/todo-app/Dockerfile +++ b/todo-app/Dockerfile @@ -4,7 +4,6 @@ ARG GROUP=psi ARG UID=1000 ARG GID=1000 ARG JAR_FILE=target/*.jar -RUN echo $JAR_FILE RUN groupadd -g ${GID} ${GROUP} && useradd -u ${UID} -g ${GROUP} ${USER} USER ${USER}:${GROUP} COPY ${JAR_FILE} app.jar diff --git a/todo-app/README.md b/todo-app/README.md index 22ce0dcb..ad11d574 100644 --- a/todo-app/README.md +++ b/todo-app/README.md @@ -56,10 +56,35 @@ If you use Gradle, you can run it with the following command docker build --build-arg JAR_FILE=build/libs/\*.jar --tag psi-todo:1.0.0 . ``` -## Run ## -```bash -docker-compose --env-file=env//Docker.env up -d -#or -# Override default docker-compose configuration -docker-compose --env-file=env//Docker.env -f docker-compose.yaml -f env//docker-compose-override.yml up -d -``` \ No newline at end of file +## Deploy ## + + - Install + + ```bash + docker-compose --env-file=env//Docker.env up -d + #or + # Override default docker-compose configuration + docker-compose --env-file=env//Docker.env -f docker-compose.yaml -f env//docker-compose-override.yml up -d + ``` + - Uninstall + + ```bash + docker-compose down + ``` + +## Configuration ## + +The following table lists the configurable parameters of the TodoApp swarm cluster and their default values. + + Parameter | Description | Default + --- | --- | --- + `PSI_TODO_IMAGE_TAG` | Image tag for Todo-App | `1.0.0` + `BASIC_AUTH_ENABLE` | Enable spring Basic-Auth | `false` + `BASIC_AUTH_USERNAME` | Username of Basic-Auth | `` + `BASIC_AUTH_PASSWORD` | Password of Basic-Auth | `` + `MYSQL_IMAGE_TAG` | Image tag for Mysql | `8.0.22` + `MYSQL_USER` | Username of new user to create | `root` + `MYSQL_PASSWORD` | Password for the new user | `root` + `MYSQL_DATABASE` | Name for new database to create | `psi` + `MYSQL_DATA_SRC_PATH` | Host path for persistence mysql data | `` + `MYSQL_DATA_DEST_PATH` | Mount directory path in mysql container | `/var/lib/mysql` \ No newline at end of file diff --git a/todo-app/docker-compose.yaml b/todo-app/docker-compose.yaml index 65aebbde..e6e1cffe 100644 --- a/todo-app/docker-compose.yaml +++ b/todo-app/docker-compose.yaml @@ -1,19 +1,15 @@ version: "3" services: psimysql: - image: mysql:${MYSQL_IMAGE_TAG:-8.0.22} - container_name: mysql - ports: - - 3306:3306 + image: mysql:${MYSQL_IMAGE_TAG} environment: MYSQL_DATABASE: ${MYSQL_DATABASE} MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} psitodoapp: - image: ${PSI_TODO_IMAGE} - container_name: psi-todo - restart: on-failure:5 + image: psi-todo:${PSI_TODO_IMAGE_TAG:-latest} depends_on: - - psimysql + - psimysql # will not wait for mysql to be “ready” before starting todo-app , It'll only confirm dependency order & + # will automatic create dependencies which include in service defination ports: - 8080:8080 environment: diff --git a/todo-app/env/ci/Docker.env b/todo-app/env/ci/Docker.env index a8095025..74108421 100644 --- a/todo-app/env/ci/Docker.env +++ b/todo-app/env/ci/Docker.env @@ -1,9 +1,8 @@ # Set these env variables -#App variables -PSI_TODO_IMAGE=psi-todo - -#Database variables +# Mysql variables +MYSQL_IMAGE_TAG=8.0.22 MYSQL_USER=root MYSQL_PASSWORD=root -MYSQL_DATABASE=psi \ No newline at end of file +MYSQL_DATABASE=psi +MYSQL_DATA_DEST_PATH=/var/lib/mysql \ No newline at end of file diff --git a/todo-app/env/local/Docker.env b/todo-app/env/local/Docker.env index 59199395..d205e7e4 100644 --- a/todo-app/env/local/Docker.env +++ b/todo-app/env/local/Docker.env @@ -1,17 +1,18 @@ # Set these env variables -#App variables +# App variables PSI_TODO_REPLICA=1 -PSI_TODO_IMAGE=psi-todo:1.0.0 -PSI_TODO_STACK_IMAGE=localhost:5000/psi-todo +PSI_TODO_IMAGE_TAG=1.0.0 +PSI_TODO_STACK_IMAGE=localhost:5000/psi-todo +#BASIC_AUTH_ENABLE +# if BASIC_AUTH_ENABLE=true [Default is false] then set below two variables +#BASIC_AUTH_USERNAME= +#BASIC_AUTH_PASSWORD= -#Database variables +# Mysql variables +MYSQL_IMAGE_TAG=8.0.22 MYSQL_USER=root MYSQL_PASSWORD=root MYSQL_DATABASE=psi #MYSQL_DATA_SRC_PATH= MYSQL_DATA_DEST_PATH=/var/lib/mysql -#BASIC_AUTH_ENABLE -# if BASIC_AUTH_ENABLE=true [Default is false] then set below two variables -#BASIC_AUTH_USERNAME= -#BASIC_AUTH_PASSWORD= \ No newline at end of file diff --git a/todo-app/env/local/docker-compose-override.yml b/todo-app/env/local/docker-compose-override.yml index e5ddcfc4..441d9f15 100644 --- a/todo-app/env/local/docker-compose-override.yml +++ b/todo-app/env/local/docker-compose-override.yml @@ -1,10 +1,14 @@ version: "3" services: - psimysql: + psimysql: + container_name: mysql #Specify a custom container name, rather than a generated default name volumes: - mysql_data:${MYSQL_DATA_DEST_PATH}:cached # Strong consistency is quite expensive, requiring coordination between all of a files writers to guarantee # they don’t inappropriately clobber each other’s changes # So Relax consistency guarantees using `cached` mode (Don’t do this in production …) + psitodoapp: + container_name: psi-todo #Specify a custom container name, rather than a generated default name + restart: on-failure:5 volumes: mysql_data: # To use below configuration for host path ensure MYSQL_DATA_SRC_PATH should exists diff --git a/todo-app/env/local/docker-stack-compose-override.yml b/todo-app/env/local/docker-stack-compose-override.yml index e16717c3..33f59d2b 100644 --- a/todo-app/env/local/docker-stack-compose-override.yml +++ b/todo-app/env/local/docker-stack-compose-override.yml @@ -1,14 +1,28 @@ version: "3" services: psimysql: - deploy: + deploy: + labels: + name: mysql + version: ${MYSQL_IMAGE_TAG:-8.0.22} + component: database + env: "local" placement: constraints: - "node.role == worker" psitodoapp: image: ${PSI_TODO_STACK_IMAGE} - deploy: + deploy: + labels: + name: psi-todo + version: ${PSI_TODO_IMAGE_TAG:-latest} + component: web + env: "local" replicas: ${PSI_TODO_REPLICA} placement: constraints: - - "node.role == worker" \ No newline at end of file + - "node.role == worker" + restart_policy: + condition: on-failure + delay: 5s + max_attempts: 5 \ No newline at end of file diff --git a/todo-app/env/local/docker-stack-persistent-compose-override.yml b/todo-app/env/local/docker-stack-persistent-compose-override.yml new file mode 100644 index 00000000..4ca359a1 --- /dev/null +++ b/todo-app/env/local/docker-stack-persistent-compose-override.yml @@ -0,0 +1,15 @@ +version: "3" +services: + psimysql: + volumes: + - mysql_data:${MYSQL_DATA_DEST_PATH}:cached # Strong consistency is quite expensive, requiring coordination between all of a files writers to guarantee + # they don’t inappropriately clobber each other’s changes + # So Relax consistency guarantees using `cached` mode (Don’t do this in production …) +volumes: + mysql_data: + # To use below configuration for host path ensure MYSQL_DATA_SRC_PATH should exists + # driver: local + # driver_opts: + # type: none + # device: $PWD/${MYSQL_DATA_SRC_PATH} + # o: bind \ No newline at end of file diff --git a/todo-app/swarm/README.md b/todo-app/swarm/README.md index 727e31f3..3442d9ee 100644 --- a/todo-app/swarm/README.md +++ b/todo-app/swarm/README.md @@ -1 +1,108 @@ -# Deploy Todo Application in swarm cluster \ No newline at end of file +# Deploy Todo Application in swarm cluster + +- Local Deployment + +## Local Deployment ## + +- Setup cluster + + Install Dependencies and run cluster + + ```bash + # Install all dependencies for local cluster + $ bash install-dependencies.sh all + + # Boot local cluster [ Swarm Manager(s)/Worker(s) node ] + $ bash cluster.sh create local + ``` + + Verify cluster + + ```bash + $ bash cluster.sh view local + ------------------------------------------------------------Nodes------------------------------------------------------------ + ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION + zsrfmw8g8wavf8nsq1uo8xz7j * manager1 Ready Active Leader 19.03.12 + quwa5fhv5vk7cuzw8m88prj8m worker1 Ready Active 19.03.12 + ------------------------------------------------------------Vm(s)------------------------------------------------------------ + NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS + manager1 * virtualbox Running tcp://192.168.99.149:2376 v19.03.12 + worker1 - virtualbox Running tcp://192.168.99.150:2376 v19.03.12 + ``` + + + - Link host docker client to the VM's docker daemon + + ```bash + $ eval $(docker-machine env manager1) + ``` + - Build + + [Build & Create Docker Image](../README.md#build) + + - Deploy the Stack + + ```bash + # Local registry + $ docker service create --name registry --publish 5000:5000 registry:2 + + # Tag the image as localhost:5000/psi-todo. This creates an additional tag for the existing image. + $ docker tag psi-todo:1.0.0 localhost:5000/psi-todo + + # Push the image to the local registry running at localhost:5000 + $ docker push localhost:5000/psi-todo + + # Remove locally cached images + $ docker image remove psi-todo + $ docker image remove psi-todo:1.0.0 + $ docker image remove localhost:5000/psi-todo + + # Deploy todo app cluster + $ docker stack deploy -c <(docker-compose --env-file=../env//Docker.env -f ../docker-compose.yaml -f ../env//docker-stack-compose-override.yml config) psi-todo + + # or + # Apply persistent with mysql + docker stack deploy -c <(docker-compose --env-file=../env/local/Docker.env -f ../docker-compose.yaml -f ../env/local/docker-stack-compose-override.yml -f ../env/local/docker-stack-persistent-compose-override.yml config) psi-todo + + ``` + + - Verify all service up & running + + ```bash + $ docker service ls + ID NAME MODE REPLICAS IMAGE PORTS + x2or6oc3lkes psi-todo_psimysql replicated 1/1 mysql:8.0.22 + zd2vezg73ksd psi-todo_psitodoapp replicated 1/1 localhost:5000/psi-todo:latest *:8080->8080/tcp + ac6h0l8eg3ko registry replicated 1/1 registry:2 *:5000->5000/tcp + + ``` + + - Access swagger api endpoint with below url. + + http://localhost:8080/swagger-ui/ + + - Uninstalling the Stack + + + ```bash + $ docker stack rm psi-todo + ``` + + - Configuration + + The following table lists the configurable parameters of the TodoApp swarm cluster and their default values. + + Parameter | Description | Default + --- | --- | --- + `PSI_TODO_REPLICA` | No of replica for Todo-app | `1` + `PSI_TODO_STACK_IMAGE` | Todo-app Image | `localhost:5000/psi-todo` + `BASIC_AUTH_ENABLE` | Enable spring Basic-Auth | `false` + `BASIC_AUTH_USERNAME` | Username of Basic-Auth | `` + `BASIC_AUTH_PASSWORD` | Password of Basic-Auth | `` + `MYSQL_IMAGE_TAG` | Image tag for Mysql | `8.0.22` + `MYSQL_USER` | Username of new user to create | `root` + `MYSQL_PASSWORD` | Password for the new user | `root` + `MYSQL_DATABASE` | Name for new database to create | `psi` + `MYSQL_DATA_SRC_PATH` | Host path for persistence mysql data | `` + `MYSQL_DATA_DEST_PATH` | Mount directory path in mysql container | `/var/lib/mysql` +