阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Postgres-XL集群搭建详细创建步骤

189次阅读
没有评论

共计 14831 个字符,预计需要花费 38 分钟才能阅读完成。

最近公司业务需求,需要使用 Postgres-XL 集群,关于这部分知识,网络资料不多。经过一段时间的查询,和各种弯路之后,终于完成安装。将详细步骤完整记录,以备查阅。也希望能帮到需要的人。

下面就开始吧:

主机列表和集群安装的角色分配

10.21.13.109  GTM
10.21.13.67  coordinator&datanode
10.21.13.60  datanode

2. 创建 postgres 用户,这部分我使用 ansible 完成的用户创建,以及相关软件包的应用,节省劳动力(yum 其实可以使用 ansible 自带的方式,所以我 ansible 写的比较业余)

ansible all -m shell -a  “useradd postgres”
ansible all -m shell -a ‘echo “postgres:postgres” | chpasswd’
ansible all -m shell -a “yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl gcc”

3. 在每个节点上下载软件,并解压缩

ansible all -m shell -a “wget https://www.postgres-xl.org/downloads/postgres-xl-9.5r1.6.tar.bz2”
ansible all -m shell -a “mv /root/postgres-xl-9.5r1.6.tar.bz2 /tmp; chown postgres:postgres /tmp/postgres-xl-9.5r1.6.tar.bz2”
ansible all -m shell -a “tar  -jxvf /tmp/postgres-xl-9.5r1.6.tar.bz2 -C /home/postgres”

4. 全部节点都创建 pgxc 路径,用来存放 postgres 文件

1 ansible all -m shell -a “su – postgres -c ‘mkdir -p /home/postgres/pgxc/nodes/'”

5. 修改每个节点的.bashrc 文件,注意:修改 /etc/profile 是不生效的。

su – postgres
vi .bashrc

添加如下内容:

export PGHOME=/home/postgres/pgxl
export PGUSER=postgres
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH

使之生效:

source ~/.bashrc

6. 关闭防火墙或者放开相应的端口,这里我直接关闭了每个虚拟机的防火墙,并且重启它们:

service iptables stop
chkconfig iptables off  – 重启后生效

7. 关闭 SELinux:

查看 SELinux 状态:

/usr/sbin/sestatus -v    ## 如果 SELinux status 参数为 enabled 即为开启状态
SELinux status:                enabled

临时关闭(不用重启机器):

setenforce 0      ## 设置 SELinux 成为 permissive 模式
                          ##setenforce 1 设置 SELinux 成为 enforcing 模式

永久关闭:

修改 /etc/selinux/config 文件
将 SELINUX=enforcing 改为 SELINUX=disabled

8. 到每一个节点执行下面的软件安装步骤,这个过程注意,如果中间有报错,需要解决到。例如确实 gcc 包之类的,不然后面的编译也不会成功的。

cd postgres-xl
./configure –prefix=/home/postgres/pgxl/
make
make install
cd contrib/
make
make instal

9. 执行 pgxc_ctl 命令来生成配置集群的模板文件:

./pgxc_ctl              — 会提示 Error 说没有配置文件,忽略即可

PGXC prepare            — 执行该命令将会生成一份配置文件模板

10. 根据模板修改对应的主机名,端口等信息,内容如下:

vi ~/pgxc_ctl/pgxc_ctl.conf
pgxcInstallDir=$HOME/pgxc
pgxcOwner=$USER
pgxcUser=$pgxcOwner
tmpDir=/tmp
localTmpDir=$tmpDir
configBackup=n
configBackupHost=pgxc-linker
configBackupDir=$HOME/pgxc
configBackupFile=pgxc_ctl.bak
gtmName=gtm
gtmMasterServer=10.21.13.109
gtmMasterPort=20001
gtmMasterDir=$HOME/pgxc/nodes/gtm
gtmExtraConfig=none
gtmMasterSpecificExtraConfig=none
gtmSlave=n
gtmProxyDir=$HOME/pgxc/nodes/gtm_pxy
gtmProxy=y
gtmProxyNames=(gtm_pxy1 gtm_pxy2)
gtmProxyServers=(10.21.13.67 10.21.13.60)
gtmProxyPorts=(20001 20001)
gtmProxyDirs=($gtmProxyDir $gtmProxyDir)
gtmPxyExtraConfig=none
gtmPxySpecificExtraConfig=(none none)
coordMasterDir=$HOME/pgxc/nodes/coord
coordSlaveDir=$HOME/pgxc/nodes/coord_slave
coordArchLogDir=$HOME/pgxc/nodes/coord_archlog
coordNames=(coord1)
coordPorts=(20004)
poolerPorts=(20010)
coordPgHbaEntries=(10.21.13.0/25)  <<<<<<<<<<<<<<<<<<<<<<<<< 非常重要
coordMasterServers=(10.21.13.67)
coordMasterDirs=($coordMasterDir)
coordMaxWALsernder=5
coordMaxWALSenders=($coordMaxWALsernder)
coordSlave=n              <<<<<<<<<<<<<<<< 没使用 slave
coordSlaveSync=y
coordSlaveServers=(10.21.13.60 10.21.13.67)
coordSlavePorts=(20004 20005 20004 20005)
coordSlavePoolerPorts=(20010 20011 20010 20011)
coordSlaveDirs=($coordSlaveDir $coordSlaveDir)
coordArchLogDirs=($coordArchLogDir $coordArchLogDir)
coordExtraConfig=coordExtraConfig
cat > $coordExtraConfig <<EOF
log_destination = ‘stderr’
logging_collector = on
log_directory = ‘pg_log’
listen_addresses = ‘*’
max_connections = 100
EOF
coordSpecificExtraConfig=(none none)
coordExtraPgHba=none
coordSpecificExtraPgHba=(none none)
coordAdditionalSlaves=n
coordAdditionalSlaveSet=(cad1)
cad1_Sync=n
cad1_Servers=(10.21.13.67 10.21.13.60)
cad1_dir=$HOME/pgxc/nodes/coord_slave_cad1
cad1_Dirs=($cad1_dir $cad1_dir)
cad1_ArchLogDir=$HOME/pgxc/nodes/coord_archlog_cad1
cad1_ArchLogDirs=($cad1_ArchLogDir $cad1_ArchLogDir)
datanodeMasterDir=$HOME/pgxc/nodes/dn_master
datanodeSlaveDir=$HOME/pgxc/nodes/dn_slave
datanodeArchLogDir=$HOME/pgxc/nodes/datanode_archlog
primaryDatanode=datanode1
datanodeNames=(datanode1 datanode2)
datanodePorts=(20008 20008) # Master ports
datanodePoolerPorts=(20012 20012)
datanodePgHbaEntries=(10.21.13.0/25)  <<<<<<<<<<<<<<<<<<<<<<<<<<<<< 非常重要
datanodeMasterServers=(10.21.13.67 10.21.13.60)
datanodeMasterDirs=($datanodeMasterDir $datanodeMasterDir)
datanodeMaxWalSender=5
datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender)
datanodeSlave=n                <<<<<<<<<<<<<<<< 没使用 slave
datanodeSlaveServers=(10.21.13.60 10.21.13.67)
datanodeSlavePorts=(20008 20008)
datanodeSlavePoolerPorts=(20012 20012)
datanodeSlaveSync=y
datanodeSlaveDirs=($datanodeSlaveDir $datanodeSlaveDir)
datanodeArchLogDirs=($datanodeArchLogDir $datanodeArchLogDir)
datanodeExtraConfig=none
datanodeSpecificExtraConfig=(none none)
datanodeExtraPgHba=none
datanodeSpecificExtraPgHba=(none none)
datanodeAdditionalSlaves=n
walArchive=n      <<<<<<<<<<<<<<<< 不开 wal 归档
walArchiveSet=(war1 war2)
war1_source=(master)
wal1_source=(slave)
wal1_source=(additiona_coordinator_slave_set additional_datanode_slave_set)
war1_host=node10
war1_backupdir=$HOME/pgxc/backup_war1
wal2_source=(master)
war2_host=node11
war2_backupdir=$HOME/pgxc/backup_war2

11. 初始话命令,如果前面没有任何错误,这里就是最后一步了,PGXL 会把全部的部署工作完成的。

pgxc_ctl -c pgxc_ctl.conf init all

完整输出如下:

[postgres@ip-10-21-13-109 pgxc_ctl]$ pgxc_ctl -c pgxc_ctl.conf init all
/bin/bash
Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.
Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.
Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash –home /home/postgres/pgxc_ctl –configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf
Finished reading configuration.
  ******** PGXC_CTL START ***************
Current directory: /home/postgres/pgxc_ctl
Initialize GTM master
ERROR: target directory (/home/postgres/pgxc/nodes/gtm) exists and not empty. Skip GTM initilialization
1:3768231680:2017-11-09 19:35:02.983 CST -FATAL:  lock file “gtm.pid” already exists
2:3768231680:2017-11-09 19:35:02.983 CST -HINT:  Is another GTM (PID 14635) running in data directory “/home/postgres/pgxc/nodes/gtm”?
LOCATION:  CreateLockFile, main.c:2099
waiting for server to shut down… done
server stopped
Done.
Start GTM master
server starting
Initialize all the gtm proxies.
Initializing gtm proxy gtm_pxy1.
Initializing gtm proxy gtm_pxy2.
waiting for server to shut down… done
server stopped
The files belonging to this GTM system will be owned by user “postgres”.
This user must also own the server process.
fixing permissions on existing directory /home/postgres/pgxc/nodes/gtm_pxy … ok
creating configuration files … ok
Success.
waiting for server to shut down… done
server stopped
The files belonging to this GTM system will be owned by user “postgres”.
This user must also own the server process.
fixing permissions on existing directory /home/postgres/pgxc/nodes/gtm_pxy … ok
creating configuration files … ok
Success.
Done.
Starting all the gtm proxies.
Starting gtm proxy gtm_pxy1.
Starting gtm proxy gtm_pxy2.
server starting
server starting
Done.
Initialize all the coordinator masters.
Initialize coordinator master coord1.
ERROR: target coordinator master coord1 is running now.  Skip initilialization.
Done.
Starting coordinator master.
Starting coordinator master coord1
ERROR: target coordinator master coord1 is already running now.  Skip initialization.
Done.
Initialize all the datanode masters.
Initialize the datanode master datanode1.
Initialize the datanode master datanode2.
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.
The database cluster will be initialized with locale “en_US.UTF-8”.
The default database encoding has accordingly been set to “UTF8”.
The default text search configuration will be set to “english”.
Data page checksums are disabled.
fixing permissions on existing directory /home/postgres/pgxc/nodes/dn_master … ok
creating subdirectories … ok
selecting default max_connections … 100
selecting default shared_buffers … 128MB
selecting dynamic shared memory implementation … posix
creating configuration files … ok
creating template1 database in /home/postgres/pgxc/nodes/dn_master/base/1 … ok
initializing pg_authid … ok
initializing dependencies … ok
creating system views … ok
creating cluster information … ok
loading system objects’ descriptions … ok
creating collations … ok
creating conversions … ok
creating dictionaries … ok
setting privileges on built-in objects … ok
creating information schema … ok
loading PL/pgSQL server-side language … ok
vacuuming database template1 … ok
copying template1 to template0 … ok
copying template1 to postgres … ok
syncing data to disk … ok
freezing database template0 … ok
freezing database template1 … ok
freezing database postgres … ok
WARNING: enabling “trust” authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
–auth-local and –auth-host, the next time you run initdb.
Success.
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.
The database cluster will be initialized with locale “en_US.UTF-8”.
The default database encoding has accordingly been set to “UTF8”.
The default text search configuration will be set to “english”.
Data page checksums are disabled.
fixing permissions on existing directory /home/postgres/pgxc/nodes/dn_master … ok
creating subdirectories … ok
selecting default max_connections … 100
selecting default shared_buffers … 128MB
selecting dynamic shared memory implementation … posix
creating configuration files … ok
creating template1 database in /home/postgres/pgxc/nodes/dn_master/base/1 … ok
initializing pg_authid … ok
initializing dependencies … ok
creating system views … ok
creating cluster information … ok
loading system objects’ descriptions … ok
creating collations … ok
creating conversions … ok
creating dictionaries … ok
setting privileges on built-in objects … ok
creating information schema … ok
loading PL/pgSQL server-side language … ok
vacuuming database template1 … ok
copying template1 to template0 … ok
copying template1 to postgres … ok
syncing data to disk … ok
freezing database template0 … ok
freezing database template1 … ok
freezing database postgres … ok
WARNING: enabling “trust” authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
–auth-local and –auth-host, the next time you run initdb.
Success.
Done.
Starting all the datanode masters.
Starting datanode master datanode1.
Starting datanode master datanode2.
LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory “pg_log”.
LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory “pg_log”.
Done.
ALTER NODE coord1 WITH (HOST=’10.21.13.67′, PORT=20004);
ALTER NODE
CREATE NODE datanode1 WITH (TYPE=’datanode’, HOST=’10.21.13.67′, PORT=20008, PRIMARY, PREFERRED);
ERROR:  PGXC Node datanode1: object already defined
CREATE NODE datanode2 WITH (TYPE=’datanode’, HOST=’10.21.13.60′, PORT=20008);
ERROR:  PGXC Node datanode2: object already defined
SELECT pgxc_pool_reload();
 pgxc_pool_reload
——————
 t
(1 row)
Done.
EXECUTE DIRECT ON (datanode1) ‘CREATE NODE coord1 WITH (TYPE=”coordinator”, HOST=”10.21.13.67”, PORT=20004)’;
EXECUTE DIRECT
EXECUTE DIRECT ON (datanode1) ‘ALTER NODE datanode1 WITH (TYPE=”datanode”, HOST=”10.21.13.67”, PORT=20008, PRIMARY, PREFERRED)’;
EXECUTE DIRECT
EXECUTE DIRECT ON (datanode1) ‘CREATE NODE datanode2 WITH (TYPE=”datanode”, HOST=”10.21.13.60”, PORT=20008)’;
EXECUTE DIRECT
EXECUTE DIRECT ON (datanode1) ‘SELECT pgxc_pool_reload()’;
 pgxc_pool_reload
——————
 t
(1 row)
EXECUTE DIRECT ON (datanode2) ‘CREATE NODE coord1 WITH (TYPE=”coordinator”, HOST=”10.21.13.67”, PORT=20004)’;
EXECUTE DIRECT
EXECUTE DIRECT ON (datanode2) ‘CREATE NODE datanode1 WITH (TYPE=”datanode”, HOST=”10.21.13.67”, PORT=20008, PRIMARY, PREFERRED)’;
EXECUTE DIRECT
EXECUTE DIRECT ON (datanode2) ‘ALTER NODE datanode2 WITH (TYPE=”datanode”, HOST=”10.21.13.60”, PORT=20008)’;
EXECUTE DIRECT
EXECUTE DIRECT ON (datanode2) ‘SELECT pgxc_pool_reload()’;
 pgxc_pool_reload
——————
 t
(1 row)
Done.

12. 验证可用性

在 coordinator 节点上,本地连接方式

psql -p 20004

远程用户连接方式

psql -h 10.21.13.67 -p 20004 -U postgres

postgres=# select * from pgxc_node;
node_name | node_type | node_port |  node_host  | nodeis_primary | nodeis_preferred |  node_id
———–+———–+———–+————-+—————-+——————+————
datanode1 | D        |    20008 | 10.21.13.67 | t              | t                |  888802358
datanode2 | D        |    20008 | 10.21.13.60 | f              | f                | -905831925
coord1    | C        |    20004 | 10.21.13.67 | f              | f                | 1885696643

13. 建表测试

postgres=#  create table test(id integer,name varchar(10));

postgres=#  insert into test(id,name) values(1,’test’);

postgres=#  insert into test(id,name) values(2,’test’);

查询结果

postgres=# select * from test;
id | name
—-+——
1 | test
2 | test
(2 rows)

问题汇总:

1. 如果登陆到数据库中,执行下面语句,发现只有 coord 的信息,说明并没有创建成功,

postgres=# select * from pgxc_node;
node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred |  node_id
———–+———–+———–+———–+—————-+——————+————
coord3    | C        |      20004 | localhost | f              | f          | 1638403545
(1 row)

而问题是出在 coordPgHbaEntries=(10.21.13.0/25)和 datanodePgHbaEntries=(10.21.13.0/25)的访问权限控制上,一定要和实际的子网掩码匹配, 不然各个 datanode 节点和 coord 节点间是无法正常通讯的。

由于没有注意各个细节,让这个问题困扰我一周时间。一直在找为什么 datanode 之间不能相互识别的原因,其实就是很小的一个点。

[postgres@ip-10-21-13-109 pgxc_ctl]$ ifconfig
eth0      Link encap:Ethernet  HWaddr 02:57:E7:56:4C:70
inet addr:10.21.13.109  Bcast:10.21.13.127  Mask:255.255.255.128<<<<<<10.21.13.0/25
inet6 addr: fe80::57:e7ff:fe56:4c70/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:9001  Metric:1
RX packets:29500 errors:0 dropped:0 overruns:0 frame:0
TX packets:28538 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3113614 (2.9 MiB)  TX bytes:4653853 (4.4 MiB)
Interrupt:160

2. 如果遇到建表,建库失败,也是遇到上面的访问控制权限问题。在部署完成之后,这个参数是在 /home/postgres/pgxc/nodes/dn_master/pg_hba.conf 和 /home/postgres/pgxc/nodes/coord/pg_hba.conf 中设置的。但是重新 init,会覆盖掉这些文件的。

postgres=# create database test;
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

3. 有很多人的安装方式,是最后通过手动执行下面命令,添加 coord 和 datanode,其实前面的 pgxc_ctl.conf 配置正确的话,脚本都是自动帮你完成的,没有必要人工干预

create node coord1 with (type=coordinator,host=’c6702′, port=20004);
create node coord2 with (type=coordinator,host=’c6703′, port=20005);
alter node coord3 with (type=coordinator,host=’c6704′, port=20004);
create node datanode1 with (type=datanode, host=’c6702′,port=20008);
create node datanode2 with (type=datanode, host=’c6703′,port=20008);
alter  node datanode3 with (type=datanode, host=’c6704′,port=20008);

4. 第 8 步,make PGXL 软件过程中,一定要仔细查看输出,有错误一定要纠正。不然后续会发现,有很多命令软件,都没有安装成功。

5. 如果你在远程登陆 postgres sql 遇到如下问题

[root@c6703 ~]# psql -h 10.21.13.67 -p 20004 -U postgres
psql: FATAL:  no pg_hba.conf entry for host “172.16.32.116”, user “postgres”, database “postgres”

修改 /home/postgres/pgxc/nodes/dn_master/pg_hba.conf 和 /home/postgres/pgxc/nodes/coord/pg_hba.conf 中的访问控制权允许对应的 IP 有访问权限,就可以登陆了。注意,要修改全部的 coordinator 和 datanode 的 pg_hba.conf 配置文件才行。然后通过 postgres 用户执行 reload 命令,使配置文件生效。或者重启使配置生效。

[root@c6703 ~]# psql -h 10.21.13.67 -p 20004 -U test
postgres=# SELECT pg_reload_conf();
[root@c6703 ~]# psql -h 10.21.13.67 -p 20004 -U test
Password for user test:
psql (PGXL 9.5r1.6, based on PG 9.5.8 (Postgres-XL 9.5r1.6))
Type “help” for help.

下面提供的是第 10 和 11 步的文本文件,可以下载修改并使用。可以到 Linux 公社资源站下载:

—————————————— 分割线 ——————————————

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2018 年资料 / 3 月 / 4 日 /Postgres-XL 集群搭建详细创建步骤 /

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 ——————————————

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计14831字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中