一 Postgres-XL简介

Postgres的-XL是一个基于PostgreSQL数据库的横向扩展开源SQL数据库集群,具有足够的灵活性来处理不同的数据库工作负载:

  • 完全ACID,保持事务一致性
  • OLTP 写频繁的业务
  • 需要MPP并行性商业智能/大数据分析
  • 操作数据存储
  • Key-value 存储
  • GIS的地理空间
  • 混合业务工作环境
  • 多租户服务提供商托管环境
  • Web 2.0

    F7KbTxlAhATKGLFL.png

    Postgres-XL架构

二 组件简介

  • Global Transaction Monitor (GTM)
    全局交易监测,确保群集范围内的事务一致性。 GTM负责发放事务ID和快照作为其多版本并发控制的一部分。
    集群可选地配置一个备用GTM,以改进可用性。此外,可以在协调器间配置代理GTM, 可用于改善可扩展性,减少GTM的通信量。

  • Coordinator
    协调员管理用户会话,并与GTM和数据节点进行交互。协调员解析,并计划查询,并给语句中的每一个组件发送下一个序列化的全局性计划。

  • Data Node
    数据节点是数据实际存储的地方。数据的分布可以由DBA来配置。为了提高可用性,可以配置数据节点的热备以便进行故障转移准备。

总结:gtm是负责ACID的,保证分布式数据库全局事务一致性。得益于此,就算数据节点是分布的,但是你在主节点操作增删改查事务时,就如同只操作一个数据库一样简单。Coordinator是调度的,将操作指令发送到各个数据节点。datanodes是数据节点,分布式存储数据。

三 Postgres-XL环境配置与安装

3.1 集群规划

准备三台Centos7服务器(或者虚拟机),集群规划如下:

主机名 IP 角色 端口 nodename 数据目录
GTM 6666 gtm /DATA/gtm
Pg1 192.168.0.125 Coordinator 1921 coord1 /DATA/coord1
Coordinator 1925 coord2 /DATA/coord2
pg2 192.168.0.126 Datanode 11921 dn1 /DATA/dn1
Datanode 11922 dn2 /DATA/dn2
pg2 192.168.0.127 Datanode 11923 dn3 /DATA/dn3
Datanode 11924 dn4 /DATA/dn4

pg1作为gtm与主节点Coordinator调度,pg2,pg3作为数据节点存储数据。

3.2 系统环境设置

以下操作,对每个服务器节点都适用。
关闭防火墙:

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service

selinux设置:

[root@localhost ~]# /etc/selinux/config

设置SELINUX=disabled,保存退出。
安装依赖包:

[root@localhost ~]# yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl

重启服务器!一定要重启!

3.3 新建用户

每个节点(pg1~pg3)都建立用户postgres,并且建立.ssh目录,并配置相应的权限:

[root@localhost ~]# useradd postgres
[root@localhost ~]# passwd postgres
[root@localhost ~]# su - postgres
[root@localhost ~]# mkdir ~/.ssh
[root@localhost ~]# chmod 700 ~/.ssh

3.4 ssh免密码登录

仅仅在pg1节点配置,pg2,pg3不执行如下操作:

[root@localhost ~]# su - postgres
[postgres@localhost ~]# ssh-keygen -t rsa
[postgres@localhost ~]# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[postgres@localhost ~]# chmod 600 ~/.ssh/authorized_keys

将刚生成的认证文件拷贝到pg2到pg3中,使得pg1可以免密码登录pg2~pg3的任意一个节点:

[postgres@localhost ~]# scp ~/.ssh/authorized_keys [email protected]:~/.ssh/
[postgres@localhost ~]# scp ~/.ssh/authorized_keys [email protected]:~/.ssh/

对所有提示都不要输入,直接enter下一步。直到最后,因为第一次要求输入目标机器的用户密码,输入即可。

3.5 Postgres-XL安装

pg1-pg3每个节点都需安装配置。切换回root用户下,执行如下步骤安装

[root@localhost ~]# tar -zxvf postgres-xl-9.5r1.3.tar.gz
[root@localhost ~]# cd postgres-xl-9.5r1.3
[root@localhost ~postgres-xl-9.5r1.3]# ./configure --prefix=/home/postgres/pgxl9.5/
[root@localhost ~postgres-xl-9.5r1.3]# make
[root@localhost ~postgres-xl-9.5r1.3]# make install
[root@localhost ~postgres-xl-9.5r1.3]# cd contrib/  
[root@localhost ~contrib]# make
[root@localhost ~contrib]# make install

cortrib中有很多postgres很牛的工具,一般要装上。如ltree,uuid,postgres_fdw等等。

3.6 配置环境变量

进入环境变量,开始编辑

[root@localhost ~]# vim /etc/profile

在打开的文件末尾,新增如下变量配置:

export PGHOME=/home/postgres/pgxl9.5
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH

按住esc,然后输入:wq!保存退出。输入以下命令对更改重启生效。

[root@localhost ~]# source/etc/profile
#输入以下语句,如果输出变量结果,代表生效
[root@localhost ~]# echo $PGHOME
#应该输出/home/postgres/pgxl9.5代表生效

如上操作,除特别强调以外,是pg1-pg3节点都要配置安装的。

四 Postgres-XL组件配置

4.1 创建节点数据目录

pg1节点创建如下:

[root@localhost~]# cd /home/postgres
[root@postgres~]# mkdir DATA
[root@postgres~]# cd DATA
[root@DATA~]# mkdir gtm
[root@DATA~]# mkdir coord1
[root@DATA~]# mkdir coord2
[root@localhost~]# chown -R postgres.postgres /home/postgres

pg2节点创建如下:

[root@localhost~]# cd /home/postgres
[root@postgres~]# mkdir DATA
[root@postgres~]# cd DATA
[root@DATA~]# mkdir dn1
[root@DATA~]# mkdir dn2
[root@localhost~]# chown -R postgres.postgres /home/postgres

pg3节点创建如下:

[root@localhost~]# cd /home/postgres
[root@postgres~]# mkdir DATA
[root@postgres~]# cd DATA
[root@DATA~]# mkdir dn3
[root@DATA~]# mkdir dn4
[root@localhost~]# chown -R postgres.postgres /home/postgres

4.2 配置gtm和coordinator

gtm与coordinator都是仅在pg1(192.168.0.125)中配置。

4.1.1 初始化

要在postgres用户下执行。

[root@localhost ~]# su - postgres
[postgres@localhost ~]# initgtm -Z gtm -D /home/postgres/DATA/gtm
[postgres@localhost ~]# initdb -D /home/postgres/DATA/coord1 --nodename coord1 -E UTF8 --locale=C -U postgres -W
[postgres@localhost ~]# initdb -D /home/postgres/DATA/coord2 --nodename coord2 -E UTF8 --locale=C -U postgres -W

4.1.2 配置gtm

[postgres@localhost ~]# vim/home/postgres/DATA/gtm/gtm.conf

打开设置如下:

nodename = 'gtm'
listen_addresses = '*'
port =6666
startup = ACT

4.1.3 配置coordinator

配置coor1如下:

[postgres@localhost ~]# 
vim/home/postgres/DATA/coord1/postgresql.conf

打开设置如下:

# - Connection Settings -
listen_addresses = '*'
port = 1921   #coord2这个端口改为1925
max_connections = 100
# DATA NODES AND CONNECTION POOLING
#----------------------------------
pooler_port = 6667       #coord2这个端口改为6668
max_pool_size = 100
# GTM CONNECTION
#--------------------------
gtm_host = '192.168.0.125'   #即pg1,也就是gtm所在的主机地址
gtm_port = 6666    #gtm配置中,gtm端口号配置为6666
pgxc_node_name = 'coord1'

设置pg_hba.conf

[postgres@localhost ~]# 
vim/home/postgres/DATA/coord1/pg_hba.conf

只对IPv4设置如下描述,后面要改成trust:

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             0.0.0.0/0            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

配置coor2步骤和coor1一致,仔细看注释说明,注意端口号不要和coor1重合。

4.3 配置datanode(pg1,pg2都要操作)

datanode是分布在pg2(192.168.0.126),pg3(192.168.0.127)上。

4.3.1 datanode初始化

pg2中初始化dn1,dn2,要在postgres用户下执行。

[root@localhost ~]#  su - postgres
[postgres@localhost ~]#  initdb -D /home/postgres/DATA/dn1 --nodename dn1 -E UTF8 --locale=C -U postgres -W
[postgres@localhost ~]#  initdb -D /home/postgres/DATA/dn2 --nodename dn2 -E UTF8 --locale=C -U postgres -W

pg3中初始化dn3,dn4与上文相同,注意数据节点名称即可。

4.3.2 datanode配置

以dn1配置举例如下:

4.3.2.1 配置postgresql.conf

[postgres@localhost ~]#  vim /home/postgres/DATA/dn1/postgresql.conf

内容更改如下:

CONNECTIONS AND AUTHENTICATION
#------------------------------------
listen_addresses = '*'
port =11921 #dn2,dn3,dn4配置文件分别改为11922、11923、11924
max_connections = 100
# DATA NODES AND CONNECTION POOLING
#----------------------------------------------
pooler_port = 6667#dn2,dn3,dn4配置文件分别改为6668、6667、6668(同一台机器要使用不同的端口,如dn1,dn2在同一台机器pg2上分别是6667,6668)
                           #dn3,dn4在pg3上端口号也是6667,6668。就是同一机器端口号不同即可。
max_pool_size = 100
# GTM CONNECTION
#-----------------------------
gtm_host = '192.168.0.125' #gtm所在的pg1的ip地址
gtm_port = 6666 #gtm端口号
pgxc_node_name = 'dn1'

4.3.2.2 配置pg_hba.conf

[postgres@localhost ~]#  vim /home/postgres/DATA/dn1/pg_hba.conf

只对IPv4设置如下描述,后面要改成trust:

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             0.0.0.0/0            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

配置完dn1,同理配置dn2,dn3,dn4等数据节点。

4.4 启动Postgres-XL

4.4.1 启动gtm

在pg1机器上,在postgres用户下启动gtm

[postgres@localhost ~]# gtm_ctl start -Z gtm -D /home/postgres/DATA/gtm

4.4.2 启动datanode

在pg2,pg3机器上,分别启动dn1,dn2,dn3,dn4数据节点,这里以pg2的dn1示范如下:

[postgres@localhost ~]# pg_ctl start -Z datanode -D /home/postgres/DATA/dn1

dn2,dn3,dn4同上。

4.4.3 启动coordinator

在pg1机器上,启动coordinator节点coord1,coord2:

[postgres@localhost ~]# pg_ctl start -Z coordinator -D /home/postgres/DATA/coord1
[postgres@localhost ~]# pg_ctl start -Z coordinator -D /home/postgres/DATA/coord2

五 Postgres-XL集群配置与测试

5.1 集群配置

pg1(192.168.0.125)配置,配置coord1节点,以postgres用户下进入psql。coord1配置端口号是1921,直接以psql加端口号进入配置。

[postgres@localhost]$ psql -p 1921
psql (PGXL 9.5r1.3, based on PG 9.5.4 (Postgres-XL 9.5r1.3))
Type "help" for help.
postgres=# select * from pgxc_node;
postgres=# alter node coord1 with (type=coordinator,host='192.168.0.125', port=1921);
postgres=# create node coord2 with (type=coordinator,host='192.168.0.125', port=1925);
postgres=# create node dn1 with (type=datanode, host='192.168.0.126',port=11921,primary,preferred);
postgres=# create node dn2 with (type=datanode, host='192.168.0.126',port=11922);
postgres=#create node dn3 with (type=datanode, host='192.168.0.127',port=11923);
postgres=# create node dn4 with (type=datanode, host='192.168.0.127',port=11924);
postgres=#select pgxc_pool_reload();
postgres=# select * from pgxc_node;

配置pg1上的coord2如下,以当初配置的1925端口号进入psql:

[postgres@localhost]$ psql -p 1925
psql (PGXL 9.5r1.3, based on PG 9.5.4 (Postgres-XL 9.5r1.3))
Type "help" for help.
# 下面的其他配置和coord1中完全一致
              .
              .
              .

通过coord1,coord2配置,总结规律是每个节点配置都是一模一样的,所以分别在pg2机器中配置dn1,dn2,pg3机器中配置dn3,dn4。

5.2 创建测试表与数据

只能在pg1中coord1,coord2节点中操作,pg2,pg3数据节点都是只读的,所以我们选择coord1(端口号为1921)中创建测试数据。

[postgres@localhost]$ psql -p 1921
psql (PGXL 9.5r1.3, based on PG 9.5.4 (Postgres-XL 9.5r1.3))
Type "help" for help.
postgres=#  create table test1(id integer,name varchar(20));
postgres=#  insert into test1(id,name) values(1,'测试');
postgres=#  insert into test1(id,name) values(2,'测试');
postgres=#  insert into test1(id,name) values(3,'测试');
postgres=#  insert into test1(id,name) values(4,'测试');
postgres=#  insert into test1(id,name) values(5,'测试');
postgres=#  insert into test1(id,name) values(6,'测试');
postgres=#  insert into test1(id,name) values(7,'测试');
postgres=#  insert into test1(id,name) values(8,'测试');

通过我的本机测试发现,在coord2,dn1,dn2,dn3,dn4节点中都已创建了一个test1表。且coord1=coord2,而dn1中test1表有3条数据,dn1有两条,dn3有0条,dn4有3条。
总结:coord1,coord2这种协调处理节点,是操作数据库的主节点,拥有完整的数据视图。
dn1到dn4是数据节点,只读的,存储数据,由coordinator协调分配存储到哪个节点,所以不同节点分配到的数据数量不一。(一开始我以为错了,原来我理解错了分布式)。

注意:由于所有的数据节点组成了完整的数据视图,所以一个数据节点down机,整个pgxl都启动不了了,所以实际生产中,为了提高可用性,一定要配置数据节点的热备以便进行故障转移准备。

六 遇到的问题

在配置的时候一定要细心,避免端口号之类的配置错误。
在创建测试数据时,我曾遇到以下错误:

postgres=# create table test1(id integer,name varchar(20));
LOG:  failed to connect to node, connection string (host=192.168.0.125 port=1925 dbname=postgres user=postgres application_name=pgxc sslmode=disable options='-c remotetype=coordinator -c parentnode=coord1  -c DateStyle=iso,mdy -c timezone=prc -c geqo=on -c intervalstyle=postgres -c lc_monetary=C'), connection error (fe_sendauth: no password supplied
        )
WARNING:  can not connect to node 16384
WARNING:  Health map updated to reflect DOWN node (16384)
LOG:  Pooler could not open a connection to node 16384
LOG:  failed to acquire connections
STATEMENT:  create table test1(id integer,name varchar(20));
ERROR:  Failed to get pooled connections
HINT:  This may happen because one or more nodes are currently unreachable, either because of node or network failure.
         Its also possible that the target node may have hit the connection limit or the pooler is configured with low connections.
         Please check if all nodes are running fine and also review max_connections and max_pool_size configuration parameters
STATEMENT:  create table test1(id integer,name varchar(20));

查阅资料可知:这个是由于某些环境或配置出了问题,我的就是pg_hba.conf配置出了问题,Ipv4要改成 0:0:0:0/0 trust才行。
但这仅仅是一个问题,开发者搭建环境遇到这个错误,一定要检查如下:

  • 各个机器的防火墙是否关闭?
  • 各个机器的SELINUX状态是否是disabled?
  • 各个机器的ssh免密登录是否成功?
  • 各个节点的pg_hba.conf,postgresql.conf是否配置为信任登录?是否有IP限制?
  • 超过某些节点的最大连接数?(对于我们测试环境来说,肯定不会是这个问题)

作者搭建pgxl是为地理大数据做技术预研的,使用postgis作为空间数据,欢迎postgis开发者参与交流。

你可能感兴趣的内容
0条评论
AS

assylias

这家伙太懒了,什么都没留下
Owner