citus的分布式集群目前在蘇寧大規(guī)模應用,蘇寧陳華軍也做了很多技術分享和博客介紹。目前所有的教程都是rpm和pg一起安裝,個人不喜歡,畢竟citus定位是個插件,我想在我已安裝的pg上源碼裝一個citus比較好。
一 集群規(guī)劃
| 主機名 | IP | 角色 | 端口 |
|---|---|---|---|
| coordinate | 192.168.0.55 | coordinate | 5432 |
| worker1 | 192.168.0.65 | worker | 5432 |
| worker2 | 192.168.0.66 | worker | 5432 |
| worker3 | 192.168.0.67 | worker | 5432 |
| worker4 | 192.168.0.68 | worker | 5432 |
coordinate:協(xié)調節(jié)點,一般稱為cn,存儲所有元數(shù)據(jù),不存實際數(shù)據(jù),該節(jié)點直接對用戶開放,等于一個客戶端。
worker:工作節(jié)點,不存儲元數(shù)據(jù),存儲實際數(shù)據(jù)。執(zhí)行協(xié)調節(jié)點發(fā)來的查詢請求。一般不直接對用戶開放。
二 安裝步驟
2.1 安裝pg
在每個主機節(jié)點上都安裝postgresql11。
詳細參考: Centos7安裝PostgreSQL
其中,coordinate節(jié)點的pg_hba.conf配置:
# IPv4 local connections:
host all all 0.0.0.0/0 md5
worker節(jié)點的pg_hba.conf配置:
# IPv4 local connections:
host all all 192.168.0.0/24 trust
2.2 安裝citus
在每個節(jié)點上都安裝citus。
從github上下載源碼:https://github.com/citusdata/citus/releases

[root@localhost opt]# wget https://github.com/citusdata/citus/archive/v8.1.1.tar.gz
[root@localhost opt]# tar -zxvf v8.1.1.tar.gz
[root@localhost opt]# cd citus8.1.1
#先安裝curl依賴
[root@localhost citus-8.1.1]# yum install curl
[root@localhost citus-8.1.1]# yum install curl-devel
# 導入pg的環(huán)境變量
[root@localhost citus-8.1.1]# source /home/postgres/.bashrc
# 安裝
[root@localhost citus-8.1.1]# ./configure
[root@localhost citus-8.1.1]# make
[root@localhost citus-8.1.1]# make install
三 集群配置
3.1 創(chuàng)建測試數(shù)據(jù)庫
在所有節(jié)點執(zhí)行以下語句:
[postgres@localhost ~]$ psql
psql (11.1)
Type "help" for help.
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create extension citus;
3.2 協(xié)調節(jié)點新增工作節(jié)點
以后的管理操作僅僅在協(xié)調節(jié)點(cn)上操作
[postgres@localhost ~]$ psql -d test
psql (11.1)
Type "help" for help.
# 節(jié)點可以是ip或者dns name
test=# SELECT * from master_add_node('worker1', 5432);
test=# SELECT * from master_add_node('worker2', 5432);
test=# SELECT * from master_add_node('worker3', 5432);
test=# SELECT * from master_add_node('worker4', 5432);
#查看工作節(jié)點:
test=# SELECT * FROM master_get_active_worker_nodes();
node_name | node_port
-----------+-----------
worker2 | 5432
worker4 | 5432
worker1 | 5432
worker3 | 5432
(4 rows)
3.3 創(chuàng)建分片表
test=# create table shiptrack(gid serial primary key,tracktime timestamp without time zone,geom geometry(Point,4326));
#舊版的master_create_distributed_table,master_create_worker_shards都已被廢棄,現(xiàn)在由create_distributed_table替換。
#配置分片策略
#設置分片數(shù),4個主機,設置分片4,每個主機一張表
test=# set citus.shard_count=4;
# 配置副本數(shù)
test=# set citus.shard_replication_factor=2;
test=# SELECT create_distributed_table('shiptrack', 'gid', 'hash');
# 查看分片分布
test=# SELECT * from pg_dist_shard_placement order by shardid, placementid;
shardid | shardstate | shardlength | nodename | nodeport | placementid
---------+------------+-------------+----------+----------+-------------
102016 | 1 | 0 | worker1 | 5432 | 25
102016 | 1 | 0 | worker2 | 5432 | 26
102017 | 1 | 0 | worker2 | 5432 | 27
102017 | 1 | 0 | worker3 | 5432 | 28
102018 | 1 | 0 | worker3 | 5432 | 29
102018 | 1 | 0 | worker4 | 5432 | 30
102019 | 1 | 0 | worker4 | 5432 | 31
102019 | 1 | 0 | worker1 | 5432 | 32
有4個worker,所以數(shù)據(jù)分片為4,每個分片,做兩個副本。通過分片分布,如102016分布在worker1,worker2上,同理102017分布在worker2,worker3上。
假設worker1機器宕機了,集群訪問102016原先是方位worker1的,現(xiàn)在會自動訪問worker2上的102016分片。 也就是說,單個數(shù)據(jù)節(jié)點故障,集群還能正常用,通過多設置副本,多個節(jié)點故障也能更強壯。
四 citus常用命令
4.1 集群創(chuàng)建function,role,extension命令
SELECT run_command_on_workers($cmd$
/* the command to run */
drop extension pg_pathman;
$cmd$);