阿里云 ECS
机器节点
- 这里我们使用两台同一区域的 ECS 机器。
- 机器配置:2 核 2 G。(ps: 阿里云 99 元一年的活动)
- 一台安装
coordinator
(协调器),这里内网 IP 为172.18.60.11
- 一台安装
worker
,这里内网 IP 为172.18.60.12
操作系统
两台机器分别安装了厂商的 Alibaba Cloud Linux 3
系统。
sh
lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: AlibabaCloud
Description: Alibaba Cloud Linux release 3 (Soaring Falcon)
Release: 3
Codename: SoaringFalcon
安装 Docker
在各机器节点上分别执行:
- 添加 docker-ce 的 dnf 源
sh
sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- 安装 Alibaba Cloud Linux 3 专用的 dnf 源兼容插件
sh
sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus
- 安装 Docker
sh
sudo dnf -y install docker-ce --nobest
- 启动 Docker 服务,并设置开机自启动
sh
sudo systemctl start docker
sudo systemctl enable docker
-
查看Docker是否启动
sudo systemctl status docker
安全组
注意:我们接下来会使用机器的 5432
端口。
为了安全,云厂商默认是屏蔽公网访问该端口的,用户同一区域的内网机器之间的访问默认是可以的。
Citus 12.1 集群搭建
这里使用官方构建的镜像 citusdata/citus:12.1
。
启动 Coordinator 节点
- 172.18.60.11(
coordinator
) 机器节点
sh
mkdir -p citus-coordinator/data
docker run -d --name citus-coordinator \
-v $(pwd)/citus-coordinator/data:/var/lib/postgresql/data \
-p 5432:5432 \
-e POSTGRES_USER=citus \
-e POSTGRES_PASSWORD=citus \
-e PGUSER=citus \
-e PGPASSWORD=citus \
-e POSTGRES_HOST_AUTH_METHOD=trust \
citusdata/citus:12.1
注意:生产环境,为了安全请另行编辑 pg_hba.conf
。
启动 Worker 节点
- 172.18.60.12(
worker
) 机器节点
sh
mkdir -p citus-worker00/data
docker run -d --name citus-worker00 \
-v $(pwd)/citus-worker00/data:/var/lib/postgresql/data \
-p 5432:5432 \
-e POSTGRES_USER=citus \
-e POSTGRES_PASSWORD=citus \
-e PGUSER=citus \
-e PGPASSWORD=citus \
-e POSTGRES_HOST_AUTH_METHOD=trust \
citusdata/citus:12.1
集群设置
- 172.18.60.11(
coordinator
) 机器节点
sh
docker exec -it citus-coordinator psql -U citus
# 设置 worker 要连接的协调器节点
SELECT citus_set_coordinator_host('172.18.60.11', 5432);
# 添加 worker 节点
SELECT * from citus_add_node('172.18.60.12', 5432);
SELECT * FROM citus_get_active_worker_nodes();
node_name | node_port
---------------+-----------
172.18.60.12 | 5432
(1 row)
验证: 官方案例(微服务存储后端)
创建 user
服务的数据库 schema
sh
docker exec -it citus-coordinator psql -U citus
CREATE USER user_service;
SET citus.enable_schema_based_sharding TO ON;
CREATE SCHEMA AUTHORIZATION user_service;
select * from citus_schemas;
\c citus user_service
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
Docker 启动 user
服务
sh
docker run -d --name usersvc \
-p 6000:5000 \
-e DB_HOST='172.18.60.11' \
-e DB_NAME=citus \
registry.cn-heyuan.aliyuncs.com/hacker-linner/citus-microsvc-user:1.0.1
创建一些用户
sh
curl -X POST -H "Content-Type: application/json" -d '[
{"name": "John Doe", "email": "john@example.com"},
{"name": "Jane Smith", "email": "jane@example.com"},
{"name": "Mike Johnson", "email": "mike@example.com"},
{"name": "Emily Davis", "email": "emily@example.com"},
{"name": "David Wilson", "email": "david@example.com"},
{"name": "Sarah Thompson", "email": "sarah@example.com"},
{"name": "Alex Miller", "email": "alex@example.com"},
{"name": "Olivia Anderson", "email": "olivia@example.com"},
{"name": "Daniel Martin", "email": "daniel@example.com"},
{"name": "Sophia White", "email": "sophia@example.com"}
]' http://localhost:6000/users
{"message":"Users created successfully","user_ids":[1,2,3,4,5,6,7,8,9,10]}
列出已创建的用户
sh
curl http://localhost:6000/users
验证数据是否被分配到 worker 节点
sh
docker exec -it citus-coordinator psql -U citus
select nodename,nodeport, table_name, pg_size_pretty(sum(shard_size))
from citus_shards
group by nodename,nodeport, table_name;
nodename | nodeport | table_name | pg_size_pretty
---------------+----------+--------------------+----------------
172.18.60.12 | 5432 | user_service.users | 32 kB
(1 row)