server1:172.16.16.34 server2:172.16.16.35 redis版本:redis3.2
搭建環境:redis集羣,server1有7001,7002,7003三主,server2有7001,7002,7003三從,總共六個節點。這樣作是爲了保證redis的集羣的高可用。redis的複製也是採用異步複製的方式。node
cd /home/maxiangqian tar xzf redis-3.2.8.tar.gz cd redis-3.2.8 yum install gcc make
2:建立redis目錄文件夾redis
mkdir /home/redis7001/data mkdir -p /home/redis7001/data /home/redis7001/log /home/redis7001/tmp mkdir -p /home/redis7002/data /home/redis7002/log /home/redis7002/tmp mkdir -p /home/redis7003/data /home/redis7003/log /home/redis7003/tmp
3:爲server1和server2的三個節點分別配置配置文件mongodb
port 7001 timeout 300 daemonize yes pidfile "/home/redis7001/tmp/redis_7001.pid" loglevel notice logfile "/home/redis7001/log/redis_7001.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/home/redis7001/data" slave-serve-stale-data yes #slave-read-only yes # yes開啓從庫只讀 repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly yes #appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 requirepass "maxiangqianredis" masterauth "maxiangqianredis" #cluster cluster-enabled yes cluster-config-file /home/redis7001/nodes7001.conf cluster-node-timeout 5000
上面是redis7001的配置文件內容數據庫
redis-server /home/redis7001/redis7001.conf
咱們看一下啓動日誌:ruby
1574:M 03 May 16:22:53.444 * No cluster configuration found, I'm 363ecec54c92c2548dcab016146bdb4c104e5e84
server1 7001 server1 7002 server1 7003 server2 7001 server2 7002 server2 7003
OK,如今已經有六個已經啓動的redis實例了。咱們下一步開始作集羣網絡
redis-trib.rb create --replicas 1 10.103.16.34:7001 10.103.16.34:7002 10.103.16.34:7003 10.103.16.35:7001 10.103.16.35:7002 10.103.16.35:7003
執行報錯:app
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from /home/maxiangqian/redis-3.2.8/src/redis-trib.rb:25
咱們須要安裝如下幾個包:less
yum -y install zlib ruby rubygems
gem install redis
而後從新啓動建立集羣的操做:異步
[root@localhost redis7003]# redis-trib.rb create --replicas 1 10.103.16.34:7001 10.103.16.34:7002 10.103.16.34:7003 10.103.16.35:7001 10.103.16.35:7002 10.103.16.35:7003 >>> Creating cluster [ERR] Sorry, can't connect to node 10.103.16.34:7001
又報錯了我擦。async
requirepass "maxiangqianredis" masterauth "maxiangqianredis"
羣集認證是要配置完成再添加的,並且兩個參數配置必須同樣,咱們如今暫時不配置認證模式:
[root@localhost redis7003]# redis-trib.rb create --replicas 1 10.103.16.34:7001 10.103.16.34:7002 10.103.16.34:7003 10.103.16.35:7001 10.103.16.35:7002 10.103.16.35:7003 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 10.103.16.35:7001 10.103.16.34:7001 10.103.16.35:7002 Adding replica 10.103.16.34:7002 to 10.103.16.35:7001 Adding replica 10.103.16.35:7003 to 10.103.16.34:7001 Adding replica 10.103.16.34:7003 to 10.103.16.35:7002 M: 363ecec54c92c2548dcab016146bdb4c104e5e84 10.103.16.34:7001 slots:5461-10922 (5462 slots) master S: 93a0e8d405959480fcbd310a5d15a92346c69d43 10.103.16.34:7002 replicates d015a22abc57c021f568973f4f1c03c7a5c7b772 S: 78f77749f9f9a5f0d7c99427e0311912a3fa04e7 10.103.16.34:7003 replicates 89147e5837e378b69233dd2b8290267975719bc4 M: d015a22abc57c021f568973f4f1c03c7a5c7b772 10.103.16.35:7001 slots:0-5460 (5461 slots) master M: 89147e5837e378b69233dd2b8290267975719bc4 10.103.16.35:7002 slots:10923-16383 (5461 slots) master S: ce9d635236567ccde4c864f78863fa0a4b26f25a 10.103.16.35:7003 replicates 363ecec54c92c2548dcab016146bdb4c104e5e84 Can I set the above configuration? (type 'yes' to accept):
OK,已經提示成功了,咱們直接選擇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 10.103.16.34:7001) M: 363ecec54c92c2548dcab016146bdb4c104e5e84 10.103.16.34:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 78f77749f9f9a5f0d7c99427e0311912a3fa04e7 10.103.16.34:7003 slots: (0 slots) slave replicates 89147e5837e378b69233dd2b8290267975719bc4 M: d015a22abc57c021f568973f4f1c03c7a5c7b772 10.103.16.35:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 89147e5837e378b69233dd2b8290267975719bc4 10.103.16.35:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: ce9d635236567ccde4c864f78863fa0a4b26f25a 10.103.16.35:7003 slots: (0 slots) slave replicates 363ecec54c92c2548dcab016146bdb4c104e5e84 S: 93a0e8d405959480fcbd310a5d15a92346c69d43 10.103.16.34:7002 slots: (0 slots) slave replicates d015a22abc57c021f568973f4f1c03c7a5c7b772 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
這樣羣集就設置成功了。
[root@mxqmongodb2 sa]# redis-cli -c -p 7001 127.0.0.1:7001> get name -> Redirected to slot [5798] located at 10.103.16.34:7001 "txt" 10.103.16.34:7001> exit [root@mxqmongodb2 sa]# redis-cli -c -p 7002 127.0.0.1:7002> get name -> Redirected to slot [5798] located at 10.103.16.34:7001 "txt" 10.103.16.34:7001> exit [root@mxqmongodb2 sa]# redis-cli -c -p 7003 127.0.0.1:7003> get name -> Redirected to slot [5798] located at 10.103.16.34:7001 "txt"
5:咱們接下來查看一下集羣的基本信息:
[root@localhost redis7003]# redis-cli -p 7001 cluster nodes 78f77749f9f9a5f0d7c99427e0311912a3fa04e7 10.103.16.34:7003 slave 89147e5837e378b69233dd2b8290267975719bc4 0 1493879665448 5 connected d015a22abc57c021f568973f4f1c03c7a5c7b772 10.103.16.35:7001 master - 0 1493879663946 4 connected 0-5460 89147e5837e378b69233dd2b8290267975719bc4 10.103.16.35:7002 master - 0 1493879664948 5 connected 10923-16383 ce9d635236567ccde4c864f78863fa0a4b26f25a 10.103.16.35:7003 slave 363ecec54c92c2548dcab016146bdb4c104e5e84 0 1493879665949 6 connected 93a0e8d405959480fcbd310a5d15a92346c69d43 10.103.16.34:7002 slave d015a22abc57c021f568973f4f1c03c7a5c7b772 0 1493879664446 4 connected 363ecec54c92c2548dcab016146bdb4c104e5e84 10.103.16.34:7001 myself,master - 0 0 1 connected 5461-10922
能夠看到如今的集羣有六個節點,三個主節點和三個從節點。並且每一個主節點都會記錄本身分配的哈希槽,從中咱們能夠看到
103.16.35:7001 master - 0 1493879663946 4 connected 0-5460 10.103.16.34:7001 myself,master - 0 0 1 connected 5461-10922 10.103.16.35:7002 master - 0 1493879664948 5 connected 10923-16383
固然咱們也能夠對這些節點的哈希槽進行從新的分配,咱們如今打算將103.16.35:7001的前100個哈希槽移動到10.103.16.34:7001
[root@localhost redis7003]# redis-trib.rb reshard 10.103.16.34:7001
而後會提示我輸入數值以及從哪裏遷移到哪裏:
How many slots do you want to move (from 1 to 16384)? 100 What is the receiving node ID? 363ecec54c92c2548dcab016146bdb4c104e5e84 Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:d015a22abc57c021f568973f4f1c03c7a5c7b772 Source node #2:done
執行完之後就能夠進行遷移了,遷移完之後咱們再打印出來節點信息看一下:
[root@localhost redis7003]# redis-cli -p 7001 cluster nodes 78f77749f9f9a5f0d7c99427e0311912a3fa04e7 10.103.16.34:7003 slave 89147e5837e378b69233dd2b8290267975719bc4 0 1493881167965 5 connected d015a22abc57c021f568973f4f1c03c7a5c7b772 10.103.16.35:7001 master - 0 1493881166460 4 connected 101-5460 89147e5837e378b69233dd2b8290267975719bc4 10.103.16.35:7002 master - 0 1493881166962 5 connected 10923-16383 ce9d635236567ccde4c864f78863fa0a4b26f25a 10.103.16.35:7003 slave 363ecec54c92c2548dcab016146bdb4c104e5e84 0 1493881167465 7 connected 93a0e8d405959480fcbd310a5d15a92346c69d43 10.103.16.34:7002 slave d015a22abc57c021f568973f4f1c03c7a5c7b772 0 1493881167965 4 connected 363ecec54c92c2548dcab016146bdb4c104e5e84 10.103.16.34:7001 myself,master - 0 0 7 connected 0-100 5461-10922
咱們能夠很清楚的看到已經遷移成功了。
[root@localhost redis7003]# redis-cli -p 7001 cluster nodes | grep master d015a22abc57c021f568973f4f1c03c7a5c7b772 10.103.16.35:7001 master - 0 1493883826713 4 connected 101-5460 89147e5837e378b69233dd2b8290267975719bc4 10.103.16.35:7002 master - 0 1493883827213 5 connected 10923-16383 363ecec54c92c2548dcab016146bdb4c104e5e84 10.103.16.34:7001 myself,master - 0 0 7 connected 0-100 5461-10922
咱們如今要使10.103.16.35:7001這個主節點斷掉,而後重啓看一下基本信息
[root@mxqmongodb2 sa]# /home/maxiangqian/redis-3.2.8/src/redis-cli -p 7001 127.0.0.1:7001> SHUTDOWN not connected> exit [root@mxqmongodb2 sa]# redis-server /home/redis7001/redis7001.conf
而後再打印一下集羣信息看一下:
[root@localhost redis7003]# redis-cli -p 7001 cluster nodes 78f77749f9f9a5f0d7c99427e0311912a3fa04e7 10.103.16.34:7003 slave 89147e5837e378b69233dd2b8290267975719bc4 0 1493884247801 5 connected d015a22abc57c021f568973f4f1c03c7a5c7b772 10.103.16.35:7001 slave 93a0e8d405959480fcbd310a5d15a92346c69d43 0 1493884247300 8 connected 89147e5837e378b69233dd2b8290267975719bc4 10.103.16.35:7002 master - 0 1493884246798 5 connected 10923-16383 ce9d635236567ccde4c864f78863fa0a4b26f25a 10.103.16.35:7003 slave 363ecec54c92c2548dcab016146bdb4c104e5e84 0 1493884246298 7 connected 93a0e8d405959480fcbd310a5d15a92346c69d43 10.103.16.34:7002 master - 0 1493884248301 8 connected 101-5460 363ecec54c92c2548dcab016146bdb4c104e5e84 10.103.16.34:7001 myself,master - 0 0 7 connected 0-100 5461-10922
經過信息咱們能夠很明顯的看到了10.103.16.35:7001這個主節點已經變成了從節點,而自己他的從節點也上升爲主節點了。
./redis-trib.rb add-node 10.103.16.34:7004 10.103.16.34:7001
這樣咱們就把10.103.16.34:7004添加爲集羣的新的主節點,不過咱們要注意的是,這時候他僅僅是一個沒有哈希槽的主節點,並不會存儲任何數據。
./redis-trib.rb add-node 10.103.16.34:7004 10.103.16.34:7001 redis 10.103.16.34::7004> cluster replicate 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e
將新節點指定爲ID爲3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e的從節點。
./redis-trib del-node 127.0.0.1:7000 `<node-id>`
可是咱們要注意一點,移除主節點的時候必須保證主節點是空的,也就是事先將要移除的主節點的哈希槽給轉移到其餘的主節點上。