一、集群规划
建立5个虚拟机构成的集群,虚拟机的os均为centos6.5,依次命名为cnode1,cnode2,cnode3,cnode4,cnode5,其中cnode1为gtm,其余4个节点均为coordinator(程序直接用)和datanode(数据库9.2,一开始还以为是管理数据库的啥玩意儿呢害我又删掉原来部署好的9.4坑爹呀!)。
hostname |
ip | function |
---|---|---|
gtm-master | 192.168.180.188 | gtm master |
gtm-slave | 192.168.180.189 | gtm slave |
pg1 | 192.168.180.191 | gtmproxy & coordinator & datanode |
pg2 | 192.168.180.192 | gtmproxy & coordinator & datanode |
pg3 | 192.168.180.193 | gtmproxy & coordinator & datanode |
pg4 | 192.168.180.194 | gtmproxy & coordinator & datanode |
二、建立用户
每个节点(cnode1~cnode5)都建立用户postgres,并且建立.ssh目录,并配置相应的权限:
useradd postgres passwd postgres su - postgres mkdir ~/.ssh chmod 700 ~/.ssh
三、ssh免密码登录
在cnode1节点配置:
su - postgres ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
将刚生成的认证文件拷贝到cnode2到cnode5中,使得cnode1可以免密码登录cnode2~cnode5的任意一个节点:
scp ~/.ssh/authorized_keys scp ~/.ssh/authorized_keys scp ~/.ssh/authorized_keys scp ~/.ssh/authorized_keys
四、安装postgres-xl
每个节点安装编译pgxl所需的依赖包:
yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl #实测时debian8.1最简安装需要安装...如果已经make过了,直接copy过去就不用重新make也不会有依赖问题 apt-get install libreadline-dev zlib1g-dev make
每个节点安装postgres-xl和pgxc_ctl(注意要在postgres用户下进行),我这里使用的版本是:postgres-xl-v9.2-src.tar.gz(实测contrib版本make的时候报错,查说是bug,建议从git下载,git clone git://git.code.sf.net/p/postgres-xl/postgres-xl postgres-xl):
tar -zxvf postgres-xl-v9.2-src.tar.gz cd postgres-xl ./configure --prefix=/usr/local/pgxl/ #如果遇到docbook-dsssl问题,,按照这个解决http://www.linuxfromscratch.org/blfs/view/stable/pst/docbook-dsssl.html make make install cd contrib/ make make install
每个节点编辑环境变量:
vim ~/.bashrc#debian环境下启动或者关闭集群的机子这里是gtm_master还得在.bash_profile加进去否则加载不了变量
添加如下内容:
export PGHOME=/usr/local/pgxl export PGUSER=postgres export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH export PATH=$PGHOME/bin:$PATH
使之生效:
source ~/.bashrc
五、配置集群
关闭防火墙或者放开相应的端口,这里我直接关闭了每个虚拟机的防火墙,并且重启它们:
chkconfig iptables off
--重启后生效
选择cnode1(gtm),进入/home/pgxc/postgres-xl/contrib/pgxc_ctl目录中,执行pgxc_ctl命令来生成配置集群的模板文件:
./pgxc_ctl ---会提示Error说没有配置文件,忽略即可 PGXC prepare ---执行该命令将会生成一份配置文件模板 PGXC ^C
根据自己的集群配置该文件:
vim ~/pgxc_ctl/pgxc_ctl.conf
我这里简单的进行了如下的配置:
#user and path pgxcInstallDir=$HOME/pgxl pgxcOwner=postgres pgxcUser=$pgxcOwner #gtm gtmName=gtm gtmMasterServer=cnode1 gtmMasterPort=20001 gtmMasterDir=$HOME/pgxl/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) gtmSlave=n #gtm proxy gtmProxy=n #gtmProxyDir=$HOME/pgxl/nodes/gtm_pxy #gtmProxyNames=(gtm_pxy1 gtm_pxy2 gtm_pxy3 gtm_pxy4) #gtmProxyServers=(cnode2 cnode3 cnode4 cnode5) #gtmProxyPorts=(20001 20001 20001 20001) #gtmProxyDirs=($gtmProxyDir $gtmProxyDir $gtmProxyDir $gtmProxyDir) #gtmPxyExtraConfig=none #gtmPxySpecificExtraConfig=(none none none none) #coordinator coordMasterDir=$HOME/pgxl/nodes/coord coordNames=(coord1 coord2 coord3 coord4) coordPorts=(20004 20004 20004 20004) poolerPorts=(20010 20010 20010 20010) coordPgHbaEntries=(0.0.0.0/0) coordMasterServers=(cnode2 cnode3 cnode4 cnode5) coordMasterDirs=($coordMasterDir $coordMasterDir $coordMasterDir $coordMasterDir) coordMaxWALsernder=0 coordMaxWALSenders=($coordMaxWALsernder $coordMaxWALsernder $coordMaxWALsernder $coordMaxWALsernder) coordSlave=n coordSpecificExtraConfig=(none none none none) coordSpecificExtraPgHba=(none none none none) #datanode datanodeNames=(datanode1 datanode2 datanode3 datanode4) datanodePorts=(20008 20008 20008 20008) datanodePoolerPorts=(20012 20012 20012 20012) datanodePgHbaEntries=(0.0.0.0/0) datanodeMasterServers=(cnode2 cnode3 cnode4 cnode5) datanodeMasterDir=$HOME/pgxl/nodes/dn_master datanodeMasterDirs=($datanodeMasterDir $datanodeMasterDir $datanodeMasterDir $datanodeMasterDir) datanodeMaxWalSender=0 datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender $datanodeMaxWalSender $datanodeMaxWalSender) datanodeSlave=n primaryDatanode=datanode1
以上各配置项的具体含义可以参考:http://files.postgres-xl.org/documentation/pgxc_ctl.html
六、初始化+启动集群
配置每一个协调者和数据节点的pg_hba.conf文件,增加配置免认证:
host all all 0.0.0.0/0 trust
在cnode1(gtm)执行命令,这个命令会初始化(配置)各个节点,并且启动相应的进程:
pgxc_ctl -c pgxc_ctl.conf init all --初始化完成后会自动启动(所以不要尝试再次启动!)
先关闭所有coor\dn\gtm_proxy\gtm再启动
pgxc_ctl -c pgxc_ctl.conf stop all pgxc_ctl -c pgxc_ctl.conf start all
可以在每个节点上查看对应的进程:
ps -ef | grep gtm ps -ef | grep postgres
七、注册节点(coordinator,datadone都需要配置,但本次测试中貌似最新版已经加入该功能了,数据库里都有就没care)
在每一个节点(除了gtm)上注册节点:
psql -p 5432 postgres //访问协调节点 psql -p 15432 postgres //访问数据节点
drop node coord1; drop node coord2; drop node coord3; drop node datanode1; drop node datanode2; drop node datanode3; create node coord1 with(TYPE=coordinator,HOST='cnode2',PORT=5432); create node coord2 with(TYPE=coordinator,HOST='cnode3',PORT=5432); create node coord3 with(TYPE=coordinator,HOST='cnode4',PORT=5432); create node datanode1 with(TYPE=datanode,HOST='cnode2',PORT=15432,primary=true); create node datanode2 with(TYPE=datanode,HOST='cnode3',PORT=15432,primary=false); create node datanode3 with(TYPE=datanode,HOST='cnode4',PORT=15432,primary=false); alter node coord1 with(TYPE=coordinator,HOST='cnode2',PORT=5432); alter node coord2 with(TYPE=coordinator,HOST='cnode3',PORT=5432); alter node coord3 with(TYPE=coordinator,HOST='cnode4',PORT=5432); alter node datanode1 with(TYPE=datanode,HOST='cnode2',PORT=15432,primary=true); alter node datanode2 with(TYPE=datanode,HOST='cnode3',PORT=15432,primary=false); alter node datanode3 with(TYPE=datanode,HOST='cnode4',PORT=15432,primary=false); select pgxc_pool_reload(); select * from pgxc_node;
修改postgres的密码
ALTER USER postgres WITH PASSWORD 'postgres';
八、测试集群
可以在任意节点上输入以下命令,查看除gtm以外的所有节点的配置情况:
select * from pgxc_node;
简单的测试,在cnode2节点上创建一个数据库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
在cnode3节点上查看刚刚在cnode2节点的更改:
psql -p 20004 -U postgres \c test SELECT * FROM test; \q
发现有刚刚插入的数据,验证成功!