1. 程式人生 > 實用技巧 >redis叢集搭建 不用ruby

redis叢集搭建 不用ruby

redis 從5開始 可以直接用redis-cli命令建立叢集了,不用那麼麻煩 安裝ruby環境

redis配置檔案需要修改的地方

port 7000
cluster-enabled yes
cluster-config-file nodes.7000.conf
cluster-node-timeout 5000
appendonly yes

  

mkdir /usr/local/cluster

cd /usr/local/cluster/

cd cluster/

mkdir 7000 7001 7002 7003 7004 7005

cd 7000/

cp /www/server/redis/redis.conf ./

vim redis.conf 修改一個配置 然後複製到其他目錄

cp redis.conf ../7001/
cp redis.conf ../7002/
cp redis.conf ../7003/
cp redis.conf ../7004/
cp redis.conf ../7005/
vim ../7001/redis.conf
vim ../7002/redis.conf
vim ../7003/redis.conf
vim ../7004/redis.conf
vim ../7005/redis.conf

替換各自埠為其他的 vim 下 :%s/7000/7001/s

vim start.sh 一個個啟動太麻煩,指令碼一次性啟動

#!/bin/bash

/www/server/redis/src/redis-server /usr/local/cluster/7000/redis.conf
/www/server/redis/src/redis-server /usr/local/cluster/7001/redis.conf /www/server/redis/src/redis-server /usr/local/cluster/7002/redis.conf /www/server/redis/src/redis-server /usr/local/cluster/7003/redis.conf /www/server/redis/src/redis-server /usr/local/cluster/7004/redis.conf /www/server/redis/src/redis-server /usr/local/cluster/7005/redis.conf

chmod +x start.sh

./start.sh

ps -ef|grep redis

[root@localhost cluster]# ps -ef|grep redis
root     10279     1  0 11:46 ?        00:00:01 /www/server/redis/src/redis-server 0.0.0.0:7000 [cluster]
root     10340     1  0 11:47 ?        00:00:01 /www/server/redis/src/redis-server 0.0.0.0:7001 [cluster]
root     10346     1  0 11:47 ?        00:00:01 /www/server/redis/src/redis-server 0.0.0.0:7002 [cluster]
root     10352     1  0 11:47 ?        00:00:01 /www/server/redis/src/redis-server 0.0.0.0:7003 [cluster]
root     10358     1  0 11:47 ?        00:00:01 /www/server/redis/src/redis-server 0.0.0.0:7004 [cluster]
root     10364     1  0 11:47 ?        00:00:01 /www/server/redis/src/redis-server 0.0.0.0:7005 [cluster]
root     10945 21050  0 11:58 pts/2    00:00:00 grep --color=auto redis
[root@localhost cluster]#

  

啟動叢集

redis-cli --cluster create 192.168.33.60:7000 192.168.33.60:7001 192.168.33.60:7002 192.168.33.60:7003 192.168.33.60:7004 192.168.33.60:7005 --cluster-replicas 1

root@localhost cluster]# redis-cli --cluster create 192.168.33.60:7000 192.168.33.60:7001 192.168.33.60:7002 192.168.33.60:7003 192.168.33.60:7004 192.168.33.60:7005 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.33.60:7004 to 192.168.33.60:7000
Adding replica 192.168.33.60:7005 to 192.168.33.60:7001
Adding replica 192.168.33.60:7003 to 192.168.33.60:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 22df8c4f2763a3d40c5533893333608df1e0b792 192.168.33.60:7000
   slots:[0-5460] (5461 slots) master
M: 4bdf950bbcaa37ff8aa159894c169b4c9fa0b056 192.168.33.60:7001
   slots:[5461-10922] (5462 slots) master
M: 93a97e0e58375b38ff4639de2e597e1adb675b83 192.168.33.60:7002
   slots:[10923-16383] (5461 slots) master
S: f68d2dde12b03b84251d69006d845cdfeaeaeb88 192.168.33.60:7003
   replicates 93a97e0e58375b38ff4639de2e597e1adb675b83
S: 6e2feeb4ee991d69adae953d47932291bb174445 192.168.33.60:7004
   replicates 22df8c4f2763a3d40c5533893333608df1e0b792
S: c0c7a26a69b09cad5d78e78c8d0c7eb327fbf8f5 192.168.33.60:7005
   replicates 4bdf950bbcaa37ff8aa159894c169b4c9fa0b056
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.33.60:7000)
M: 22df8c4f2763a3d40c5533893333608df1e0b792 192.168.33.60:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: f68d2dde12b03b84251d69006d845cdfeaeaeb88 192.168.33.60:7003
   slots: (0 slots) slave
   replicates 93a97e0e58375b38ff4639de2e597e1adb675b83
S: 6e2feeb4ee991d69adae953d47932291bb174445 192.168.33.60:7004
   slots: (0 slots) slave
   replicates 22df8c4f2763a3d40c5533893333608df1e0b792
S: c0c7a26a69b09cad5d78e78c8d0c7eb327fbf8f5 192.168.33.60:7005
   slots: (0 slots) slave
   replicates 4bdf950bbcaa37ff8aa159894c169b4c9fa0b056
M: 93a97e0e58375b38ff4639de2e597e1adb675b83 192.168.33.60:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 4bdf950bbcaa37ff8aa159894c169b4c9fa0b056 192.168.33.60:7001
   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@localhost cluster]#

  

檢視叢集狀態

redis-cli -h 127.0.0.1 -c -p 7000 info replication

redis-cli -h 127.0.0.1 -c -p 7001 info replication

redis-cli -h 127.0.0.1 -c -p 7002 info replication

redis-cli -h 127.0.0.1 -c -p 7003 info replication

redis-cli -h 127.0.0.1 -c -p 7004 info replication

redis-cli -h 127.0.0.1 -c -p 7005 info replication

[root@localhost logs]# redis-cli -h 127.0.0.1 -c -p 7002 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.33.60,port=7003,state=online,offset=1344,lag=0
master_replid:f1b66456b6204b293b39702faea1dee558929070
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1344
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1344

  

結果 0 1 2 為主 3 4 5 為從

現在kill掉0 試試

kill之前我看了 4是從

[root@localhost logs]# redis-cli -h 127.0.0.1 -c -p 7004 info replication
# Replication
role:slave
master_host:192.168.33.60
master_port:7000
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:1330
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4d2c448c8477544dccfa20b248ce37351a725293
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1330
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1330

  

kill之後 在看 4變成主了

[root@localhost logs]# redis-cli -h 127.0.0.1 -c -p 7004 info replication
# Replication
role:master
connected_slaves:0
master_replid:ddf63dedae765e3612002687217d63b20c7356ab
master_replid2:4d2c448c8477544dccfa20b248ce37351a725293
master_repl_offset:1470
second_repl_offset:1471
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1470

設定值 連線7001

127.0.0.1:7003> set name wang
-> Redirected to slot [5798] located at 192.168.33.60:7001
OK
192.168.33.60:7001>

7002上get

127.0.0.1:7002> get name
-> Redirected to slot [5798] located at 192.168.33.60:7001
"wang"
192.168.33.60:7001>

https://www.jianshu.com/p/a1e62e78667c

https://segmentfault.com/a/1190000017151802