五、搭建Redis 集群

准备6台redis服务器,具体要求如下:
ip地址 端口 etho 日志文件名
192.168.4.51 6051 都可以接收连接请求 redis51.log
192.168.4.52 6052 都可以接收连接请求 redis52.log
192.168.4.53 6053 都可以接收连接请求 redis53.log
192.168.4.54 6054 都可以接收连接请求 redis54.log
192.168.4.55 6055 都可以接收连接请求 redis55.log
192.168.4.56 6056 都可以接收连接请求 redis56.log
环境准备
1 在6台redis服务器上运行服务,按照如下要求修改配置文件后,重启redis服务。
]#vim /etc/redis/6379.conf
bind 192.168.4.56 #只指定物理接口ip地址
port 6056 #不允许相同
logfile /var/log/redis56.log #配置日志文件名称
daemonize yes #守护进程方式运行
cluster-enabled yes #开启集群
cluster-config-file nodes-6056.conf #集群的配置文件不要使用默认名称不允许相同
cluster-node-timeout 5000 #集群节点之间通信超时时间单位秒
:wq
停止服务
]# redis-cli -h 192.168.4.56 -p 6056 [ -a 密码 ] shutdown
启动服务
]# /etc/init.d/redis_6379 start
连接服务
[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6056
装包: 在执行创建集群命令的主机安装ruby软件包(192.168.4.51)
安装解释ruby代码的软件包
[root@host51 ~]# yum -y install ruby rubygems
[root@host51 ~]# rpm -q rubygems ruby
rubygems-2.0.14.1-30.el7.noarch
ruby-2.0.0.648-30.el7.x86_64
[root@host51 ~]#
[root@host51 redis-cluster]# rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm
安装ruby连接redis 接口程序 gem
[root@host51 redis-cluster]# which gem
/usr/bin/gem
[root@host51 redis-cluster]# gem install redis-3.2.1.gem
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
Installing ri documentation for redis-3.2.1
1 gem installed
[root@host51 redis-cluster]#
生成创建集群的脚本redis-trib.rb
[root@host51 src]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host51 src]# cd
[root@host51 ~]# cp redis-4.0.8/src/redis-trib.rb /usr/local/bin/
创建集群:
[root@host51 ~]# redis-trib.rb create --replicas 1 \
192.168.4.51:6051 \
192.168.4.52:6052 \
192.168.4.53:6053 \
192.168.4.54:6054 \
192.168.4.55:6055 \
192.168.4.56:6056
可能遇到以下错误:
[ERR] Node 192.168.4.54:6054 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决办法如下:
[root@db54 ~]# redis-cli -h 192.168.4.54 -p 6054
192.168.4.54:6054> flushall
OK
192.168.4.54:6054> keys *
(empty list or set)
192.168.4.54:6054> flushall
OK
192.168.4.54:6054> save
OK
192.168.4.54:6054> quit
[root@db54 ~]#

槽位个数是16384
范围0-16383
[root@host51 ~]# redis-trib.rb create --replicas 1 192.168.4.51:6051 192.168.4.52:6052
192.168.4.53:6053 192.168.4.54:6054 192.168.4.55:6055 192.168.4.56:6056 #创建集群
--replicas 1 表示给master 配置1个slave
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6051
192.168.4.52:6052
192.168.4.53:6053
Adding replica 192.168.4.55:6055 to 192.168.4.51:6051
Adding replica 192.168.4.56:6056 to 192.168.4.52:6052
Adding replica 192.168.4.54:6054 to 192.168.4.53:6053
M: 0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051
slots:0-5460 (5461 slots) master
M: 5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052
slots:5461-10922 (5462 slots) master
M: 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053
slots:10923-16383 (5461 slots) master
S: 388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054
replicates 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc
S: 651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055
replicates 0ec903c572270a90f3b140fba31aac15aaf5336b
S: a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056
replicates 5278df7384edc9774b1a36b0b9d60a813a7424a9
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.4.51:6051)
M: 0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054
slots:9678 (1 slots) master
0 additional replica(s)
S: 651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055
slots: (0 slots) slave
replicates 0ec903c572270a90f3b140fba31aac15aaf5336b
S: a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056
slots: (0 slots) slave
replicates 5278df7384edc9774b1a36b0b9d60a813a7424a9
M: 6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052
slots:5461-9677,9679-10922 (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@host51 ~]#
[root@db52 redis-4.0.8]# cat /var/lib/redis/6052/nodes-6052.conf #集群配置文件
6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053@16053 master - 0 15260966113743 connected 10923-16383
0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051@16051 master - 0 15260966120001 connected 0-5460
388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054@16054 master - 0 15260966110434 connected 9678
a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056@16056 slave 5278df7384edc9774b1a36b0b9d60a813a7424a9 0 1526096611745 6 connected
651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055@16055 slave 0ec903c572270a90f3b140fba31aac15aaf5336b 0 1526096613000 5 connected
5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052@16052 myself,master - 01526096612000 2 connected 5461-9677 9679-10922 vars currentEpoch 6 lastVoteEpoch 0
[root@db52 redis-4.0.8]#
每台 redis服务 在本机登录 查看集群信息
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
-c 连接集群中的主机
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> CLUSTER info #查看本机信息
192.168.4.51:6051> CLUSTER nodes #查看集群信息
192.168.4.51:6051> quit
192.168.4.51:6051> CLUSTER nodes
388c33e7128fc961b381ad3b3c27c3c217912666 192.168.4.54:6054@16054 master - 0 15261088379424 connected 9678
651f7d99965316c1b8a27a2e9b034a5b14c2be55 192.168.4.55:6055@16055 slave 0ec903c572270a90f3b140fba31aac15aaf5336b 0 1526108838444 5 connected
a3af3096ee214c92a178eadf6e9299584899e62f 192.168.4.56:6056@16056 slave 5278df7384edc9774b1a36b0b9d60a813a7424a9 0 1526108838946 6 connected
6cdb4c64c48c0ee2ca35bf139660f31ca92821dc 192.168.4.53:6053@16053 master - 0 15261088379423 connected 10923-16383
0ec903c572270a90f3b140fba31aac15aaf5336b 192.168.4.51:6051@16051 myself,master - 01526108836000 1 connected 0-5460
5278df7384edc9774b1a36b0b9d60a813a7424a9 192.168.4.52:6052@16052 master - 01526108839447 2 connected 5461-9677 9679-10922
192.168.4.51:6051> 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:4
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:26580
cluster_stats_messages_pong_sent:20656
cluster_stats_messages_update_sent:2
cluster_stats_messages_sent:47238
cluster_stats_messages_ping_received:20651
cluster_stats_messages_pong_received:26580
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:47236
192.168.4.51:6051>
测试集群:连接master库 存储数据,对应的从库 会自动同步数据
[root@host51 ~]# redis-cli -c -h master库ip地址 -p 对应的端口号
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> CLUSTER nodes
192.168.4.51:6051>
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> set age 99
OK
192.168.4.51:6051> get age
"99"
192.168.4.51:6051>

[root@db54 ~]# redis-cli -c -h 192.168.4.54 -p 6054
192.168.4.54:6054> get age
-> Redirected to slot [741] located at 192.168.4.51:6051
"99"
192.168.4.51:6051>

上一篇:android – java.lang.ClassNotFoundException:没有找到类“kotlinx.coroutines.experimental.Deferred”


下一篇:源码笔记之Retrofit