一、安裝環境:centos7、Redis5.0.5 stable
二、集羣安裝
2.1 下載源文件:
[root@centos60 yzh]# pwd
/home/yzh
[root@centos60 yzh]# wget http://download.redis.io/releases/redis-5.0.5.tar.gz
[root@centos60 yzh]# tar xzf redis-5.0.5.tar.gz
[root@centos60 yzh]# cd redis-5.0.5/
或者官網本地下載好後scp傳到Linux服務器:http://download.redis.io/releases/redis-5.0.5.tar.gz
2.2 redis是C語言寫的,下載C語言運行環境:
[root@centos60 redis-5.0.5]# yum install gcc
編譯源碼與安裝:
[root@centos60 redis-5.0.5]# make MALLOC=libc
[root@centos60 redis-5.0.5]# cd ..
[root@centos60 yzh]# mkdir redis01
[root@centos60 yzh]# cd redis-5.0.5/
[root@centos60 redis-5.0.5]# make install PREFIX=/home/yzh/redis01/
注意:make後跟着MALLOC=libc,原因是 jemalloc 重載了Linux下的 ANSIC 的 malloc 和 free 函數,否則報錯:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
2.3 默認配置文件redis.conf的修改:
[root@centos60 redis-5.0.5]# cd ../redis01/bin/
[root@centos60 bin]# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
[root@centos60 bin]# cp /home/yzh/redis-5.0.5/redis.conf /home/yzh/redis01/bin/
[root@centos60 bin]# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis.conf redis-sentinel redis-server
[root@centos60 bin]# vim redis.conf
需要修改的地方有3處:
2.3.1 daemonize no 改爲 daemonize yes,後臺啓動。(/dae然後回車,vi下的搜索模式)
2.3.2 #cluster-enabled yes 的註釋放開,集羣模式支持開啓。
2.3.3 port 6379 改爲 port 7001,修改默認端口號(可選)。
三、啓動集羣
3.1 先複製5份redis-server文件夾:
[root@centos60 bin]# cd ../..
[root@centos60 yzh]# ls
redis01 redis-5.0.5 redis-5.0.5.tar.gz
[root@centos60 yzh]# cp -r redis01 redis02
[root@centos60 yzh]# cp -r redis01 redis03
[root@centos60 yzh]# cp -r redis01 redis04
[root@centos60 yzh]# cp -r redis01 redis05
[root@centos60 yzh]# cp -r redis01 redis06
[root@centos60 yzh]# ls
redis01 redis02 redis03 redis04 redis05 redis06 redis-5.0.5 redis-5.0.5.tar.gz
3.2 修改另外5份文件夾的redis.conf的port 7001:
[root@centos60 yzh]# vim redis02/bin/redis.conf
[root@centos60 yzh]# vim redis03/bin/redis.conf
[root@centos60 yzh]# vim redis04/bin/redis.conf
[root@centos60 yzh]# vim redis05/bin/redis.conf
[root@centos60 yzh]# vim redis06/bin/redis.conf
分別修改爲:port 7002,port 7003,port 7004,port 7005,port 7006。
3.3 啓動6個redis-server:
[root@centos60 yzh]# cd redis01/bin/
[root@centos60 bin]# ./redis-server redis.conf
1697893:C 10 Jul 2019 14:40:57.292 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1697893:C 10 Jul 2019 14:40:57.292 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1697893, just started
1697893:C 10 Jul 2019 14:40:57.292 # Configuration loaded
[root@centos60 bin]# cd ../../redis02/bin/
[root@centos60 bin]# ./redis-server redis.conf
1698861:C 10 Jul 2019 14:41:19.899 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1698861:C 10 Jul 2019 14:41:19.899 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1698861, just started
1698861:C 10 Jul 2019 14:41:19.899 # Configuration loaded
[root@centos60 bin]# cd ../../redis03/bin/
[root@centos60 bin]# ./redis-server redis.conf
1699093:C 10 Jul 2019 14:41:27.717 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1699093:C 10 Jul 2019 14:41:27.717 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1699093, just started
1699093:C 10 Jul 2019 14:41:27.717 # Configuration loaded
[root@centos60 bin]# cd ../../redis04/bin/
[root@centos60 bin]# ./redis-server redis.conf
1699329:C 10 Jul 2019 14:41:33.780 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1699329:C 10 Jul 2019 14:41:33.781 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1699329, just started
1699329:C 10 Jul 2019 14:41:33.781 # Configuration loaded
[root@centos60 bin]# cd ../../redis05/bin/
[root@centos60 bin]# ./redis-server redis.conf
1699511:C 10 Jul 2019 14:41:39.785 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1699511:C 10 Jul 2019 14:41:39.785 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1699511, just started
1699511:C 10 Jul 2019 14:41:39.785 # Configuration loaded
[root@centos60 bin]# cd ../../redis06/bin/
[root@centos60 bin]# ./redis-server redis.conf
1699687:C 10 Jul 2019 14:41:44.992 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1699687:C 10 Jul 2019 14:41:44.992 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1699687, just started
1699687:C 10 Jul 2019 14:41:44.992 # Configuration loaded
[root@centos60 bin]# ps aux | grep redis
root 1697894 0.1 0.0 144532 2548 ? Ssl 14:40 0:00 ./redis-server 127.0.0.1:7001 [cluster]
root 1698862 0.0 0.0 144532 2548 ? Ssl 14:41 0:00 ./redis-server 127.0.0.1:7002 [cluster]
root 1699094 0.0 0.0 144532 2544 ? Ssl 14:41 0:00 ./redis-server 127.0.0.1:7003 [cluster]
root 1699330 0.0 0.0 144532 2544 ? Ssl 14:41 0:00 ./redis-server 127.0.0.1:7004 [cluster]
root 1699512 0.0 0.0 144532 2544 ? Ssl 14:41 0:00 ./redis-server 127.0.0.1:7005 [cluster]
root 1699688 0.0 0.0 144532 2544 ? Ssl 14:41 0:00 ./redis-server 127.0.0.1:7006 [cluster]
root 1699974 0.0 0.0 112712 988 pts/8 S+ 14:41 0:00 grep --color=auto redis
3.4 創建集羣:
redis3、4使用的是redis-trib.rb工具包來管理集羣,可參考我的另一篇https://blog.csdn.net/yzh_1346983557/article/details/91044739
redis5使用redis-cli客戶端直接管理集羣:
[root@centos60 yzh]# redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
檢查集羣狀態:
[root@centos60 yzh]# redis-cli --cluster check 127.0.0.1:7001
127.0.0.1:7001 (e9e60742...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:7002 (338b1957...) -> 0 keys | 5462 slots | 1 slaves.
127.0.0.1:7003 (11a82bf9...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: e9e60742e53c19df7852d6bbc136291e468b1018 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: c9ebeb669d4f325959f2e172271349a83ae5b15c 127.0.0.1:7004
slots: (0 slots) slave
replicates 11a82bf9c5f59783911c1437bc327d7964c46515
S: d8cecea8ac6376c46d184c1efc47eb6686449c35 127.0.0.1:7006
slots: (0 slots) slave
replicates 338b19575f516df229fee30ffef64b2c9708ece5
S: 8d136d07dfdb1a61c8d3de70bf26e777a19acc88 127.0.0.1:7005
slots: (0 slots) slave
replicates e9e60742e53c19df7852d6bbc136291e468b1018
M: 338b19575f516df229fee30ffef64b2c9708ece5 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 11a82bf9c5f59783911c1437bc327d7964c46515 127.0.0.1:7003
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.
或者使用redis-cli客戶端登錄:
[root@centos60 yzh]# redis-cli -c -p 7001
127.0.0.1:7001> cluster nodes
c9ebeb669d4f325959f2e172271349a83ae5b15c 127.0.0.1:7004@17004 slave 11a82bf9c5f59783911c1437bc327d7964c46515 0 1562742181000 4 connected
d8cecea8ac6376c46d184c1efc47eb6686449c35 127.0.0.1:7006@17006 slave 338b19575f516df229fee30ffef64b2c9708ece5 0 1562742182671 6 connected
e9e60742e53c19df7852d6bbc136291e468b1018 127.0.0.1:7001@17001 myself,master - 0 1562742183000 1 connected 0-5460
8d136d07dfdb1a61c8d3de70bf26e777a19acc88 127.0.0.1:7005@17005 slave e9e60742e53c19df7852d6bbc136291e468b1018 0 1562742182000 5 connected
338b19575f516df229fee30ffef64b2c9708ece5 127.0.0.1:7002@17002 master - 0 1562742183674 2 connected 5461-10922
11a82bf9c5f59783911c1437bc327d7964c46515 127.0.0.1:7003@17003 master - 0 1562742181000 3 connected 10923-16383
127.0.0.1:7001> 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:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:187
cluster_stats_messages_pong_sent:172
cluster_stats_messages_sent:359
cluster_stats_messages_ping_received:167
cluster_stats_messages_pong_received:187
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:359
參考redis官網:https://redis.io/topics/cluster-tutorial