Skip to content

Commit

Permalink
Swarm mode options local env (#62)
Browse files Browse the repository at this point in the history
* Add docker volume

1. Add local docker volume through override docker-compose
2. Default no volume mount

* Add comment for cached mode

* 1. Use default mysql images mysql:8.0.22
2. Remove from Docker.env
3. Open changes for mysql image tag using `MYSQL_IMAGE_TAG` environment variable

* Update README

* add named volume

* Add labels in swarm mmode

* Remove invalid env variable

* remove echo

* Remove container_name & restart from docker-compose.yml

* Add comment for depends_on

* Update build.yml

include override file in ci pipeline

* Add mount path

* Remove quote

* Update all step for local deployment in README

* Update README

* Update README

* Update README

* Add persistent for mysql in swarm mode

* Update README

* Update README

* Update README

* Correct env path in README

Co-authored-by: Raghav2211 <[email protected]>
  • Loading branch information
Raghav2211 and devraghv authored Dec 21, 2020
1 parent acf3ccb commit 4689898
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion todo-app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 32 additions & 7 deletions todo-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<env>/Docker.env up -d
#or
# Override default docker-compose configuration
docker-compose --env-file=env/<env>/Docker.env -f docker-compose.yaml -f env/<env>/docker-compose-override.yml up -d
```
## Deploy ##

- Install

```bash
docker-compose --env-file=env/<env>/Docker.env up -d
#or
# Override default docker-compose configuration
docker-compose --env-file=env/<env>/Docker.env -f docker-compose.yaml -f env/<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`
12 changes: 4 additions & 8 deletions todo-app/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
9 changes: 4 additions & 5 deletions todo-app/env/ci/Docker.env
Original file line number Diff line number Diff line change
@@ -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
MYSQL_DATABASE=psi
MYSQL_DATA_DEST_PATH=/var/lib/mysql
17 changes: 9 additions & 8 deletions todo-app/env/local/Docker.env
Original file line number Diff line number Diff line change
@@ -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=
6 changes: 5 additions & 1 deletion todo-app/env/local/docker-compose-override.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 17 additions & 3 deletions todo-app/env/local/docker-stack-compose-override.yml
Original file line number Diff line number Diff line change
@@ -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"
- "node.role == worker"
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 5
15 changes: 15 additions & 0 deletions todo-app/env/local/docker-stack-persistent-compose-override.yml
Original file line number Diff line number Diff line change
@@ -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
109 changes: 108 additions & 1 deletion todo-app/swarm/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,108 @@
# Deploy Todo Application in swarm cluster
# 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/<env>/Docker.env -f ../docker-compose.yaml -f ../env/<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`

0 comments on commit 4689898

Please sign in to comment.