Redis-集羣
一、準備六臺Redis服務器
準備源碼包redis-4.0.8.tar.gz
源碼編譯安裝
]# rpm -q gcc
]# yum -y install gcc
]# tar -zxf redis-4.0.8.tar.gz
]# cd redis-4.0.8/
redis-4.0.8]# make && make install
初始化
- 使用默認配置
redis-4.0.8]# ./utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
]# netstat -utnlp |grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 5140/redis-server 1
二、創建集羣
2.1、啓用每臺redis服務器集羣功能
- Redis1
[root@redis1 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.51 //修改IP地址
port 6351 //定義端口號,集羣中多臺Redis服務器端口不能相同
cluster-enabled yes //啓用集羣功能
cluster-config-file nodes-6379.conf //存儲集羣信息文件
cluster-node-timeout 5000 //超時時間
[root@redis1 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis1 ~]# netstat -utnlp | grep redis
tcp 0 0 192.168.4.51:16351 0.0.0.0:* LISTEN 1626/redis-server 1
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 1626/redis-server 1
[root@redis1 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.51:6351> exit
[root@redis1 ~]# ls /var/lib/redis/6379/
dump.rdb nodes-6379.conf
[root@redis1 ~]# cat /var/lib/redis/6379/nodes-6379.conf
c476e663fa6bf2e43ea7592edd74b3e143446d56 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
- Redis2
[root@redis2 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.52
port 6352
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@redis2 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis2 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis2 ~]# netstat -utnlp | grep redis
tcp 0 0 192.168.4.52:16352 0.0.0.0:* LISTEN 5450/redis-server 1
tcp 0 0 192.168.4.52:6352 0.0.0.0:* LISTEN 5450/redis-server 1
[root@redis2 ~]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.52:6352> exit
[root@redis2 ~]# ls /var/lib/redis/6379/
dump.rdb nodes-6379.conf
[root@redis2 ~]# cat /var/lib/redis/6379/nodes-6379.conf
9e83ee6b3249077c63396b85f623fa12fe27dccc :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
- Redis3
[root@redis3 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.53
port 6353
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@redis3 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis3 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis3 ~]# netstat -utnlp | grep redis
tcp 0 0 192.168.4.53:16353 0.0.0.0:* LISTEN 6838/redis-server 1
tcp 0 0 192.168.4.53:6353 0.0.0.0:* LISTEN 6838/redis-server 1
[root@redis3 ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.53:6353> exit
[root@redis3 ~]# ls /var/lib/redis/6379/
dump.rdb nodes-6379.conf
[root@redis3 ~]# cat /var/lib/redis/6379/nodes-6379.conf
13067f3c56639214b51f4f7e187e7608e2a99d4a :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
- Redis4
[root@redis4 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.54
port 6354
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@redis4 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis4 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis4 ~]# netstat -utnlp | grep redis
tcp 0 0 192.168.4.54:16354 0.0.0.0:* LISTEN 5324/redis-server 1
tcp 0 0 192.168.4.54:6354 0.0.0.0:* LISTEN 5324/redis-server 1
[root@redis4 ~]# redis-cli -h 192.168.4.54 -p 6354
192.168.4.54:6354> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.54:6354> exit
[root@redis4 ~]# ls /var/lib/redis/6379/
dump.rdb nodes-6379.conf
[root@redis4 ~]# cat /var/lib/redis/6379/nodes-6379.conf
5988a140790cb3e8c39e81acf1e585094656e0a2 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
- Redis5
[root@redis5 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.55
port 6355
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@redis5 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis5 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis5 ~]# netstat -utnlp | grep redis
tcp 0 0 192.168.4.55:16355 0.0.0.0:* LISTEN 4899/redis-server 1
tcp 0 0 192.168.4.55:6355 0.0.0.0:* LISTEN 4899/redis-server 1
[root@redis5 ~]# redis-cli -h 192.168.4.55 -p 6355
192.168.4.55:6355> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.55:6355> exit
[root@redis5 ~]# ls /var/lib/redis/6379/
dump.rdb nodes-6379.conf
[root@redis5 ~]# cat /var/lib/redis/6379/nodes-6379.conf
2ecf108a2e96e70f67bfcc4661e943d5c9f5dc77 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
- Redis6
[root@redis6 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.56
port 6356
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@redis6 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis6 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis6 ~]# netstat -utnlp | grep redis
tcp 0 0 192.168.4.56:16356 0.0.0.0:* LISTEN 5137/redis-server 1
tcp 0 0 192.168.4.56:6356 0.0.0.0:* LISTEN 5137/redis-server 1
[root@redis6 ~]# redis-cli -h 192.168.4.56 -p 6356
192.168.4.56:6356> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.56:6356> exit
[root@redis6 ~]# ls /var/lib/redis/6379/
dump.rdb nodes-6379.conf
[root@redis6 ~]# cat /var/lib/redis/6379/nodes-6379.conf
6cff1adadc8488674645023e4fa06993419f32f8 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
2.2、部署管理主機
2.2.1、部署ruby腳本運行環境
[root@redismgm ~]# yum -y install ruby rubygems
[root@redismgm ~]# ls redis-3.2.1.gem
redis-3.2.1.gem
[root@redismgm ~]# 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
2.2.2、創建管理腳本
[root@redismgm ~]# cd redis-4.0.8/
[root@redismgm redis-4.0.8]# ls src/*.rb
src/redis-trib.rb
[root@redismgm redis-4.0.8]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@redismgm redis-4.0.8]# mkdir /root/bin
[root@redismgm redis-4.0.8]# cp src/redis-trib.rb /root/bin
[root@redismgm redis-4.0.8]# ls /root/bin
redis-trib.rb
[root@redismgm redis-4.0.8]# chmod +x /root/bin/redis-trib.rb
[root@redismgm redis-4.0.8]# cd
[root@redismgm ~]# redis-trib.rb help
2.2.3、創建集羣
[root@redismgm ~]# redis-trib.rb create --replicas 1 \
192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 \
192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
//定義從服務器數量爲1
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters: //選舉了3臺主服務器
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: c476e663fa6bf2e43ea7592edd74b3e143446d56 192.168.4.51:6351
slots:0-5460 (5461 slots) master
M: 9e83ee6b3249077c63396b85f623fa12fe27dccc 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
M: 13067f3c56639214b51f4f7e187e7608e2a99d4a 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
S: 5988a140790cb3e8c39e81acf1e585094656e0a2 192.168.4.54:6354
replicates 13067f3c56639214b51f4f7e187e7608e2a99d4a
S: 2ecf108a2e96e70f67bfcc4661e943d5c9f5dc77 192.168.4.55:6355
replicates c476e663fa6bf2e43ea7592edd74b3e143446d56
S: 6cff1adadc8488674645023e4fa06993419f32f8 192.168.4.56:6356
replicates 9e83ee6b3249077c63396b85f623fa12fe27dccc
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:6351)
M: c476e663fa6bf2e43ea7592edd74b3e143446d56 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 2ecf108a2e96e70f67bfcc4661e943d5c9f5dc77 192.168.4.55:6355
slots: (0 slots) slave
replicates c476e663fa6bf2e43ea7592edd74b3e143446d56
S: 5988a140790cb3e8c39e81acf1e585094656e0a2 192.168.4.54:6354
slots: (0 slots) slave
replicates 13067f3c56639214b51f4f7e187e7608e2a99d4a
S: 6cff1adadc8488674645023e4fa06993419f32f8 192.168.4.56:6356
slots: (0 slots) slave
replicates 9e83ee6b3249077c63396b85f623fa12fe27dccc
M: 13067f3c56639214b51f4f7e187e7608e2a99d4a 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 9e83ee6b3249077c63396b85f623fa12fe27dccc 192.168.4.52:6352
slots:5461-10922 (5462 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@redismgm ~]#
2.2.4、在管理主機查看集羣信息
- 查看集羣信息
[root@redismgm ~]# redis-trib.rb info 192.168.4.53:6353
192.168.4.53:6353 (13067f3c...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.52:6352 (9e83ee6b...) -> 0 keys | 5462 slots | 1 slaves.
192.168.4.51:6351 (c476e663...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
- 查看集羣詳細信息
[root@redismgm ~]# redis-trib.rb check 192.168.4.53:6353
>>> Performing Cluster Check (using node 192.168.4.53:6353)
M: 13067f3c56639214b51f4f7e187e7608e2a99d4a 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 5988a140790cb3e8c39e81acf1e585094656e0a2 192.168.4.54:6354
slots: (0 slots) slave
replicates 13067f3c56639214b51f4f7e187e7608e2a99d4a
M: 9e83ee6b3249077c63396b85f623fa12fe27dccc 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 2ecf108a2e96e70f67bfcc4661e943d5c9f5dc77 192.168.4.55:6355
slots: (0 slots) slave
replicates c476e663fa6bf2e43ea7592edd74b3e143446d56
S: 6cff1adadc8488674645023e4fa06993419f32f8 192.168.4.56:6356
slots: (0 slots) slave
replicates 9e83ee6b3249077c63396b85f623fa12fe27dccc
M: c476e663fa6bf2e43ea7592edd74b3e143446d56 192.168.4.51:6351
slots:0-5460 (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@redismgm ~]#
2.3、客戶端訪問存取數據
(客戶端可以連接集羣中的任意1臺機器存取數據)
存三個數據查看存儲位置
[root@client ~]# redis-cli -c -h 192.168.4.54 -p 6354
192.168.4.54:6354> keys *
(empty list or set)
192.168.4.54:6354> set x 101
-> Redirected to slot [16287] located at 192.168.4.53:6353
OK
192.168.4.53:6353> keys *
1) "x"
192.168.4.53:6353> set y 201
OK
192.168.4.53:6353> keys *
1) "y"
2) "x"
192.168.4.53:6353> set z 301
-> Redirected to slot [8157] located at 192.168.4.52:6352 //z存在了52上
OK
192.168.4.52:6352> keys *
1) "z"
192.168.4.52:6352> get x
-> Redirected to slot [16287] located at 192.168.4.53:6353 //x存儲在了53上
"101"
192.168.4.53:6353> get y
"201" //存到54本地
192.168.4.53:6353> get z
-> Redirected to slot [8157] located at 192.168.4.52:6352
"301"
192.168.4.52:6352>
三、測試集羣功能
- 停止master主機的redis服務
- master宕機後對應的slave自動被選舉爲master
- 原master啓動後,會自動配置爲當前master的slave
3.1、查看當前集羣狀態
[root@redismgm ~]# redis-trib.rb check 192.168.4.53:6353
>>> Performing Cluster Check (using node 192.168.4.53:6353)
M: 13067f3c56639214b51f4f7e187e7608e2a99d4a 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 5988a140790cb3e8c39e81acf1e585094656e0a2 192.168.4.54:6354
slots: (0 slots) slave
replicates 13067f3c56639214b51f4f7e187e7608e2a99d4a
M: 9e83ee6b3249077c63396b85f623fa12fe27dccc 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 2ecf108a2e96e70f67bfcc4661e943d5c9f5dc77 192.168.4.55:6355
slots: (0 slots) slave
replicates c476e663fa6bf2e43ea7592edd74b3e143446d56
S: 6cff1adadc8488674645023e4fa06993419f32f8 192.168.4.56:6356
slots: (0 slots) slave
replicates 9e83ee6b3249077c63396b85f623fa12fe27dccc
M: c476e663fa6bf2e43ea7592edd74b3e143446d56 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
3.2、停止51的服務
[root@redis1 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> SHUTDOWN
not connected>
3.3、在查看集羣狀態
[root@redismgm ~]# redis-trib.rb check 192.168.4.53:6353
>>> Performing Cluster Check (using node 192.168.4.53:6353)
M: 13067f3c56639214b51f4f7e187e7608e2a99d4a 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 5988a140790cb3e8c39e81acf1e585094656e0a2 192.168.4.54:6354
slots: (0 slots) slave
replicates 13067f3c56639214b51f4f7e187e7608e2a99d4a
M: 9e83ee6b3249077c63396b85f623fa12fe27dccc 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 2ecf108a2e96e70f67bfcc4661e943d5c9f5dc77 192.168.4.55:6355
slots:0-5460 (5461 slots) master
0 additional replica(s)
S: 6cff1adadc8488674645023e4fa06993419f32f8 192.168.4.56:6356
slots: (0 slots) slave
replicates 9e83ee6b3249077c63396b85f623fa12fe27dccc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
3.4、插入幾條數據
192.168.4.52:6352> set a 401
-> Redirected to slot [15495] located at 192.168.4.53:6353
OK
192.168.4.53:6353> set b 501
-> Redirected to slot [3300] located at 192.168.4.55:6355
OK
192.168.4.55:6355> set c 601
-> Redirected to slot [7365] located at 192.168.4.52:6352
OK
3.5、重新將51服務啓動
[root@redis1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
3.6、51自動稱爲55的slave
[root@redismgm ~]# redis-trib.rb check 192.168.4.53:6353
>>> Performing Cluster Check (using node 192.168.4.53:6353)
M: 13067f3c56639214b51f4f7e187e7608e2a99d4a 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 5988a140790cb3e8c39e81acf1e585094656e0a2 192.168.4.54:6354
slots: (0 slots) slave
replicates 13067f3c56639214b51f4f7e187e7608e2a99d4a
M: 9e83ee6b3249077c63396b85f623fa12fe27dccc 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 2ecf108a2e96e70f67bfcc4661e943d5c9f5dc77 192.168.4.55:6355
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 6cff1adadc8488674645023e4fa06993419f32f8 192.168.4.56:6356
slots: (0 slots) slave
replicates 9e83ee6b3249077c63396b85f623fa12fe27dccc
S: c476e663fa6bf2e43ea7592edd74b3e143446d56 192.168.4.51:6351
slots: (0 slots) slave
replicates 2ecf108a2e96e70f67bfcc4661e943d5c9f5dc77
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered
四、集羣管理
4.1、slave角色服務器移除和添加
- 登錄集羣服務器,移除55的從服務器51
[root@redismgm ~]# redis-trib.rb del-node 192.168.4.52:6352 c476e663fa6bf2e43ea7592edd74b3e143446d56
>>> Removing node c476e663fa6bf2e43ea7592edd74b3e143446d56 from cluster 192.168.4.52:6352
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@redismgm ~]# redis-trib.rb info 192.168.4.53:6353
192.168.4.53:6353 (13067f3c...) -> 5 keys | 5461 slots | 1 slaves.
192.168.4.52:6352 (9e83ee6b...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (2ecf108a...) -> 1 keys | 5461 slots | 0 slaves.
- 添加58做55的slave服務器
]#redis-trib.rb add-node --slave [ --master-id id值 ] ip地址:端口 192.168.4.51:635
- 移除master服務器
[root@redismgm ~]# redis-trib.rb reshard 192.168.4.51:6351
[root@redismgm ~]# redis-trib.rb del-node 192.168.4.51:6351 master主機id值