CockroachDB集群部署

news2024/11/27 19:38:18

CockroachDB集群部署

1、CockroachDB简介

CockroachDB(有时简称为CRDB)是一个免费的、开源的分布式 SQL 数据库,它建立在一个事务性和强一致性的键

值存储之上。它由 PebbleDB(一个受 RocksDB/leveldb 启发的 K/B 存储库)支持,并使用 Raft 分布式共识算法来

确保一致性。

官方文档:https://www.cockroachlabs.com/docs/

常用命令:https://www.cockroachlabs.com/docs/v22.2/cockroach-commands

2、集群安装环境准备

2.1 集群环境和数据库版本说明

# 操作系统
$ cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
# 数据库安装版本:v22.1.0
# 数据库安装包:cockroach-v22.1.0.linux-amd64.tgz

如果想要安装其它版本,可以去下面的地址进行下载:

https://www.cockroachlabs.com/docs/releases/index.html

2.2 集群环境服务器规划

本次使用的是 3 台机虚拟机组成的集群,一主两备。

主机ip主机name角色
192.168.164.170mastermaster
192.168.164.171slave1slave1
192.168.164.172slave2slave2
192.168.164.173slave3slave3
192.168.164.174slave4slave4

2.3 关闭防火墙

# 停止防火墙
$ systemctl stop firewalld
# 关闭防火墙
$ systemctl disable firewalld
# 查看防火墙状态
$ systemctl status firewalld

2.4 配置hosts

修改 /etc/hosts

$ cat <<EOF >> /etc/hosts
192.168.164.170 master
192.168.164.171 slave1
192.168.164.172 slave2
192.168.164.173 slave3
192.168.164.174 slave4
EOF

确认是否成功,ping一下即可。

2.5 设置hostname

每台机器都需要设置 hostname。

# 每个节点分别设置
$ hostnamectl set-hostname master
$ hostnamectl set-hostname slave1
$ hostnamectl set-hostname slave2
$ hostnamectl set-hostname slave3
$ hostnamectl set-hostname slave4
# 查看设置是否生效
$ cat /etc/hostname

3、CockroachDB安装(每个节点)

参考地址:https://www.cockroachlabs.com/docs/v22.1/install-cockroachdb-linux

3.1 安装包下载和解压

本文下载的版本:https://binaries.cockroachdb.com/cockroach-v22.1.0.linux-amd64.tgz

cd ~ 
mkdir software
cd software 
curl https://binaries.cockroachdb.com/cockroach-v22.1.0.linux-amd64.tgz -o cockroach-v22.1.0.linux-amd64.tgz
tar -xzvf cockroach-v22.1.0.linux-amd64.tgz
cd cockroach-v22.1.0.linux-amd64 
cp -i cockroach /usr/local/bin/
$ which cockroach
/usr/local/bin/cockroach
$ cockroach --version
cockroach version details:
Build Tag:        v22.1.0
Build Time:       2022/05/23 16:27:47
Distribution:     CCL
Platform:         linux amd64 (x86_64-pc-linux-gnu)
Go Version:       go1.17.6
C Compiler:       gcc 6.5.0
Build Commit ID:  5b78463ed2e7106a8477b63fa837564ad02bb510
Build Type:       release
(use 'cockroach version --build-tag' to display only the build tag)

下面的三步是地理空间函数支持,如果不需要可以跳过。

3.2 创建外部存储库

mkdir -p /usr/local/lib/cockroach

3.3 将库文件复制到外部存储库

cp -i lib/libgeos.so /usr/local/lib/cockroach/
cp -i lib/libgeos_c.so /usr/local/lib/cockroach/

3.4 验证CockroachDB可以执行空间查询

# 在内存中建立一个临时的连接
$ cockroach demo
# 执行SQL
> SELECT ST_IsValid(ST_MakePoint(1,2));

4、集群搭建

参考地址:https://www.cockroachlabs.com/docs/stable/start-a-local-cluster.html

4.1 master节点启动集群

[root@master ~]# cockroach start --insecure --store=master --listen-addr=192.168.164.170:26257 --http-addr=192.168.164.170:8080 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259 --background
*
* WARNING: ALL SECURITY CONTROLS HAVE BEEN DISABLED!
*
* This mode is intended for non-production testing only.
*
* In this mode:
* - Your cluster is open to any client that can access 192.168.164.170.
* - Intruders with access to your machine or network can observe client-server traffic.
* - Intruders can log in without password and read or write any data in the cluster.
* - Intruders can consume all your server's resources and cause unavailability.
*
*
* INFO: To start a secure server without mandating TLS for clients,
* consider --accept-sql-without-tls instead. For other options, see:
*
* - https://go.crdb.dev/issue-v/53404/v22.1
* - https://www.cockroachlabs.com/docs/v22.1/secure-a-cluster.html
*
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

可选参数介绍:https://www.cockroachlabs.com/docs/v22.2/cockroach-start#flags

下面是一些常用的参数:

--insecure:表示通信数据未被加密,不安全。

--listen-addr:表示数据库实例的监听地址。

--http-addr:表示数据库实例 web 端的管理地址。

--store:表示数据存放路径,默认为执行命令时的当前路径下的 cockroach-data 文件夹下(如果没有该文件夹

则会自动创建)。

--join:表示集群初始化时所有集群中的节点。

--background:表示在后台启动进程,您可以继续使用同一终端进行其他操作。

# 查看进程
[root@master ~]# ps -ef | grep cockroach
root      55463      1  2 13:37 pts/1    00:00:01 cockroach start --insecure --store=master --listen-addr=192.168.164.170:26257 --http-addr=192.168.164.170:8080 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259
root      55817  42150  0 13:38 pts/1    00:00:00 grep --color=auto cockroach

4.2 slave1节点启动集群

[root@slave1 ~]# cockroach start --insecure --store=slave1 --listen-addr=192.168.164.171:26258 --http-addr=192.168.164.171:8081 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259 --background
*
* WARNING: ALL SECURITY CONTROLS HAVE BEEN DISABLED!
*
* This mode is intended for non-production testing only.
*
* In this mode:
* - Your cluster is open to any client that can access 192.168.164.171.
* - Intruders with access to your machine or network can observe client-server traffic.
* - Intruders can log in without password and read or write any data in the cluster.
* - Intruders can consume all your server's resources and cause unavailability.
*
*
* INFO: To start a secure server without mandating TLS for clients,
* consider --accept-sql-without-tls instead. For other options, see:
*
* - https://go.crdb.dev/issue-v/53404/v22.1
* - https://www.cockroachlabs.com/docs/v22.1/secure-a-cluster.html
*
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

查看进程:

[root@slave1 ~]# ps -ef | grep cockroach
root      18247      1  4 13:40 pts/1    00:00:03 cockroach start --insecure --store=slave1 --listen-addr=192.168.164.171:26258 --http-addr=192.168.164.171:8081 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259
root      18442  15594  0 13:41 pts/1    00:00:00 grep --color=auto cockroach

4.3 slave2节点启动集群

[root@slave2 ~]# cockroach start --insecure --store=slave2 --listen-addr=192.168.164.172:26259 --http-addr=192.168.164.172:8082 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259 --background
*
* WARNING: ALL SECURITY CONTROLS HAVE BEEN DISABLED!
*
* This mode is intended for non-production testing only.
*
* In this mode:
* - Your cluster is open to any client that can access 192.168.164.172.
* - Intruders with access to your machine or network can observe client-server traffic.
* - Intruders can log in without password and read or write any data in the cluster.
* - Intruders can consume all your server's resources and cause unavailability.
*
*
* INFO: To start a secure server without mandating TLS for clients,
* consider --accept-sql-without-tls instead. For other options, see:
*
* - https://go.crdb.dev/issue-v/53404/v22.1
* - https://www.cockroachlabs.com/docs/v22.1/secure-a-cluster.html
*
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

查看进程:

[root@slave2 ~]# ps -ef | grep cockroach
root      31015      1  5 13:40 pts/1    00:00:04 cockroach start --insecure --store=slave2 --listen-addr=192.168.164.172:26259 --http-addr=192.168.164.172:8081 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259
root      31422  19727  0 13:42 pts/1    00:00:00 grep --color=auto cockroach

4.4 集群初始化(master节点)

[root@master ~]# cockroach init --insecure --host=192.168.164.170:26257
Cluster successfully initialized

使用 cockroach init 命令执行集群的一次性初始化,将请求发送到 --join 列表中的任何节点。

可以查看启动日志:

[root@master ~]# grep 'node starting' master/logs/cockroach.log -A 11
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +CockroachDB node starting at 2023-04-23 05:42:59.758971263 +0000 UTC (took 309.5s)
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +webui:               ‹http://192.168.164.170:8080›
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +sql:                 ‹postgresql://root@192.168.164.170:26257/defaultdb?sslmode=disable›
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +sql (JDBC):          ‹jdbc:postgresql://192.168.164.170:26257/defaultdb?sslmode=disable&user=root›
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +RPC client flags:    ‹cockroach <client cmd> --host=192.168.164.170:26257 --insecure›
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +logs:                ‹/root/master/logs›
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +temp dir:            ‹/root/master/cockroach-temp755285367›
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +external I/O path:   ‹/root/master/extern›
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +store[0]:            ‹path=/root/master›
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +storage engine:      pebble
I230423 05:42:59.759084 64 1@cli/start.go:1028 ⋮ [n1] 555 +clusterID:           ‹19278025-71b4-4162-b8cc-12e692867f40›
[root@slave1 ~]# grep 'node starting' slave1/logs/cockroach.log -A 11
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +CockroachDB node starting at 2023-04-23 05:42:59.792449189 +0000 UTC (took 165.2s)
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +webui:               ‹http://192.168.164.171:8081›
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +sql:                 ‹postgresql://root@192.168.164.171:26258/defaultdb?sslmode=disable›
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +sql (JDBC):          ‹jdbc:postgresql://192.168.164.171:26258/defaultdb?sslmode=disable&user=root›
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +RPC client flags:    ‹cockroach <client cmd> --host=192.168.164.171:26258 --insecure›
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +logs:                ‹/root/slave1/logs›
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +temp dir:            ‹/root/slave1/cockroach-temp267500894›
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +external I/O path:   ‹/root/slave1/extern›
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +store[0]:            ‹path=/root/slave1›
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +storage engine:      pebble
I230423 05:42:59.792531 49 1@cli/start.go:1028 ⋮ [n2] 249 +clusterID:           ‹19278025-71b4-4162-b8cc-12e692867f40›
[root@slave2 ~]# grep 'node starting' slave2/logs/cockroach.log -A 11
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +CockroachDB node starting at 2023-04-23 05:43:00.067426003 +0000 UTC (took 129.2s)
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +webui:               ‹http://192.168.164.172:8082›
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +sql:                 ‹postgresql://root@192.168.164.172:26259/defaultdb?sslmode=disable›
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +sql (JDBC):          ‹jdbc:postgresql://192.168.164.172:26259/defaultdb?sslmode=disable&user=root›
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +RPC client flags:    ‹cockroach <client cmd> --host=192.168.164.172:26259 --insecure›
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +logs:                ‹/root/slave2/logs›
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +temp dir:            ‹/root/slave2/cockroach-temp3856293659›
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +external I/O path:   ‹/root/slave2/extern›
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +store[0]:            ‹path=/root/slave2›
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +storage engine:      pebble
I230423 05:43:00.067598 72 1@cli/start.go:1028 ⋮ [n3] 196 +clusterID:           ‹19278025-71b4-4162-b8cc-12e692867f40›

4.5 使用内置的SQL客户端

[root@master ~]# cockroach sql --insecure --host=192.168.164.170:26257
#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
# Server version: CockroachDB CCL v22.1.0 (x86_64-pc-linux-gnu, built 2022/05/23 16:27:47, go1.17.6) (same version as client)
# Cluster ID: 19278025-71b4-4162-b8cc-12e692867f40
#
# Enter \? for a brief introduction.
#
root@192.168.164.170:26257/defaultdb> CREATE DATABASE bank;
CREATE DATABASE


Time: 23ms total (execution 23ms / network 0ms)

root@192.168.164.170:26257/defaultdb> CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
CREATE TABLE


Time: 25ms total (execution 25ms / network 0ms)

root@192.168.164.170:26257/defaultdb> INSERT INTO bank.accounts VALUES (1, 1000.50);
INSERT 1


Time: 32ms total (execution 32ms / network 0ms)

root@192.168.164.170:26257/defaultdb> SELECT * FROM bank.accounts;
  id | balance
-----+----------
   1 | 1000.50
(1 row)


Time: 1ms total (execution 1ms / network 0ms)

root@192.168.164.170:26257/defaultdb> \q
[root@slave1 ~]# cockroach sql --insecure --host=192.168.164.171:26258
#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
# Server version: CockroachDB CCL v22.1.0 (x86_64-pc-linux-gnu, built 2022/05/23 16:27:47, go1.17.6) (same version as client)
# Cluster ID: 19278025-71b4-4162-b8cc-12e692867f40
#
# Enter \? for a brief introduction.
#
root@192.168.164.171:26258/defaultdb> SELECT * FROM bank.accounts;
  id | balance
-----+----------
   1 | 1000.50
(1 row)


Time: 37ms total (execution 37ms / network 0ms)

root@192.168.164.171:26258/defaultdb> \q
[root@slave2 ~]# cockroach sql --insecure --host=192.168.164.172:26259
#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
# Server version: CockroachDB CCL v22.1.0 (x86_64-pc-linux-gnu, built 2022/05/23 16:27:47, go1.17.6) (same version as client)
# Cluster ID: 19278025-71b4-4162-b8cc-12e692867f40
#
# Enter \? for a brief introduction.
#
root@192.168.164.172:26259/defaultdb> SELECT * FROM bank.accounts;
  id | balance
-----+----------
   1 | 1000.50
(1 row)


Time: 2ms total (execution 2ms / network 0ms)

root@192.168.164.172:26259/defaultdb> \q

4.6 使用dbeaver进行连接

在这里插入图片描述在这里插入图片描述在这里插入图片描述

4.7 运行一个简单的工作负载

CockroachDB 还附带了许多用于模拟客户端流量的内置工作负载,让我们运行基于 CockroachDB 的示例车辆共

享应用程序 MovR 的工作负载。

1、加载初始数据集:

[root@master ~]# cockroach workload init movr 'postgresql://root@192.168.164.170:26257?sslmode=disable'
I230423 05:49:57.284356 1 workload/workloadsql/dataload.go:146  [-] 1  imported users (0s, 50 rows)
I230423 05:49:57.298137 1 workload/workloadsql/dataload.go:146  [-] 2  imported vehicles (0s, 15 rows)
I230423 05:49:57.342088 1 workload/workloadsql/dataload.go:146  [-] 3  imported rides (0s, 500 rows)
I230423 05:49:57.389217 1 workload/workloadsql/dataload.go:146  [-] 4  imported vehicle_location_histories (0s, 1000 rows)
I230423 05:49:57.450854 1 workload/workloadsql/dataload.go:146  [-] 5  imported promo_codes (0s, 1000 rows)
I230423 05:49:57.470483 1 workload/workloadsql/dataload.go:146  [-] 6  imported user_promo_codes (0s, 5 rows)
I230423 05:49:57.506481 1 workload/workloadsql/workloadsql.go:136  [-] 7  starting 8 splits
I230423 05:49:57.695964 1 workload/workloadsql/workloadsql.go:136  [-] 8  starting 8 splits
I230423 05:49:57.899969 1 workload/workloadsql/workloadsql.go:136  [-] 9  starting 8 splits

2、运行工作负载5分钟:

[root@master ~]# cockroach workload run movr --duration=5m 'postgresql://root@192.168.164.170:26257?sslmode=disable'
I230423 05:54:34.453949 1 workload/cli/run.go:414  [-] 1  creating load generator...
I230423 05:54:34.454167 1 workload/cli/run.go:445  [-] 2  creating load generator... done (took 221.841µs)
_elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)
    1.0s        0            7.0            7.0      4.2     10.0     10.0     10.0 addUser
    1.0s        0            2.0            2.0      8.1      8.9      8.9      8.9 addVehicle
    1.0s        0            6.0            6.0      7.3     11.5     11.5     11.5 applyPromoCode
    1.0s        0            3.0            3.0      3.0      4.7      4.7      4.7 createPromoCode
    1.0s        0            3.0            3.0      5.5      6.3      6.3      6.3 endRide
    1.0s        0          452.4          452.6      0.7      1.3      2.0      6.8 readVehicles
    1.0s        0           10.0           10.0     14.2     17.8     17.8     17.8 startRide
    1.0s        0           31.0           31.0      8.4     37.7     39.8     39.8 updateActiveRides
......

4.8 访问数据库控制台

CockroachDB 控制台让您深入了解集群的整体健康状况以及客户端工作负载的性能。

访问地址:http://192.168.164.170:8080

Overview 中,请注意有三个节点处于活动状态,每个节点上具有相同的副本计数:

在这里插入图片描述

单击 Metrics 可访问各种时间序列仪表板,包括SQL查询和服务延迟随时间变化的图表:

在这里插入图片描述
使用 DatabasesSQL ActivityJobs 可以分别查看数据库和表的详细信息,评估特定查询的性能,以及监

视长时间运行的操作(如架构更改)的状态。

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

4.9 模拟节点维护

在新终端中,优雅地关闭节点,这通常在节点维护之前完成。

获取节点的进程ID:

[root@master ~]# ps -ef | grep cockroach | grep -v grep
root      55463      1 12 13:37 pts/1    00:02:54 cockroach start --insecure --store=master --listen-addr=192.168.164.170:26257 --http-addr=192.168.164.170:8080 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259
[root@slave1 ~]# ps -ef | grep cockroach | grep -v grep
root      18247      1  9 13:40 pts/1    00:01:55 cockroach start --insecure --store=slave1 --listen-addr=192.168.164.171:26258 --http-addr=192.168.164.171:8081 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259
[root@slave2 ~]# ps -ef | grep cockroach | grep -v grep
root      31015      1 10 13:40 pts/1    00:02:05 cockroach start --insecure --store=slave2 --listen-addr=192.168.164.172:26259 --http-addr=192.168.164.172:8081 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259

关闭掉 slave2 节点:

[root@slave2 ~]# kill -TERM 31015
[root@slave2 ~]# initiating graceful shutdown of server
server drained and shutdown completed

回到数据库控制台,尽管有一个节点是可疑的,但请注意持续的SQL流量:

在这里插入图片描述在这里插入图片描述
重新启动 slave2

[root@slave2 ~]# cockroach start --insecure --store=slave2 --listen-addr=192.168.164.172:26259 --http-addr=192.168.164.172:8081 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259 --background
*
* WARNING: ALL SECURITY CONTROLS HAVE BEEN DISABLED!
*
* This mode is intended for non-production testing only.
*
* In this mode:
* - Your cluster is open to any client that can access 192.168.164.172.
* - Intruders with access to your machine or network can observe client-server traffic.
* - Intruders can log in without password and read or write any data in the cluster.
* - Intruders can consume all your server's resources and cause unavailability.
*
*
* INFO: To start a secure server without mandating TLS for clients,
* consider --accept-sql-without-tls instead. For other options, see:
*
* - https://go.crdb.dev/issue-v/53404/v22.1
* - https://www.cockroachlabs.com/docs/v22.1/secure-a-cluster.html
*

4.10 缩放群集

增加容量非常简单,再启动2个节点:

[root@slave3 ~]# cockroach start --insecure --store=slave3 --listen-addr=192.168.164.173:26260 --http-addr=192.168.164.173:8083 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259 --background
*
* WARNING: ALL SECURITY CONTROLS HAVE BEEN DISABLED!
*
* This mode is intended for non-production testing only.
*
* In this mode:
* - Your cluster is open to any client that can access 192.168.164.173.
* - Intruders with access to your machine or network can observe client-server traffic.
* - Intruders can log in without password and read or write any data in the cluster.
* - Intruders can consume all your server's resources and cause unavailability.
*
*
* INFO: To start a secure server without mandating TLS for clients,
* consider --accept-sql-without-tls instead. For other options, see:
*
* - https://go.crdb.dev/issue-v/53404/v22.1
* - https://www.cockroachlabs.com/docs/v22.1/secure-a-cluster.html
*
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

[root@slave3 ~]# ps -ef | grep cockroach
root       3834      1 23 14:43 pts/0    00:00:04 cockroach start --insecure --store=slave3 --listen-addr=192.168.164.173:26260 --http-addr=192.168.164.173:8083 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259
root       3967   2870  0 14:43 pts/0    00:00:00 grep --color=auto cockroach

[root@slave3 ~]# grep 'node starting' slave3/logs/cockroach.log -A 11
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +CockroachDB node starting at 2023-04-23 06:43:04.532073647 +0000 UTC (took 0.2s)
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +webui:               ‹http://192.168.164.173:8083›
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +sql:                 ‹postgresql://root@192.168.164.173:26260/defaultdb?sslmode=disable›
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +sql (JDBC):          ‹jdbc:postgresql://192.168.164.173:26260/defaultdb?sslmode=disable&user=root›
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +RPC client flags:    ‹cockroach <client cmd> --host=192.168.164.173:26260 --insecure›
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +logs:                ‹/root/slave3/logs›
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +temp dir:            ‹/root/slave3/cockroach-temp3567256357›
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +external I/O path:   ‹/root/slave3/extern›
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +store[0]:            ‹path=/root/slave3›
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +storage engine:      pebble
I230423 06:43:04.532190 77 1@cli/start.go:1028 ⋮ [n4] 62 +clusterID:           ‹19278025-71b4-4162-b8cc-12e692867f40›
[root@slave4 ~]# cockroach start --insecure --store=slave4 --listen-addr=192.168.164.174:26261 --http-addr=192.168.164.174:8084 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259 --background
*
* WARNING: ALL SECURITY CONTROLS HAVE BEEN DISABLED!
*
* This mode is intended for non-production testing only.
*
* In this mode:
* - Your cluster is open to any client that can access 192.168.164.174.
* - Intruders with access to your machine or network can observe client-server traffic.
* - Intruders can log in without password and read or write any data in the cluster.
* - Intruders can consume all your server's resources and cause unavailability.
*
*
* INFO: To start a secure server without mandating TLS for clients,
* consider --accept-sql-without-tls instead. For other options, see:
*
* - https://go.crdb.dev/issue-v/53404/v22.1
* - https://www.cockroachlabs.com/docs/v22.1/secure-a-cluster.html
*
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

[root@slave4 ~]# ps -ef | grep cockroach
root       6541      1 26 14:43 pts/0    00:00:18 cockroach start --insecure --store=slave4 --listen-addr=192.168.164.174:26261 --http-addr=192.168.164.174:8084 --join=192.168.164.170:26257,192.168.164.171:26258,192.168.164.172:26259
root       6829   2344  0 14:44 pts/0    00:00:00 grep --color=auto cockroach

[root@slave4 ~]# grep 'node starting' slave4/logs/cockroach.log -A 11
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +CockroachDB node starting at 2023-04-23 06:43:48.753388019 +0000 UTC (took 0.3s)
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +webui:               ‹http://192.168.164.174:8084›
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +sql:                 ‹postgresql://root@192.168.164.174:26261/defaultdb?sslmode=disable›
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +sql (JDBC):          ‹jdbc:postgresql://192.168.164.174:26261/defaultdb?sslmode=disable&user=root›
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +RPC client flags:    ‹cockroach <client cmd> --host=192.168.164.174:26261 --insecure›
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +logs:                ‹/root/slave4/logs›
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +temp dir:            ‹/root/slave4/cockroach-temp3631943978›
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +external I/O path:   ‹/root/slave4/extern›
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +store[0]:            ‹path=/root/slave4›
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +storage engine:      pebble
I230423 06:43:48.753541 71 1@cli/start.go:1028 ⋮ [n5] 66 +clusterID:           ‹19278025-71b4-4162-b8cc-12e692867f40›

在数据库控制台查看 Overview,可以看到5个节点:

在这里插入图片描述

4.11 停止群集(每个节点)

完成测试集群后,停止节点。

获取节点的进程ID:

$ ps -ef | grep cockroach | grep -v grep

优雅地关闭每个节点,指定其进程ID:

$ kill -TERM pid

如果您不打算重新启动集群,您可能希望删除节点的数据存储:

$ cd ~
$ rm -rf master
$ rm -rf slave1
$ rm -rf slave2
$ rm -rf slave3
$ rm -rf slave4

5、集群安全模式搭建

根据 4.11 中的操作停止集群,然后重新进行搭建。

参考文档:https://www.cockroachlabs.com/docs/v21.1/secure-a-cluster

5.1 生成证书

您可以使用 cockroach 证书命令或 openssl 命令来生成安全证书,本节介绍 cockroach 证书命令。

5.1.1 创建两个目录(每个节点)

$ mkdir certs my-safe-directory

5.1.2 创建CA(证书授权中心)证书和密钥对(master节点)

$ cockroach cert create-ca --certs-dir=certs --ca-key=my-safe-directory/ca.key
$ zip -r certs.zip certs/ my-safe-directory/
$ scp certs.zip root@slave1:~
$ scp certs.zip root@slave2:~
$ scp certs.zip root@slave3:~
$ scp certs.zip root@slave4:~

5.1.3 为节点创建证书和密钥对(每个节点)

$ unzip certs.zip
$ cockroach cert create-node $(hostname) --certs-dir=certs --ca-key=my-safe-directory/ca.key

5.1.4 为root用户创建客户端证书和密钥对(每个节点)

$ cockroach cert create-client root --certs-dir=certs --ca-key=my-safe-directory/ca.key

5.2 启动集群

使用 cockroach start 命令启动第一个节点:

[root@master ~]# cockroach start --certs-dir=certs --store=master --listen-addr=master:26257 --http-addr=master:8080 --join=master:26257,slave1:26258,slave2:26259 --background
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

[root@master ~]# ps -ef | grep cockroach
root     109054      1 12 20:06 pts/0    00:00:00 cockroach start --certs-dir=certs --store=master --listen-addr=master:26257 --http-addr=master:8080 --join=master:26257,slave1:26258,slave2:26259
root     109101  94099  0 20:06 pts/0    00:00:00 grep --color=auto cockroach

其它两个节点:

[root@slave1 ~]# cockroach start --certs-dir=certs --store=slave1 --listen-addr=slave1:26258 --http-addr=slave1:8081 --join=master:26257,slave1:26258,slave2:26259 --background
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

[root@slave1 ~]# ps -ef | grep cockroach
root      24756      1 17 20:06 pts/0    00:00:00 cockroach start --certs-dir=certs --store=slave1 --listen-addr=slave1:26258 --http-addr=slave1:8081 --join=master:26257,slave1:26258,slave2:26259
root      24797  20785  0 20:06 pts/0    00:00:00 grep --color=auto cockroach
[root@slave2 ~]# cockroach start --certs-dir=certs --store=slave2 --listen-addr=slave2:26259 --http-addr=slave2:8082 --join=master:26257,slave1:26258,slave2:26259 --background
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

[root@slave2 ~]# ps -ef | grep cockroach
root      47348      1 13 20:07 pts/0    00:00:00 cockroach start --certs-dir=certs --store=slave2 --listen-addr=slave2:26259 --http-addr=slave2:8082 --join=master:26257,slave1:26258,slave2:26259
root      47395  41565  0 20:07 pts/0    00:00:00 grep --color=auto cockroach

5.3 初始化(master节点)

[root@master ~]# cockroach init --certs-dir=certs --host=master:26257
Cluster successfully initialized

查看日志:

[root@master ~]# grep 'node starting' master/logs/cockroach.log -A 11
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +CockroachDB node starting at 2023-04-23 12:07:15.980761826 +0000 UTC (took 37.4s)
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +webui:               ‹https://master:8080›
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +sql:                 ‹postgresql://root@master:26257/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt›
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +sql (JDBC):          ‹jdbc:postgresql://master:26257/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt&user=root›
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +RPC client flags:    ‹cockroach <client cmd> --host=master:26257 --certs-dir=certs›
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +logs:                ‹/root/master/logs›
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +temp dir:            ‹/root/master/cockroach-temp1023318723›
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +external I/O path:   ‹/root/master/extern›
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +store[0]:            ‹path=/root/master›
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +storage engine:      pebble
I230423 12:07:15.981008 82 1@cli/start.go:1028 ⋮ [n1] 138 +clusterID:           ‹ca6b2b9a-bd80-41e7-858b-17286c037742›
[root@slave1 ~]# grep 'node starting' slave1/logs/cockroach.log -A 11
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +CockroachDB node starting at 2023-04-23 12:07:16.259982879 +0000 UTC (took 25.2s)
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +webui:               ‹https://slave1:8081›
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +sql:                 ‹postgresql://root@slave1:26258/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt›
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +sql (JDBC):          ‹jdbc:postgresql://slave1:26258/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt&user=root›
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +RPC client flags:    ‹cockroach <client cmd> --host=slave1:26258 --certs-dir=certs›
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +logs:                ‹/root/slave1/logs›
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +temp dir:            ‹/root/slave1/cockroach-temp108267462›
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +external I/O path:   ‹/root/slave1/extern›
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +store[0]:            ‹path=/root/slave1›
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +storage engine:      pebble
I230423 12:07:16.260805 63 1@cli/start.go:1028 ⋮ [n2] 95 +clusterID:           ‹ca6b2b9a-bd80-41e7-858b-17286c037742›
[root@slave2 ~]# grep 'node starting' slave2/logs/cockroach.log -A 11
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +CockroachDB node starting at 2023-04-23 12:07:16.350705255 +0000 UTC (took 13.2s)
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +webui:               ‹https://slave2:8082›
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +sql:                 ‹postgresql://root@slave2:26259/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt›
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +sql (JDBC):          ‹jdbc:postgresql://slave2:26259/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt&user=root›
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +RPC client flags:    ‹cockroach <client cmd> --host=slave2:26259 --certs-dir=certs›
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +logs:                ‹/root/slave2/logs›
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +temp dir:            ‹/root/slave2/cockroach-temp1112707351›
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +external I/O path:   ‹/root/slave2/extern›
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +store[0]:            ‹path=/root/slave2›
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +storage engine:      pebble
I230423 12:07:16.351527 83 1@cli/start.go:1028 ⋮ [n3] 77 +clusterID:           ‹ca6b2b9a-bd80-41e7-858b-17286c037742›

查看节点状态:

[root@master ~]# cockroach node status --certs-dir certs --host master:26257
  id |   address    | sql_address  |  build  |         started_at         |         updated_at         | locality | is_available | is_live
-----+--------------+--------------+---------+----------------------------+----------------------------+----------+--------------+----------
   1 | master:26257 | master:26257 | v22.1.0 | 2023-04-23 12:07:15.734543 | 2023-04-23 12:07:20.249576 |          | true         | true
   2 | slave1:26258 | slave1:26258 | v22.1.0 | 2023-04-23 12:07:16.120306 | 2023-04-23 12:07:20.637823 |          | true         | true
   3 | slave2:26259 | slave2:26259 | v22.1.0 | 2023-04-23 12:07:16.196019 | 2023-04-23 12:07:20.711792 |          | true         | true
(3 rows)

5.4 使用内置的SQL客户端

[root@master ~]# cockroach sql --certs-dir=certs --host=master:26257
#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
# Server version: CockroachDB CCL v22.1.0 (x86_64-pc-linux-gnu, built 2022/05/23 16:27:47, go1.17.6) (same version as client)
# Cluster ID: ca6b2b9a-bd80-41e7-858b-17286c037742
#
# Enter \? for a brief introduction.
#
root@master:26257/defaultdb> CREATE DATABASE bank;
CREATE DATABASE


Time: 20ms total (execution 19ms / network 0ms)

root@master:26257/defaultdb> CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
CREATE TABLE


Time: 21ms total (execution 21ms / network 0ms)

root@master:26257/defaultdb> INSERT INTO bank.accounts VALUES (1, 1000.50);               
INSERT 1

root@master:26257/defaultdb> SELECT * FROM bank.accounts;
  id | balance
-----+----------
   1 | 1000.50
(1 row)


Time: 1ms total (execution 1ms / network 0ms)

root@master:26257/defaultdb> \q

现在创建一个带有密码的用户,您需要该密码才能访问DB控制台:

[root@master ~]# cockroach sql --certs-dir=certs --host=master:26257
#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
# Server version: CockroachDB CCL v22.1.0 (x86_64-pc-linux-gnu, built 2022/05/23 16:27:47, go1.17.6) (same version as client)
# Cluster ID: ca6b2b9a-bd80-41e7-858b-17286c037742
#
# Enter \? for a brief introduction.
#
root@master:26257/defaultdb> CREATE USER max WITH PASSWORD 'roach';
CREATE ROLE


Time: 130ms total (execution 129ms / network 0ms)

root@master:26257/defaultdb> GRANT admin TO max;
GRANT


Time: 63ms total (execution 63ms / network 0ms)


root@master:26257/defaultdb> \q

5.5 使用dbeaver进行连接

在这里插入图片描述
在这里插入图片描述

5.6 运行一个简单的工作负载

加载初始化数据集:

[root@master ~]# cockroach workload init movr 'postgresql://root@master:26257?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt'
I230423 12:21:00.606622 1 workload/workloadsql/dataload.go:146  [-] 1  imported users (0s, 50 rows)
I230423 12:21:00.620323 1 workload/workloadsql/dataload.go:146  [-] 2  imported vehicles (0s, 15 rows)
I230423 12:21:00.681262 1 workload/workloadsql/dataload.go:146  [-] 3  imported rides (0s, 500 rows)
I230423 12:21:00.733286 1 workload/workloadsql/dataload.go:146  [-] 4  imported vehicle_location_histories (0s, 1000 rows)
I230423 12:21:00.810774 1 workload/workloadsql/dataload.go:146  [-] 5  imported promo_codes (0s, 1000 rows)
I230423 12:21:00.822346 1 workload/workloadsql/dataload.go:146  [-] 6  imported user_promo_codes (0s, 5 rows)
I230423 12:21:00.840055 1 workload/workloadsql/workloadsql.go:136  [-] 7  starting 8 splits
I230423 12:21:00.979159 1 workload/workloadsql/workloadsql.go:136  [-] 8  starting 8 splits
I230423 12:21:01.154081 1 workload/workloadsql/workloadsql.go:136  [-] 9  starting 8 splits

运行5分钟:

[root@master ~]# cockroach workload run movr --duration=5m 'postgresql://root@master:26257?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt'
I230423 12:21:57.311429 1 workload/cli/run.go:414  [-] 1  creating load generator...
I230423 12:21:57.312644 1 workload/cli/run.go:445  [-] 2  creating load generator... done (took 1.218486ms)
_elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)
    1.0s        0            6.0            6.0      2.8      3.5      3.5      3.5 addUser
    1.0s        0            2.0            2.0      5.8      8.9      8.9      8.9 addVehicle
    1.0s        0            4.0            4.0     11.5     14.7     14.7     14.7 applyPromoCode
    1.0s        0            3.0            3.0      3.1      3.8      3.8      3.8 createPromoCode
    1.0s        0            3.0            3.0      3.5      5.5      5.5      5.5 endRide
    1.0s        0          380.0          380.7      1.4      3.0      4.5     10.5 readVehicles
    1.0s        0            7.0            7.0     13.1     14.7     14.7     14.7 startRide
    1.0s        0           24.9           25.0      6.0     23.1     26.2     26.2 updateActiveRides

5.7 访问数据库控制台

CockroachDB 控制台让您深入了解集群的整体健康状况以及客户端工作负载的性能。

访问地址:http://192.168.164.170:8080

需要输入用户名和密码:

在这里插入图片描述

输入刚才创建的用户名和密码:

在这里插入图片描述

5.8 模拟节点维护

1、模拟一个失败的节点

[root@slave2 ~]# cockroach quit --certs-dir=certs --host=slave2:26259
Command "quit" is deprecated, see 'cockroach node drain' instead to drain a
server without terminating the server process (which can in turn be done using
an orchestration layer or a process manager, or by sending a termination signal
directly).
warning: draining a node without node ID or passing --self explicitly is deprecated.
node is draining... remaining: 39
node is draining... remaining: 0 (complete)
ok

2、查看节点状态

在这里插入图片描述

3、启动节点

[root@slave2 ~]# cockroach quit --certs-dir=certs --host=slave2:26259
Command "quit" is deprecated, see 'cockroach node drain' instead to drain a
server without terminating the server process (which can in turn be done using
an orchestration layer or a process manager, or by sending a termination signal
directly).
warning: draining a node without node ID or passing --self explicitly is deprecated.
node is draining... remaining: 39
node is draining... remaining: 0 (complete)
ok

4、再次查看节点状态

在这里插入图片描述

5.9 缩放群集

增加容量非常简单,再启动2个节点:

[root@slave3 ~]# cockroach start --certs-dir=certs --store=slave3 --listen-addr=slave3:26260 --http-addr=slave3:8083 --join=master:26257,slave1:26258,slave2:26259 --background
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

[root@slave3 ~]# ps -ef | grep cockroach
root      17440      1 22 20:37 pts/0    00:00:02 cockroach start --certs-dir=certs --store=slave3 --listen-addr=slave3:26260 --http-addr=slave3:8083 --join=master:26257,slave1:26258,slave2:26259
root      17505  16678  0 20:37 pts/0    00:00:00 grep --color=auto cockroach

[root@slave3 ~]# grep 'node starting' slave3/logs/cockroach.log -A 11
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +CockroachDB node starting at 2023-04-23 12:37:06.538251957 +0000 UTC (took 0.3s)
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +webui:               ‹https://slave3:8083›
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +sql:                 ‹postgresql://root@slave3:26260/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt›
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +sql (JDBC):          ‹jdbc:postgresql://slave3:26260/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt&user=root›
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +RPC client flags:    ‹cockroach <client cmd> --host=slave3:26260 --certs-dir=certs›
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +logs:                ‹/root/slave3/logs›
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +temp dir:            ‹/root/slave3/cockroach-temp2625116361›
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +external I/O path:   ‹/root/slave3/extern›
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +store[0]:            ‹path=/root/slave3›
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +storage engine:      pebble
I230423 12:37:06.538812 64 1@cli/start.go:1028 ⋮ [n4] 62 +clusterID:           ‹ca6b2b9a-bd80-41e7-858b-17286c037742›
[root@slave4 ~]# cockroach start --certs-dir=certs --store=slave4 --listen-addr=slave4:26261 --http-addr=slave4:8084 --join=master:26257,slave1:26258,slave2:26259 --background
*
* INFO: initial startup completed.
* Node will now attempt to join a running cluster, or wait for `cockroach init`.
* Client connections will be accepted after this completes successfully.
* Check the log file(s) for progress.
*

[root@slave4 ~]# ps -ef | grep cockroach
root      20234      1 27 20:40 pts/0    00:00:02 cockroach start --certs-dir=certs --store=slave4 --listen-addr=slave4:26261 --http-addr=slave4:8084 --join=master:26257,slave1:26258,slave2:26259
root      20287  18651  0 20:40 pts/0    00:00:00 grep --color=auto cockroach

[root@slave4 ~]# grep 'node starting' slave4/logs/cockroach.log -A 11
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +CockroachDB node starting at 2023-04-23 12:40:49.597667487 +0000 UTC (took 0.3s)
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +build:               CCL v22.1.0 @ 2022/05/23 16:27:47 (go1.17.6)
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +webui:               ‹https://slave4:8084›
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +sql:                 ‹postgresql://root@slave4:26261/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt›
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +sql (JDBC):          ‹jdbc:postgresql://slave4:26261/defaultdb?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt&user=root›
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +RPC client flags:    ‹cockroach <client cmd> --host=slave4:26261 --certs-dir=certs›
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +logs:                ‹/root/slave4/logs›
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +temp dir:            ‹/root/slave4/cockroach-temp3992876162›
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +external I/O path:   ‹/root/slave4/extern›
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +store[0]:            ‹path=/root/slave4›
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +storage engine:      pebble
I230423 12:40:49.598861 82 1@cli/start.go:1028 ⋮ [n5] 65 +clusterID:           ‹ca6b2b9a-bd80-41e7-858b-17286c037742›

在数据库控制台查看 Overview,可以看到5个节点:

在这里插入图片描述

5.10 停止集群

$ cockroach quit --certs-dir=certs --host=master:26257
$ cockroach quit --certs-dir=certs --host=slave1:26258
$ ockroach quit --certs-dir=certs --host=slave2:26259
$ cockroach quit --certs-dir=certs --host=slave3:26260
$ cockroach quit --certs-dir=certs --host=slave4:26261
$ cd ~
$ rm -rf master
$ rm -rf slave1
$ rm -rf slave2
$ rm -rf slave3
$ rm -rf slave4

至此,CockroachDB集群搭建完毕!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1023246.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

利用Java EE相关技术实现一个简单的Web聊天室系统

利用Java EE相关技术实现一个简单的Web聊天室系统 &#xff08;1&#xff09;编写一个登录页面&#xff0c;登录信息中有用户名和密码&#xff0c;分别用两个按钮来提交和重置登录信息。 &#xff08;2&#xff09;通过请求指派来处理用户提交的登录信息&#xff0c;如果用户名…

基于YOLOv8模型的烟火目标检测系统(PyTorch+Pyside6+YOLOv8模型)

摘要&#xff1a;基于YOLOv8模型的烟火目标检测系统可用于日常生活中检测与定位烟火目标&#xff0c;利用深度学习算法可实现图片、视频、摄像头等方式的目标检测&#xff0c;另外本系统还支持图片、视频等格式的结果可视化与结果导出。本系统采用YOLOv8目标检测算法训练数据集…

linux中的开发工具

在刚开始使用linux的时候&#xff0c;我们需要在系统上写一些简单的代码&#xff0c;来熟悉环境以及各种指令 并且熟悉属于linux的一套开发的环境&#xff0c;而这对于c来说需要三个软件就可以进行简单的编码 和使用&#xff0c;让我们来认识一下下列工具&#xff0c;以及工具的…

【Java 基础篇】Java字符打印流详解:文本数据的输出利器

在Java编程中&#xff0c;我们经常需要将数据输出到文件或其他输出源中。Java提供了多种输出流来帮助我们完成这项任务&#xff0c;其中字符打印流是一个非常有用的工具。本文将详细介绍Java字符打印流的用法&#xff0c;以及如何在实际编程中充分利用它。 什么是字符打印流&a…

电脑丢失d3dcompiler47.dll怎么办,这个四个修复方法都可以解决

d3dcompiler_47.dll 是一个与 DirectX 相关的动态链接库文件&#xff0c;它包含了 DirectX 编译器的一些函数和类&#xff0c;对于许多应用程序和游戏来说都是必需的。如果您的系统中缺失了这个文件&#xff0c;可能会导致程序无法正常运行。下面我们将介绍四个修复 d3dcompile…

(图论) 1020. 飞地的数量 ——【Leetcode每日一题】

❓ 1020. 飞地的数量 难度&#xff1a;中等 给你一个大小为 m x n 的二进制矩阵 grid &#xff0c;其中 0 表示一个 海洋单元格、1 表示一个 陆地单元格。 一次 移动 是指从一个陆地单元格走到另一个相邻&#xff08;上、下、左、右&#xff09;的陆地单元格或跨过 grid 的边…

vant 组件库的基本使用

文章目录 vant组件库1、什么是组件库2、vant组件 全部导入 和 按需导入的区别3、全部导入的使用步骤&#xff1a;4、按需导入的使用步骤&#xff1a;5、封装vant文件包 vant组件库 该项目将使用到vant-ui组件库&#xff0c;这里的目标就是认识他&#xff0c;铺垫知识 1、什么…

PyG-GAT-Cora(在Cora数据集上应用GAT做节点分类)

文章目录 model.pymain.py参数设置运行图 model.py import torch.nn as nn from torch_geometric.nn import GATConv import torch.nn.functional as F class gat_cls(nn.Module):def __init__(self,in_dim,hid_dim,out_dim,dropout_size0.5):super(gat_cls,self).__init__()s…

安达发APS|国货品牌崛起,制造业迎来智能排产新机遇

随着国货品牌的不断崛起&#xff0c;制造业的生产也面临着巨大的挑战。为应对这一挑战&#xff0c;越来越多的企业开始引入APS智能排产技术&#xff0c;以优化生产线布局、提升设备利用率、缩短生产周期、减少生产成本&#xff0c;从而增强市场竞争力。本文将为您详细解读APS智…

数据结构-----栈(栈的初始化、建立、入栈、出栈、遍历、清空等操作)

目录 前言 栈 1.定义 2.栈的特点 3.栈的储存方式 3.1数组栈 3.2链栈 4.栈的基本操作&#xff08;C语言&#xff09; 4.1初始化 4.2判断是否满栈 4.3判断空栈 4.4 入栈 4.5 出栈 4.6获取栈顶元素 4.7遍历栈 4.8清空栈 完整代码示例 前言 大家好呀&#xff01;今天我…

登录业务实现

登录业务实现&#xff1a; 登录成功/失败实现 -> pinia管理用户数据及数据持久化 -> 不同登录状态的模板适配 -> 请求拦截器携带token -> 退出登录实现 -> token失效&#xff08;401响应拦截&#xff09; 1. 登录成功/失败实现 当表单校验通过时&a…

华为云云耀云服务器L实例评测|云耀云服务器L实例部署odoo开源ERP平台

华为云云耀云服务器L实例评测&#xff5c;云耀云服务器L实例部署odoo开源ERP平台 一、云耀云服务器L实例介绍1.1 云耀云服务器L实例简介1.2 云耀云服务器L实例使用场景1.3 云耀云服务器L实例特点 二、odoo介绍2.1 odoo简介2.2 odoo特点 三、本次实践介绍3.1 本次实践简介3.2 本…

解决Java应用程序中的SQLException:服务器时区值未识别问题;MySQL连接问题:服务器时区值 ‘Öйú±ê׼ʱ¼ä‘ 未被识别的解决方法

目录 ​编辑 问题背景 解决方案 问题背景 今天遇见一个这个问题&#xff0c;解决后发出来分享一下&#xff1a; java.sql.SQLException: The server time zone value is unrecognized or represents more than one time zone. You must configure either the server or J…

STP介绍

目录 STP概述 二层环路带来的问题 1.广播风暴 2.MAC地址漂移问题 3.多帧复制---这个好理解&#xff0c;同一个数据帧被重复收到多次&#xff0c;被称为多帧复制。 802.1D生成树 STP的BPDU BPDU主要分为两大类 配置BPDU RPC COST 配置BPDU的工作过程 TCN BPDU TCN…

【python爬虫】——历史天气信息爬取

文章目录 1、任务描述1.1、需求分析1.2 页面分析 2、获取网页源码、解析、保存数据3、结果展示 1、任务描述 1.1、需求分析 在2345天气信息网2345天气网依据地点和时间对相关城市的历史天气信息进行爬取。 1.2 页面分析 网页使用get方式发送请求&#xff0c;所需参数包括a…

c语言练习63:用malloc开辟二维数组的三种办法

用malloc开辟二维数组的三种办法 使用malloc函数模拟开辟一个3*5的整型二维数组&#xff0c;开辟好后&#xff0c;使用二维数组的下标访问形式&#xff0c;访问空间。 第一种办法&#xff1a;用指针数组&#xff1a; #include<stdio.h> int main() {int** p (int**)m…

2023-09-19 LeetCode每日一题(打家劫舍 IV)

2023-09-19每日一题 一、题目编号 2560. 打家劫舍 IV二、题目链接 点击跳转到题目位置 三、题目描述 沿街有一排连续的房屋。每间房屋内都藏有一定的现金。现在有一位小偷计划从这些房屋中窃取现金。 由于相邻的房屋装有相互连通的防盗系统&#xff0c;所以小偷 不会窃取…

【C++代码】二叉树的最大深度,二叉树的最小深度,完全二叉树的节点个数--代码随想录

题目&#xff1a;二叉树的最大深度 给定一个二叉树 root &#xff0c;返回其最大深度。二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。 题解 如果我们知道了左子树和右子树的最大深度 l 和 r&#xff0c;那么该二叉树的最大深度即为 m a x ( l , r ) …

令人惊艳的AI项目,这也太猛了...

大家好&#xff0c;我是 Jack。 这两天&#xff0c;我在网上冲浪&#xff0c;发现了一款神器&#xff01; 我在使用 AI 绘画 Stable Diffsuion 和 Midjourney 的时候&#xff0c;花费时间最多的就是写 prompt 描述词了&#xff0c;绞尽脑汁地调试 prompt。 同样&#xff0c;…

pdf怎么转换成word?推荐这几种方法

pdf怎么转换成word&#xff1f;pdf转换成Word是一项常见的需求&#xff0c;首先&#xff0c;Word文档是一种常用的文档格式&#xff0c;几乎在任何计算机上都可以打开和编辑。与PDF相比&#xff0c;Word文档更加灵活和可编辑&#xff0c;可以轻松地修改和更新文档内容。在使用这…