docker已老, 函数式运维即未来-05:Postgres-XL-10.0集群搭建

postgres-xl是个好东西?为什么呢?

postgres-xl基于postgresql数据库, postgres可以吊打很多数据库.

  1. 它可以搞oltp, 抗衡mysql, mysql没有分析函数功能
  2. 它可以搞oltp, 抗衡oracle, oracle生态弱, 没法实时
  3. 它对json支持好, 抗衡mongodb, mongodb用得人越来越少了。。。
  4. 它还恐怖地支持各种语言扩展, java, javascript, r, python, haskell。。。
  5. 并且开源免费,简单强大的没朋友。。。

postgres-xl是postgresql MPP集群版, 继续吊打大数据数据库...

  1. 它比greenpulm版本新: 它跟greenpulm本是一家人,都是MPP架构的postgresql集群
    但是原生改造, 版本基本上与postgresql一致, greenpulm的版本升不动啊。。。
  2. 它比oracle RAC/Teradata便宜,免费使用
  3. 它比hadoop省资源,没有GC,基于C语言资源利用率高,并且生态圈丰富,可视化方便
  4. 它出道早,版本稳定性强。

既然这么优秀,那么我们简单介绍一下。。。

postgres-xl分为以下组件:
a. gtm 负责全局事务
b. coordinator 处理分发执行
c. datanode 负责底层处理

datanode跟coordinator都 连接到gtm,
客户端连接到coordinator运行sql,
coordinator使用gtm进行一些事务功能分发给datanode执行

大致如此,了解得不深入,后续会深入学习,再做补充

一切就绪,现在我们开始正题,一键搭建postgres-xl集群。

项目源码:https://github.com/clojurians-org/my-env
部署脚本: run.sh.d/postgres-xl-example/{createvm.sh, package.sh, deploy.sh, start.sh}

  1. 创建虚拟机-createvm.sh
  2. 打包依赖-package.sh(打包依赖后即可离线拷贝部署)
  3. 部署集群-deploy.sh
  4. 启动集群-start.sh
  5. 测试集群

过程很简单,依次运行createvm.sh, package.sh, deploy.sh, start.sh即可

下面详细解解释一下发生了什么。

第0步, 创建虚拟机

[larluo@larluo-nixos:~/my-env]$ cat run.sh.d/postgres-xl-example/createvm.sh 
set -e
my=$(cd -P -- "$(dirname -- "${BASH_SOURCE-$0}")" > /dev/null && pwd -P) && cd $my/../..

echo -e "\n==== bash nix.sh create-vm nixos-postgres-xl-001" && bash nix.sh create-vm nixos-postgres-xl-001
echo -e "\n==== bash nix.sh create-vm nixos-postgres-xl-002" && bash nix.sh create-vm nixos-postgres-xl-002
echo -e "\n==== bash nix.sh create-vm nixos-postgres-xl-003" && bash nix.sh create-vm nixos-postgres-xl-003
image.png

第1步,打包依赖

[larluo@larluo-nixos:~/my-env]$ cat run.sh.d/postgres-xl-example/package.sh
set -e
my=$(cd -P -- "$(dirname -- "${BASH_SOURCE-$0}")" > /dev/null && pwd -P) && cd $my/../..

echo -e "\n==== bash nix.sh build nix.postgres-xl-10.0" && bash nix.sh build nix.postgres-xl-10.0

echo -e "\n==== bash nix.sh export tgz.nix-2.0.4" && bash nix.sh export tgz.nix-2.0.4
echo -e "\n==== bash nix.sh export nix.rsync-3.1.3" && bash nix.sh export nix.rsync-3.1.3
echo -e "\n==== bash nix.sh export nix.gettext-0.19.8.1" && bash nix.sh export nix.gettext-0.19.8.1

rsync是可选的(避免脚本多次运行scp多次),
这里的gettext依赖是为了启动脚本中使用envsubst命令替换环境变量

这里由于nix官方还没有支持postgres-xl,并且postgres-xl对系统底层库有依赖,我们不想使用root权限安装污染系统,所以自己编写nix构建脚本自动打包依赖

[larluo@larluo-nixos:~/my-env]$ cat nix.conf/postgres-xl-10.0/default.nix
{ fetchgit, stdenv, readline, zlib, perl, bison, flex, ... }:
stdenv.mkDerivation rec {
  name = "postgres-xl-${version}" ;
  version = "10.0" ;
  rev = "0e7174157b7762959089ab0dd508237679a301c8" ;

  buildInputs = [ readline zlib perl bison flex ];

  src = fetchgit {
    url= "git://git.postgresql.org/git/postgres-xl.git" ;
    rev = rev ;
    sha256 = "0nv8s4wyswfikb6pbmj9mq6qr8j7vc1d9nhkr6jmnymfkx4rwjxk" ;
  } ;
}
image.png

第2步,部署依赖

[larluo@larluo-nixos:~/my-env]$ cat run.sh.d/postgres-xl-example/deploy.sh
set -e
my=$(cd -P -- "$(dirname -- "${BASH_SOURCE-$0}")" > /dev/null && pwd -P) && cd $my/../..

echo -e "\n==== bash nix.sh create-user 192.168.56.101" && bash nix.sh create-user 192.168.56.101
echo -e "\n==== bash nix.sh create-user 192.168.56.102" && bash nix.sh create-user 192.168.56.102
echo -e "\n==== bash nix.sh create-user 192.168.56.103" && bash nix.sh create-user 192.168.56.103

echo -e "\n==== bash nix.sh install 192.168.56.101 tgz.nix-2.0.4" && bash nix.sh install 192.168.56.101 tgz.nix-2.0.4
echo -e "\n==== bash nix.sh install 192.168.56.102 tgz.nix-2.0.4" && bash nix.sh install 192.168.56.102 tgz.nix-2.0.4
echo -e "\n==== bash nix.sh install 192.168.56.103 tgz.nix-2.0.4" && bash nix.sh install 192.168.56.103 tgz.nix-2.0.4

echo -e "\n==== bash nix.sh install 192.168.56.101 nix.rsync-3.1.3" && bash nix.sh install 192.168.56.101 nix.rsync-3.1.3
echo -e "\n==== bash nix.sh install 192.168.56.102 nix.rsync-3.1.3" && bash nix.sh install 192.168.56.102 nix.rsync-3.1.3
echo -e "\n==== bash nix.sh install 192.168.56.103 nix.rsync-3.1.3" && bash nix.sh install 192.168.56.103 nix.rsync-3.1.3

echo -e "\n==== bash nix.sh install 192.168.56.101 nix.gettext-0.19.8.1" && bash nix.sh install 192.168.56.101 nix.gettext-0.19.8.1
echo -e "\n==== bash nix.sh install 192.168.56.102 nix.gettext-0.19.8.1" && bash nix.sh install 192.168.56.102 nix.gettext-0.19.8.1
echo -e "\n==== bash nix.sh install 192.168.56.103 nix.gettext-0.19.8.1" && bash nix.sh install 192.168.56.103 nix.gettext-0.19.8.1

echo -e "\n==== bash nix.sh install 192.168.56.101 nix.postgres-xl-10.0" && bash nix.sh install 192.168.56.101 nix.postgres-xl-10.0
echo -e "\n==== bash nix.sh install 192.168.56.102 nix.postgres-xl-10.0" && bash nix.sh install 192.168.56.102 nix.postgres-xl-10.0
echo -e "\n==== bash nix.sh install 192.168.56.103 nix.postgres-xl-10.0" && bash nix.sh install 192.168.56.103 nix.postgres-xl-10.0

首先创建用户,接着安装nix, 然后安装rsync跟gettext工具,最后部署postgres-xl


image.png

第3步: 启动集群

[larluo@larluo-nixos:~/my-env]$ cat run.sh.d/postgres-xl-example/start.sh
my=$(cd -P -- "$(dirname -- "${BASH_SOURCE-$0}")" > /dev/null && pwd -P) && cd $my/../..

echo -e "\n==== bash nix.sh start 192.168.56.101:6666 postgres-xl-10.0:gtm"
                bash nix.sh start 192.168.56.101:6666 postgres-xl-10.0:gtm
export GTM="192.168.56.101:6666"

export DATANODES="192.168.56.101:15432,192.168.56.102:15432,192.168.56.103:15432"
export COORDINATORS="192.168.56.101:5432,192.168.56.102:5432,192.168.56.103:5432"
echo -e "\n==== bash nix.sh start 192.168.56.101:15432 postgres-xl-10.0:datanode --gtm ${GTM}"
                bash nix.sh start 192.168.56.101:15432 postgres-xl-10.0:datanode --gtm ${GTM}
echo -e "\n==== bash nix.sh start 192.168.56.102:15432 postgres-xl-10.0:datanode --gtm ${GTM}"
                bash nix.sh start 192.168.56.102:15432 postgres-xl-10.0:datanode --gtm ${GTM}
echo -e "\n==== bash nix.sh start 192.168.56.103:15432 postgres-xl-10.0:datanode --gtm ${GTM}"
                bash nix.sh start 192.168.56.103:15432 postgres-xl-10.0:datanode --gtm ${GTM}
echo -e "\n==== bash nix.sh start 192.168.56.101:5432 postgres-xl-10.0:coordinator --gtm ${GTM}"
                bash nix.sh start 192.168.56.101:5432 postgres-xl-10.0:coordinator --gtm ${GTM}
echo -e "\n==== bash nix.sh start 192.168.56.102:5432 postgres-xl-10.0:coordinator --gtm ${GTM}"
                bash nix.sh start 192.168.56.102:5432 postgres-xl-10.0:coordinator --gtm ${GTM}
echo -e "\n==== bash nix.sh start 192.168.56.103:5432 postgres-xl-10.0:coordinator --gtm ${GTM}"
                bash nix.sh start 192.168.56.103:5432 postgres-xl-10.0:coordinator --gtm ${GTM}

echo -e "\n==== sleep 10" && sleep 10

echo -e "\n==== bash nix.sh start 192.168.56.101:15432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}"
                bash nix.sh start 192.168.56.101:15432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}
echo -e "\n==== bash nix.sh start 192.168.56.102:15432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}"
                bash nix.sh start 192.168.56.102:15432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}
echo -e "\n==== bash nix.sh start 192.168.56.103:15432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}"
                bash nix.sh start 192.168.56.103:15432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}
echo -e "\n==== bash nix.sh start 192.168.56.101:5432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}"
                bash nix.sh start 192.168.56.101:5432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}
echo -e "\n==== bash nix.sh start 192.168.56.102:5432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}"
                bash nix.sh start 192.168.56.102:5432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}
echo -e "\n==== bash nix.sh start 192.168.56.103:5432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}"
                bash nix.sh start 192.168.56.103:5432 postgres-xl-10.0:_connector --coordinators ${COORDINATORS} --datanodes ${DATANODES}

这里首先启动gtm,接着指定gtm参数启动datanode跟coordinator.
最后,对于每个datanode及coordinator与其它datanode及coordinator进行连接


image.png

第4步: 测试集群


postgres=# select * from pgxc_node;
            node_name            | node_type | node_port |   node_host    | nodeis_primary | nodeis_preferred |   node_id   
---------------------------------+-----------+-----------+----------------+----------------+------------------+-------------
 coordinator_192_168_56_102_5432 | C         |      5432 | 192.168.56.102 | f              | f                | -1979140469
 coordinator_192_168_56_103_5432 | C         |      5432 | 192.168.56.103 | f              | f                |   580660846
 datanode_192_168_56_101_15432   | D         |     15432 | 192.168.56.101 | f              | f                |  1317903039
 datanode_192_168_56_102_15432   | D         |     15432 | 192.168.56.102 | f              | f                |  -102979159
 datanode_192_168_56_103_15432   | D         |     15432 | 192.168.56.103 | f              | f                |  -556920164
 coordinator_192_168_56_101_5432 | C         |      5432 | 192.168.56.101 | f              | f                |  -307067894
(6 rows)

postgres=# create table test1(id int,name text);
CREATE TABLE
postgres=# insert into test1(id,name) select generate_series(1,8),'测试';
INSERT 0 8
postgres=# SELECT xc_node_id, count(*) FROM test1 group by xc_node_id ;
 xc_node_id | count 
------------+-------
 -556920164 |     3
 -102979159 |     3
 1317903039 |     2
(3 rows)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335