测试环境
系统版本:
Distributor ID: CentOS
Description: CentOS Linux release 7.3.1611 (Core)
Release: 7.3.1611
redis版本:redis-4.0.10
1、安装redis
2、修改对应的redis节点配置,修改项:
daemonize yes
pidfile /var/run/redis_6379.pid
cluster-config-file nodes-6379.conf
port 6379
cluster-enabled yes
3、启动所有节点
[root@localhost redis-6379]# ./bin/redis-server ect/redis.conf
5669:C 25 Jun 03:39:23.641 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5669:C 25 Jun 03:39:23.641 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5669, just started
5669:C 25 Jun 03:39:23.641 # Configuration loaded
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6380.conf
5690:C 25 Jun 03:45:58.093 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5690:C 25 Jun 03:45:58.093 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5690, just started
5690:C 25 Jun 03:45:58.093 # Configuration loaded
[root@localhost redis-6379]#
[root@localhost redis-6379]#
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6381.conf
5695:C 25 Jun 03:46:04.122 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5695:C 25 Jun 03:46:04.122 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5695, just started
5695:C 25 Jun 03:46:04.123 # Configuration loaded
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6382.conf
5700:C 25 Jun 03:46:07.380 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5700:C 25 Jun 03:46:07.380 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5700, just started
5700:C 25 Jun 03:46:07.380 # Configuration loaded
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6383.conf
5705:C 25 Jun 03:46:10.661 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5705:C 25 Jun 03:46:10.661 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5705, just started
5705:C 25 Jun 03:46:10.661 # Configuration loaded
[root@localhost redis-6379]# ./bin/redis-server ect/redis-6384.conf
5710:C 25 Jun 03:46:13.807 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5710:C 25 Jun 03:46:13.808 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5710, just started
5710:C 25 Jun 03:46:13.808 # Configuration loaded
4、查看节点启动情况,日志有无报错,进程是否存在
[root@localhost redis-6379]# ps -ef | grep redis
root 5670 1 0 03:39 ? 00:00:01 ./bin/redis-server 127.0.0.1:6379 [cluster]
root 5691 1 0 03:45 ? 00:00:00 ./bin/redis-server 127.0.0.1:6380 [cluster]
root 5696 1 0 03:46 ? 00:00:00 ./bin/redis-server 127.0.0.1:6381 [cluster]
root 5701 1 0 03:46 ? 00:00:00 ./bin/redis-server 127.0.0.1:6382 [cluster]
root 5706 1 0 03:46 ? 00:00:00 ./bin/redis-server 127.0.0.1:6383 [cluster]
root 5711 1 0 03:46 ? 00:00:00 ./bin/redis-server 127.0.0.1:6384 [cluster]
5、连接其中一个节点
[root@localhost redis-6379]# ./bin/redis-cli
127.0.0.1:6379> CLUSTER NODES
9768ddf51689eaf6a4143fd946e5138bde292e1e :6379@16379 myself,master - 0 0 0 connected
看到集群节点数只有自己一个节点,这是因为各个单独的子节点间没有进行通讯和关联
6、各个节点间建立关联
127.0.0.1:6379> cluster meet 127.0.0.1 6380
OK
127.0.0.1:6379> CLUSTER NODES
9768ddf51689eaf6a4143fd946e5138bde292e1e 127.0.0.1:6379@16379 myself,master - 0 0 1 connected
9ca580d9bfd309125cacb3f3d84014adce3f559b 127.0.0.1:6380@16380 master - 0 1529912974096 0 connected
127.0.0.1:6379> cluster meet 127.0.0.1 6381
OK
127.0.0.1:6379> cluster meet 127.0.0.1 6382
OK
127.0.0.1:6379> cluster meet 127.0.0.1 6383
OK
127.0.0.1:6379> cluster meet 127.0.0.1 6384
OK
127.0.0.1:6379> CLUSTER NODES
9768ddf51689eaf6a4143fd946e5138bde292e1e 127.0.0.1:6379@16379 myself,master - 0 1529913014000 1 connected
c87c6c9d81e854380533eb6eda1f93bc0d54d5c5 127.0.0.1:6382@16382 master - 0 1529913015462 0 connected
9ca580d9bfd309125cacb3f3d84014adce3f559b 127.0.0.1:6380@16380 master - 0 1529913014000 2 connected
d5543d31a40230c58854986eab3ec43aa4e9b0b2 127.0.0.1:6384@16384 master - 0 1529913014000 0 connected
8a813463c796cdb515837d4d4a0e9ae5a76cb912 127.0.0.1:6383@16383 master - 0 1529913014454 4 connected
a24733a1f2e080b6f638086fa74c6904c2468eec 127.0.0.1:6381@16381 master - 0 1529913014000 3 connected
6、solt划分。这时候集群节点已经可以相互感知,但是现在的集群状态还是失败的,因为还没有进行solt的划分
127.0.0.1:6379> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:74
cluster_stats_messages_pong_sent:86
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:165
cluster_stats_messages_ping_received:86
cluster_stats_messages_pong_received:79
cluster_stats_messages_received:165
solt划分命令:
[root@localhost redis-6379]# ./bin/redis-cli -h 127.0.0.1 -p 6379 cluster addslots {0..5461}
OK
[root@localhost redis-6379]# ./bin/redis-cli -h 127.0.0.1 -p 6380 cluster addslots {5462..10922}
OK
[root@localhost redis-6379]# ./bin/redis-cli -h 127.0.0.1 -p 6381 cluster addslots {10922..16383}
(error) ERR Slot 10922 is already busy
[root@localhost redis-6379]# ./bin/redis-cli -h 127.0.0.1 -p 6381 cluster addslots {10923..16383}
OK
这时候再查看集群状态
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:242
cluster_stats_messages_pong_sent:263
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:510
cluster_stats_messages_ping_received:263
cluster_stats_messages_pong_received:247
cluster_stats_messages_received:510
7、主从节点配置。现在看集群各个节点的状态会发现显示都是master节点。
127.0.0.1:6379> cluster nodes
9768ddf51689eaf6a4143fd946e5138bde292e1e 127.0.0.1:6379@16379 myself,master - 0 1529913448000 1 connected 0-5461
c87c6c9d81e854380533eb6eda1f93bc0d54d5c5 127.0.0.1:6382@16382 master - 0 1529913450386 5 connected
9ca580d9bfd309125cacb3f3d84014adce3f559b 127.0.0.1:6380@16380 master - 0 1529913447361 2 connected 5462-10922
d5543d31a40230c58854986eab3ec43aa4e9b0b2 127.0.0.1:6384@16384 master - 0 1529913446000 0 connected
8a813463c796cdb515837d4d4a0e9ae5a76cb912 127.0.0.1:6383@16383 master - 0 1529913449377 4 connected
a24733a1f2e080b6f638086fa74c6904c2468eec 127.0.0.1:6381@16381 master - 0 1529913448371 3 connected 10923-16383
接下来进行从节点的指定:
[root@localhost bin]# ./redis-cli -h 127.0.0.1 -p 6382 cluster replicate 9768ddf51689eaf6a4143fd946e5138bde292e1e
OK
[root@localhost bin]# ./redis-cli -h 127.0.0.1 -p 6383 cluster replicate 9ca580d9bfd309125cacb3f3d84014adce3f559b
OK
[root@localhost bin]# ./redis-cli -h 127.0.0.1 -p 6384 cluster replicate a24733a1f2e080b6f638086fa74c6904c2468eec
OK
然后再查看集群各节点状态
127.0.0.1:6379> cluster nodes
9768ddf51689eaf6a4143fd946e5138bde292e1e 127.0.0.1:6379@16379 myself,master - 0 1529913983000 1 connected 0-5461
c87c6c9d81e854380533eb6eda1f93bc0d54d5c5 127.0.0.1:6382@16382 slave 9768ddf51689eaf6a4143fd946e5138bde292e1e 0 1529913984000 5 connected
9ca580d9bfd309125cacb3f3d84014adce3f559b 127.0.0.1:6380@16380 master - 0 1529913984693 2 connected 5462-10922
d5543d31a40230c58854986eab3ec43aa4e9b0b2 127.0.0.1:6384@16384 slave a24733a1f2e080b6f638086fa74c6904c2468eec 0 1529913981000 3 connected
8a813463c796cdb515837d4d4a0e9ae5a76cb912 127.0.0.1:6383@16383 slave 9ca580d9bfd309125cacb3f3d84014adce3f559b 0 1529913984000 4 connected
a24733a1f2e080b6f638086fa74c6904c2468eec 127.0.0.1:6381@16381 master - 0 1529913982000 3 connected 10923-16383
发现已经是三主三从的状态。
至此三主三从集群已经搭建完成,可以进行连接测试了。
连接方式:redis_cli -c -h 127.0.0.1 -p 6379
测试方法:登录其中一个主节点写信息,然后登录另一个主节点查询