PostgreSQL High Availability Kurulumu
# OS DEBIAN10
10.200.56.186 VTGENDBPQL01
10.200.56.187 VTGENDBPQL02
# ETCD + Haproxy
10.200.56.185 VTGENDBETC01
1. Postgresql ve Patroni Kurulumu
Öncelikle VTGENDBPQL01 ve VTGENDBPQL02 sunucularına postgresql kurulumu yapılır.
Patroni : Postgresql databese cluster yönetimi sağlayan management tool.
ETCD: Cluster bilgilerini tutan key/value database.
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -sudo apt-get updateapt install postgresql postgresql-contrib -ysystemctl stop postgresqlln -s /usr/lib/postgresql/14/bin/* /usr/sbin/
Postgresql kurulumu tamamlandı. Aşağıdaki komutlar ile VTGENDBPQL01 ve VTGENDBPQL02 patroni kurulumunu yapalım.
apt install python3-pip python3-dev libpq-dev -ypip3 install --upgrade pippip install patronipip install python-etcdpip install psycopg2
VTGENDBETC01 sunucusuna ETCD ve haproxykurulumu yapılır.
apt install etcd haproxy -y
/etc/default/etcd içerisine aşağıdaki konfig eklenir.
ETCD_LISTEN_PEER_URLS="http://10.200.56.185:2380,http://127.0.0.1:7001"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379, http://10.200.56.185:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.200.56.185:2380"
ETCD_INITIAL_CLUSTER="etcd0=http://10.200.56.185:2380,"
ETCD_ADVERTISE_CLIENT_URLS="http://10.200.56.185:2379"
ETCD_INITIAL_CLUSTER_TOKEN="node1"
ETCD_INITIAL_CLUSTER_STATE="new"
Daha sonra etcd servisi çalıştırılır.
systemctl restart etcdsystemctl status etcd
Postgresql ile birlikte kurduğumuz patroni konfigürsayonu için /etc/patroni.yml içerisine aşağıdaki konfigler yazılır.
#VTGENDBPQL02
scope: postgres
namespace: /db/
name: patronidb1restapi:
listen: 10.200.56.187:8008
connect_address: 10.200.56.187:8008etcd:
host: 10.200.56.185:2379bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: trueinitdb:
- encoding: UTF8
- data-checksumspg_hba:
- host replication replicator 127.0.0.1/32 md5
- host replication replicator 10.200.56.187/0 md5
- host replication replicator 10.200.56.186/0 md5
- host all all 0.0.0.0/0 md5users:
admin:
password: admin
options:
- createrole
- createdbpostgresql:
listen: 10.200.56.187:5432
connect_address: 10.200.56.187:5432
data_dir: /data/patroni
pgpass: /tmp/pgpass
authentication:
replication:
username: replicator
password: ur1btgSDoPa4ss_bJkaDgtanGy
superuser:
username: postgres
password: ur1btgSDoPa4ss_bJkaDgtanGy
parameters:
unix_socket_directories: '.'tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false#VTGENDBPQL02
scope: postgres
namespace: /db/
name: patronidb2restapi:
listen: 10.200.56.186:8008
connect_address: 10.200.56.186:8008etcd:
host: 10.200.56.185:2379bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: trueinitdb:
- encoding: UTF8
- data-checksumspg_hba:
- host replication replicator 127.0.0.1/32 md5
- host replication replicator 10.200.56.186/0 md5
- host replication replicator 10.200.56.187/0 md5
- host all all 0.0.0.0/0 md5users:
admin:
password: admin
options:
- createrole
- createdbpostgresql:
listen: 10.200.56.186:5432
connect_address: 10.200.56.186:5432
data_dir: /data/patroni
pgpass: /tmp/pgpass
authentication:
replication:
username: replicator
password: ur1btgSDoPa4ss_bJkaDgtanGy
superuser:
username: postgres
password: ur1btgSDoPa4ss_bJkaDgtanGy
parameters:
unix_socket_directories: '.'tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false
Konfig içerisinde belirtilen data dizin oluşturulur ve yetkileri verilir.
mkdir -p /data/patronichown postgres:postgres /data/patroni/chmod 700 /data/patroni/
Patroni servisi oluşturmak için /etc/systemd/system/patroni.service dosya oluşturulur ve aşağıdaki bilgiler yazılır.
[Unit]
Description=Runners to orchestrate a high-availability PostgreSQL
After=syslog.target network.target
[Service]
Type=simple
User=postgres
Group=postgres
ExecStart=/usr/local/bin/patroni /etc/patroni.yml
KillMode=process
TimeoutSec=30
Restart=no
[Install]
WantedBy=multi-user.targ
Servisler başlatılır.
systemctl daemon-reloadsystemctl start patronisystemctl start postgresql
Oluşturduğumuz clusterda LB işlemini görmesi için kurduğumuz Haproxy konfigürasyonu yapılır. Bu işlem için /etc/haproxy/haproxy.cfg dosya içerisine aşağıdaki bilgiler girilir.
global
maxconn 100defaults
log global
mode tcp
retries 2
timeout client 30m
timeout connect 4s
timeout server 30m
timeout check 5slisten stats
mode http
bind *:7000
stats enable
stats uri /listen postgres
bind *:5000
option httpchk
http-check expect status 200
default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
server postgresql_10.30.90.23_5432 10.30.90.23:5432 maxconn 100 check port 8008
server postgresql_10.30.90.24_5432 10.30.90.24:5432 maxconn 100 check port 8008
http://10.30.90.25:7000/ adresinden cluster durumunu görüntüleyebiliriz.
yada patroni komutu ile de bunu görüntüleyebiliriz.
patronictl -c /etc/patroni.yml list