Linux安装citus(离线、shell)
目录
需求:需要在用户方面离线部署citus集群,
操作流程大致如下:
一、背景
centos7.4系统
一共6台4U机器
二、有网络~搭建本地yum源:
搭建本地yum源:
官网参考地址:
https://docs.citusdata.com/en/v7.4/installation/multi_machine_rhel.html
需要先在有网的环境开启缓存:
#cache ,系统默认是关闭的
#配置文件在: vim /etc/yum.conf ,修改
keepcache = 1
gpgcheck = 0
#cache本地缓存目录: /var/cache/yum
[root@localhost ~]# mount -o loop -t iso9660 CentOS-7-x86_64-DVD-1708.iso /mnt/
在有网环境安装完后,拷贝走/var/cache/yum目录,放到离线环境即可。
find / -type f -name *.rpm -exec cp {} /mnt/Packages/ \;
有网环境安装如下:
- #1. Add repository
curl https://install.citusdata.com/community/rpm.sh | sudo bash
- #2. Install PostgreSQL + Citus and initialize a database
sudo yum install -y citus74_10
sudo service postgresql-10 initdb || sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
echo "shared_preload_libraries = 'citus'" | sudo tee -a /var/lib/pgsql/10/data/postgresql.conf
- #3. Configure connection and authentication
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /var/lib/pgsql/10/data/postgresql.conf
sed -i "s/127.0.0.1\/32 ident/0.0.0.0\/0 trust/g" /var/lib/pgsql/10/data/pg_hba.conf
- #4. Start database servers, create Citus extension
sudo service postgresql-10 restart
sudo chkconfig postgresql-10 on
sudo -i -u postgres psql -c "CREATE EXTENSION citus;"
三、离线安装
- 修改主机名 /etc/hostname
IP | hostname |
---|---|
172.16.100.242 | master |
172.16.100.243 | worker1 |
172.16.100.244 | worker2 |
172.16.100.123 | worker3 |
172.16.100.134 | worker4 |
172.16.100.135 | worker5 |
总结下步骤
先
挂载CentOS.7.4.1708.iso
在有网环境下安装好citus的安装包,搭建好本地yum源,最后形成一个yum-iso-new这个zip包
3、下载yum-iso(5.1G)
链接:https://pan.baidu.com/s/1UTfeXmUSpgJk31Hxyvg1aA 密码:33lp
服务器运行环境
前提:
sed -i 's/SELINUX=enforce/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
find /etc/yum.repos.d/ -type f |xargs sed -i "s/gpgcheck=1/gpgcheck=0/g"
systemctl stop firewalld.service
systemctl disable firewalld.service
init 6
3.1、安装
每个节点都执行一遍
先:拷贝yum-iso-new到目标机器的 /home/pccuser ,或者拷贝到一台,再scp。
#!/bin/bash
dir="/root/yum-iso-new"
if [ ! -d ${dir} ];then
/usr/bin/mkdir ${dir}
else
cd ${dir}
fi
mv /home/pccuser/yum-iso-new /root/
/usr/bin/mkdir /etc/yum.repos.d/bak
/usr/bin/mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/
/usr/bin/echo -e "[yum-local]\nname=local\nbaseurl=file:///root/yum-iso-new/\ngpgcheck=0\nenabled=1" > /etc/yum.repos.d/CentOS-Media.repo
/usr/bin/rpm -ivh /root/yum-iso-new/*.rpm --nodeps --force
sudo service postgresql-10 initdb || sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
echo "shared_preload_libraries = 'citus'" | sudo tee -a /var/lib/pgsql/10/data/postgresql.conf
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /var/lib/pgsql/10/data/postgresql.conf
sed -i "s/127.0.0.1\/32 ident/0.0.0.0\/0 trust/g" /var/lib/pgsql/10/data/pg_hba.conf
sudo service postgresql-10 restart
sudo chkconfig postgresql-10 on
sudo -i -u postgres psql -c "CREATE EXTENSION citus;"
sudo -i -u postgres psql -c "CREATE EXTENSION postgis;"
sudo -i -u postgres psql -c "alter user postgres with password 'postgres';"
在每个节点都执行完上述shell命令后,再往下走:
3.2、添加节点
- 添加节点
sudo -i -u postgres psql -c "SELECT * from master_add_node('worker1', 5432);"
sudo -i -u postgres psql -c "SELECT * from master_add_node('worker2', 5432);"
sudo -i -u postgres psql -c "SELECT * from master_add_node('worker3', 5432);"
sudo -i -u postgres psql -c "SELECT * from master_add_node('worker4', 5432);"
sudo -i -u postgres psql -c "SELECT * from master_add_node('worker5', 5432);"
- 查看所有节点
sudo -i -u postgres psql -c "SELECT * FROM master_get_active_worker_nodes();"
– 删除节点
sudo -i -u postgres psql -c "SELECT * from select master_remove_node('主机名或IP',5432);"
如果是在其他库:
一、在每个节点上都执行
create database test;
\c test
CREATE EXTENSION citus;
CREATE EXTENSION postgis;
二、只在主上执行:
SELECT * from master_add_node('worker1', 5432);
SELECT * from master_add_node('worker2', 5432);
SELECT * from master_add_node('worker3', 5432);
SELECT * from master_add_node('worker4', 5432);
SELECT * from master_add_node('worker5', 5432);
三、只在主上执行
1.建表
2.建立集群触发器
CREATE TRIGGER "truncate_trigger_58860" BEFORE TRUNCATE ON "public"."data_3"
FOR EACH STATEMENT
EXECUTE PROCEDURE "citus_truncate_trigger"();
3.创建广播模式
SELECT create_reference_table('table_name');
4.插入数据
5.测试postgis函数
select st_union(geom) from data_4 where gid in (1,2);
3.3、建表测试
有2种方式
- 1、分片式
- 2、广播式
3.3.1、分布式
- 先登录
sudo -i -u postgres psql
截图如下:
– 建序列
CREATE SEQUENCE public.isla_objectid_seq
INCREMENT 1
START 1
MINVALUE 1
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.isla_objectid_seq OWNER TO postgres;
- 建表
CREATE TABLE "public"."test_xiao" (
"gid" int4 NOT NULL DEFAULT nextval('isla_objectid_seq'::regclass),
"cname" varchar(160) COLLATE "pg_catalog"."default",
"geom" "public"."geometry",
CONSTRAINT "test_mian_pkey" PRIMARY KEY ("gid")
)
;
CREATE INDEX "xiao_idx" ON "public"."test_xiao" USING gist (
"geom" "public"."gist_geometry_ops_2d"
);
- 分发
SELECT create_distributed_table('test_xiao','gid');
截图如下:
- 确认表是否分发到了所有子节点上
这是Master上的:
这是worker1节点上的:
可以用同样的语句,查看worker2、worker3、4、5节点上的东西。
可以看出 并不是每个节点上的数据都相同,是散列分布的。
分布式Success!
分片式的缺点:
不支持聚合函数:
调用st_extent()函数的时候报错,截图如下:
解决:
采用3.3.2的广播式存储
3.3.2、采用广播式:
大致流程跟分布式一样,只有选择存储方式时,调用的函数不一样:
1.建表 data_3
DROP TABLE IF EXISTS "public"."ttt";
CREATE TABLE "public"."ttt" (
"gid" int8 NOT NULL,
"AREA_CODE" varchar(255) COLLATE "default",
"NAME" varchar(255) COLLATE "default",
"geom" "public"."geometry",
"STATE" int8 NOT NULL
)
WITH (OIDS=FALSE);
2.建立集群触发器
CREATE TRIGGER "truncate_trigger_ttt" BEFORE TRUNCATE ON "public"."ttt"
FOR EACH STATEMENT
EXECUTE PROCEDURE "citus_truncate_trigger"();
3.创建广播模式
SELECT create_reference_table('ttt');
4.插入数据
5.测试postgis函数
select st_union(geom) from data_3 where gid in (1,2);
select st_extent(geom) from data_3;
四、补充:修改数据目录:
背景:
由于根目录划分的磁盘空间太小,而用户的数据量太大,现在需要把已经搭建好的citus集群的数据目录全部修改到/data/ 目录:
磁盘空间如下:
[root@worker3 pccuser]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/nlas-root xfs 100G 27G 74G 27% /
devtmpfs devtmpfs 63G 0 63G 0% /dev
tmpfs tmpfs 63G 8.0K 63G 1% /dev/shm
tmpfs tmpfs 63G 19M 63G 1% /run
tmpfs tmpfs 63G 0 63G 0% /sys/fs/cgroup
/dev/sda2 xfs 1016M 171M 845M 17% /boot
/dev/sda1 vfat 200M 9.8M 191M 5% /boot/efi
/dev/mapper/nlas-app ext4 197G 61M 187G 1% /app
/dev/mapper/nlas-data ext4 5.1T 89M 4.9T 1% /data
tmpfs tmpfs 13G 12K 13G 1% /run/user/42
tmpfs tmpfs 13G 0 13G 0% /run/user/1001
数据目录默认如下:
<可以看到默认是/var/lib/pgsql/10/data/>
[root@worker3 pccuser]# ps -ef|grep postgres
postgres 174510 1 0 7月15 ? 00:00:02 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/
postgres 174512 174510 0 7月15 ? 00:00:00 postgres: logger process
postgres 174514 174510 0 7月15 ? 00:00:00 postgres: checkpointer process
postgres 174515 174510 0 7月15 ? 00:00:01 postgres: writer process
postgres 174516 174510 0 7月15 ? 00:00:01 postgres: wal writer process
postgres 174517 174510 0 7月15 ? 00:00:01 postgres: autovacuum launcher process
postgres 174518 174510 0 7月15 ? 00:00:05 postgres: stats collector process
postgres 174519 174510 0 7月15 ? 00:00:17 postgres: bgworker: task tracker
postgres 174520 174510 0 7月15 ? 00:00:00 postgres: bgworker: logical replication launcher
postgres 174585 174510 0 7月15 ? 00:01:17 postgres: bgworker: Citus Maintenance Daemon: 13806/10
root 250278 250242 0 21:57 pts/0 00:00:00 grep --color=auto postgres
从service postgresql-10 initdb这句话中找到这个系统服务的名称,然后:
找到postgresql-10这个服务所在路径
查一下发现一共有2个地方:
[root@worker2 pccuser]# locate postgresql-10
/etc/systemd/system/multi-user.target.wants/postgresql-10.service
/usr/lib/systemd/system/postgresql-10.service
操作步骤:
systemctl stop postgresql-10
mkdir -p /data/pgsql_10/data
mv /var/lib/pgsql/10/data /data/pgsql_10/
sed -i "s/Environment=PGDATA=\/var\/lib\/pgsql\/10\/data\//Environment=PGDATA=\/data\/pgsql_10\/data\//g" /etc/systemd/system/multi-user.target.wants/postgresql-10.service
sed -i "s/Environment=PGDATA=\/var\/lib\/pgsql\/10\/data\//Environment=PGDATA=\/data\/pgsql_10\/data\//g" /usr/lib/systemd/system/postgresql-10.service
systemctl start postgresql-10
systemctl daemon-reload
systemctl start postgresql-10
systemctl status postgresql-10
确认下:
<可以看到数据目录默认位置已经修改成/data/pgsql_10/data/>
[root@worker2 pccuser]# ps -ef|grep postgres
postgres 250667 1 0 21:46 ? 00:00:00 /usr/pgsql-10/bin/postmaster -D /data/pgsql_10/data/
postgres 250669 250667 0 21:46 ? 00:00:00 postgres: logger process
postgres 250671 250667 0 21:46 ? 00:00:00 postgres: checkpointer process
postgres 250672 250667 0 21:46 ? 00:00:00 postgres: writer process
postgres 250673 250667 0 21:46 ? 00:00:00 postgres: wal writer process
postgres 250674 250667 0 21:46 ? 00:00:00 postgres: autovacuum launcher process
postgres 250675 250667 0 21:46 ? 00:00:00 postgres: stats collector process
postgres 250676 250667 0 21:46 ? 00:00:00 postgres: bgworker: task tracker
postgres 250677 250667 0 21:46 ? 00:00:00 postgres: bgworker: logical replication launcher
root 250687 250290 0 21:47 pts/0 00:00:00 grep --color=auto postgres
转载自:https://blog.csdn.net/ha_123_qq/article/details/80737804