Skip to content

Commit f5c4f1b

Browse files
committed
Switch from services to stacks
1 parent 634a195 commit f5c4f1b

File tree

7 files changed

+188
-185
lines changed

7 files changed

+188
-185
lines changed

services/rabbit/.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
docker-compose.yml
2-
docker-compose*.yaml
1+
*.yml
2+
*.env
3+
!template*.env
34
!erlang.cookie.secret.template
45
rabbitmq.conf
56
haproxy.cfg
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#
2+
# Variables
3+
#
4+
5+
NODE_IXS := $(shell seq 1 $(NODE_COUNT))
6+
7+
LOAD_BALANCER_STACK_NAME := rabbit-loadbalancer
8+
9+
#
10+
# Helpers
11+
#
12+
13+
define create_rabbit_node_name
14+
rabbit-node0$(1)
15+
endef
16+
17+
#
18+
# Cluster level
19+
#
20+
21+
up: start-cluster
22+
23+
start-cluster: start-all-nodes start-loadbalancer
24+
25+
down:
26+
@$(error This operation may break cluster. Perform it per component)
27+
28+
#
29+
# Load Balancer
30+
#
31+
32+
start-loadbalancer: docker-compose.loadbalancer.yml
33+
@docker stack deploy -c $< $(LOAD_BALANCER_STACK_NAME)
34+
35+
update-loadbalancer: start-loadbalancer
36+
37+
stop-loadbalancer:
38+
@docker stack rm $(LOAD_BALANCER_STACK_NAME)
39+
40+
#
41+
# Rabbit all Nodes together
42+
#
43+
44+
start-all-nodes: guard-NODE_COUNT $(foreach i,$(NODE_IXS),start-node0$(i))
45+
46+
update-all-nodes:
47+
@$(error Updating all nodes at the same time may break cluster. Update one node at a time)
48+
49+
stop-all-nodes:
50+
@$(error Stopping all nodes at the same time may break cluster. Update one node at a time)
51+
52+
#
53+
# Rabbit Node level
54+
#
55+
56+
start-node0%: docker-compose.node0%.yml
57+
@STACK_NAME=$(call create_rabbit_node_name,$*); \
58+
if docker stack ls --format '{{.Name}}' | grep --silent "$$STACK_NAME"; then \
59+
echo "Rabbit Node $* is already running, skipping"; \
60+
else \
61+
echo "Starting Rabbit Node $* ..."; \
62+
docker stack deploy -c $< $(call create_rabbit_node_name,$*)
63+
fi
64+
65+
update-node0%: docker-compose.node0%.yml
66+
@docker stack deploy --detach=false -c $< $(call create_rabbit_node_name,$*)
67+
68+
stop-node0%: docker-compose.node0%.yml
69+
@docker stack rm --detach=false $(call create_rabbit_node_name,$*)

services/rabbit/Makefile

Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,40 @@ include ${REPO_BASE_DIR}/scripts/common-services.Makefile
66
include ${REPO_BASE_DIR}/scripts/common.Makefile
77

88
#
9-
# Running cluster
9+
# Operations
1010
#
1111

12-
STACK_IS_RUNNING := $(shell docker stack ls --format '{{.Name}}' | grep -q "^$(STACK_NAME)$$" && echo true || echo false)
12+
include ${REPO_BASE_DIR}/services/rabbit/.operations.Makefile
1313

14-
.PHONY: up
15-
up: ${TEMP_COMPOSE} \
16-
prune-docker-stack-configs \
17-
prune-docker-stack-secrets \
18-
guard-optional-bool-FORCE
19-
up: ## Deploy rabbit
20-
@if [ "$(STACK_IS_RUNNING)" = "false" ] || [ "$(FORCE)" = "true" ] ; then \
21-
docker stack deploy --detach=false --with-registry-auth --prune --compose-file ${TEMP_COMPOSE} ${STACK_NAME}; \
22-
else \
23-
echo "Stack $(STACK_NAME) is already running. To avoid unexpected behavior, skipping deploy"; \
24-
echo "Check README to learn more about updates. To enforce deploy, use 'FORCE=true'"; \
25-
fi
26-
27-
up-aws: up
28-
29-
up-master: up
30-
31-
up-dalco: up
14+
#
15+
# Docker compose files
16+
#
3217

33-
up-local: up
18+
### Load Balancer
19+
docker-compose.loadbalancer.yml: docker-compose.loadbalancer.yml.j2 \
20+
.env \
21+
configs/rabbitmq.conf \
22+
configs/erlang.cookie.secret \
23+
configs/haproxy.cfg \
24+
venv \
25+
$(VENV_BIN)/j2
26+
@$(call jinja, $<, .env, $@)
3427

35-
up-public: up
28+
### Node
3629

37-
${TEMP_COMPOSE}: docker-compose.yml .env
38-
@${REPO_BASE_DIR}/scripts/docker-stack-config.bash -e .env $< > $@
30+
node0%.env: template.node0x.env
31+
@set -o allexport; . $(REPO_CONFIG_LOCATION); set +o allexport; \
32+
export NODE_INDEX=$*; \
33+
envsubst < $< > $@
3934

40-
docker-compose.yml: docker-compose.yml.j2 \
41-
.env \
35+
docker-compose.node0%.yml: docker-compose.node0x.yml.j2 \
36+
node0%.env \
4237
configs/rabbitmq.conf \
4338
configs/erlang.cookie.secret \
4439
configs/haproxy.cfg \
4540
venv \
4641
$(VENV_BIN)/j2
47-
@$(call jinja, $<, .env, $@)
42+
@$(call jinja, $<, node0$*.env, $@)
4843

4944
#
5045
# Config / Secret files
@@ -61,41 +56,3 @@ configs/rabbitmq.conf: configs/rabbitmq.conf.j2 .env venv
6156
configs/haproxy.cfg: configs/haproxy.cfg.j2 .env venv
6257
# generate $@
6358
@$(call jinja, $<, .env, $@)
64-
65-
#
66-
# Deleting volumes (data)
67-
#
68-
69-
### May be needed to start cluster fresh new to enforce new configuration
70-
71-
CLEAN_VOLUMES_TEMP_COMPOSE := .stack.${STACK_NAME}.clean-volumes.yaml
72-
CLEAN_VOLUMES_STACK_NAME := ${STACK_NAME}-clean-volumes
73-
74-
docker-compose.clean-volumes.yaml: docker-compose.clean-volumes.yaml.j2 .env
75-
@$(call jinja, $<, .env, $@)
76-
77-
${CLEAN_VOLUMES_TEMP_COMPOSE}: docker-compose.clean-volumes.yaml .env
78-
@${REPO_BASE_DIR}/scripts/docker-stack-config.bash -e .env $< > $@
79-
80-
TIMEOUT = 2m
81-
delete-volumes: ${CLEAN_VOLUMES_TEMP_COMPOSE} guard-optional-TIMEOUT guard-optional-bool-FORCE_CONFIRM
82-
delete-volumes: ## Delete rabbit data volumes
83-
@$(call confirm_action,Forever delete all rabbit data?,deleteforever,${FORCE_CONFIRM})
84-
## avoid accumulating tasks https://github.com/moby/moby/issues/45443
85-
@docker stack rm --detach=false ${CLEAN_VOLUMES_STACK_NAME}
86-
## use --detach=false to wait until all jobs complete successfully
87-
@timeout ${TIMEOUT} \
88-
docker stack deploy --detach=false --with-registry-auth --prune --compose-file ${CLEAN_VOLUMES_TEMP_COMPOSE} ${CLEAN_VOLUMES_STACK_NAME}
89-
## cleaning up
90-
@docker stack rm ${CLEAN_VOLUMES_STACK_NAME}
91-
92-
#
93-
# Destroy cluster with data (to start fresh new)
94-
#
95-
96-
down: guard-optional-bool-FORCE_CONFIRM ## Stop rabbit cluster
97-
@$(call confirm_action,Are you sure you want to stop the Rabbit cluster?,stopcluster,${FORCE_CONFIRM})
98-
@docker stack rm ${STACK_NAME}
99-
100-
destroy-cluster-with-data: down delete-volumes ## Destroy rabbit cluster with data
101-
@echo "Cluster has been destroyed. Data has been deleted"

services/rabbit/docker-compose.clean-volumes.yaml.j2

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
services:
2+
loadbalancer:
3+
image: haproxy:3.2
4+
deploy:
5+
update_config:
6+
order: start-first
7+
parallelism: 1
8+
delay: 30s
9+
failure_action: rollback
10+
# https://discourse.haproxy.org/t/haproxy-high-availability-configuration/11983
11+
replicas: ${RABBIT_LB_REPLICAS}
12+
# necessary to preserve client ip
13+
# otherwise we see overlay rabbit network lb ip
14+
# (rabbitmq management dashboard connection section)
15+
endpoint_mode: dnsrr
16+
resources:
17+
limits:
18+
# https://help.hcl-software.com/digital-experience/dx-95-doc-archive/CF203/platform/kubernetes/haproxy-migration/haproxy-configuration.html
19+
cpus: "1"
20+
memory: "2G"
21+
# according to local observations and link below
22+
# https://github.com/haproxytech/helm-charts/blob/haproxy-1.24.0/haproxy/values.yaml#L403
23+
reservations:
24+
cpus: "0.1"
25+
memory: "128M"
26+
healthcheck: # https://stackoverflow.com/a/76513320/12124525
27+
test: bash -c 'echo "" > /dev/tcp/127.0.0.1/32087 || exit 1'
28+
start_period: 5s
29+
timeout: 2s
30+
retries: 2
31+
interval: 10s
32+
networks:
33+
- rabbit
34+
configs:
35+
- source: haproxy.cfg
36+
target: /usr/local/etc/haproxy/haproxy.cfg
37+
38+
subscriber:
39+
image: python:3.11
40+
deploy:
41+
replicas: 1
42+
command: sh -c "pip install pika && python /app/sub.py"
43+
environment:
44+
- RABBIT_HOSTS=rabbit_loadbalancer
45+
- RABBIT_USER=${RABBIT_USER}
46+
- RABBIT_PASS=${RABBIT_PASSWORD}
47+
networks:
48+
- rabbit
49+
volumes:
50+
- ./test/sub.py:/app/sub.py
51+
52+
publisher:
53+
image: python:3.11
54+
deploy:
55+
replicas: 1
56+
command: sh -c "pip install pika && python /app/pub.py"
57+
environment:
58+
- RABBIT_HOSTS=rabbit_loadbalancer
59+
- RABBIT_USER=${RABBIT_USER}
60+
- RABBIT_PASS=${RABBIT_PASSWORD}
61+
networks:
62+
- rabbit
63+
volumes:
64+
- ./test/pub.py:/app/pub.py
65+
66+
networks:
67+
rabbit:
68+
name: ${RABBIT_NETWORK}
69+
external: true
70+
71+
configs:
72+
haproxy.cfg:
73+
file: ./configs/haproxy.cfg
74+
name: rabbit_haproxy_conf_{{ "./configs/haproxy.cfg" | sha256file | substring(0,10) }}

0 commit comments

Comments
 (0)