citus 之一 setup

本文详细介绍了在Ubuntu16.04环境下,利用Citus扩展为PostgreSQL9.6实现水平数据分片和复制的过程。Citus通过将数据和查询分布到多个机器上的集群中,使PostgreSQL能够横向扩展,提供快速响应大型数据集的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

os: ubuntu 16.04
postgresql: 9.6.8
citus: postgresql-9.6-citus 8.0.0

What is Citus?
Citus is basically worry-free Postgres that is built to scale out. It’s an extension to Postgres that distributes data and queries in a cluster of multiple machines. As an extension (rather than a fork), Citus supports new PostgreSQL releases, allowing users to benefit from new features while maintaining compatibility with existing PostgreSQL tools.

Citus horizontally scales PostgreSQL across multiple machines using sharding and replication. Its query engine parallelizes incoming SQL queries across these servers to enable human real-time (less than a second) responses on large datasets.

Available in Three Ways:

As open source to add to existing Postgres servers
On-premise with additional enterprise grade security and cluster management tools
As a fully-managed database as a service, called Citus Cloud

citus 是 postgresql 一种分库分表解决方案。是作为 extension使用的。

数据库的实例分为两类:

coordinator 节点: 存储数据库分表的元数据,不存储实际的数据。
worker 节点: 真实存储数据,是shard节点。

用户只连接 coordinator 节点,由 coordinator 解析sql,发给 worker节点执行,执行结果再返回给 coordinator 节点。

ip规划如下:

192.168.0.92 pgsql1 --coordinator 节点

192.168.0.90 pgsql2 --worker 节点
192.168.0.88 pgsql3 --worker 节点

生产环境一定要创建各个节点的salve节点,必须创建。

安装好 postgresql 9.6

剩下的参考其它blog

# vi /etc/hosts
192.168.0.92 pgsql1

192.168.0.90 pgsql2
192.168.0.88 pgsql3

下载安装 citus

所有节点都需要操作
使用 pgdg 源

# vi /etc/apt/sources.list.d/pgdg.list
deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main

# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# apt-get update
# apt -y install postgresql-9.6-citus

或者使用 citus 源

# curl https://install.citusdata.com/community/deb.sh | sudo bash
# apt update
# apt -y install postgresql-9.6-citus-8.0

列出一些相关的文件及及文件

# dpkg -L postgresql-9.6-citus

/usr/include/postgresql/9.6/server
/usr/include/postgresql/9.6/server/citus_version.h
/usr/include/postgresql/9.6/server/distributed/*

/usr/share/postgresql/9.6
/usr/share/postgresql/9.6/extension
/usr/share/postgresql/9.6/extension/citus*.sql
/usr/share/postgresql/9.6/extension/citus.control

/usr/lib/postgresql/9.6/lib/citus.so

查看 postgres 用户环境变量
所有节点都需要操作

# su - postgres
$ cat ~/.profile

调整参数

所有节点都需要操作

# vi /etc/postgresql/9.6/main/postgresql.conf
shared_preload_libraries = 'citus,pg_stat_statements'

# /etc/init.d/postgresql restart

配置 citus

所有节点都需要操作

# su - postgres
postgres@pgsql1:~$ psql
psql (9.6.8)
Type "help" for help.
postgres=# select * from pg_available_extensions where name like '%citus%';
 name  | default_version | installed_version |          comment           
-------+-----------------+-------------------+----------------------------
 citus | 8.0-8           |                   | Citus distributed database
(1 row)

由于 extension 是针对 database 级别的,所以需要先创建指定的业务数据库和 extension。
所有节点都需要操作

$ psql  
psql (9.6.8)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=# create user cituser with superuser; 
postgres=# alter user cituser with password 'citusercituser';
postgres=# create database citusdb with owner = cituser;
postgres=# \q

$ psql -h 127.0.0.1 -U cituser citusdb -c "create extension citus;"

citusdb=# \dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description          
---------+---------+------------+------------------------------
 citus   | 8.0-8   | pg_catalog | Citus distributed database
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(2 rows)

citusdb=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 citus  | postgres
 public | postgres
(2 rows)

配置 pg_hba.conf
所有节点都需要操作

$ vi /etc/postgresql/9.6/main/pg_hba.conf
host    all             cituser         192.168.0.92/32            trust
host    all             cituser         192.168.0.90/32            trust
host    all             cituser         192.168.0.88/32            trust

$ psql -c "select pg_reload_conf();"

注意,pgsql2、pgsql3 是限定用户访问的,用户只能连接 pgsql1 。

plsql1节点上添加 work节点


$ psql -h 192.168.0.92 -U cituser citusdb
Password for user cituser: 
psql (9.6.8)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

citusdb=# select * from master_add_node('192.168.0.90',5432);
citusdb=# select * from master_add_node('192.168.0.88',5432);

citusdb=# select * from master_get_active_worker_nodes();
  node_name   | node_port 
--------------+-----------
 192.168.0.88 |      5432
 192.168.0.90 |      5432
(2 rows)

安装结束,下一篇blog介绍下如何创建表。

参考:
https://www.citusdata.com/
https://docs.citusdata.com/en/v8.0/
https://docs.citusdata.com/en/stable/index.html

https://github.com/citusdata/citus

文章目录概况安装PostgreSQL设置主节点设置从节点验证故障处理,主从节点切换从节点扩容概况CentOS Linux release 7.7.1908PostgreSQL13三台服务器,一主两从,实时复制。主节点读写,从节点只读,读写分离不借助插件或第三方中间件,仅使用PostgreSQL自带的流复制功能Replication主节点可建库建表、可读写,两个从节点为只读,程序使用时可以将查询和统计相关服务读取从节点,通过读写分离减少数据库读写压力设置同步复制,为热备份,数据实时同步,对于大多数业务来说,可以认为三个库数据完全相同,当一个库故障时,其他库仍然可提供服务,增加服务可用性当主节点故障时,数据无法更新,只能手动将从节点升格为主节点,需要修改服务的数据库连接配置等,服务短暂不可用(也可提前准备好监控和切换脚本,服务不可用时尽早发现和切换)。需要根据自己业务特点,是否能接受此种情况,来决定是否使用此方式。单点故障,自动切换的集群,可以考虑使用PGPool-II 等第三方中间件搭建,后面研究好我会出一篇文档安装PostgreSQL命令如下,具体可参考我之前的博客《PostgreSQL简介和安装部署》# 安装 repository 源:sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm# 安装 PostgreSQL13:sudo yum install -y postgresql13-server# 初始化数据库,并设置启动命令和开机启动:sudo /usr/pgsql-13/bin/postgresql-13-setup initdbsudo systemctl enable postgresql-13sudo systemctl start postgresql-13# 另外,查看状态,关闭命名和重启命令sudo systemctl status postgresql-13sudo systemctl stop postgresql-13sudo systemctl restart postgresql-13123456789101112131415三个节点安装完成后,就可以开始使用了可以创建用户、赋予权限、修改密码,可以建库建表早一些功能测试等默认安装创建的数据库,没有主从之分,都是主节点,可以使用这条语句验证下是否为从节点,查询结果为"f"表示false,当前数据库节点为主库节点sudo -u postgres /usr/pgsql-13/bin/psql -c "select pg_is_in_recovery()"1下面开始搭建集群,关闭三个PostgreSQL服务,开始改配置文件设置主节点主从节点可以自己规划好,准备好不同机器就行,也可以使用最小集群一主一从主要是修改2个配置文件,都在data目录下,一个是数据库基础配置,一个是访问权限控制默认初始化的数据库,data目录在/var/lib/pgsql/13/data修改文件夹里面的pg_hba.conf,设置 Replication 访问策略,允许流复制# replication privilege.# 追加一条“允许postgres用户,通过全部网络地址使用 Replication ”的策略host replication postgres 0.0.0.0/0 trust123继续修改数据库配置文件postgres.conf,进行一些参数设置,允许流复制# -----------------------------# PostgreSQL configuration file# -----------------------------# - Connection Settings -listen_addresses = '*' # what IP address(es) to listen on;#port = 5432 # (change requires restart)max_connections = 200 # (change requires restart)# - Authentication -#authentication_timeout = 1min # 1s-600spassword_encryption = scram-sha-256 # md5 or scram-sha-256#------------------------------------------------------------------------------# RESOURCE USAGE (except WAL)#------------------------------------------------------------------------------# - Memory -shared_buffers = 1GB # min 128kB 官方推荐内存设置带大小为系统内存的1/4temp_buffers = 64MB # min 800kBwork_mem = 64MB # min 64kBmax_stack_depth = 4MB # min 100kBdynamic_shared_memory_type = posix # the default is the first option#------------------------------------------------------------------------------# WRITE-AHEAD LOG#------------------------------------------------------------------------------wal_level = replica # minimal, replica, or logicalfsync = on # flush data to disk for crash safetysynchronous_commit = on # synchronization level;wal_log_hints = on # also do full page writes of non-critical updates# - Checkpoints -checkpoint_timeout = 10min # range 30s-1dmax_wal_size = 1GBmin_wal_size = 80MB# - Archiving -archive_mode = on # enables archiving; off, on, or always# (change requires restart)archive_command = 'cp %p /data/postgresql/archive/%f' # command to use to archive a logfile segment#-----------------------------------------------------------------------------# REPLICATION#------------------------------------------------------------------------------max_wal_senders = 10 # max number of walsender processes 设置了可以最多有几个流复制的链接# (change requires restart)wal_keep_size = 1024 # in megabytes; 0 disablesmax_slot_wal_keep_size = 10 # in megabytes; -1 disableswal_sender_timeout = 120s # in milliseconds; 0 disables# - Standby Servers -hot_standby = on # "off" disallows queries during recovery#------------------------------------------------------------------------------# REPORTING AND LOGGING#------------------------------------------------------------------------------# - Where to Log -log_destination = 'stderr' # Valid values are combinations oflogging_collector = on # Enable capturing of stderr and csvloglog_directory = 'log' # directory where log files are written,# can be absolute or relative to PGDATAlog_filename = 'postgresql-%a.log' # log file name pattern,log_truncate_on_rotation = on # If on, an existing log file with thelog_rotation_age = 1d # Automatic rotation of logfiles willlog_rotation_size = 50MB # Automatic rotation of logfiles willlog_min_duration_statement = 2000 # 配置数据库慢查询时间,如果超过配置的时间,就会将查询的SQL语句记录到日志# - What to Log -log_connections = onlog_disconnections = onlog_line_prefix = '%m [%p] ' # special values:log_timezone = 'Asia/Shanghai'datestyle = 'iso, mdy'#intervalstyle = 'postgres'timezone = 'Asia/Shanghai'# These settings are initialized by initdb, but they can be changed.lc_messages = 'en_US.UTF-8' # locale for system error message# stringslc_monetary = 'en_US.UTF-8' # locale for monetary formattinglc_numeric = 'en_US.UTF-8' # locale for number formattinglc_time = 'en_US.UTF-8' # locale for time formatting# default configuration for text searchdefault_text_search_config = 'pg_catalog.english'shared_preload_libraries = '' # citus (change requires restart)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586启动服务,使配置生效,主节点设置完毕sudo systemctl start postgresql-13# 或sudo systemctl restart postgresql-13123设置从节点主从要保持一致,主要是 data 保持一致,我们可以直接将主节点data同步到从节点如果从节点没有关闭,可以先关闭 sudo systemctl stop postgresql-13去默认的data位置,删除所有文件
03-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据库人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值