Bootstrap

redis集群部署 (通过redis工具快速部署,手动部署)

目录

一、快速部署集群

1、 进入集群目录,创建集群

2、 查看正常启动

二、部署集群 

 1、分配集群节点         

 2、验证集群可用性

 3、停止redis进程

三、手动部署集群

1、配置redis.conf配置文件

2、启动redis集群

3、手动创建redis集群

4、验证

四、集群操作指令 

#-c 自动跳转对应的槽位

#把俩个key放道同一个槽位,使用打印标签的方式{里面的内容随便,写什么都可以}

#查看集群信息(可以查看对应的集群里面的keys有多少)

#检查集群状态

#查看redis集群槽位分布 


-----------------------------跳过安装过程---------------------------------------

一、快速部署集群

       1、 进入集群目录,创建集群

[root@localhost ~]# cd /usr/local/redis-6.2.12/utils/create-cluster
[root@localhost create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006

2、 查看正常启动

[root@localhost create-cluster]# netstat -pltun
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:30001           0.0.0.0:*               LISTEN      1644/../../src//red
tcp        0      0 0.0.0.0:30002           0.0.0.0:*               LISTEN      1646/../../src//red
tcp        0      0 0.0.0.0:30003           0.0.0.0:*               LISTEN      1648/../../src//red
tcp        0      0 0.0.0.0:30004           0.0.0.0:*               LISTEN      1656/../../src//red
tcp        0      0 0.0.0.0:30005           0.0.0.0:*               LISTEN      1661/../../src//red
tcp        0      0 0.0.0.0:30006           0.0.0.0:*               LISTEN      1666/../../src//red
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1086/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1359/master
tcp        0      0 0.0.0.0:40001           0.0.0.0:*               LISTEN      1644/../../src//red
tcp        0      0 0.0.0.0:40002           0.0.0.0:*               LISTEN      1646/../../src//red
tcp        0      0 0.0.0.0:40003           0.0.0.0:*               LISTEN      1648/../../src//red
tcp        0      0 0.0.0.0:40004           0.0.0.0:*               LISTEN      1656/../../src//red
tcp        0      0 0.0.0.0:40005           0.0.0.0:*               LISTEN      1661/../../src//red
tcp        0      0 0.0.0.0:40006           0.0.0.0:*               LISTEN      1666/../../src//red
tcp6       0      0 :::30001                :::*                    LISTEN      1644/../../src//red
tcp6       0      0 :::30002                :::*                    LISTEN      1646/../../src//red
tcp6       0      0 :::30003                :::*                    LISTEN      1648/../../src//red
tcp6       0      0 :::30004                :::*                    LISTEN      1656/../../src//red
tcp6       0      0 :::30005                :::*                    LISTEN      1661/../../src//red
tcp6       0      0 :::30006                :::*                    LISTEN      1666/../../src//red
tcp6       0      0 :::22                   :::*                    LISTEN      1086/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1359/master
tcp6       0      0 :::40001                :::*                    LISTEN      1644/../../src//red
tcp6       0      0 :::40002                :::*                    LISTEN      1646/../../src//red
tcp6       0      0 :::40003                :::*                    LISTEN      1648/../../src//red
tcp6       0      0 :::40004                :::*                    LISTEN      1656/../../src//red
tcp6       0      0 :::40005                :::*                    LISTEN      1661/../../src//red
tcp6       0      0 :::40006                :::*                    LISTEN      1666/../../src//red
udp        0      0 0.0.0.0:68              0.0.0.0:*                           902/dhclient
[root@localhost create-cluster]# ps -ef|grep redis
root       1644      1  0 00:00 ?        00:00:00 ../../src//redis-server *:30001 [cluster]
root       1646      1  0 00:00 ?        00:00:00 ../../src//redis-server *:30002 [cluster]
root       1648      1  0 00:00 ?        00:00:00 ../../src//redis-server *:30003 [cluster]
root       1656      1  0 00:00 ?        00:00:00 ../../src//redis-server *:30004 [cluster]
root       1661      1  0 00:00 ?        00:00:00 ../../src//redis-server *:30005 [cluster]
root       1666      1  0 00:00 ?        00:00:00 ../../src//redis-server *:30006 [cluster]
root       1675   1608  0 00:00 pts/0    00:00:00 grep --color=auto redis

二、部署集群 

       1、分配集群节点         

[root@localhost create-cluster]# ll
total 8
-rwxrwxr-x. 1 root root 2787 Apr 17  2023 create-cluster
-rw-rw-r--. 1 root root 1437 Apr 17  2023 README
[root@localhost create-cluster]# ./create-cluster create
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: f8ec4c4889fb9a8c83e3b732bdd4df4ae529970c 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: 7c602a2fa09e149f4afa678da9deb4ba7a623b7a 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: a8c24cf5fdc40cb32ea8a8122fb6f553bfb1343f 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: 361ee2234efa8527ba9d255b29dcebb60a5a2f35 127.0.0.1:30004
   replicates f8ec4c4889fb9a8c83e3b732bdd4df4ae529970c
S: 7f307cd20409072ea9aa7c297fab6fa8be008a72 127.0.0.1:30005
   replicates 7c602a2fa09e149f4afa678da9deb4ba7a623b7a
S: 9083610ae287234adfb957adfa0d7d3e9f9a5e42 127.0.0.1:30006
   replicates a8c24cf5fdc40cb32ea8a8122fb6f553bfb1343f
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: f8ec4c4889fb9a8c83e3b732bdd4df4ae529970c 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 361ee2234efa8527ba9d255b29dcebb60a5a2f35 127.0.0.1:30004
   slots: (0 slots) slave
   replicates f8ec4c4889fb9a8c83e3b732bdd4df4ae529970c
S: 7f307cd20409072ea9aa7c297fab6fa8be008a72 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 7c602a2fa09e149f4afa678da9deb4ba7a623b7a
M: 7c602a2fa09e149f4afa678da9deb4ba7a623b7a 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 9083610ae287234adfb957adfa0d7d3e9f9a5e42 127.0.0.1:30006
   slots: (0 slots) slave
   replicates a8c24cf5fdc40cb32ea8a8122fb6f553bfb1343f
M: a8c24cf5fdc40cb32ea8a8122fb6f553bfb1343f 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost create-cluster]#

 2、验证集群可用性

[root@localhost create-cluster]# redis-cli -p 30001
127.0.0.1:30001> set name vV
(error) MOVED 5798 127.0.0.1:30002   #这里提示应该在5798 槽位放入键值对,但是你30001的不包含
127.0.0.1:30001> exit
[root@localhost create-cluster]# redis-cli -p 30001 -c   #加参数 -c 可以自行跳转对应的redis
127.0.0.1:30001> set name vV
-> Redirected to slot [5798] located at 127.0.0.1:30002
OK
127.0.0.1:30002> get name
"vV"
127.0.0.1:30002>

[root@localhost create-cluster]# ps -aux|grep reids
root       1727  0.0  0.0 112812   968 pts/0    S+   00:13   0:00 grep --color=auto reids
[root@localhost create-cluster]# ps -aux|grep redis
root       1644  0.1  0.0 146232  3252 ?        Ssl  00:00   0:01 ../../src//redis-server *:30001 [cluster]
root       1646  0.1  0.0 146140  3156 ?        Ssl  00:00   0:01 ../../src//redis-server *:30002 [cluster]
root       1648  0.1  0.0 146240  3252 ?        Ssl  00:00   0:01 ../../src//redis-server *:30003 [cluster]
root       1656  0.1  0.0 146136  3112 ?        Ssl  00:00   0:01 ../../src//redis-server *:30004 [cluster]
root       1661  0.1  0.0 146136  3124 ?        Ssl  00:00   0:01 ../../src//redis-server *:30005 [cluster]
root       1666  0.1  0.0 146136  3104 ?        Ssl  00:00   0:01 ../../src//redis-server *:30006 [cluster]
root       1729  0.0  0.0 112812   972 pts/0    S+   00:13   0:00 grep --color=auto redis
[root@localhost create-cluster]# pkill -9 redis
[root@localhost create-cluster]#
[root@localhost create-cluster]# ps -aux|grep redis
root       1732  0.0  0.0 112812   968 pts/0    S+   00:13   0:00 grep --color=auto redis

3、停止redis进程

[root@localhost create-cluster]# ps -aux|grep reids
root       1727  0.0  0.0 112812   968 pts/0    S+   00:13   0:00 grep --color=auto reids
[root@localhost create-cluster]# ps -aux|grep redis
root       1644  0.1  0.0 146232  3252 ?        Ssl  00:00   0:01 ../../src//redis-server *:30001 [cluster]
root       1646  0.1  0.0 146140  3156 ?        Ssl  00:00   0:01 ../../src//redis-server *:30002 [cluster]
root       1648  0.1  0.0 146240  3252 ?        Ssl  00:00   0:01 ../../src//redis-server *:30003 [cluster]
root       1656  0.1  0.0 146136  3112 ?        Ssl  00:00   0:01 ../../src//redis-server *:30004 [cluster]
root       1661  0.1  0.0 146136  3124 ?        Ssl  00:00   0:01 ../../src//redis-server *:30005 [cluster]
root       1666  0.1  0.0 146136  3104 ?        Ssl  00:00   0:01 ../../src//redis-server *:30006 [cluster]
root       1729  0.0  0.0 112812   972 pts/0    S+   00:13   0:00 grep --color=auto redis
[root@localhost create-cluster]# pkill -9 redis
[root@localhost create-cluster]#
[root@localhost create-cluster]# ps -aux|grep redis
root       1732  0.0  0.0 112812   968 pts/0    S+   00:13   0:00 grep --color=auto redis

三、手动部署集群

1、配置redis.conf配置文件

[root@localhost redis]# cd /usr/local/redis/
[root@localhost redis]# mkdir cluster

#创建6个redis.config文件
[root@localhost cluster]# vim redis-cluster-6379.conf
[root@localhost cluster]# vim redis-cluster-6380.conf
[root@localhost cluster]# vim redis-cluster-6381.conf
[root@localhost cluster]# vim redis-cluster-6382.conf
[root@localhost cluster]# vim redis-cluster-6383.conf
[root@localhost cluster]# vim redis-cluster-6382.conf
[root@localhost cluster]# vim redis-cluster-6384.conf
#里面内容如下,记得根据不同的配置文件修改文件里面的内容

root@localhost cluster]# cat redis-cluster-6379.conf
# 数据存储目录
# 指定 Redis 数据文件和配置文件的存储位置
dir "/usr/local/redis/cluster/db/6379"

# 启用集群模式
# 启用 Redis 集群功能,使该实例作为集群的一部分工作
cluster-enabled yes

# 集群配置文件
# 指定 Redis 集群的配置文件名,Redis 在运行时将存储集群状态
cluster-config-file 6379.conf

# 端口号
# 指定 Redis 实例监听的端口号
port 6379

# 守护进程模式
# 使 Redis 以守护进程方式运行,即在后台运行
daemonize yes

# 绑定 IP 地址
# 指定 Redis 监听的 IP 地址,0.0.0.0 表示监听所有网络接口
bind 0.0.0.0

# 密码保护
# 设置访问 Redis 实例所需的密码
#requirepass your_password_here

# 日志文件
# 指定 Redis 日志文件的存储位置
logfile "/usr/local/redis/cluster/logs/6379.log"

# 日志级别
# 指定日志记录的级别,可以是 debug、verbose、notice、warning
loglevel info




#创建目录
[root@localhost cluster]# mkdir db
[root@localhost cluster]# cd db/
[root@localhost db]# mkdir {6379,6380,6381,6382,6383,6384}
[root@localhost db]# ll
total 0
drwxr-xr-x. 2 root root 6 Jul  9 00:26 6379
drwxr-xr-x. 2 root root 6 Jul  9 00:26 6380
drwxr-xr-x. 2 root root 6 Jul  9 00:26 6381
drwxr-xr-x. 2 root root 6 Jul  9 00:26 6382
drwxr-xr-x. 2 root root 6 Jul  9 00:26 6383
drwxr-xr-x. 2 root root 6 Jul  9 00:26 6384


2、启动redis集群

[root@localhost cluster]# ../../redis/bin/redis-server redis-cluster-6379.conf
[root@localhost cluster]# ../../redis/bin/redis-server redis-cluster-6380.conf

[root@localhost cluster]# ../../redis/bin/redis-server redis-cluster-6381.conf

[root@localhost cluster]# ../../redis/bin/redis-server redis-cluster-6382.conf
[root@localhost cluster]# ../../redis/bin/redis-server redis-cluster-6383.conf
[root@localhost cluster]# ../../redis/bin/redis-server redis-cluster-6384.conf

[root@localhost cluster]# ps -ef|grep redis

root       1847      1  0 00:32 ?        00:00:00 ../../redis/bin/redis-server *:6379 [cluster]
root       1852      1  0 00:32 ?        00:00:00 ../../redis/bin/redis-server *:6380 [cluster]
root       1825      1  0 00:32 ?        00:00:00 ../../redis/bin/redis-server *:6381 [cluster]
root       1832      1  0 00:32 ?        00:00:00 ../../redis/bin/redis-server *:8382 [cluster]
root       1837      1  0 00:32 ?        00:00:00 ../../redis/bin/redis-server *:6383 [cluster]
root       1842      1  0 00:32 ?        00:00:00 ../../redis/bin/redis-server *:6384 [cluster]
 

 3、手动创建redis集群

[root@localhost cluster]# redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 2730
Master[1] -> Slots 2731 - 5460
Master[2] -> Slots 5461 - 8191
Master[3] -> Slots 8192 - 10922
Master[4] -> Slots 10923 - 13652
Master[5] -> Slots 13653 - 16383
M: 04f33b8ebfe689b5841ec5bf2e42a7583d4320ef 127.0.0.1:6379
   slots:[0-2730] (2731 slots) master
M: 7976cdeba2788063b916bccaac71d06a89ff63c4 127.0.0.1:6380
   slots:[2731-5460] (2730 slots) master
M: 268a39068026beef18fb5d88410c0d9f72779d4f 127.0.0.1:6381
   slots:[5461-8191] (2731 slots) master
M: 39d35937546e37d31d350d8b8357a1405a6336c7 127.0.0.1:6382
   slots:[8192-10922] (2731 slots) master
M: d4302a52d4d67f4834fd9933e0a6286b0d1803aa 127.0.0.1:6383
   slots:[10923-13652] (2730 slots) master
M: a28ebb8ef8fa184b9b6c2dc080fc7a458b12e228 127.0.0.1:6384
   slots:[13653-16383] (2731 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
......
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: 04f33b8ebfe689b5841ec5bf2e42a7583d4320ef 127.0.0.1:6379
   slots:[0-2730] (2731 slots) master
M: 39d35937546e37d31d350d8b8357a1405a6336c7 127.0.0.1:6382
   slots:[8192-10922] (2731 slots) master
M: 7976cdeba2788063b916bccaac71d06a89ff63c4 127.0.0.1:6380
   slots:[2731-5460] (2730 slots) master
M: 268a39068026beef18fb5d88410c0d9f72779d4f 127.0.0.1:6381
   slots:[5461-8191] (2731 slots) master
M: d4302a52d4d67f4834fd9933e0a6286b0d1803aa 127.0.0.1:6383
   slots:[10923-13652] (2730 slots) master
M: a28ebb8ef8fa184b9b6c2dc080fc7a458b12e228 127.0.0.1:6384
   slots:[13653-16383] (2731 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost cluster]#

4、验证

[root@localhost cluster]# redis-cli  -p 6379 -c
127.0.0.1:6379> set name vV
-> Redirected to slot [5798] located at 127.0.0.1:6381
OK
127.0.0.1:6381> get name
"vV"
127.0.0.1:6381>
127.0.0.1:6381>

四、集群操作指令 

#-c 自动跳转对应的槽位

 redis-cli  -p 6379 -c

127.0.0.1:6379> set name vV
-> Redirected to slot [5798] located at 127.0.0.1:6381
OK
127.0.0.1:6381> get name
"vV"

#把俩个key放道同一个槽位,使用打印标签的方式{里面的内容随便,写什么都可以}

127.0.0.1:6383> set a 123
-> Redirected to slot [15495] located at 127.0.0.1:6384
OK
127.0.0.1:6384> set b 456
-> Redirected to slot [3300] located at 127.0.0.1:6380
OK
127.0.0.1:6380> set {user}a 123
-> Redirected to slot [5474] located at 127.0.0.1:6381
OK
127.0.0.1:6381> set {user}b 123
OK
127.0.0.1:6381> set {user}c 123
OK
127.0.0.1:6381>

#查看集群信息(可以查看对应的集群里面的keys有多少)

[root@localhost cluster]# redis-cli --cluster info  127.0.0.1 6379
127.0.0.1:6379 (04f33b8e...) -> 0 keys | 2731 slots | 0 slaves.
127.0.0.1:6382 (39d35937...) -> 0 keys | 2731 slots | 0 slaves.
127.0.0.1:6380 (7976cdeb...) -> 1 keys | 2730 slots | 0 slaves.
127.0.0.1:6381 (268a3906...) -> 6 keys | 2731 slots | 0 slaves.
127.0.0.1:6383 (d4302a52...) -> 1 keys | 2730 slots | 0 slaves.
127.0.0.1:6384 (a28ebb8e...) -> 3 keys | 2731 slots | 0 slaves.
[OK] 11 keys in 6 masters.
0.00 keys per slot on average.
[root@localhost cluster]#

#检查集群状态

[root@localhost cluster]# redis-cli --cluster check 127.0.0.1 6379
127.0.0.1:6379 (04f33b8e...) -> 0 keys | 2731 slots | 0 slaves.
127.0.0.1:6382 (39d35937...) -> 0 keys | 2731 slots | 0 slaves.
127.0.0.1:6380 (7976cdeb...) -> 1 keys | 2730 slots | 0 slaves.
127.0.0.1:6381 (268a3906...) -> 6 keys | 2731 slots | 0 slaves.
127.0.0.1:6383 (d4302a52...) -> 1 keys | 2730 slots | 0 slaves.
127.0.0.1:6384 (a28ebb8e...) -> 3 keys | 2731 slots | 0 slaves.
[OK] 11 keys in 6 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: 04f33b8ebfe689b5841ec5bf2e42a7583d4320ef 127.0.0.1:6379
   slots:[0-2730] (2731 slots) master
M: 39d35937546e37d31d350d8b8357a1405a6336c7 127.0.0.1:6382
   slots:[8192-10922] (2731 slots) master
M: 7976cdeba2788063b916bccaac71d06a89ff63c4 127.0.0.1:6380
   slots:[2731-5460] (2730 slots) master
M: 268a39068026beef18fb5d88410c0d9f72779d4f 127.0.0.1:6381
   slots:[5461-8191] (2731 slots) master
M: d4302a52d4d67f4834fd9933e0a6286b0d1803aa 127.0.0.1:6383
   slots:[10923-13652] (2730 slots) master
M: a28ebb8ef8fa184b9b6c2dc080fc7a458b12e228 127.0.0.1:6384
   slots:[13653-16383] (2731 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost cluster]#

#查看redis集群槽位分布 


127.0.0.1:6379> cluster slots
1) 1) (integer) 0
   2) (integer) 2730
   3) 1) "127.0.0.1"
      2) (integer) 6379
      3) "04f33b8ebfe689b5841ec5bf2e42a7583d4320ef"
2) 1) (integer) 2731
   2) (integer) 5460
   3) 1) "127.0.0.1"
      2) (integer) 6380
      3) "7976cdeba2788063b916bccaac71d06a89ff63c4"
3) 1) (integer) 5461
   2) (integer) 8191
   3) 1) "127.0.0.1"
      2) (integer) 6381
      3) "268a39068026beef18fb5d88410c0d9f72779d4f"
4) 1) (integer) 8192
   2) (integer) 10922
   3) 1) "127.0.0.1"
      2) (integer) 6382
      3) "39d35937546e37d31d350d8b8357a1405a6336c7"
5) 1) (integer) 10923
   2) (integer) 13652
   3) 1) "127.0.0.1"
      2) (integer) 6383
      3) "d4302a52d4d67f4834fd9933e0a6286b0d1803aa"
6) 1) (integer) 13653
   2) (integer) 16383
   3) 1) "127.0.0.1"
      2) (integer) 6384
      3) "a28ebb8ef8fa184b9b6c2dc080fc7a458b12e228"
127.0.0.1:6379>

;