Skip to content

Commit c16016c

Browse files
authored
Merge pull request #218 from grillazz/217-simple-ha-behind-nginx
add simple hs pattern
2 parents 6a976e0 + ca89d54 commit c16016c

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ PYTHONDONTWRITEBYTECODE=1
22
PYTHONUNBUFFERED=1
33

44
# Postgres
5-
POSTGRES_HOST=localhost
5+
POSTGRES_HOST=postgres
66
POSTGRES_PORT=5432
77
POSTGRES_DB=devdb
88
POSTGRES_USER=devdb
99
POSTGRES_PASSWORD=secret
1010

1111
# Redis
12-
REDIS_HOST=localhost
12+
REDIS_HOST=redis
1313
REDIS_PORT=6379
1414
REDIS_DB=2
1515

compose-ha.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
services:
2+
api1:
3+
container_name: panettone_api1
4+
build: .
5+
environment:
6+
- PYTHONPATH=/panettone
7+
env_file:
8+
- .env
9+
- .secrets
10+
command: bash -c "
11+
uvicorn app.main:app
12+
--host 0.0.0.0 --port 8080
13+
--lifespan=on --use-colors --loop uvloop --http httptools
14+
"
15+
volumes:
16+
- ./app:/panettone/app
17+
- ./tests:/panettone/tests
18+
- ./templates:/panettone/templates
19+
- ./alembic:/panettone/alembic
20+
ports:
21+
- "8081:8080"
22+
depends_on:
23+
- postgres
24+
- redis
25+
26+
api2:
27+
container_name: panettone_api2
28+
build: .
29+
environment:
30+
- PYTHONPATH=/panettone
31+
env_file:
32+
- .env
33+
- .secrets
34+
command: bash -c "
35+
uvicorn app.main:app
36+
--host 0.0.0.0 --port 8080
37+
--lifespan=on --use-colors --loop uvloop --http httptools
38+
"
39+
volumes:
40+
- ./app:/panettone/app
41+
- ./tests:/panettone/tests
42+
- ./templates:/panettone/templates
43+
- ./alembic:/panettone/alembic
44+
ports:
45+
- "8082:8080"
46+
depends_on:
47+
- postgres
48+
- redis
49+
50+
51+
postgres:
52+
container_name: panettone_postgres
53+
build:
54+
context: ./db
55+
dockerfile: Dockerfile
56+
volumes:
57+
- panettone_postgres_data:/var/lib/postgresql/data
58+
env_file:
59+
- .env
60+
ports:
61+
- 5432:5432
62+
environment:
63+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
64+
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
65+
- POSTGRES_DB=${POSTGRES_DB?Variable not set}
66+
healthcheck:
67+
test:
68+
[
69+
"CMD-SHELL", "pg_isready -d $POSTGRES_DB -U $POSTGRES_USER"
70+
]
71+
interval: 5s
72+
timeout: 5s
73+
retries: 5
74+
75+
redis:
76+
image: redis:latest
77+
container_name: panettone_redis
78+
ports:
79+
- "6379:6379"
80+
env_file:
81+
- .env
82+
entrypoint: redis-server --appendonly yes
83+
84+
loadbalancer:
85+
container_name: panettone_loadbalancer
86+
build: ./nginx
87+
ports:
88+
- "8080:80"
89+
depends_on:
90+
- api1
91+
- api2
92+
93+
volumes:
94+
panettone_postgres_data: {}

nginx/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM nginx
2+
RUN rm /etc/nginx/conf.d/default.conf
3+
COPY nginx.conf /etc/nginx/conf.d/default.conf

nginx/nginx.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
upstream loadbalancer {
2+
server api1:8080 weight=1;
3+
server api2:8080 weight=1;
4+
}
5+
6+
server {
7+
listen 80;
8+
9+
location / {
10+
proxy_pass http://loadbalancer;
11+
}
12+
}

0 commit comments

Comments
 (0)