Redis叢集主備模式部署
阿新 • • 發佈:2018-12-25
網上有很多用Ruby安裝Redis-cluster的文章,但是在實際環境下不想安裝Ruby,所以本文主要介紹了用Redis命令部署Redis叢集,並且為叢集中每一個master例項增加一個slave例項。
0 編譯安裝
$ wget http://download.redis.io/releases/redis-3.0.3.tar.gz
$ tar xzvf redis-3.0.3.tar.gz
$ cd redis-3.0.3
$ make all
$ make install # use sudo user.
1 建立目錄
$ mkdir 7000 7001 7002 7003
2 修改如下配置redis.conf
port 7000 #7000修改為對應的7001、7002、7003
daemonize yes
cluster-enabled yes
cluster-config-file nodes-7000.conf #7000修改為對應的7001、7002、7003
luster-node-timeout 15000
將修改好的配置檔案放到對用的目錄下:./7000, ./7001, ./7002, ./7003 。
3 啟動redis服務
$ cd 7000
$ redis-server ./redis.conf
$ cd ../7001
$ redis-server ./redis.conf
$ cd ../7002
$ redis-server ./redis.conf
$ cd ../7003
$ redis-server ./redis.conf
$ # 檢查redis啟動情況
$ ps -ef | grep redis | grep -v grep
cjf 5834 1 0 16:33 ? 00:00:00 redis-server 127.0.0.1:7000 [cluster]
cjf 5839 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster]
cjf 5843 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster]
cjf 5847 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster]
4 將redis例項增加到叢集中
主要是通過用cluster meet <host> <port>
給叢集增加節點,如下:
$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
14d4071b97121e703e44bbc834f42d1b31c8ea95 :7000 myself,master - 0 0 0 connected
127.0.0.1:7000> cluster meet 127.0.0.1 7001
OK
127.0.0.1:7000> cluster meet 127.0.0.1 7002
OK
127.0.0.1:7000> cluster meet 127.0.0.1 7003
OK
127.0.0.1:7000> cluster nodes
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850262990 0 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850265007 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850263998 2 connected
127.0.0.1:7000>
如果cluster nodes
出現上面的四行記錄就表示meet
成功,標識myself的是客戶端正在連線的redis例項。
可以通過cluster forget
清除叢集中的某個例項。
5 分配slots
redis-cluster總共有16384個槽位(0-16383),按如下方式修改槽位:
$ cat 7000/nodes-7000.conf #分配slots:0-4095(注意127.0.0.1:7000對應的行尾)
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850266016 3 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850265007 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850263998 2 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7001/nodes-7001.conf #分配slots:4096-8191(注意127.0.0.1:7001對應的行尾)
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850265714 3 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 myself,master - 0 0 2 connected 4096-8191
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850264705 0 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263695 1 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7002/nodes-7002.conf #分配slots:8192-12287(注意127.0.0.1:7002對應的行尾)
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850262787 2 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263796 1 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 myself,master - 0 0 3 connected 8192-12287
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850264806 0 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7003/nodes-7003.conf #分配slots:12288-16383(注意127.0.0.1:7003對應的行尾)
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850264808 2 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850265819 3 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263798 1 connected
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 myself,master - 0 0 0 connected 12288-16383
vars currentEpoch 3 lastVoteEpoch 0
$
配置修改成功之後,重新啟動redis:
$ # 停止redis
$ ps -ef|grep redis
cjf 5834 1 0 16:33 ? 00:00:00 redis-server 127.0.0.1:7000 [cluster]
cjf 5839 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster]
cjf 5843 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster]
cjf 5847 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster]
cjf 5921 5252 0 16:46 pts/2 00:00:00 grep redis
$ kill 5834 5839 5843 5847
$
$ # 重新啟動redis
$ cd 7000
$ redis-server ./redis.conf
$ cd ../7001
$ redis-server ./redis.conf
$ cd ../7002
$ redis-server ./redis.conf
$ cd ../7003
$ redis-server ./redis.conf
$
$ # 檢查分配的slots是否生效:
$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850861230 0 connected 12288-16383
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850860221 2 connected 4096-8191
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850859209 3 connected 8192-12287
127.0.0.1:7000>
127.0.0.1:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:4
cluster_size:4
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:416
cluster_stats_messages_received:73
127.0.0.1:7000>
當前情況下,redis-cluster部署完畢。
6 給master節點配一個slave節點
在前面,我們配置好了redis的叢集並分配了slots,叢集已經可用了,叢集中現在有4個master節點,所有寫入這個叢集中的資料都會分片到這4個redis例項中,但是現在存在這樣一個問題:如果某個master節點掛掉的話,那麼這個叢集就不可用了,所以我們現在為叢集中的4個master例項各啟一個slave例項做主備。
$ mkdir 8000 8001 8002 8003
$ cp 7000/redis.conf 8000
$ cp 7000/redis.conf 8001
$ cp 7000/redis.conf 8002
$ cp 7000/redis.conf 8003
將redis.conf中下面兩個配置修改為對應的埠:
port 800X
cluster-config-file nodes-800X.conf
然後啟動這4個例項:
$ cd 8000/
$ redis-server ./redis.conf
$ cd ../8001
$ redis-server ./redis.conf
$ cd ../8002
$ redis-server ./redis.conf
$ cd ../8003
$ redis-server ./redis.conf
$
$ ps -ef|grep redis
cjf 5928 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7000 [cluster]
cjf 5933 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7001 [cluster]
cjf 5937 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7002 [cluster]
cjf 5941 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7003 [cluster]
cjf 6136 1 0 17:20 ? 00:00:00 redis-server 127.0.0.1:8000 [cluster]
cjf 6141 1 0 17:20 ? 00:00:00 redis-server 127.0.0.1:8001 [cluster]
cjf 6145 1 0 17:21 ? 00:00:00 redis-server 127.0.0.1:8002 [cluster]
cjf 6150 1 0 17:21 ? 00:00:00 redis-server 127.0.0.1:8003 [cluster]
cjf 6157 5252 0 17:21 pts/2 00:00:00 grep redis
[cjf@host1 8003]$
再將這4個redis例項cluster meet
到叢集中:
$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438852991236 0 connected 12288-16383
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438852992245 2 connected 4096-8191
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438852993255 3 connected 8192-12287
127.0.0.1:7000> cluster meet 127.0.0.1 8000
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8001
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8002
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8003
OK
127.0.0.1:7000> cluster nodes
79c41ed9454f686fe7c35e0a6c505732b951b226 127.0.0.1:8000 master - 0 1438853011586 4 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438853014613 7 connected 12288-16383
833bb1d37b22b2055d2281a5f2dad3d869195f0d 127.0.0.1:8002 master - 0 1438853012596 6 connected
0d03ff5e90fa369c5c39c9415db39f711b943494 127.0.0.1:8001 master - 0 1438853013605 5 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438853010578 3 connected 8192-12287
a34eb2aa56f309793d78ba2f69141be268a23659 127.0.0.1:8003 master - 0 1438853012092 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438853009564 2 connected 4096-8191
127.0.0.1:7000>
最後用cluster replicate <nodeid>
將8000、8001、8002、8003分別掛到7000、7001、7002、7003上:
$ redis-cli -p 8000
127.0.0.1:8000> cluster replicate 14d4071b97121e703e44bbc834f42d1b31c8ea95
OK
127.0.0.1:8000>
$ redis-cli -p 8001
127.0.0.1:8001> cluster replicate 2875f2155e3214d7950dd5916f5ecf5edd3d9fee
OK
127.0.0.1:8001>
$ redis-cli -p 8002
127.0.0.1:8002> cluster replicate 75334aae3ced44cf0e8416691aeef8249e7a0621
OK
127.0.0.1:8002>
$ redis-cli -p 8003
127.0.0.1:8003> cluster replicate 8b721ff1ac2e383ffcbcf133d502c6957f8df713
OK
127.0.0.1:8003> cluster nodes
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438853245955 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438853242927 7 connected 12288-16383
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438853242423 2 connected 4096-8191
833bb1d37b22b2055d2281a5f2dad3d869195f0d 127.0.0.1:8002 slave 75334aae3ced44cf0e8416691aeef8249e7a0621 0 1438853245955 6 connected
a34eb2aa56f309793d78ba2f69141be268a23659 127.0.0.1:8003 myself,slave 8b721ff1ac2e383ffcbcf133d502c6957f8df713 0 0 0 connected
0d03ff5e90fa369c5c39c9415db39f711b943494 127.0.0.1:8001 slave 2875f2155e3214d7950dd5916f5ecf5edd3d9fee 0 1438853241925 5 connected
79c41ed9454f686fe7c35e0a6c505732b951b226 127.0.0.1:8000 slave 14d4071b97121e703e44bbc834f42d1b31c8ea95 0 1438853244945 4 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438853243935 3 connected 8192-12287
127.0.0.1:8003>
如上,配置成功。
附錄:常用的Redis-cluster命令
#### 叢集(cluster)
CLUSTER INFO 列印叢集的資訊
CLUSTER NODES 列出叢集當前已知的所有節點(node),以及這些節點的相關資訊。
#### 節點(node)
CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節點新增到叢集當中,讓它成為叢集的一份子。
CLUSTER FORGET <node_id> 從叢集中移除 node_id 指定的節點。
CLUSTER REPLICATE <node_id> 將當前節點設定為 node_id 指定的節點的從節點。
CLUSTER SAVECONFIG 將節點的配置檔案儲存到硬盤裡面。
#### 槽(slot)
CLUSTER ADDSLOTS <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。
CLUSTER DELSLOTS <slot> [slot ...] 移除一個或多個槽對當前節點的指派。
CLUSTER FLUSHSLOTS 移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。
CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麼先讓另一個節點刪除該槽>,然後再進行指派。
CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。
CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節點中匯入槽 slot 到本節點。
CLUSTER SETSLOT <slot> STABLE 取消對槽 slot 的匯入(import)或者遷移(migrate)。
#### 鍵(key)
CLUSTER KEYSLOT <key> 計算鍵 key 應該被放置在哪個槽上。
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對數量。
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個 slot 槽中的鍵。