• pgpool-II主备流复制的架设


    1.环境

    OS:  CentOS release 6.4 (Final)
    DB: postgresql 9.3.6
    pgpool服务器: pgpool 172.16.0.240
    数据库主服务器:master 172.16.0.241
    数据库从服务器:slave  172.16.0.242 

    其中主从数据库使用的流复制,并且已经配置完毕,新配置的pgpool使用postgres用户进行管理。新架构使用的主备模式外加流复制,此架构支持流复制、负载均衡、故障恢复,不支持复制和并行查询,主库可以支持读写,从库只读。

    2.pgpool安装

    [root@pgpool opt]# yum install -y http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-1.noarch.rpm
    [root@pgpool opt]# yum install postgresql93-devel
    [root@pgpool opt]# wget http://www.pgpool.net/download.php?f=pgpool-II-3.4.2.tar.gz
    [root@pgpool opt]# mv download.php?f=pgpool-II-3.4.2.tar.gz pgpool-II-3.4.2.tar.gz
    [root@pgpool opt]# tar -zxvf pgpool-II-3.4.2.tar.gz 
    [root@pgpool opt]# cd pgpool-II-3.4.2
    [root@pgpool pgpool-II-3.4.2]# ./configure --with-pgsql=/usr/pgsql-9.3

    如果不加--with-pgsql=/usr/pgsql-9.3,可能会出现configure: error: libpq is not installed or libpq is old错误

    [root@pgpool pgpool-II-3.4.2]# make
    [root@pgpool pgpool-II-3.4.2]# make install

    3.修改pgpool配置

    启用并修改配置文件pgpool.conf

    [root@pgpool etc]# cp /usr/local/etc/pgpool.conf.sample-stream /usr/local/etc/pgpool.conf

    修改pgpool.conf文件中的内容

    # - pgpool Communication Manager Connection Settings -
    listen_addresses = '*'
    
    ---默认0是主库,其它是从库,backend_weight可以控制数据库读在两台机器上的分配比例
    
    # - Backend Connection Settings -
    backend_hostname0 = '172.16.0.241'
    backend_port0 = 5432
    backend_weight0 = 1
    backend_data_directory0 = '/var/lib/pgsql/9.3/data'
    backend_flag0 = 'ALLOW_TO_FAILOVER'
    
    backend_hostname1 = '172.16.0.242'
    backend_port1 = 5432
    backend_weight1 = 1
    backend_data_directory1 = '/data1'
    backend_flag1 = 'ALLOW_TO_FAILOVER'
    backend_data_directory0 = '/var/lib/pgsql/9.3/data'
    
    # - Authentication -
    
    ---开启pgpool的hba认证
    
    enable_pool_hba = on
    #------------------------------------------------------------------------------
    # MASTER/SLAVE MODE
    #------------------------------------------------------------------------------
    sr_check_user = 'postgres'
    sr_check_password = '123456'
    
    #------------------------------------------------------------------------------
    # HEALTH CHECK
    #------------------------------------------------------------------------------
    
    health_check_period = 1
    health_check_user = 'postgres'
    health_check_password = '123456'
    
    #------------------------------------------------------------------------------
    # FAILOVER AND FAILBACK
    #------------------------------------------------------------------------------
    
    ---用来在主库失败时,把只读的从库切为主库
    
    failover_command = '/usr/local/bin/failover_stream.sh %d %H /tmp/trigger_file0'

    启用并修改配置文件pcp.conf

    [root@pgpool etc]# cp /usr/local/etc/pcp.conf.sample /usr/local/etc/pcp.conf

    此文件是管理端口,可以暂时不用配置。

    启用并修改配置文件pool_hba.conf

    [root@pgpool etc]# cp /usr/local/etc/pool_hba.conf.sample /usr/local/etc/pool_hba.conf
    ---添加一行:
    host    all         all         172.16.0.0/24   md5
    ---删除一行
    host    all         all         127.0.0.1/32          trust

    对pgpool的访问策略,要设置为md5的方式。

    启用配置文件pool_passwd

    [root@pgpool etc]# pg_md5 -m -p -u postgres pool_passwd
    password:

    密码填的是123456,该文件会在执行文件后自动生成。从远程连接pgpool的时候,需要使用该密码来访问,pgpool使用该用户密码来访问后面的真正的数据库。

    添加主备脚本切换脚本failover_stream.sh

    [root@pgpool bin]# vi /usr/local/bin/failover_stream.sh 
    # Failover command for streaming replication.
    # This script assumes that DB node 0 is primary, and 1 is standby.
    # 
    # If standby goes down, do nothing. If primary goes down, create a
    # trigger file so that standby takes over primary node.
    #
    # Arguments: $1: failed node id. $2: new master hostname. $3: path to
    # trigger file.
    
    failed_node=$1
    new_master=$2
    trigger_file=$3
    
    # Do nothing if standby goes down.
    if [ $failed_node = 1 ]; then
            exit 0;
    fi
    
    # Create the trigger file.
    /usr/bin/ssh -T $new_master /bin/touch $trigger_file
    
    exit 0;
    
    [root@pgpool bin]# chmod 700 failover_stream.sh

    对应的从库slave的recovery.conf配置:

    [root@slave data]# cat recovery.conf 
    standby_mode = 'on'
    primary_conninfo = 'host=172.16.0.241 port=5432 user=rep_user'
    trigger_file = '/tmp/trigger_file0'

    该trigger_file的参数要和pgpool.conf文件中failover_command对应的文件保持一致。

    4.设置主机互信

    修改hosts文件

    对每个主机的/etc/hosts文件添加以下内容,

    172.16.0.240 pgpool
    172.16.0.241 master
    172.16.0.242 slave

    pgpool主机生成公钥和私钥

    [root@pgpool bin]# su - postgres
    -bash-4.1$ ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/var/lib/pgsql/.ssh/id_rsa): 
    Created directory '/var/lib/pgsql/.ssh'.
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /var/lib/pgsql/.ssh/id_rsa.
    Your public key has been saved in /var/lib/pgsql/.ssh/id_rsa.pub.
    The key fingerprint is:
    a0:93:d4:b5:ed:26:d0:94:a5:e7:99:95:6b:d6:18:af postgres@pgpool
    The key's randomart image is:
    +--[ RSA 2048]----+
    |        oo.      |
    |     . +.+   .   |
    |    . + + o +    |
    |   . o o + + *   |
    |    +   S * = o  |
    |     .   o o .   |
    |            E    |
    |                 |
    |                 |
    +-----------------+

    输入命令后一直回车即可。在master和slave节点同样执行上面的操作。

    同步公钥

    在pgpool节点上执行

    -bash-4.1$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@172.16.0.241
    -bash-4.1$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@172.16.0.242

    在master节点上执行

    -bash-4.1$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@172.16.0.241
    -bash-4.1$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@172.16.0.242 

    在slave节点上执行

    -bash-4.1$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@172.16.0.241
    -bash-4.1$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@172.16.0.242
    

      

    上面的操作执行后,在每个机器检查一下,是否可以用ssh username@nodename的方式,对其他节点进行免密码的ssh访问,第一次访问的时候会记录known_hosts,否则ssh时候会有是否信任节点的提示。

    5.启动pgpool

    使用postgres用户启动pgpool服务的话,需要修改对应目录的权限,否则pgpool会启动失败

    [root@pgpool etc]# mkdir /var/run/pgpool
    [root@pgpool run]# chown -R postgres:postgres /var/run/pgpool
    [root@pgpool local]# chown -R postgres:postgres /usr/local/etc
    [root@pgpool local]# chown -R postgres:postgres /usr/local/bin
    

      

    检验主从数据库切换的脚本是否可以正常运行,在pgpool服务器执行命令,检测在slave服务器是否生成了trigger_file0文件

    shell脚本在pgpool执行

    -bash-4.1$ /usr/local/bin/failover_stream.sh 0 slave /tmp/trigger_file0

    切换到slave主机检查

    [root@slave ~]# ls /tmp
    ssh-rangWC1783  trigger_file0  yum.log 

    启动pgpool

    -bash-4.1$ pgpool -n -d > /tmp/pgpool.log 2>&1 &
    [1] 10474  

    6.检测pgpool功能

    任一节点(如slave节点)连接pgpool

    [root@slave ~]# psql -h 172.16.0.240 -p 9999 -d test -U postgres
    Password for user postgres: 
    psql (9.3.6)
    Type "help" for help.
    
    test=# select * from t4;
     id 
    ----
      1
    (1 row)
    
    test=# show pool_nodes;
     node_id |   hostname   | port | status | lb_weight |  role   
    ---------+--------------+------+--------+-----------+---------
     0       | 172.16.0.241 | 5432 | 2      | 0.500000  | primary
     1       | 172.16.0.242 | 5432 | 2      | 0.500000  | standby
    (2 rows)

    可以看到此时241数据库是主节点,242是从节点。

    关闭主要节点,可以关闭服务也可以直接杀进程模拟数据库崩溃

    [root@master data]# service postgresql-9.3 stop
    Stopping postgresql-9.3 service: [  OK  ]
      
    ---master关闭后,从节点的连接中断后又成功连接上
    
    server closed the connection unexpectedly
            This probably means the server terminated abnormally
            before or while processing the request.
    The connection to the server was lost. Attempting reset: Succeeded.
    test=# show pool_nodes;
     node_id |   hostname   | port | status | lb_weight |  role   
    ---------+--------------+------+--------+-----------+---------
     0       | 172.16.0.241 | 5432 | 3      | 0.500000  | standby
     1       | 172.16.0.242 | 5432 | 2      | 0.500000  | primary
    
    test=# insert into t4 values (6);
    INSERT 0 1
    test=# select * from t4;
     id 
    ----
      1
      6
    (2 rows)
    
    test=#  

    pgpool发生主从切换后,242节点变为主节点,241节点关闭(status为3)。

    上面查询show pool_nodes中得status字段含义

    Status 由数字 [0 - 3]来表示。
    0 - 该状态仅仅用于初始化,PCP从不显示它。
    1 - 节点已启动,还没有连接。
    2 - 节点已启动,连接被缓冲。
    3 - 节点已关闭。

    可以看到主备已经切换完成。

    7.其它注意事项

      • 若pgpool上的主从切换脚本忘记写,或者没能正常执行,show pool_nodes命令会显示两个节点都是standby,而集群整体是只读的,此时可以关闭并启动节点的数据库服务,然后重启pgpool。
      • 测试trigger文件的时候,注意要及时删除生成的trigger 文件,否则会破坏主从架构,导致后面的实验失败。
  • 相关阅读:
    python数据分析常用包之Matplotlib
    python数据分析常用包之Scipy
    python数据分析常用包之Pandas
    python数据分析常用包之numpy
    常见算法之‘插入类排序’
    CRM管理系统流程.king_admin
    常见算法之‘选择排序’
    常见的算法排序之交换类排序
    常用正则表达式
    scss在ide的命令参数
  • 原文地址:https://www.cnblogs.com/opalyao/p/4581514.html
Copyright © 2020-2023  润新知