Xin chào, hôm nay tôi sẽ giải thích việc cài đặt patroni có tính khả dụng cao.
Cài đặt phần mềm 1-PostgreSQL
Cài đặt phần mềm 2-Patroni
Cài đặt phần mềm 3-etcd, etcd conf.
4-patroni conf.
5-pgbackrest setup and conf.
> 6-haproxy và keepalived
7-Tất cả các conf khác.
Phần mềm PostgreSQL
yum install -y postgresql12-server postgresql12-client postgresql12-contrib postgresql12-devel
Phần mềm Patroni
export PATH="$PATH:/usr/pgsql-12/bin" yum install -y python3-pip-9.0.3-5.el7.noarch gcc python3-devel pip3 install --upgrade pip wheel pip3 install psycopg2-binary pip3 install psycopg2>=2.5.4 pip3 install patroni[etcd]
cài đặt etcd và conf
yum install -y etcd
--vi /etc/etcd/etcd.conf ##--pgdb-1 ETCD_INITIAL_CLUSTER="etcd1=http://xxx.xxx.xx.xx:2380,etcd2=http://xxx.xxx.xx.xx:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-01" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://xxx.xxx.xx.xx:2380" ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="http://xxx.xxx.xx.xx:2380" ETCD_LISTEN_CLIENT_URLS="http://xxx.xxx.xx.xx:2379,http://127.0.0.1:2379" ETCD_ADVERTISE_CLIENT_URLS="http://xxx.xxx.xx.xx:2379" ETCD_NAME="etcd1" ##--pgdb-2 ETCD_INITIAL_CLUSTER="etcd1=http://xxx.xxx.xx.xx:2380,etcd2=http://xxx.xxx.xx.xx:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-01" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://xxx.xxx.xx.xx:2380" ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="http://xxx.xxx.xx.xx:2380" ETCD_LISTEN_CLIENT_URLS="http://xxx.xxx.xx.xx:2379,http://127.0.0.1:2379" ETCD_ADVERTISE_CLIENT_URLS="http://xxx.xxx.xx.xx:2379" ETCD_NAME="etcd2" systemctl enable etcd systemctl start etcd systemctl status etcd systemctl stop etcd
Patroni conf.
Thư mục được tạo cho tệp patroni yaml. và postgres được ủy quyền
mkdir /etc/patroni/ chown postgres:postgres /etc/patroni/ mkdir /postgres/ chown postgres:postgres /postgres/
- // tệp yml nên được đặt theo địa chỉ IP máy chủ và bộ nhớ, v.v. tùy theo tài nguyên máy chủ. cài đặt phải được thực hiện.
scope: pgdb-patroni name: postgres1 restapi: listen: xxx.xxx.xx.xx:8008 connect_address: xxx.xxx.xx.xx:8008 etcd: hosts: 1xxx.xxx.xx.xx:2379,xxx.xxx.xx.xx:2379 bootstrap: dcs: ttl: 30 loop_wait: 10 retry_timeout: 10 maximum_lag_on_failover: 1048576 postgresql: use_pg_rewind: true parameters: wal_level: hot_standby hot_standby: "on" wal_keep_segments: 8 max_wal_senders: 10 max_replication_slots: 10 wal_log_hints: "on" max_connections: 200 shared_buffers: 1GB effective_cache_size: 3GB maintenance_work_mem: 256MB checkpoint_completion_target: 0.7 wal_buffers: 16MB default_statistics_target: 100 random_page_cost: 1.1 effective_io_concurrency: 300 work_mem: 5242kB min_wal_size: 1GB max_wal_size: 4GB max_worker_processes: 2 max_parallel_workers_per_gather: 1 max_parallel_workers: 2 max_parallel_maintenance_workers: 1 archive_mode: "on" archive_timeout: 1800s archive_command: "pgbackrest --stanza=pgdb-patroni archive-push %p" recovery_conf: restore_command: "pgbackrest --stanza=pgdb-patroni archive-get %f %p" standby_mode: "on" initdb: - encoding: UTF8 - data-checksums pg_hba: - host replication replicator xxx.xxx.xx.xx/24 md5 - host all all 0.0.0.0/0 md5 users: admin: password: admin options: - createrole - createdb postgresql: listen: xxx.xxx.xx.xx:5432 connect_address: xxx.xxx.xx.xx:5432 data_dir: /postgres/data/ bin_dir: /usr/pgsql-12/bin pgpass: /tmp/pgpass0 authentication: replication: username: replicator password: rep-pass superuser: username: postgres password: postgres rewind: username: rewind_user password: rewind_password parameters: unix_socket_directories: '/var/run/postgresql/' tags: nofailover: false noloadbalance: false clonefrom: false nosync: false scope: pgdb-patroni name: postgres2 restapi: listen: xxx.xxx.xx.xx:8008 connect_address: xxx.xxx.xx.xx:8008 etcd: hosts: xxx.xxx.xx.xx:2379,xxx.xxx.xx.xx:2379 bootstrap: dcs: ttl: 30 loop_wait: 10 retry_timeout: 10 maximum_lag_on_failover: 1048576 postgresql: use_pg_rewind: true parameters: wal_level: hot_standby hot_standby: "on" wal_keep_segments: 8 max_wal_senders: 10 max_replication_slots: 10 wal_log_hints: "on" max_connections: 200 shared_buffers: 1GB effective_cache_size: 3GB maintenance_work_mem: 256MB checkpoint_completion_target: 0.7 wal_buffers: 16MB default_statistics_target: 100 random_page_cost: 1.1 effective_io_concurrency: 300 work_mem: 5242kB min_wal_size: 1GB max_wal_size: 4GB max_worker_processes: 2 max_parallel_workers_per_gather: 1 max_parallel_workers: 2 max_parallel_maintenance_workers: 1 archive_mode: "on" archive_timeout: 1800s archive_command: "pgbackrest --stanza=pgdb-patroni archive-push %p" recovery_conf: restore_command: "pgbackrest --stanza=pgdb-patroni archive-get %f %p" standby_mode: "on"
initdb: - encoding: UTF8 - data-checksums pg_hba: - host replication replicator xxx.xxx.xx.xx/24 md5 - host all all 0.0.0.0/0 md5 users: admin: password: admin options: - createrole - createdb postgresql: listen: xxx.xxx.xx.xx:5432 connect_address: xxx.xxx.xx.xx:5432 data_dir: /postgres/data/ bin_dir: /usr/pgsql-12/bin pgpass: /tmp/pgpass0 authentication: replication: username: replicator password: rep-pass superuser: username: postgres password: postgres rewind: username: rewind_user password: rewind_password parameters: unix_socket_directories: '/var/run/postgresql/' tags: nofailover: false noloadbalance: false clonefrom: false nosync: false
patroni service is created more /etc/systemd/system/patroni.service [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/patroni.yml KillMode=process TimeoutSec=30 Restart=no [Install] WantedBy=multi-user.target
systemctl enable patroni systemctl start patroni systemctl status patroni systemctl stop patroni journalctl -xe -f -u patron
Các thông số tương tự được thực hiện trong các máy chủ khác và dịch vụ được chạy.
patronictl -d xxx.xxx.xx.xx list pgdb-patroni + Cluster: pgdb-patroni (xxx.xxx.xx.xx) -+----+-----------+ | Member | Host | Role | State | TL | Lag in MB | +-----------+---------------+--------+---------+----+-----------+ | postgres1 | xxx.xxx.xx.xx | Leader | running | 1 | | | postgres2 | xxx.xxx.xx.xx | | running | 1 | 0 | +-----------+---------------+--------+---------+----+-----------+ Note: Since we haven't installed pgbackrest until now, we may get an error when installing postgresql via patroni on 2. If we restart patroni services after installing backrest, the problem will be solved. Or you can set up a backrest before starting these patrons.
patronictl -d xxx.xxx.xx.xx list pgdb-patroni patronictl -d xxx.xxx.xx.xx reinit pgdb-patroni patronictl -c /etc/patroni/patroni.yml list