Tiga setup:
pg-replication-basic/→ Primary + Replica (belajar/testing, failover manual)pg-cluster-haproxy/→ Primary + Replica + HAProxy (1 endpoint, failover manual)pg-ha/→ Patroni + etcd + HAProxy (auto-failover, read/write split)
cd pg-replication-basic
docker compose up -d
# Test insert ke primary
docker exec -it pg_primary psql -U postgres -d mydb -c "INSERT INTO test_table(name) VALUES ('hello');"
# Cek data di replica
docker exec -it pg_replica psql -U postgres -d mydb -c "TABLE test_table;"cd pg-cluster-haproxy
docker compose up -d
# Insert lewat HAProxy (port 5000)
PGPASSWORD=postgres psql -h localhost -p 5000 -U postgres -d mydb -c "INSERT INTO test_table(name) VALUES ('from haproxy');"
# Cek data di replica langsung
docker exec -it pg_replica1 psql -U postgres -d mydb -c "TABLE test_table;"Failover manual: promote replica jika primary mati
docker exec -it pg_replica1 psql -U postgres -d mydb -c "SELECT pg_promote();"cd pg-ha
docker compose up -d
# Insert ke leader via HAProxy (port 5000 = write)
PGPASSWORD=postgres psql -h localhost -p 5000 -U postgres -d postgres -c "INSERT INTO test_table(name) VALUES ('from patroni leader');"
# Baca dari replica (port 5001 = read pool)
PGPASSWORD=postgres psql -h localhost -p 5001 -U postgres -d postgres -c "TABLE test_table;"Test failover otomatis: stop leader
docker stop patroni1Patroni otomatis promote node lain → HAProxy tetap arahkan backend ke leader baru di port 5000.
-
Basic
- Primary →
localhost:5432 - Replica →
localhost:5433
- Primary →
-
Cluster HAProxy
- Write →
localhost:5000
- Write →
-
Patroni HA
- Write (leader) →
localhost:5000 - Read (replica pool) →
localhost:5001 - HAProxy stats → http://localhost:8404
- Write (leader) →