天天看點

Redis叢集——Cluster模式搭建

作者:面向百度工作

環境準備

作業系統:Centos 7.7

Redis:5.0.9

IP 端口 作用
10.4.7.41 6000,6001 每台機器啟動兩個執行個體
10.4.7.42 6000,6001 每台機器啟動兩個執行個體
10.4.7.43 6000,6001 每台機器啟動兩個執行個體

安裝redis

所有redis使用二進制的方式部署,這裡僅用一台機器的操作為例,詳細步驟不做贅述

這裡我提前将redis的二進制軟體包放到了/usr/local/src目錄下

[root@localhost src]# cd /usr/loca/src
[root@localhost src]# tar zxvf redis-5.0.9.tar.gz
[root@localhost src]# mv redis-5.0.9 /usr/local/redis
[root@localhost src]# yum install -y gcc tcl
[root@localhost src]# cd /usr/local/redis/deps
[root@localhost deps]# make hiredis jemalloc linenoise lua
[root@localhost deps]# cd ..
[root@localhost redis]# make install PREFIX=/usr/local/redis           

修改配置檔案

10.4.7.41

[root@localhost src]# cd /usr/local/redis
[root@localhost redis]# mkdir cluster
[root@localhost redis]# cp redis.conf cluster/redis_6000.conf
[root@localhost redis]# cp redis.conf cluster/redis_6001.conf
[root@localhost redis]# mkdir -p /data/redis/data/{redis_6000,redis_6001}
[root@localhost redis]# mkdir -p /data/redis/logs
[root@localhost redis]# vi cluster/redis_6000.conf
bind 10.4.7.41
port 6000
daemonize yes
pidfile "/var/run/redis_6000.pid"
logfile "/data/redis/logs/redis_6000.log"
dir "/data/redis/data/redis_6000"
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_6000.conf
[root@localhost redis]# vi cluster/redis_6001.conf
bind 10.4.7.41
port 6001
daemonize yes
pidfile "/var/run/redis_6001.pid"
logfile "/usr/local/redis/cluster/redis_6001.log"
dir "/data/redis/cluster/redis_6001"
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_6001.conf           

10.4.7.42

[root@localhost src]# cd /usr/local/redis
[root@localhost redis]# mkdir cluster
[root@localhost redis]# cp redis.conf cluster/redis_6000.conf
[root@localhost redis]# cp redis.conf cluster/redis_6001.conf
[root@localhost redis]# mkdir -p /data/redis/data/{redis_6000,redis_6001}
[root@localhost redis]# mkdir -p /data/redis/logs
[root@localhost redis]# vi cluster/redis_6000.conf
bind 10.4.7.42
port 6000
daemonize yes
pidfile "/var/run/redis_6000.pid"
logfile "/data/redis/logs/redis_6000.log"
dir "/data/redis/data/redis_6000"
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_6000.conf
[root@localhost redis]# vi cluster/redis_6001.conf
bind 10.4.7.42
port 6001
daemonize yes
pidfile "/var/run/redis_6001.pid"
logfile "/usr/local/redis/cluster/redis_6001.log"
dir "/data/redis/cluster/redis_6001"
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_6001.conf           

10.4.7.43

[root@localhost src]# cd /usr/local/redis
[root@localhost redis]# mkdir cluster
[root@localhost redis]# cp redis.conf cluster/redis_6000.conf
[root@localhost redis]# cp redis.conf cluster/redis_6001.conf
[root@localhost redis]# mkdir -p /data/redis/data/{redis_6000,redis_6001}
[root@localhost redis]# mkdir -p /data/redis/logs
[root@localhost redis]# vi cluster/redis_6000.conf
bind 10.4.7.43
port 6000
daemonize yes
pidfile "/var/run/redis_6000.pid"
logfile "/data/redis/logs/redis_6000.log"
dir "/data/redis/data/redis_6000"
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_6000.conf
[root@localhost redis]# vi cluster/redis_6001.conf
bind 10.4.7.43
port 6001
daemonize yes
pidfile "/var/run/redis_6001.pid"
logfile "/usr/local/redis/cluster/redis_6001.log"
dir "/data/redis/cluster/redis_6001"
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_6001.conf           

啟動redis

10.4.7.41

[root@localhost redis]# ./bin/redis-server ./cluster/redis_6000.conf
[root@localhost redis]# ./bin/redis-server ./cluster/redis_6001.conf           

10.4.7.42

[root@localhost redis]# ./bin/redis-server ./cluster/redis_6000.conf
[root@localhost redis]# ./bin/redis-server ./cluster/redis_6001.conf           

10.4.7.43

[root@localhost redis]# ./bin/redis-server ./cluster/redis_6000.conf
[root@localhost redis]# ./bin/redis-server ./cluster/redis_6001.conf           

叢集建立

[root@localhost redis]# ./bin/redis-cli -a 123456 --cluster create 10.4.7.41:6000 10.4.7.41:6001 10.4.7.42:6000 10.4.7.42:6001 10.4.7.43:6000 10.4.7.43:6001 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.4.7.42:6001 to 10.4.7.41:6000
Adding replica 10.4.7.43:6001 to 10.4.7.42:6000
Adding replica 10.4.7.41:6001 to 10.4.7.43:6000
M: 27b4b1e72ec83b47c45f6a54f4e5029d54ab52fc 10.4.7.41:6000
   slots:[0-5460] (5461 slots) master
S: 2579f7e50448218ecb5d8ab2d1ab42683212c084 10.4.7.41:6001
   replicates c0d1c39f868af00bd49a7b166519f12fbc0abdb3
M: 5698cd7e6d20276add4ed6a0d3c11e29ae781d8c 10.4.7.42:6000
   slots:[5461-10922] (5462 slots) master
S: 9aee1d6d7b789674f2aeb38da56bdb6c8f8bbfb8 10.4.7.42:6001
   replicates 27b4b1e72ec83b47c45f6a54f4e5029d54ab52fc
M: c0d1c39f868af00bd49a7b166519f12fbc0abdb3 10.4.7.43:6000
   slots:[10923-16383] (5461 slots) master
S: 932af8fad5124d3339578fc3398289f3269134dc 10.4.7.43:6001
   replicates 5698cd7e6d20276add4ed6a0d3c11e29ae781d8c
Can I set the above configuration? (type 'yes' to accept): yes  #輸入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.4.7.41:6000)
M: 27b4b1e72ec83b47c45f6a54f4e5029d54ab52fc 10.4.7.41:6000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 932af8fad5124d3339578fc3398289f3269134dc 10.4.7.43:6001
   slots: (0 slots) slave
   replicates 5698cd7e6d20276add4ed6a0d3c11e29ae781d8c
M: 5698cd7e6d20276add4ed6a0d3c11e29ae781d8c 10.4.7.42:6000
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 2579f7e50448218ecb5d8ab2d1ab42683212c084 10.4.7.41:6001
   slots: (0 slots) slave
   replicates c0d1c39f868af00bd49a7b166519f12fbc0abdb3
S: 9aee1d6d7b789674f2aeb38da56bdb6c8f8bbfb8 10.4.7.42:6001
   slots: (0 slots) slave
   replicates 27b4b1e72ec83b47c45f6a54f4e5029d54ab52fc
M: c0d1c39f868af00bd49a7b166519f12fbc0abdb3 10.4.7.43:6000
   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叢集——Cluster模式搭建

注意:redis5.0版本之前的建立方法不同,之前版本建立叢集需要使用redis-trib.rb進行建立

相關目錄下也生成了我們的叢集配置檔案

[root@localhost redis]# ls -al /data/redis/data/redis_6000/
total 4
drwxr-xr-x 2 root root  50 Aug 15 10:46 .
drwxr-xr-x 6 root root  79 Aug 15 10:47 ..
-rw-r--r-- 1 root root   0 Aug 15 10:46 appendonly.aof
-rw-r--r-- 1 root root 781 Aug 15 10:55 node_6000.conf           

叢集檢查

登入叢集檢視相關資訊

[root@localhost redis]# ./bin/redis-cli -c -h 10.4.7.41 -p 6000 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.4.7.41:6000> 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:277
cluster_stats_messages_pong_sent:283
cluster_stats_messages_sent:560
cluster_stats_messages_ping_received:278
cluster_stats_messages_pong_received:277
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:560
10.4.7.41:6000> CLUSTER NODES
27b4b1e72ec83b47c45f6a54f4e5029d54ab52fc 10.4.7.41:6000@16000 myself,master - 0 1597460440000 1 connected 0-5460
932af8fad5124d3339578fc3398289f3269134dc 10.4.7.43:6001@16001 slave 5698cd7e6d20276add4ed6a0d3c11e29ae781d8c 0 1597460441296 6 connected
5698cd7e6d20276add4ed6a0d3c11e29ae781d8c 10.4.7.42:6000@16000 master - 0 1597460440289 3 connected 5461-10922
2579f7e50448218ecb5d8ab2d1ab42683212c084 10.4.7.41:6001@16001 slave c0d1c39f868af00bd49a7b166519f12fbc0abdb3 0 1597460439280 5 connected
9aee1d6d7b789674f2aeb38da56bdb6c8f8bbfb8 10.4.7.42:6001@16001 slave 27b4b1e72ec83b47c45f6a54f4e5029d54ab52fc 0 1597460438276 4 connected
c0d1c39f868af00bd49a7b166519f12fbc0abdb3 10.4.7.43:6000@16000 master - 0 1597460441000 5 connected 10923-16383           

繼續閱讀