在CentOS系統上使用citus搭建分布式數據庫系統

1.概述

1.1什么是Citus

?Citus is an extension to Postgres that intelligently distributes your database & your queries across multiple nodes. However you query and however your data is organized, Citus scales your per-query performance, not just the number of queries you can run. Available as open source, as enterprise software you can run anywhere, and as a fully-managed database as a service.

?Citus是Postgres的一個擴展。Citus的優勢在于:無論你的數據和查詢怎么組織,它都可以智能的將你的數據庫以及查詢分布在多個節點上。Citus可以優化你的每個查詢的表現,不僅僅是你能運行的查詢數量。Citus是一個開源,可在任何地點運行的企業軟件,同時也是一個提供數據庫全方位管理的服務。

?All queries to a Citus database cluster are managed by the Citus coordinator node, which looks just like single-node Postgres to the application—modulo the performance benefits from scaling out. The Citus coordinator is equipped with a distributed SQL engine that transforms SQL queries & routes the transformed queries to the correct shards on the correct worker nodes. In this animation, the co-located join and update scenarios are examples of single-tenant SQL queries, hence they only need to query a subset of shards on a subset of the worker nodes. Whereas the distributed aggregate and distributed transaction scenarios need to access shards across multiple nodes.

?所有對于Citus數據庫集群的查詢都要經過Citus協調節點的管理。這個協調節點看起來就像應用層的一個單postgres節點,目的是通過縮小規模將功能模塊化。此Citus協調節點備有一個既可翻譯SQL查詢,又可將翻譯后的查詢分發到各個正確節點上對應分片的分布式SQL引擎。在這個過程中,那些級聯和更新的情況就演變為單一的SQL查詢,因此他們只需要在一個工作節點的集合中尋找某個小節點中的分片。而分布式集群和分布式事務都需要通過多節點的方式訪問分片。

1.2 Citus包括些什么

  1. coordinator node 協調節點</br>存儲元數據,不存儲實際數據。向各個工作節點發送查詢請求,并匯總結果。對于應用程序而言是服務端,對工作節點來說是客戶端。
  2. worker node 工作節點</br>存儲實際數據。執行協調節點發來的查詢請求。原則上不能作為直接為應用系統服務。但是可以直接操作工作節點上的表。

1.3 分片與副本

  • 分片(shards): 將同一個邏輯表中的數據,分別存儲到不同的物理表中。這個物理表被稱為分片。同一個工作節點可以有多個分片,甚至可以將一個邏輯表分為多個分片,并將這多個分片都存在同一工作節點上。

    分片的原則
    設計者要清除數據如何分部。數據如何劃分,那些數據應該分布式存放,那些不應該分布式存放,那些需要冗余等。
    策略:
    ?● 完備性:所有的全局數據都要映射到某個片上;
    ?● 可重構:所有的片段必須能重新構成全局數據;
    ?● 不相交:劃分的各個片段無交集。

  • 副本(placement):分片的冗余。

2搭建Citus分布式數據庫

2.1準備部分

1.使用三個計算機名節點,一個master 兩個segment

主機名稱 IP地址
master 192.168.32.144
segment1 192.168.32.145
segment2 192.168.32.146

2.搭建三個CentOS系統,并安裝PostgreSQL數據庫。修改host主機名和ip地址

3.除此之外,還要修改etc/hosts下的內容

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.32.144   master
192.168.32.145   segment1
192.168.32.146   segment2
#目的是告訴系統各自的IP叫什么主機名,如何去進行通信

2.2安裝Citus(master + segment 1 + segmengt 2)

首先將Citus源碼放入目錄,并解壓縮。通過命令進入源碼目錄

cd citus -realease-7.5/

在運行configure前,要通過pg_config獲取postgresql的環境信息,故此處配置PG_CONFIG環境變量

export PG_CONFIG = /usr/local/postgresql/bin/pg_config

運行configure命令,生成編譯安裝環境

./configure

編譯 運行

make && make install

3加載Citus插件(master + segment 1+segment 2)

Citus以插入的方式讓postgreSQL支持分布式部署,在加載Citus之前需要先配置potgreSQL并啟動,下面簡述方法。

3.1 配置PostgreSQL

添加postgres用戶,創建目錄,同時進入目錄。

useradd postgres
mkdir -p /home/postgres/pgdata/
cd /home/postgres/pgdata

初始化數據庫環境

/usr/local/postgresql/bin/initdb -D /home/postgres/pgdata

修改postgresql.conf文件

vim /home/postgres/pgdata/postgresql.conf
listen_address='*'
shared_preload_libraries = 'citus'

配置遠程訪問權限

vim /home/postgres/pgdata/pg_hba.conf
#讓所有的IP地址可信
host       all        all       0.0.0.0/0      trust

創建一個citus測試用數據庫

/usr/local/postgresql/bin/createdb testcitus

啟動postgresql數據庫

/usr/local/postgresql/bin/pg_ctl -D /home/postgres/pgdata/ -l logfile start

3.2加載citus插件(master + segment1 + segment2)

切換到root用戶目錄下

su - 
sudo -i -u postgres /usr/local/postgresql/bin -d testcitus -c "create extension citus"
#if success
CREATE EXTENSION

切換到postgres用戶

su - postgres

使用psql連接testcitus數據庫

/usr/local/postgresql/bin/psql -d testcitus

使用 \dx命令查看已加載的插件:

testcitus=# \dx
                      List of installed extensions
    Name      |    Version    |     Schema    |            Description
--------------+---------------+---------------+------------------------------
   citus      |    7.5-7      |  pg_catalog   |   Citus distriuted database
  plpgsql     |     1.0       |  pg_catalog   |  PL/pgSQL procedural language
(2 raws)                                   

4單master部署(master)

截止目前,所有的數據庫都加載了citus插件,master已經使用psql連接到本機的testcitus數據庫。在單master方式下,只有master節點可以對全局數據進行操作,包括DDL和DML語句
使用master_add_node()函數添加worker有兩個參數,第一個參數為節點名稱,第二個參數為數據庫節點監聽的端口號。

testcitus=# select * from master_add_node('pg1',5432);
testcitus=# select * from master_add_node('pg2',5432);

通過master_get_active_worker_nodes()函數可以查看加入的workers節點;

testcitus=# select * from master_get_active_worker_nodes();
  node_name  |  node_port
     gp2     |    5432
     gp1     |    5432

創建用戶數據表:

create table test(id int primary key,id2 int);

這時候創建的表只存在于master節點上,其他節點還無法進行訪問
創建表的分布,可以先使用master_create_worker_distributed_table()函數定義表,然后使用master_create_worker_shards()函數創建表,在workers節點上創建分片和副本,或者使用create_distributed_table()函數完成上面兩個函數的功能,下面介紹第一種方法。

master_create_distributed_table()函數:

參數名稱 描述
參數1 table_name 分布表的名稱
參數2 distribution_column 表中用于數據分布算法的字段名稱,通過哦給出的字段計算數據的分布
參數3 distribution_method 數據分布算法,取值:append、hash、range

master_create_worker_shards()

參數名稱 描述
參數1 table_name 表的名字
參數2 shard_count 分片的個數,每個節點可以設置多個分片
參數3 replication_factor 每個分片的副本數量

定義分布表并創建分片副片的命令如下:

select master_create_distributed_table('test','id','hash');
select master_create_worker_shards('test',2,1);

如果上述命令不能執行,就systemctl stop firewalld.service #停止firewall

執行完成后可以再master節點上查看pg_dist_partition中的信息,pg_dist_partition表中會有存放每個分布表的定義,當創建表的分片和副本后,可以通過pg_dist_shard_placement視圖查看到所有的分片信息,并可以在works節點上查看到創建的分片表。

使用這種方式部署后可以再master節點上進行表的增刪改查等操作,在workers節點是看不到創建的test表的,所以無法在workers節點查詢全局的數據,只能查詢該節點上分片表的數據,當然也可以在workers節點上對分片表中的數據進行增刪改查的訪問。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容