我这里是用于学习,所以使用的是虚拟机VMware,使用克隆功能复制了第二台服务器。第一台作为管理机,开7001,7002,7003端口的redis服务,第二台开7004,7005,7006端口的服务。
//多机多节点集群
Xshell 5 (Build 1339)
Copyright (c) 2002-2017 NetSarang Computer, Inc. All rights reserved.
Type `help' to learn how to use Xshell prompt.
[d:\~]$
Connecting to 192.168.0.154:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Wed Jan 31 17:24:43 2018 from 192.168.0.122
[root@bogon ~]# cd /usr/local/redis/
//清理一下持久化数据和记录文件,防止启动集群时报错。文件有需要的可以剪切走,启动完后复制回来
[root@bogon redis_cluster]# ll
总用量 32
drwxr-xr-x. 2 root root 23 1月 30 23:12 7001
drwxr-xr-x. 2 root root 23 1月 30 23:12 7002
drwxr-xr-x. 2 root root 23 1月 30 23:13 7003
drwxr-xr-x. 2 root root 23 1月 30 23:13 7004
drwxr-xr-x. 2 root root 23 1月 30 23:13 7005
drwxr-xr-x. 2 root root 23 1月 30 23:13 7006
-rw-r--r--. 1 root root 54 1月 31 00:03 appendonly.aof
-rw-r--r--. 1 root root 88 1月 31 00:10 dump.rdb
-rw-r--r--. 1 root root 715 1月 31 00:06 nodes-7001.conf
-rw-r--r--. 1 root root 733 1月 30 23:55 nodes-7002.conf
-rw-r--r--. 1 root root 715 1月 31 00:06 nodes-7003.conf
-rw-r--r--. 1 root root 715 1月 31 00:06 nodes-7004.conf
-rw-r--r--. 1 root root 715 1月 31 00:06 nodes-7005.conf
-rw-r--r--. 1 root root 715 1月 31 00:06 nodes-7006.conf
[root@bogon redis_cluster]# rm -rf dump.rdb
[root@bogon redis_cluster]# rm -rf appendonly.aof
[root@bogon redis_cluster]# rm -rf nodes-7001.conf
[root@bogon redis_cluster]# rm -rf nodes-7002.conf
[root@bogon redis_cluster]# rm -rf nodes-7003.conf
[root@bogon redis_cluster]# rm -rf nodes-7004.conf
[root@bogon redis_cluster]# rm -rf nodes-7005.conf
[root@bogon redis_cluster]# rm -rf nodes-7006.conf
[root@bogon redis_cluster]# ll
总用量 0
drwxr-xr-x. 2 root root 23 1月 30 23:12 7001
drwxr-xr-x. 2 root root 23 1月 30 23:12 7002
drwxr-xr-x. 2 root root 23 1月 30 23:13 7003
drwxr-xr-x. 2 root root 23 1月 30 23:13 7004
drwxr-xr-x. 2 root root 23 1月 30 23:13 7005
drwxr-xr-x. 2 root root 23 1月 30 23:13 7006
//因为要机器互相访问,修改配置文件bind绑定为本机ip,而不是127.0.0.1
//同理另一台服务器也做一样的操作,只是把修改的端口改为7004,7005,7006
[root@bogon redis_cluster]# vi 7001/redis.conf
[root@bogon redis_cluster]# vi 7002/redis.conf
[root@bogon redis_cluster]# vi 7003/redis.conf
[root@bogon redis_cluster]# cd
//启动redis服务,另一台服务器也启动
[root@bogon ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf
[root@bogon ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf
[root@bogon ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf
//查看是否启动成功
[root@bogon ~]# ps -ef | grep -i redis
root 3039 1 0 17:47 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.154:7001 [cluster]
root 3043 1 0 17:47 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.154:7002 [cluster]
root 3047 1 0 17:47 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.0.154:7003 [cluster]
root 3053 2880 0 17:48 pts/0 00:00:00 grep --color=auto -i redis
//涉及端口比较多这里吧防火墙关闭了,实际应用可以一个个开打对应端口。同理另一台服务器的也关闭
[root@bogon ~]# systemctl stop firewalld.service
//启动集群
[root@bogon ~]# redis-trib.rb create --replicas 1 192.168.0.154:7001 192.168.0.154:7002 192.168.0.154:7003 192.168.0.143:7004 192.168.0.143:7005 192.168.0.143:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.0.154:7001
192.168.0.143:7004
192.168.0.154:7002
//集群主从关系
Adding replica 192.168.0.143:7005 to 192.168.0.154:7001
Adding replica 192.168.0.154:7003 to 192.168.0.143:7004
Adding replica 192.168.0.143:7006 to 192.168.0.154:7002
M: 73ad1e1f7163ee72e5c8ee5edd46222918eabcce 192.168.0.154:7001
slots:0-5460 (5461 slots) master
M: 891fe35184253ba97eba679da510516ceb6b9a7b 192.168.0.154:7002
slots:10923-16383 (5461 slots) master
S: 36719cfe980d6eb5b81f82a1fec3a33e35493c19 192.168.0.154:7003
replicates 116564333cbe786490e578d2253d1bc278097cba
M: 116564333cbe786490e578d2253d1bc278097cba 192.168.0.143:7004
slots:5461-10922 (5462 slots) master
S: bccfbda54e7f5014ccf539c7d339e15d9886620a 192.168.0.143:7005
replicates 73ad1e1f7163ee72e5c8ee5edd46222918eabcce
S: 387f38c366285d7e1775efc46b57c46d2c46a1a1 192.168.0.143:7006
replicates 891fe35184253ba97eba679da510516ceb6b9a7b
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 192.168.0.154:7001)
M: 73ad1e1f7163ee72e5c8ee5edd46222918eabcce 192.168.0.154:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 387f38c366285d7e1775efc46b57c46d2c46a1a1 192.168.0.143:7006
slots: (0 slots) slave
replicates 891fe35184253ba97eba679da510516ceb6b9a7b
M: 116564333cbe786490e578d2253d1bc278097cba 192.168.0.143:7004
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: bccfbda54e7f5014ccf539c7d339e15d9886620a 192.168.0.143:7005
slots: (0 slots) slave
replicates 73ad1e1f7163ee72e5c8ee5edd46222918eabcce
M: 891fe35184253ba97eba679da510516ceb6b9a7b 192.168.0.154:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 36719cfe980d6eb5b81f82a1fec3a33e35493c19 192.168.0.154:7003
slots: (0 slots) slave
replicates 116564333cbe786490e578d2253d1bc278097cba
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
//打开客户端,测试集群,-h指定是那台服务器的,-c表示集群,-p指定端口
[root@bogon ~]# /usr/local/redis/bin/redis-cli -h 192.168.0.154 -c -p 7002
192.168.0.154:7002> set a "hihi"
OK
//自动计算分配
192.168.0.154:7002> set b "haha"
-> Redirected to slot [3300] located at 192.168.0.154:7001
OK
192.168.0.154:7001> quit
[root@bogon ~]# /usr/local/redis/bin/redis-cli -h 192.168.0.143 -c -p 7004
//自动计算获取
192.168.0.143:7004> get a
-> Redirected to slot [15495] located at 192.168.0.154:7002
"hihi"
192.168.0.154:7002> get b
-> Redirected to slot [3300] located at 192.168.0.154:7001
"haha"
192.168.0.154:7001> quit
//宕机测试,杀死7002端口redis进程
[root@bogon ~]# ps -ef | grep -i redis
root 3039 1 0 17:47 ? 00:00:04 /usr/local/redis/bin/redis-server 192.168.0.154:7001 [cluster]
root 3043 1 0 17:47 ? 00:00:04 /usr/local/redis/bin/redis-server 192.168.0.154:7002 [cluster]
root 3047 1 0 17:47 ? 00:00:04 /usr/local/redis/bin/redis-server 192.168.0.154:7003 [cluster]
root 3179 2880 0 18:01 pts/0 00:00:00 grep --color=auto -i redis
[root@bogon ~]# kill -9 3043
[root@bogon ~]# ps -ef | grep -i redis
root 3039 1 0 17:47 ? 00:00:04 /usr/local/redis/bin/redis-server 192.168.0.154:7001 [cluster]
root 3047 1 0 17:47 ? 00:00:04 /usr/local/redis/bin/redis-server 192.168.0.154:7003 [cluster]
root 3182 2880 0 18:01 pts/0 00:00:00 grep --color=auto -i redis
//用redis-trib.rb工具的check命令查询主从关系
[root@bogon ~]# redis-trib.rb check 192.168.0.154:7001
>>> Performing Cluster Check (using node 192.168.0.154:7001)
M: 73ad1e1f7163ee72e5c8ee5edd46222918eabcce 192.168.0.154:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
//原本的从节点自带转换为主节点,系统正常运行。假如把7006也杀死则,主从节点都消失,此段哈希槽空缺,系统报错。
M: 387f38c366285d7e1775efc46b57c46d2c46a1a1 192.168.0.143:7006
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 116564333cbe786490e578d2253d1bc278097cba 192.168.0.143:7004
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: bccfbda54e7f5014ccf539c7d339e15d9886620a 192.168.0.143:7005
slots: (0 slots) slave
replicates 73ad1e1f7163ee72e5c8ee5edd46222918eabcce
S: 36719cfe980d6eb5b81f82a1fec3a33e35493c19 192.168.0.154:7003
slots: (0 slots) slave
replicates 116564333cbe786490e578d2253d1bc278097cba
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@bogon ~]#