一、集群的规划
hostname | ip | roule | port |
---|---|---|---|
gtm | 192.168.1.1 | gtm | 6666 |
datanode1 | 192.168.1.5 | coordinator & datanode1 & gtm proxy | 5432 5433 |
datanode2 | 192.168.1.6 | coordinator & datanode2 & gtm proxy | |
backupnode1 | 192.168.1.7 | datanode1 slaver | 15433 |
backupnode2 | 192.168.1.8 | datanode2 slaver & gtm slaver | 20001 |
注:角色都可以根据自己的需求来划分,可灵活的调整
二、创建用户并装备环境,以下操作无特殊说明,每个节点都需要进行操作
1、创建postgres用户,并且创建ssh免密登陆的目录,授予权限
useradd postgres
passwd postgres
su - postgres
mkdir ~/.ssh
chmod 700 ~/.ssh
2、关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld #重启不会启动
systemctl status firewalld #查看一下状态,确定一下
关闭selinux,编辑/etc/selinux/config文件
将SELINUX的值设置为disabled
3、进行域名解析,配置/etc/hosts,在末尾添加如下内容
192.168.1.1 gtm
192.168.1.5 datanode1
192.168.1.6 datanode2
192.168.1.7 backupnode1
192.168.1.8 backupnode2
4、配置gtm到其他节点免密ssh登陆,在gtm节点操作
su – postgres
ssh-keygen -t rsa #这里一定要一直按回车,不要输入任何字符
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys postgres@datanode1:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys postgres@datanode2:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys postgres@backupnode1:~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys postgres@backupnode2:~/.ssh/authorized_keys
注:我们配置完可以验证一下,在gtm上用postgres用户ssh其他机器
#ssh postgres@datanode1 其他测试方法相同,看gtm能否免密登陆其他设备
三、安装postgress数据库,我们采用的源码安装方式,每个节点需要安装,切换为root用户
1、安装基础环境
yum install -y make gcc tar readline readline-devel zlib zlib-devel flex bison openjade docbook-style-dsssl
2、上传postgres-xl-9.5r1.6.tar.gz压缩包
3、安装postgres-xl和pgxc_ctl
tar -zxvf postgres-xl-9.5r1.6.tar.gz
cd postgres-xl
./configure --prefix=/home/postgres/pgxl/
make
make install
cd contrib/
make
make install
4、配置环境变量,在每个节点操作,需切换到postgres用户
su – postgres
# vi ~/.bashrc
export PGHOME=/usr/local/pgsql
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
# source ~/.bashrc
注:可以用echo $PGHOME查看变量是否生效
5、每个节点目录权限设置,切换为root用户
su root
# chown -R postgres:postgres /usr/local/pgsql
6、配置集群配置文件,仅在gtm上配置,先生成一份模板,在修改其中内容,然后同步到其他设备,
gxc_ctl ---忽略错误提示
PGXC prepare ---该命令会生成一份模板配置文件
PGXC ^C
编辑/home/postgres/pgxc_ctl/pgxc_ctl.conf
#---- GTM Master -----------------------------------------------
#---- Overall ----
gtmName=gtm
gtmMasterServer=gtm
gtmMasterPort=6666
gtmMasterDir=$HOME/pgxc/nodes/gtm
#---- Configuration ---
gtmExtraConfig=none # Will be added gtm.conf for both Master and Slave (done at initilization only)
gtmMasterSpecificExtraConfig=none # Will be added to Master's gtm.conf (done at initialization only)
#---- GTM Slave -----------------------------------------------
# Because GTM is a key component to maintain database consistency, you may want to configure GTM slave
# for backup.
#---- Overall ------
gtmSlave=y # Specify y if you configure GTM Slave. Otherwise, GTM slave will not be configured and
# all the following variables will be reset.
gtmSlaveName=gtm_slaver
gtmSlaveServer=backupnode1 # value none means GTM slave is not available. Give none if you don't configure GTM Slave.
gtmSlavePort=20001 # Not used if you don't configure GTM slave.
gtmSlaveDir=$HOME/pgxc/nodes/gtm # Not used if you don't configure GTM slave.
# Please note that when you have GTM failover, then there will be no slave available until you configure the slave
# again. (pgxc_add_gtm_slave function will handle it)
#---- Configuration ----
gtmSlaveSpecificExtraConfig=none # Will be added to Slave's gtm.conf (done at initialization only)
注:我只贴出部分配置,每个模块都一样,根据自己的规划图来配置
四、初始化集群并且启动集群,在gtm上初始化,操作会自动初始化(配置)各个节点。
pgxc_ctl init all #会初始化其他节点
pgxc_ctl monitor all #查看节点是否运行
五、测试集群
可以在任意节点上输入以下命令,查看除gtm以外的所有节点的配置情况:
select * from pgxc_node;
简单的测试,在datanode1节点上创建一个数据库test,并创建一个表test,插入数据:
psql -p 20004 -U postgres
CREATE DATABASE test;
\c test
CREATE TABLE test(id int);
INSERT INTO test VALUES(1);
SELECT * FROM test;
\q
在datanode2节点上查看刚刚在datanode1节点的更改:
psql -p 20004 -U postgres
\c test
SELECT * FROM test;
\q
测试是有数据,至此我们的pg_xl集群搭建完毕!