Skip to content

Commit

Permalink
Add docker swarm (#72)
Browse files Browse the repository at this point in the history
* docker-swarm: [WIP] compose

* Added a lot

* Added versions to all services.

* added portainer

* adding option to use swarm with local images or production images

* Added make files.

* Updated make again.

* updated make

* trying portainer with linux

---------

Co-authored-by: Ahmed Elwasefi <a.m.elwasefi@gmail.com>
Co-authored-by: HusseinYasser <husseinyasser388@gmail.com>
  • Loading branch information
3 people authored May 12, 2024
1 parent 49af6a0 commit 3e534ed
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 53 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

build:
mvn -DskipTests package
docker compose build
docker build ./services/jobs --tag workup:service_jobs
docker build ./services/payments --tag workup:service_payments
docker build ./services/users --tag workup:service_users
docker build ./services/contracts --tag workup:service_contracts

up:
docker compose up --force-recreate
docker stack deploy -c compose.yaml -c compose.override.yaml workup

portainer:
docker stack deploy -c portainer-agent-stack.yml portainer

reload:
mvn -DskipTests package
Expand Down
11 changes: 11 additions & 0 deletions compose.override.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

version: '3.7'
services:
service_jobs:
image: workup:service_jobs

service_payments:
image: workup:service_payments

service_contracts:
image: workup:service_contracts
98 changes: 58 additions & 40 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This docker compose is for testing a real production env locally..
# Simulates a network etc.
version: '3.7'

services:
# ----- GENERAL SERVICES -------
Expand All @@ -11,76 +12,93 @@ services:
timeout: 30s
retries: 3
networks:
- microservices
ports:
- "5672:5672"
- "15672:15672"

- default

service_redis:
image: redis:latest
image: redis:7.2.4
healthcheck:
test: redis-cli ping
interval: 30s
timeout: 30s
retries: 3
networks:
- microservices
ports:
- "6379:6379"
- default

# ----- JOBS MICROSERVICE -------
service_jobs:
build: ./services/jobs
image: ahmad45123/workup:service_jobs
depends_on:
jobs_db:
condition: service_healthy
service_mq:
condition: service_healthy
- jobs_db
- service_mq
networks:
- microservices
- default
- jobs

# ----- PAYMENTS MICROSERVICE -------
service_payments:
build: ./services/payments
depends_on:
payments_db:
condition: service_healthy
service_mq:
condition: service_healthy
service_redis:
condition: service_healthy
networks:
- microservices
- payments
env_file:
- ./services/jobs/.env

jobs_db:
image: cassandra:4.0.7
networks:
- jobs # only accessible by the jobs service
volumes:
- ./services/jobs/cassandra-config/cassandra.yaml:/etc/cassandra/cassandra.yaml
healthcheck:
test: [ "CMD", "cqlsh", "-e", "describe keyspaces" ]
interval: 20s
timeout: 10s
retries: 60
ports:
- "9042:9042"
networks:
- jobs

# ----- PAYMENTS MICROSERVICE -------
service_payments:
image: ahmad45123/workup:service_payments
depends_on:
- payments_db
- service_mq
- service_redis
networks:
- default
- payments
env_file:
- ./services/payments/.env

payments_db:
image: postgres:latest
image: postgres:12.18
environment:
POSTGRES_PASSWORD: payments_password
POSTGRES_USER: payments_user
POSTGRES_DB: payments_database
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "pg_isready"]
interval: 20s
timeout: 10s
retries: 10
networks:
- payments

# ----- CONTRACTS MICROSERVICE -------
service_contracts:
image: ahmad45123/workup:service_contracts
depends_on:
- contracts_db
- service_mq
networks:
- default
- contracts
env_file:
- ./services/contracts/.env

contracts_db:
image: cassandra:4.0.7
healthcheck:
test: [ "CMD", "cqlsh", "-e", "describe keyspaces" ]
interval: 20s
timeout: 10s
retries: 60
networks:
- contracts

networks:
microservices:
driver: bridge
default:
jobs:
driver: bridge
payments:
driver: bridge
contracts:
39 changes: 39 additions & 0 deletions portainer-agent-stack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: '3.2'

services:
agent:
image: portainer/agent:2.19.5
volumes:
- /var/run/docker.sock:/var/run/docker.sock:z
- /var/lib/docker/volumes:/var/lib/docker/volumes
networks:
- agent_network
deploy:
mode: global
placement:
constraints: [node.platform.os == linux]

portainer:
image: portainer/portainer-ce:2.19.5
command: -H tcp://tasks.agent:9001 --tlsskipverify
ports:
- "9443:9443"
- "9000:9000"
- "8000:8000"
volumes:
- portainer_data:/data
networks:
- agent_network
deploy:
mode: replicated
replicas: 1
placement:
constraints: [node.role == manager]

networks:
agent_network:
driver: overlay
attachable: true

volumes:
portainer_data:
2 changes: 2 additions & 0 deletions services/contracts/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONTRACTS_MQ_URL=service_mq
CONTRACTS_DB_URL=contracts_db:9042
4 changes: 2 additions & 2 deletions services/contracts/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
spring.application.name=contract
spring.rabbitmq.host=localhost
spring.rabbitmq.host=${CONTRACTS_MQ_URL}
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cassandra.local-datacenter=datacenter1
spring.cassandra.keyspace-name=contracts_data
spring.cassandra.contact-points=localhost:9042
spring.cassandra.contact-points=${CONTRACTS_DB_URL}
spring.cassandra.schema-action=CREATE_IF_NOT_EXISTS


2 changes: 2 additions & 0 deletions services/jobs/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JOBS_DB_URL=jobs_db:9042
JOBS_MQ_URL=service_mq
8 changes: 4 additions & 4 deletions services/jobs/cassandra-config/cassandra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ seed_provider:
parameters:
# seeds is actually a comma-delimited list of addresses.
# Ex: "<ip1>,<ip2>,<ip3>"
- seeds: "172.18.0.2"
- seeds: "10.0.11.6"

# For workloads with more data than can fit in memory, Cassandra's
# bottleneck will be reads that need to fetch data from
Expand Down Expand Up @@ -663,7 +663,7 @@ ssl_storage_port: 7001
#
# Setting listen_address to 0.0.0.0 is always wrong.
#
listen_address: 172.18.0.2
listen_address: 10.0.11.6

# Set listen_address OR listen_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
Expand All @@ -677,7 +677,7 @@ listen_address: 172.18.0.2

# Address to broadcast to other Cassandra nodes
# Leaving this blank will set it to the same value as listen_address
broadcast_address: 172.18.0.2
broadcast_address: 10.0.11.6

# When using multiple physical network interfaces, set this
# to true to listen on broadcast_address in addition to
Expand Down Expand Up @@ -763,7 +763,7 @@ rpc_address: 0.0.0.0
# be set to 0.0.0.0. If left blank, this will be set to the value of
# rpc_address. If rpc_address is set to 0.0.0.0, broadcast_rpc_address must
# be set.
broadcast_rpc_address: 172.18.0.2
broadcast_rpc_address: 10.0.11.6

# enable or disable keepalive on rpc/native connections
rpc_keepalive: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand All @@ -24,4 +25,11 @@ public Queue myQueue() {
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}

@Bean
public ApplicationRunner runner() {
return args -> {
System.out.println("WE ARE NEW IN JOBS");
};
}
}
4 changes: 2 additions & 2 deletions services/jobs/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
spring.rabbitmq.host=service_mq
spring.rabbitmq.host=${JOBS_MQ_URL}
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest


spring.cassandra.local-datacenter=datacenter1
spring.cassandra.keyspace-name=jobs_data
spring.cassandra.contact-points=jobs_db:9042
spring.cassandra.contact-points=${JOBS_DB_URL}
spring.cassandra.schema-action=CREATE_IF_NOT_EXISTS
2 changes: 2 additions & 0 deletions services/payments/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PAYMENTS_DB_URL=jdbc:postgresql://payments_db:5432/payments_database
PAYMENTS_MQ_URL=service_mq
4 changes: 2 additions & 2 deletions services/payments/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring.application.name=payments
spring.datasource.url=jdbc:postgresql://localhost:5432/payments_database
spring.datasource.url=${PAYMENTS_DB_URL}
spring.datasource.username=payments_user
spring.datasource.password=payments_password
spring.jpa.hibernate.ddl-auto=create-drop
Expand All @@ -8,7 +8,7 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
server.error.include-message=always

spring.rabbitmq.host=service_mq
spring.rabbitmq.host=${PAYMENTS_MQ_URL}
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
Expand Down
2 changes: 1 addition & 1 deletion shared/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.cache.type=redis
spring.cache.host=localhost
spring.cache.host=service_redis
spring.cache.port=6379
spring.cache.redis.time-to-live=600000

0 comments on commit 3e534ed

Please sign in to comment.