0x10 - PostgreSQL installation of CentOS7 + Patroni

PostgreSQL + CentOS7 + Patroni

background

PostgreSQL high availability environment

surroundings

  • CentOS 7
    • pg01 (192.168.1.120)
    • pg02 (192.168.1.121)
    • pg03 (192.168.1.122)
  • PostgreSQL 11.2
  • patrons 1.6.0
  • Python 2.7
  • etcdctl version: 3.3.11,API version: 2
  • Effective clock synchronization service

Installation configuration etcd Service

Installation service on each node, configured to trunked mode.
Reference within the chain: the ETCD cluster mounted
referencing the chain: the ETCD Cluster Installation

Installation configuration patroni Service

  • Note: All operations are run under the root user.
  • Note: need to be installed and configured on three nodes.

Prepare the data directory

This directory is the database where the data, configured according to actual needs.

mkdir -p /opt/data/patroni
chown -R postgres:postgres /opt/data
chmod -R 700 /opt/data

Installation package

yum install -y gcc python-devel epel-release
yum install -y python2-pip
pip install --upgrade setuptools
pip install psycopg2-binary
pip install python-etcd
pip install patroni

Configuring startup scripts

patroni service to create a startup script: vi /etc/systemd/system/patroni.service add the following.

[Unit]
Description=Runners to orchestrate a high-availability PostgreSQL
After=syslog.target network.target

[Service]
Type=simple
User=postgres
Group=postgres
ExecStart=/bin/patroni /etc/patroni.yml
KillMode=process
TimeoutSec=30
Restart=no

[Install]
WantedBy=multi-user.targ

Postgresql prohibited from the start, to manage the use patroni

# 关闭 postgresql 的自动启动配置
systemctl disable postgresql-11

Configure the startup file

Create a configuration file vi /etc/patroni.yml , start to close the file control postgresql instances and so on.

  • Note on the configuration file: password values ​​need to add single quotes.

The first configuration of the nodes: 192.168.1.120 - pg01

scope: postgres
name: pg01

restapi:
    listen: 192.168.1.120:8008
    connect_address: 192.168.1.120:8008

etcd:
    host: 192.168.1.120:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replica 127.0.0.1/32 md5
    - host replication replica 192.168.1.120/0 md5
    - host replication replica 192.168.1.121/0 md5
    - host replication replica 192.168.1.122/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: 192.168.1.120:5432
    bin_dir: /usr/pgsql-11/bin
    connect_address: 192.168.1.120:5432
    data_dir: /opt/data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replica
            password: 'replica'
        superuser:
            username: postgres
            password: '123456'
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false

Configure the second node: 192.168.1.121 - pg02

scope: postgres
name: pg02

restapi:
    listen: 192.168.1.121:8008
    connect_address: 192.168.1.121:8008

etcd:
    host: 192.168.1.121:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replica 127.0.0.1/32 md5
    - host replication replica 192.168.1.120/0 md5
    - host replication replica 192.168.1.121/0 md5
    - host replication replica 192.168.1.122/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: 192.168.1.121:5432
    bin_dir: /usr/pgsql-11/bin
    connect_address: 192.168.1.121:5432
    data_dir: /opt/data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replica
            password: 'replica'
        superuser:
            username: postgres
            password: '123456'
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false

The third node configuration: 192.168.1.122 - pg03

scope: postgres
name: pg03

restapi:
    listen: 192.168.1.122:8008
    connect_address: 192.168.1.122:8008

etcd:
    host: 192.168.1.122:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replica 127.0.0.1/32 md5
    - host replication replica 192.168.1.120/0 md5
    - host replication replica 192.168.1.121/0 md5
    - host replication replica 192.168.1.122/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: 192.168.1.122:5432
    bin_dir: /usr/pgsql-11/bin
    connect_address: 192.168.1.122:5432
    data_dir: /opt/data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replica
            password: 'replica'
        superuser:
            username: postgres
            password: '123456'
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false

Start and Authentication Service

Now we can start patroni service, it creates a new database in the background, according to (the configuration file /etc/patroni.yml create user content) inside.
Note: It is recommended not to set the boot from the start, because no manual debugging when you manually start, restart after down if the need to manually handle.

# 启动服务(会自动启动 pg 实例)
systemctl start patroni
# 关闭服务(会自动关闭 pg 实例)
systemctl stop patroni

# 查看集群状态
patronictl -c /etc/patroni.yml list
[root@postgres0 patroni]# patronictl -c /etc/patroni.yml list
+----------+--------+---------------+--------+---------+----+-----------+
| Cluster  | Member |      Host     |  Role  |  State  | TL | Lag in MB |
+----------+--------+---------------+--------+---------+----+-----------+
| postgres |  pg01  | 192.168.1.120 | Leader | running |  1 |       0.0 |
| postgres |  pg02  | 192.168.1.121 |        | running |  1 |       0.0 |
| postgres |  pg03  | 192.168.1.122 |        | running |  1 |       0.0 |
+----------+--------+---------------+--------+---------+----+-----------+

You can see boot messages in / var / log / messages inside, you can also view the startup information by command ().

JDBC connection

PG has a targetServerType of jdbc parameter set here, it will automatically find the master node, i.e. node data may be written.
jdbc:postgresql://192.168.1.120:5432,192.168.1.121:5432,192.168.1.122:5432/test1?targetServerType=master&user=postgres&password=123456

Patroni command

# 可以查看命令的使用说明
patronictl --help
# 查看版本号
patronictl -c /etc/patroni.yml version
# 查看所有成员信息
patronictl -c /etc/patroni.yml list
# 重新加载配置
patronictl -c /etc/patroni.yml reload
# 移除集群,重新配置的时候使用
patronictl -c /etc/patroni.yml remove postgres
# 重启数据库集群
patronictl -c /etc/patroni.yml restart postgres
# 切换 Leader,将一个 slave 切换成 leader。
patronictl -c /etc/patroni.yml switchover

Reference Documents

Dynamically modify the database using the attribute Restful API
https://patroni.readthedocs.io/en/latest/dynamic_configuration.html#dynamic-configuration

Official documents
https://patroni.readthedocs.io/en/latest/

JDBC parameters, how to use the HA environment.
https://jdbc.postgresql.org/documentation/head/connect.html

Guess you like

Origin www.cnblogs.com/duchaoqun/p/12058010.html