Skip to content

Commit 5131666

Browse files
Issue (#1): Initial Commit.
1 parent a29f39a commit 5131666

39 files changed

Lines changed: 6685 additions & 2668 deletions

INSTALL.md

Lines changed: 408 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,26 @@ This project provides distributed consensus capabilities for PostgreSQL using cu
3535
## Architecture
3636

3737
```
38-
┌───────────────────────────┐
39-
│ ramctrl CLI │
40-
│ (Control Utility) │
41-
└─────────────┬─────────────┘
42-
│ REST / SQL
43-
┌─────────────┴─────────────┐
44-
│ PostgreSQL + pgraft │
45-
│ + ramd (PRIMARY) │
46-
│ LEADER (Node 1) │
47-
└─────────────┬─────────────┘
38+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
39+
│ Primary Node │ │ Standby Node 1 │ │ Standby Node 2 │
40+
│ │ │ │ │ │
41+
│ ┌───────────┐ │ │ ┌───────────┐ │ │ ┌───────────┐ │
42+
│ │ PostgreSQL│ │ │ │ PostgreSQL│ │ │ │ PostgreSQL│ │
43+
│ └───────────┘ │ │ └───────────┘ │ │ └───────────┘ │
44+
│ ┌───────────┐ │ │ ┌───────────┐ │ │ ┌───────────┐ │
45+
│ │ pgraft │ │ │ │ pgraft │ │ │ │ pgraft │ │
46+
│ └───────────┘ │ │ └───────────┘ │ │ └───────────┘ │
47+
│ ┌───────────┐ │ │ ┌───────────┐ │ │ ┌───────────┐ │
48+
│ │ ramd │ │ │ │ ramd │ │ │ │ ramd │ │
49+
│ └───────────┘ │ │ └───────────┘ │ │ └───────────┘ │
50+
└─────────────────┘ └─────────────────┘ └─────────────────┘
51+
│ │ │
52+
└───────────────────────┼───────────────────────┘
4853
49-
┌──────────────────────┼──────────────────────┐
50-
│ │
51-
│ Physical Physical │
52-
│ Replication Replication │
53-
│ │
54-
┌─────────┴───────┐ ┌─────────┴───────┐
55-
│ PostgreSQL │ │ PostgreSQL │
56-
│ + pgraft │ │ + pgraft │
57-
│ + ramd │ │ + ramd │
58-
│ (REPLICA) │ │ (REPLICA) │
59-
│ FOLLOWER │ │ FOLLOWER │
60-
│ (Node 2) │ │ (Node 3) │
61-
└─────────────────┘ └─────────────────┘
54+
┌─────────────────┐
55+
│ Raft Consensus │
56+
│ Network │
57+
└─────────────────┘
6258
```
6359

6460

docker-compose.yml

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
version: '3.8'
2+
3+
services:
4+
# PostgreSQL primary node
5+
postgres-primary:
6+
build:
7+
context: .
8+
dockerfile: docker/Dockerfile.postgres
9+
container_name: pgraft-primary
10+
environment:
11+
POSTGRES_DB: testdb
12+
POSTGRES_USER: postgres
13+
POSTGRES_PASSWORD: postgres
14+
PGRaft_NODE_ID: 1
15+
PGRaft_NODE_NAME: primary
16+
PGRaft_CLUSTER_NAME: test-cluster
17+
PGRaft_RAFT_PORT: 5433
18+
PGRaft_LOG_LEVEL: 2
19+
ports:
20+
- "5433:5432"
21+
- "8080:8080" # ramd HTTP API
22+
volumes:
23+
- postgres_primary_data:/var/lib/postgresql/data
24+
- ./pgraft:/usr/local/share/postgresql/extension/pgraft
25+
networks:
26+
- pgraft-network
27+
command: >
28+
postgres
29+
-c shared_preload_libraries=pgraft
30+
-c pgraft.node_id=1
31+
-c pgraft.node_name=primary
32+
-c pgraft.cluster_name=test-cluster
33+
-c pgraft.raft_port=5433
34+
-c pgraft.log_level=2
35+
-c wal_level=replica
36+
-c max_wal_senders=10
37+
-c max_replication_slots=10
38+
-c hot_standby=on
39+
healthcheck:
40+
test: ["CMD-SHELL", "pg_isready -U postgres -d testdb"]
41+
interval: 10s
42+
timeout: 5s
43+
retries: 5
44+
45+
# PostgreSQL standby node 1
46+
postgres-standby1:
47+
build:
48+
context: .
49+
dockerfile: docker/Dockerfile.postgres
50+
container_name: pgraft-standby1
51+
environment:
52+
POSTGRES_DB: testdb
53+
POSTGRES_USER: postgres
54+
POSTGRES_PASSWORD: postgres
55+
PGRaft_NODE_ID: 2
56+
PGRaft_NODE_NAME: standby1
57+
PGRaft_CLUSTER_NAME: test-cluster
58+
PGRaft_RAFT_PORT: 5434
59+
PGRaft_LOG_LEVEL: 2
60+
ports:
61+
- "5434:5432"
62+
- "8081:8080" # ramd HTTP API
63+
volumes:
64+
- postgres_standby1_data:/var/lib/postgresql/data
65+
- ./pgraft:/usr/local/share/postgresql/extension/pgraft
66+
networks:
67+
- pgraft-network
68+
depends_on:
69+
- postgres-primary
70+
command: >
71+
bash -c "
72+
until pg_basebackup -h postgres-primary -U postgres -D /var/lib/postgresql/data -R -S standby1;
73+
do echo 'Waiting for primary to be ready...'; sleep 2; done &&
74+
postgres
75+
-c shared_preload_libraries=pgraft
76+
-c pgraft.node_id=2
77+
-c pgraft.node_name=standby1
78+
-c pgraft.cluster_name=test-cluster
79+
-c pgraft.raft_port=5434
80+
-c pgraft.log_level=2
81+
-c wal_level=replica
82+
-c max_wal_senders=10
83+
-c max_replication_slots=10
84+
-c hot_standby=on
85+
-c primary_conninfo='host=postgres-primary port=5432 user=postgres'
86+
"
87+
healthcheck:
88+
test: ["CMD-SHELL", "pg_isready -U postgres -d testdb"]
89+
interval: 10s
90+
timeout: 5s
91+
retries: 5
92+
93+
# PostgreSQL standby node 2
94+
postgres-standby2:
95+
build:
96+
context: .
97+
dockerfile: docker/Dockerfile.postgres
98+
container_name: pgraft-standby2
99+
environment:
100+
POSTGRES_DB: testdb
101+
POSTGRES_USER: postgres
102+
POSTGRES_PASSWORD: postgres
103+
PGRaft_NODE_ID: 3
104+
PGRaft_NODE_NAME: standby2
105+
PGRaft_CLUSTER_NAME: test-cluster
106+
PGRaft_RAFT_PORT: 5435
107+
PGRaft_LOG_LEVEL: 2
108+
ports:
109+
- "5435:5432"
110+
- "8082:8080" # ramd HTTP API
111+
volumes:
112+
- postgres_standby2_data:/var/lib/postgresql/data
113+
- ./pgraft:/usr/local/share/postgresql/extension/pgraft
114+
networks:
115+
- pgraft-network
116+
depends_on:
117+
- postgres-primary
118+
command: >
119+
bash -c "
120+
until pg_basebackup -h postgres-primary -U postgres -D /var/lib/postgresql/data -R -S standby2;
121+
do echo 'Waiting for primary to be ready...'; sleep 2; done &&
122+
postgres
123+
-c shared_preload_libraries=pgraft
124+
-c pgraft.node_id=3
125+
-c pgraft.node_name=standby2
126+
-c pgraft.cluster_name=test-cluster
127+
-c pgraft.raft_port=5435
128+
-c pgraft.log_level=2
129+
-c wal_level=replica
130+
-c max_wal_senders=10
131+
-c max_replication_slots=10
132+
-c hot_standby=on
133+
-c primary_conninfo='host=postgres-primary port=5432 user=postgres'
134+
"
135+
healthcheck:
136+
test: ["CMD-SHELL", "pg_isready -U postgres -d testdb"]
137+
interval: 10s
138+
timeout: 5s
139+
retries: 5
140+
141+
# ramd daemon for primary
142+
ramd-primary:
143+
build:
144+
context: .
145+
dockerfile: docker/Dockerfile.ramd
146+
container_name: ramd-primary
147+
environment:
148+
RAMD_CONFIG_FILE: /etc/ramd/ramd.conf
149+
RAMD_LOG_LEVEL: INFO
150+
RAMD_HTTP_PORT: 8080
151+
ports:
152+
- "8080:8080"
153+
volumes:
154+
- ./ramd/conf:/etc/ramd
155+
- ./ramd/logs:/var/log/ramd
156+
networks:
157+
- pgraft-network
158+
depends_on:
159+
- postgres-primary
160+
command: ["ramd", "--config", "/etc/ramd/ramd.conf", "--daemon"]
161+
162+
# ramd daemon for standby1
163+
ramd-standby1:
164+
build:
165+
context: .
166+
dockerfile: docker/Dockerfile.ramd
167+
container_name: ramd-standby1
168+
environment:
169+
RAMD_CONFIG_FILE: /etc/ramd/ramd.conf
170+
RAMD_LOG_LEVEL: INFO
171+
RAMD_HTTP_PORT: 8080
172+
ports:
173+
- "8081:8080"
174+
volumes:
175+
- ./ramd/conf:/etc/ramd
176+
- ./ramd/logs:/var/log/ramd
177+
networks:
178+
- pgraft-network
179+
depends_on:
180+
- postgres-standby1
181+
command: ["ramd", "--config", "/etc/ramd/ramd.conf", "--daemon"]
182+
183+
# ramd daemon for standby2
184+
ramd-standby2:
185+
build:
186+
context: .
187+
dockerfile: docker/Dockerfile.ramd
188+
container_name: ramd-standby2
189+
environment:
190+
RAMD_CONFIG_FILE: /etc/ramd/ramd.conf
191+
RAMD_LOG_LEVEL: INFO
192+
RAMD_HTTP_PORT: 8080
193+
ports:
194+
- "8082:8080"
195+
volumes:
196+
- ./ramd/conf:/etc/ramd
197+
- ./ramd/logs:/var/log/ramd
198+
networks:
199+
- pgraft-network
200+
depends_on:
201+
- postgres-standby2
202+
command: ["ramd", "--config", "/etc/ramd/ramd.conf", "--daemon"]
203+
204+
# Monitoring and management
205+
pgraft-monitor:
206+
build:
207+
context: .
208+
dockerfile: docker/Dockerfile.monitor
209+
container_name: pgraft-monitor
210+
environment:
211+
MONITOR_INTERVAL: 30
212+
MONITOR_TARGETS: "postgres-primary:5432,postgres-standby1:5432,postgres-standby2:5432"
213+
ports:
214+
- "3000:3000" # Grafana
215+
- "9090:9090" # Prometheus
216+
volumes:
217+
- ./monitoring/grafana:/etc/grafana
218+
- ./monitoring/prometheus:/etc/prometheus
219+
- ./monitoring/dashboards:/var/lib/grafana/dashboards
220+
networks:
221+
- pgraft-network
222+
depends_on:
223+
- postgres-primary
224+
- postgres-standby1
225+
- postgres-standby2
226+
227+
volumes:
228+
postgres_primary_data:
229+
postgres_standby1_data:
230+
postgres_standby2_data:
231+
232+
networks:
233+
pgraft-network:
234+
driver: bridge
235+
ipam:
236+
config:
237+
- subnet: 172.20.0.0/16

docker/Dockerfile.monitor

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM prom/prometheus:latest
2+
3+
# Install additional monitoring tools
4+
USER root
5+
RUN apk add --no-cache curl jq
6+
7+
# Copy Prometheus configuration
8+
COPY monitoring/prometheus/prometheus.yml /etc/prometheus/prometheus.yml
9+
10+
# Copy Grafana configuration
11+
COPY monitoring/grafana/grafana.ini /etc/grafana/grafana.ini
12+
COPY monitoring/dashboards/ /var/lib/grafana/dashboards/
13+
14+
# Create startup script
15+
COPY docker/monitor-entrypoint.sh /usr/local/bin/monitor-entrypoint.sh
16+
RUN chmod +x /usr/local/bin/monitor-entrypoint.sh
17+
18+
# Expose ports
19+
EXPOSE 9090 3000
20+
21+
# Set entrypoint
22+
ENTRYPOINT ["/usr/local/bin/monitor-entrypoint.sh"]

docker/Dockerfile.postgres

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
FROM postgres:15-alpine
2+
3+
# Install build dependencies
4+
RUN apk add --no-cache \
5+
build-base \
6+
postgresql-dev \
7+
libpq-dev \
8+
pkgconfig \
9+
git \
10+
make \
11+
gcc \
12+
musl-dev
13+
14+
# Copy source code
15+
COPY . /usr/src/pgraft
16+
WORKDIR /usr/src/pgraft
17+
18+
# Build pgraft extension
19+
RUN cd pgraft && make clean && make && make install
20+
21+
# Build ramd daemon
22+
RUN cd ramd && make clean && make && make install
23+
24+
# Build ramctrl utility
25+
RUN cd ramctrl && make clean && make && make install
26+
27+
# Create ramd user and directories
28+
RUN adduser -D -s /bin/sh ramd && \
29+
mkdir -p /etc/ramd /var/log/ramd /var/lib/ramd && \
30+
chown -R ramd:ramd /etc/ramd /var/log/ramd /var/lib/ramd
31+
32+
# Copy configuration files
33+
COPY docker/ramd.conf /etc/ramd/ramd.conf
34+
RUN chown ramd:ramd /etc/ramd/ramd.conf
35+
36+
# Create startup script
37+
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
38+
RUN chmod +x /usr/local/bin/entrypoint.sh
39+
40+
# Expose ports
41+
EXPOSE 5432 8080
42+
43+
# Set entrypoint
44+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
45+
CMD ["postgres"]

docker/Dockerfile.ramd

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM alpine:3.18
2+
3+
# Install runtime dependencies
4+
RUN apk add --no-cache \
5+
postgresql-client \
6+
libpq \
7+
curl \
8+
jq
9+
10+
# Install ramd daemon
11+
COPY ramd/ramd /usr/local/bin/ramd
12+
RUN chmod +x /usr/local/bin/ramd
13+
14+
# Create ramd user and directories
15+
RUN adduser -D -s /bin/sh ramd && \
16+
mkdir -p /etc/ramd /var/log/ramd /var/lib/ramd && \
17+
chown -R ramd:ramd /etc/ramd /var/log/ramd /var/lib/ramd
18+
19+
# Copy configuration files
20+
COPY docker/ramd.conf /etc/ramd/ramd.conf
21+
RUN chown ramd:ramd /etc/ramd/ramd.conf
22+
23+
# Switch to ramd user
24+
USER ramd
25+
26+
# Expose HTTP API port
27+
EXPOSE 8080
28+
29+
# Set entrypoint
30+
ENTRYPOINT ["/usr/local/bin/ramd"]
31+
CMD ["--config", "/etc/ramd/ramd.conf", "--daemon"]

0 commit comments

Comments
 (0)