CockroachDB集群部署

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 DATABASETime: 23ms total (execution 23ms / network 0ms)root@192.168.164.170:26257/defaultdb> CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
CREATE TABLETime: 25ms total (execution 25ms / network 0ms)root@192.168.164.170:26257/defaultdb> INSERT INTO bank.accounts VALUES (1, 1000.50);
INSERT 1Time: 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 addUser1.0s        0            2.0            2.0      8.1      8.9      8.9      8.9 addVehicle1.0s        0            6.0            6.0      7.3     11.5     11.5     11.5 applyPromoCode1.0s        0            3.0            3.0      3.0      4.7      4.7      4.7 createPromoCode1.0s        0            3.0            3.0      5.5      6.3      6.3      6.3 endRide1.0s        0          452.4          452.6      0.7      1.3      2.0      6.8 readVehicles1.0s        0           10.0           10.0     14.2     17.8     17.8     17.8 startRide1.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:26257id |   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         | true2 | slave1:26258 | slave1:26258 | v22.1.0 | 2023-04-23 12:07:16.120306 | 2023-04-23 12:07:20.637823 |          | true         | true3 | 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 DATABASETime: 20ms total (execution 19ms / network 0ms)root@master:26257/defaultdb> CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
CREATE TABLETime: 21ms total (execution 21ms / network 0ms)root@master:26257/defaultdb> INSERT INTO bank.accounts VALUES (1, 1000.50);               
INSERT 1root@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 ROLETime: 130ms total (execution 129ms / network 0ms)root@master:26257/defaultdb> GRANT admin TO max;
GRANTTime: 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 addUser1.0s        0            2.0            2.0      5.8      8.9      8.9      8.9 addVehicle1.0s        0            4.0            4.0     11.5     14.7     14.7     14.7 applyPromoCode1.0s        0            3.0            3.0      3.1      3.8      3.8      3.8 createPromoCode1.0s        0            3.0            3.0      3.5      5.5      5.5      5.5 endRide1.0s        0          380.0          380.7      1.4      3.0      4.5     10.5 readVehicles1.0s        0            7.0            7.0     13.1     14.7     14.7     14.7 startRide1.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.mzph.cn/news/82617.shtml

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

相关文章

TypeScript入门

目录 一&#xff1a;语言特性 二&#xff1a;TypeScript安装 NPM 安装 TypeScript 三&#xff1a;TypeScript基础语法 第一个 TypeScript 程序 四&#xff1a;TypeScript 保留关键字 空白和换行 TypeScript 区分大小写 TypeScript 注释 TypeScript 支持两种类型的注释 …

初识C语言——详细入门一(系统性学习day4)

目录 前言 一、C语言简单介绍、特点、基本构成 简单介绍&#xff1a; 特点&#xff1a; 基本构成&#xff1a; 二、认识C语言程序 标准格式&#xff1a; 简单C程序&#xff1a; 三、基本构成分类详细介绍 &#xff08;1&#xff09;关键字 &#xff08;2&#xf…

fork函数

二.fork函数 2.1函数原型 fork()函数在 C 语言中的原型如下&#xff1a; #include <unistd.h>pid_t fork(void);其中pid_t是一个整型数据类型&#xff0c;用于表示进程ID。fork()函数返回值是一个pid_t类型的值&#xff0c;具体含义如下&#xff1a; 如果调用fork()的…

MyBatis中当实体类中的属性名和表中的字段名不一样,怎么办

方法1&#xff1a; 在mybatis核心配置文件中指定&#xff0c;springboot加载mybatis核心配置文件 springboot项目的一个特点就是0配置&#xff0c;本来就省掉了mybatis的核心配置文件&#xff0c;现在又加回去算什么事&#xff0c;总之这种方式可行但没人这样用 具体操作&…

Python灰帽编程——错误异常处理与面向对象

文章目录 错误异常处理与面向对象1. 错误和异常1.1 基本概念1.1.1 Python 异常 1.2 检测&#xff08;捕获&#xff09;异常1.2.1 try except 语句1.2.2 捕获多种异常1.2.3 捕获所有异常 1.3 处理异常1.4 特殊场景1.4.1 with 语句 1.5 脚本完善 2. 内网主机存活检测程序2.1 scap…

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

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

DNG格式详解,DNG是什么?为何DNG可以取代RAW统一单反相机、苹果安卓移动端相机拍摄输出原始图像数据标准

返回图像处理总目录&#xff1a;《JavaCV图像处理合集总目录》 前言 在DNG格式发布之前&#xff0c;我们先了解一下之前单反相机、苹果和安卓移动端相机拍照输出未经处理的原始图像格式是什么&#xff1f; RAW 什么是RAW&#xff1f; RAW是未经处理、也未经压缩的格式。可以…

Rust通用编程概念(3)

Rust通用编程概念 1.变量和可变性1.执行cargo run2.变量3.变量的可变性4.常量5.遮蔽5.1遮蔽与mut区别1.遮蔽2.mut 2.数据类型1.标量类型1.1整数类型1.2浮点数类型1.3数字运算1.4布尔类型1.5字符类型 2.复合类型2.1元组类型2.2数组类型1.访问数组2.无效的数组元素访问 3.函数3.1…

如何解决 503 Service Temporarily Unavailable?

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f405;&#x1f43e;猫头虎建议程序员必备技术栈一览表&#x1f4d6;&#xff1a; &#x1f6e0;️ 全栈技术 Full Stack: &#x1f4da…

想要精通算法和SQL的成长之路 - 填充书架

想要精通算法和SQL的成长之路 - 填充书架 前言一. 填充书架1.1 优化 前言 想要精通算法和SQL的成长之路 - 系列导航 一. 填充书架 原题链接 题目中有一个值得注意的点就是&#xff1a; 需要按照书本顺序摆放。每一层当中&#xff0c;只要厚度不够了&#xff0c;当前层最高…

【考研数学】高等数学第六模块 —— 空间解析几何(1,向量基本概念与运算)

文章目录 引言一、空间解析几何的理论1.1 基本概念1.2 向量的运算 写在最后 引言 我自认空间想象能力较差&#xff0c;所以当初学这个很吃力。希望现在再接触&#xff0c;能好点。 一、空间解析几何的理论 1.1 基本概念 1.向量 —— 既有大小&#xff0c;又有方向的量称为向…

C语言指针,深度长文全面讲解

指针对于C来说太重要。然而&#xff0c;想要全面理解指针&#xff0c;除了要对C语言有熟练的掌握外&#xff0c;还要有计算机硬件以及操作系统等方方面面的基本知识。所以本文尽可能的通过一篇文章完全讲解指针。 为什么需要指针&#xff1f; 指针解决了一些编程中基本的问题。…

spring aop源码解析

spring知识回顾 spring的两个重要功能&#xff1a;IOC、AOP&#xff0c;在ioc容器的初始化过程中&#xff0c;会触发2种处理器的调用&#xff0c; 前置处理器(BeanFactoryPostProcessor)后置处理器(BeanPostProcessor)。 前置处理器的调用时机是在容器基本创建完成时&#xff…

Axure原型设计累加器计时器设计效果(职业院校技能大赛物联网技术应用项目原型设计题目)

目录 前言 一、本题实现效果 二、操作步骤 1.新建文件 2.界面设计 2.1文本框 2.2 按钮 2.3设计界面完成 3.交互 3.1启动交互设置 3.2 分别设置三个属性 3.2.1 设置值为“0” 3.2.2 文字于文本框 3.2.3 获取焦点时 3.3 停止按钮的交互动作 3.3.1 设置变量值 3.4 重…

私有化部署企业即时通讯(企业im)除了钉钉还有这些

在现代企业中&#xff0c;私有化部署企业即时通讯平台已经成为确保数据安全和实现高效通信的重要手段。除了众所周知的钉钉&#xff0c;WorkPlus作为领先品牌&#xff0c;提供私有化部署企业即时通讯的领先选择。本文将介绍WorkPlus为企业提供的广阔领域和精彩特点&#xff0c;…

详解FreeRTOS:FreeRTOS任务删除过程源码分析(进阶篇—2)

本篇博文讲解FreeRTOS中任务删除过程的源代码,帮助各位更好理解删除任务的原理和流程。 在详解FreeRTOS:FreeRTOS任务管理函数(基础篇—11)中,讲述了可以使用vTaskDelete()函数实现删除任务。 函数源码如下: 程序说明如下: (1)、调用函数 prvGetTCBFromHandle()获取要删…

MQTT Paho Android 支持SSL/TLS(亲测有效)

MQTT Paho Android 支持SSL/TLS(亲测有效) 登录时支持ssl的交互 这是调测登录界面设计 代码中对ssl/tls的支持 使用MqttAndroidClient配置mqtt客户端请求时&#xff0c;不加密及加密方式连接存在以下几点差异&#xff1a; url及端口差异 val uri: String if (tlsConnect…

链表oj题1(Leetcode)——移除链表元素,反转链表,链表的中间节点,

链表OJ 一&#xff0c;移除链表元素1.1分析1.2代码 二&#xff0c;找到链表的中间节点2.1分析2.2代码 三&#xff0c;反转链表3.1分析3.2代码 四&#xff0c;找到链表中倒数第k个节点4.1分析4.2代码 一&#xff0c;移除链表元素 移除链表元素 1.1分析 这里的删除要分成两种…

9月19日作业

完成文本编辑器的保存工作-代码&#xff1a; void Widget::on_pushButton_4_clicked() {//创建保存文件对话框QString filename QFileDialog::getSaveFileName(this,"保存文件","./","All(*.*);;Text files (*.txt)");//创建一个文件对象&…

【VisualStudio】NuGet包管理器下载缓存packages文件夹过大怎么清理

使用Visual Studio 开发工具时间长了&#xff0c;会发现整个项目的总大小越来越大&#xff0c;默认是存放在电脑系统盘里的&#xff0c;随着Windows11系统常常更新重启&#xff0c;导致系统盘闲置空间越来越小&#xff0c;该怎么办呢。 描述问题 整个解决方案项目会越变越大&…