postgresql主从+repmgr+keepalive安装

  1. Postgresql16数据库相关介质,统一上传到/tmp/pgsoft/(创建此目录)下:以下操作,所有节点都需要执行。

    1. 安装介质准备

    |---|--------------------------------------------------|
    | | postgresql16-16.4-1PGDG.rhel8.x86_64.rpm |
    | | postgresql16-contrib-16.4-1PGDG.rhel8.x86_64.rpm |
    | | postgresql16-libs-16.4-1PGDG.rhel8.x86_64.rpm |
    | | postgresql16-server-16.4-1PGDG.rhel8.x86_64.rpm |

    1. 主机目录建议:

      |------------------|------------------------|
      | /usr/pgsql-16/ | postgresql安装目录 |
      | /data/pgdata | postgresql数据库目录 |
      | /data/pglogs | Postgresql数据库运行日志目录 |
      | /data/archivelog | Postgresql数据库归档日志目录 |
      | /data/pgtbs | Postgresql数据库用户数据表空间目录 |
      | /data/pgwal | Postgresql数据库WAL日志存放路径 |
      | | |

    2. 存储空间规划

    |---------|--------|--------|---------------------|
    | 磁盘名 | 冗余 | 容量 | 备注 |
    | /data | 根据存储确定 | 500G | 存放potgresql数据库数据及日志 |
    | / | 根据存储确定 | 100G | 用于安装postgresql数据库软件 |

    1. 完装前配置准备

    2. 操作系统准备条件检查

    3. 检查主机名

      #hostname

      修改主机名

      #hostnamectl set-hostname pbgpamsdb01

      检查系统内存

      free -g

      检查虚拟内存

      swapon -s

      检查磁盘空间

      #df -h

      检查时间和时区

      date -R

      timedatectl |grep "Time zone"

      Time zone: Asia/Shanghai (CST, +0800)

      注: 保证所有节点时间和时区一致。

      1. 配置Chrony服务
    4. 检查chrony服务:由系统管理员完成。

      |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
      | #cat /etc/chrony.conf |grep -i server # Use public servers from the pool.ntp.org project. #server 0.rhel.pool.ntp.org iburst #server 1.rhel.pool.ntp.org iburst #server 2.rhel.pool.ntp.org iburst #server 3.rhel.pool.ntp.org iburst server 192.168.10.1 iburst server 192.168.10.2 iburst #systemctl status chronyd #systemctl restart chronyd #cat /etc/chrony.conf #chkconfig --list # chkconfig chronyd on 查看时间同步源: # chronyc sources -v 立即手工同步 # chronyc -a makestep 查看时间同步源状态: # chronyc sourcestats -v ##未配置NTP时间同步,需要修改ntp.conf文件,让CTSSD服务处于ACTIVE状态: # mv /etc/ntp.conf /etc/ntp.conf.bak |

      1. 调整系统内核参数
    5. 编辑/etc/sysctl.conf文件

    6. cat << EOF >> /etc/sysctl.conf

      net.core.rmem_default = 262144

      net.core.rmem_max = 4194304

      net.core.wmem_default = 262144

      net.core.wmem_max = 1048576

      fs.aio-max-nr = 1048576

      fs.file-max = 6815744

      fs.aio-max-nr = 1048576

      kernel.panic_on_oops=1

      net.core.somaxconn = 8192

      net.ipv4.tcp_keepalive_time = 600 #默认7200

      net.ipv4.ip_local_port_range = 10000 65000 #默认32768到61000

      net.ipv4.tcp_max_syn_backlog = 8192 #默认1024

      net.ipv4.tcp_max_tw_buckets = 5000 #默认65535

      #vm.nr_hugepages = 81920 #share_pool /2MB *1.2

      kernel.sem = 4096 128000 64 512

      vm.swappiness=10

      EOF

      (2)加载内核参数:

      sysctl -p

      1. 配置limit.conf
    7. #vi /etc/security/limits.conf

      |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
      | #POSTGRESQL SETTING postgres soft nofile 1048576 postgres hard nofile 1048576 postgres soft nproc 131072 postgres hard nproc 131072 postgres soft stack 10240 postgres hard stack 32768 postgres soft core 6291456 postgres hard core 6291456 postgres soft memlock -1 postgres hard memlock -1 |

      cat << EOF >> /etc/security/limits.conf

      #POSTGRESQL SETTING

      postgres soft nofile 1048576

      postgres hard nofile 1048576

      postgres soft nproc 131072

      postgres hard nproc 131072

      postgres soft stack 10240

      postgres hard stack 32768

      postgres soft core 6291456

      postgres hard core 6291456

      postgres soft memlock -1

      postgres hard memlock -1

      EOF

      备注:修改前需要运行如下命令:sed -i 's/4096/unlimited/g' /etc/security/limits.d/20-nproc.conf

      1. 关闭TRANSPARENT HUGEPAGES
    8. 关闭前查看

      cat /sys/kernel/mm/transparent_hugepage/enabled

      always madvise never

      关闭TRANSPARENT HUGEPAGES 实施

      #vi /etc/default/grub

      在GRUB_CMDLINE_LINUX选项后追加参数transparent_hugepage=never:

      GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet transparent_hugepage=never"

      #grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

      说明:非/boot/efi模式,使用#grub2-mkconfig -o /boot/grub2/grub.cfg

      #重启系统后查看:

      cat /sys/kernel/mm/transparent_hugepage/enabled

      always madvise never

    9. 创建postgres用户及目录

    • groupadd postgres

      useradd -g postgres postgres

      passwd postgres

      echo "Prod_postgres2025"|passwd --stdin postgres

      mkdir -p /data/pgdata

      mkdir -p /data/archivelog

      mkdir -p /data/pglogs

      mkdir -p /data/pgtbs

      mkdir -p /data/repmgr

      mkdir -p /data/pgwal

      chown -R postgres.postgres /data

    以root用户身份执行,在两节点上做如下配置。
    1.

    1. 设置环境变量

    2. 编辑/etc/profile文件,在文件末尾添加下述内容

      cat << EOF >> /etc/profile

      #POSTGRESQL SETTING

      PATH=\$PATH:/usr/pgsql-16/bin

      export PATH

      EOF

      编辑/home/postgres/.bash_profile

      cat << EOF >> /home/postgres/.bash_profile

      #POSTGRESQL SETTING

      export PGPORT=5432

      export LANG=en_US.UTF8

      export PGDATA=/data/pgdata

      export PGHOME=/usr/pgsql-16/

      export PATH=\PATH:\\PGHOME/bin

      EOF

    3. 安装postgresql

      1. 上传并解压介质

    将所有安装介质上传到/home/postgres下:

    postgresql16-16.4-1PGDG.rhel8.x86_64.rpm

    postgresql16-contrib-16.4-1PGDG.rhel8.x86_64.rpm

    postgresql16-libs-16.4-1PGDG.rhel8.x86_64.rpm

    postgresql16-server-16.4-1PGDG.rhel8.x86_64.rpm
    5.

    1. 安装postgres

    2. yum install postgresql16-16.4-1PGDG.rhel8.x86_64.rpm postgresql16-contrib-16.4-1PGDG.rhel8.x86_64.rpm postgresql16-libs-16.4-1PGDG.rhel8.x86_64.rpm postgresql16-server-16.4-1PGDG.rhel8.x86_64.rpm

      1. 初始化数据库
    3. su - postgres

      initdb -D /data/pgdata --waldir=/data/pgwal --wal-segsize=64

      编辑pg_hba.conf文件加入

      local replication repmgr trust

      host replication repmgr 127.0.0.1/32 trust

      host replication repmgr 192.168.10.33/32 trust

      host replication repmgr 192.168.10.34/32 trust

      local repmgr repmgr trust

      host repmgr repmgr 127.0.0.1/32 trust

      host repmgr repmgr 192.168.10.33/32 trust

      host repmgr repmgr 192.168.10.34/32 trust

      host all all 0.0.0.0/0 md5

      1. 编辑参数文件
    4. Vi /data/pgdata/postgresql.conf,在文件末尾加入如下内容

      ##general

      listen_addresses = '*'

      unix_socket_directories = '/run/postgresql, /tmp'

      max_connections = 300

      ##内存设置

      #取总内存一半

      shared_buffers=8GB

      #取总内存的0.75

      effective_cache_size =12GB

      work_mem=16MB

      temp_buffers=16MB

      #maintenance_work_mem一版按照该1GB分配50MB,如果小于1G,侧设置为1GB,对于autovacuum_work_mem默认值是-1,则使用maintenance_work_mem的设置值

      maintenance_work_mem=1GB

      ##设置log日志

      log_statement=ddl

      log_truncate_on_rotation = on

      log_checkpoints = on

      log_connections = on

      log_disconnections = on

      log_min_duration_statement = 10000

      log_directory='/data/pg_logs/'

      log_line_prefix = '%m %p %u %d %r %a '

      log_rotation_age = 91d

      log_rotation_size = 20MB

      log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

      log_file_mode = 0604

      ##指定用户数据保存表空间

      default_tablespace = pgtbs

      ##设置wal日志

      wal_level =replica

      wal_log_hints = on

      max_wal_size = 8GB

      min_wal_size = 1GB

      wal_keep_size=1GB

      max_standby_archive_delay = '300s'

      max_standby_streaming_delay = '300s'

      bgwriter_delay = '50ms'

      bgwriter_lru_maxpages = '500'

      bgwriter_lru_multiplier = '3.0'

      ##设置归档

      archive_mode = on

      #for pg14

      #archive_command = 'cp %p /data/archivelog/%f'

      #for pg16

      archive_library = 'basic_archive'

      basic_archive.archive_directory = '/data/archivelog/'

      archive_timeout = 900s

      shared_preload_libraries='repmgr,pg_stat_statements,pg_buffercache,pg_prewarm,file_fdw ,pg_visibility ,pgcrypto ,pgrowlocks,uuid-ossp'

      1. 启动数据库
    5. pg_ctl -l /data/pglogs/pg_log.log start

      1. 创建表空间及加载插件
    6. create tablespace pgtbs location '/data/pgtbs/';

      create extension pg_buffercache;

      create extension pg_stat_statements;

      CREATE EXTENSION pg_prewarm;

      CREATE EXTENSION file_fdw;

      CREATE EXTENSION pg_visibility;

      create extension pgcrypto;

      create extension pgrowlocks;

      create extension "uuid-ossp";

      1. 创建维护用户
    7. 为方便维护创建my_cf维护用户,用户平时查看,监控,备份

      CREATE USER mon_pg WITH PASSWORD 'password' INHERIT;

      GRANT pg_monitor TO mon_pg;

      创建repmgr同步用户

      create user repmgr with password ' password ' superuser replication;

      create database repmgr owner repmgr;

      alter user repmgr set search_path to repmgr, "$user", public;

    8. 配置集群

      1. 配置个节点ssh互信
    9. ssh-keygen -t rsa

      ssh-copy-id postgres@192.168.10.33

      ssh-copy-id postgres@192.168.10.34

      1. 安装同步插件repmgr
    10. yum install repmgr_16-5.4.1-1PGDG.rhel8.x86_64.rpm

      复制配置文件至指定目录

      cp /etc/repmgr/16/repmgr.conf /data/repmgr/repmgr.conf

      1. 配置repmgr配置文件
    11. 在主从节点/data/repmgr/repmgr.conf尾部加入

      节点一:

      leader节点

      node_id=1

      node_name='pwdrzhdb01'

      conninfo='host=192.168.10.33 port=5432 user=repmgr dbname=repmgr connect_timeout=2'

      data_directory='/data/pgdata'

      failover='automatic'

      promote_command='/usr/pgsql-16/bin/repmgr standby promote --config-file=/data/repmgr/repmgr.conf --log-to-file'

      follow_command='/usr/pgsql-16/bin/repmgr standby follow --config-file=/data/repmgr/repmgr.conf --log-to-file --upstream-node-id=%n'

      monitoring_history=yes

      connection_check_type=ping

      reconnect_attempts=6

      reconnect_interval=5

      standby_disconnect_on_failover=true

      repmgrd_pid_file='/data/repmgr/repmgrd.pid'

      repmgrd_service_start_command='/usr/pgsql-16/bin/repmgrd -f /data/repmgr/repmgr.conf start'

      repmgrd_service_stop_command='kill -9 `cat /data/repmgr/repmgrd.pid`'

      log_level=INFO

      log_file='/data/repmgr/repmgrd.log'

      log_status_interval=10

      节点二:

      standby节点

      node_id=2

      node_name='pwdrzhdb02'

      conninfo='host=192.168.10.34 port=5432 user=repmgr dbname=repmgr connect_timeout=2'

      data_directory='/data/pgdata'

      failover='automatic'

      promote_command='/usr/pgsql-16/bin/repmgr standby promote --config-file=/data/repmgr/repmgr.conf --log-to-file'

      follow_command='/usr/pgsql-16/bin/repmgr standby follow --config-file=/data/repmgr/repmgr.conf --log-to-file --upstream-node-id=%n'

      monitoring_history=yes

      connection_check_type=ping

      reconnect_attempts=6

      reconnect_interval=5

      standby_disconnect_on_failover=true

      repmgrd_pid_file='/data/repmgr/repmgrd.pid'

      repmgrd_service_start_command='/usr/pgsql-16/bin/repmgrd -f /data/repmgr/repmgr.conf start'

      repmgrd_service_stop_command='kill -9 `cat /data/repmgr/repmgrd.pid`'

      log_level=INFO

      log_file='/data/repmgr/repmgrd.log'

      log_status_interval=10

      1. 注册主节点
    12. 在主节点执行

      repmgr -f /data/repmgr/repmgr.conf primary register

      1. 克隆备库
    13. 在备节点执行

      测试备库是否可以克隆

      repmgr -h 192.168.10.33 -U repmgr -d repmgr -f /data/repmgr/repmgr.conf standby clone --dry-run

      无报错即可开始克隆

      repmgr -h 192.168.10.33 -U repmgr -d repmgr -f /data/repmgr/repmgr.conf standby clone

      1. 启动备库
    复制代码
    pg_ctl -l /data/pglogs/pg_log.log start
      1. 注册备库
    1. repmgr -f /data/repmgr/repmgr.conf standby register

      1. 启动自动切换监控进程
    2. 主备节点都运行

      repmgr -f /data/repmgr/repmgr.conf daemon start

      1. 常用命令
    3. 检测集群状态

      postgres@pwbrzhdb01:/data/repmgr$repmgr -f /data/repmgr/repmgr.conf cluster show

      ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string

      ----+------------+---------+-----------+------------+----------+----------+----------+---------------------------------------------------------------------------------------------

      1 | pwdrzhdb01 | primary | * running | | default | 100 | 3 | host=192.168.10.33 port=5432 user=repmgr password=repmgr_123 dbname=repmgr connect_timeout=2

      2 | pwdrzhdb02 | standby | running | pwdrzhdb01 | default | 100 | 3 | host=192.168.10.34 port=5432 user=repmgr dbname=repmgr connect_timeout=2

      主从节点切换

      postgres@pwbrzhdb02:/data$repmgr -f /data/repmgr/repmgr.conf standby switchover

      NOTICE: executing switchover on node "pwdrzhdb02" (ID: 2)

      NOTICE: attempting to pause repmgrd on 2 nodes

      NOTICE: local node "pwdrzhdb02" (ID: 2) will be promoted to primary; current primary "pwdrzhdb01" (ID: 1) will be demoted to standby

      NOTICE: stopping current primary node "pwdrzhdb01" (ID: 1)

      NOTICE: issuing CHECKPOINT on node "pwdrzhdb01" (ID: 1)

      DETAIL: executing server command "/usr/pgsql-14/bin/pg_ctl -D '/data/pgdata' -W -m fast stop"

      INFO: checking for primary shutdown; 1 of 60 attempts ("shutdown_check_timeout")

      INFO: checking for primary shutdown; 2 of 60 attempts ("shutdown_check_timeout")

      NOTICE: current primary has been cleanly shut down at location 0/1C000028

      NOTICE: promoting standby to primary

      DETAIL: promoting server "pwdrzhdb02" (ID: 2) using pg_promote()

      NOTICE: waiting up to 60 seconds (parameter "promote_check_timeout") for promotion to complete

      NOTICE: STANDBY PROMOTE successful

      DETAIL: server "pwdrzhdb02" (ID: 2) was successfully promoted to primary

      NOTICE: node "pwdrzhdb02" (ID: 2) promoted to primary, node "pwdrzhdb01" (ID: 1) demoted to standby

      NOTICE: switchover was successful

      DETAIL: node "pwdrzhdb02" is now primary and node "pwdrzhdb01" is attached as standby

      NOTICE: STANDBY SWITCHOVER has completed successfully

      postgres@pwbrzhdb02:/data$

      检测node状态:

      postgres@pwbrzhdb02:/data$repmgr -f /data/repmgr/repmgr.conf node status

      Node "pwdrzhdb02":

      PostgreSQL version: 14.11

      Total data size: 34 MB

      Conninfo: host=192.168.10.34 port=5432 user=repmgr dbname=repmgr connect_timeout=2

      Role: primary

      WAL archiving: enabled

      Archive command: cp %p /data/archivelog/%f

      WALs pending archiving: 0 pending files

      Replication connections: 1 (of maximal 10)

      Replication slots: 0 physical (of maximal 10; 0 missing)

      Replication lag: n/a

      检测node复制状态

      postgres@pwbrzhdb01:/data/repmgr$repmgr -f /data/repmgr/repmgr.conf node check

      Node "pwdrzhdb01":

      Server role: OK (node is standby)

      Replication lag: OK (0 seconds)

      WAL archiving: OK (0 pending archive ready files)

      Upstream connection: OK (node "pwdrzhdb01" (ID: 1) is attached to expected upstream node "pwdrzhdb02" (ID: 2))

      Downstream servers: OK (this node has no downstream nodes)

      Replication slots: OK (node has no physical replication slots)

      Missing physical replication slots: OK (node has no missing physical replication slots)

      Configured data directory: OK (configured "data_directory" is "/data/pgdata")

相关推荐
吃糖的小孩16 小时前
给 QQ AI 机器人设计“可控记忆”:会话摘要、手动长期记忆与角色卡边界
数据库
笃行3501 天前
金仓数据库数据安全双防线:静态存储加密与传输加密实战
数据库
笃行3501 天前
金仓数据库物理备份实战:sys_rman 全流程演练与误覆盖抢救
数据库
笃行3501 天前
金仓数据库逻辑备份实战:从全库导出到 Schema 替换的完整闭环
数据库
SelectDB2 天前
阶跃星辰基于 SelectDB 构建 PB 级 Agent 可观测平台
大数据·数据库·aigc
这个DBA有点耶2 天前
GROUP BY优化全解:如何写出既不丢数据又飞快的分组查询
数据库·mysql·架构
掉头发的王富贵2 天前
【StarRocks】极限十分钟入门StarRocks
数据库·sql·mysql
Nturmoils2 天前
WHERE 条件别凭习惯写,常用查询先跑一遍
数据库
Databend3 天前
在 AWS 中国峰会逛了一天,我在 Databend 展台看到了 Agent 数据基础设施的新思路
数据库·人工智能·agent