高可用Redis(十):Redis原生命令搭建叢集
阿新 • • 發佈:2018-11-10
1.搭建Redis Cluster主要步驟
1.配置開啟節點
2.meet
3.指派槽
4.主從關係分配
2.環境說明
兩臺虛擬機器,IP地址分別為:192.168.81.100和192.168.81.101 兩臺虛擬機器作業系統均為:CentOS 7.5 64位 兩臺虛擬機器系統說明: 使用yum方式安裝Redis 關閉firewalld防火牆 使用兩臺虛擬機器的7000,7001,7002埠搭建三主三從的Redis Cluster 其中192.168.81.100主機三個埠啟動的Redis Server為主節點 192.168.81.101主機三個埠啟動的Redis Server為從節點
3.具體步驟
3.1 在192.168.81.100虛擬機器上操作
[[email protected] etc]# cd /opt
[[email protected] opt]# mkdir config
[[email protected] opt]# cd config
[[email protected] config]# vi redis_7000.conf # 建立7000埠執行需要的配置檔案,檔案內容如下
port 7000
daemonize yes
dir '/var/lib/redis'
logfile '/var/log/redis/redis_7000.log'
dbfilename 'redis_7000.data'
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-require-full-coverage no
[ [email protected] config]# sed 's/7000/7001/g' redis_7000.conf > redis_7001.conf # 把redis_7000.conf檔案中的7000替換成7001,並生成redis_7001.conf檔案
[[email protected] config]# sed 's/7000/7002/g' redis_7000.conf > redis_7002.conf # 把redis_7000.conf檔案中的7000替換成7002,並生成redis_7002.conf檔案
[[email protected] config]# redis-server /opt/config/redis_7000.conf # 指定配置檔案,啟動redis server節點
[ [email protected] config]# redis-server /opt/config/redis_7001.conf # 指定配置檔案,啟動redis server節點
[[email protected] config]# redis-server /opt/config/redis_7002.conf # 指定配置檔案,啟動redis server節點
[[email protected] config]# ps aux | grep redis-server # 檢視redis-server程序執行情況
root 2444 0.1 0.5 142904 5328 ? Ssl 19:38 0:00 redis-server *:7000 [cluster]
root 2475 0.0 0.5 142904 5328 ? Ssl 19:39 0:00 redis-server *:7001 [cluster]
root 2479 0.2 0.5 142904 5328 ? Ssl 19:39 0:00 redis-server *:7002 [cluster]
root 2483 0.0 0.0 112648 964 pts/0 R+ 19:39 0:00 grep --color=auto redis-server
3.2 在192.168.81.101虛擬機器上進行第一步的操作,生成三個配置檔案,並啟動Redis server
[[email protected] config]# redis-server /opt/config/redis_7000.conf # 指定配置檔案,啟動redis server節點
[[email protected] config]# redis-server /opt/config/redis_7001.conf # 指定配置檔案,啟動redis server節點
[[email protected] config]# redis-server /opt/config/redis_7002.conf # 指定配置檔案,啟動redis server節點
[[email protected] config]# ps aux | grep redis-server # 檢視redis-server的程序執行情況
root 1704 0.1 0.2 142916 5344 ? Ssl 19:41 0:00 redis-server *:7000 [cluster]
root 1736 0.0 0.2 142916 5344 ? Ssl 19:41 0:00 redis-server *:7001 [cluster]
root 1740 0.0 0.2 142916 5340 ? Ssl 19:42 0:00 redis-server *:7002 [cluster]
3.3 在192.168.81.100虛擬機器上檢視cluster節點的資訊
[[email protected] config]# redis-cli -p 7000
127.0.0.1:7000> set hello world # 向當前節點執行寫入命令,失敗
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:7000> cluster nodes # 檢視叢集節點資訊,只有當前節點的資訊,且顯示為主節點
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed :7000 myself,master - 0 0 0 connected
127.0.0.1:7000> cluster info # 檢視cluster的資訊
cluster_state:fail # 叢集為失敗狀態
cluster_slots_assigned:0
cluster_slots_ok:0 # 當前叢集中有0個slot(槽)
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1 # 當前叢集中節點個數為1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
127.0.0.1:7000> exit
[[email protected] config]# cd /var/lib/redis/
[[email protected] redis]# ls
dump.rdb nodes-7000.conf nodes-7001.conf nodes-7002.conf
[[email protected] redis]# cat nodes-7000.conf # 檢視7000埠上執行的redis-server的run_id與執行cluster nodes命令得到的run_id相同
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed :0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
3.4 在192.168.81.100上執行meet操作
[[email protected] redis]# redis-cli -p 7000 cluster meet 127.0.0.1 7001 # 通過7000埠的redis server向7001埠的redis server傳送meet操作
OK
[[email protected] redis]# redis-cli -p 7000 cluster nodes # 通過7000埠執行的redis server檢視cluster的節點資訊,已經新增7001埠執行的redis server,且都為master節點
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 myself,master - 0 0 0 connected
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 master - 0 1539719395866 1 connected
[[email protected] redis]# redis-cli -p 7001 cluster nodes # 通過7001埠執行的redis server檢視cluster的節點資訊,也可以看到7000埠執行的redis server
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 master - 0 1539719408423 0 connected
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 myself,master - 0 0 1 connected
[[email protected] redis]# redis-cli -p 7002 cluster nodes # 通過7002埠的redis server檢視cluster的節點資訊,7000埠和7001埠執行的redis server都不在其中,因為7002埠執行的redis server沒有meet
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a :7002 myself,master - 0 0 0 connected
[[email protected] redis]# redis-cli -p 7000 cluster meet 127.0.0.1 7002 # 向7002埠執行的redis server傳送meet操作
OK
[[email protected] redis]# redis-cli -p 7001 cluster nodes # 7002埠執行的redis server已經新增到叢集中了
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 127.0.0.1:7002 master - 0 1539719438785 0 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 master - 0 1539719439689 2 connected
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 myself,master - 0 0 1 connected
[[email protected] redis]# redis-cli -p 7002 cluster nodes
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 master - 0 1539719442308 1 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 master - 0 1539719441301 2 connected
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 127.0.0.1:7002 myself,master - 0 0 0 connected
[[email protected] redis]# redis-cli -p 7000 cluster meet 192.168.81.101 7002 # 向192.168.81.101:7002埠執行的redis server執行meet操作
OK
[[email protected] redis]# redis-cli -p 7000 cluster meet 192.168.81.101 7000 # 向192.168.81.101:7000埠執行的redis server執行meet操作
OK
[[email protected] redis]# redis-cli -p 7000 cluster meet 192.168.81.101 7001 # 向192.168.81.101:7001埠執行的redis server執行meet操作
OK
[[email protected] redis]# redis-cli -p 7000 cluster nodes
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 master - 0 1539719546060 5 connected
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 master - 0 1539719548113 3 connected
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 192.168.81.101:7001 master - 0 1539719547106 4 connected
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 master - 0 1539719544042 1 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 myself,master - 0 0 2 connected
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 127.0.0.1:7002 master - 0 1539719545050 0 connected
[[email protected] redis]# redis-cli -p 7002 cluster nodes # 檢視叢集中的節點資訊,發現6個node都已經新增到當前叢集中了
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 master - 0 1539719554166 1 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 master - 0 1539719554668 2 connected
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 192.168.81.101:7001 master - 0 1539719555174 4 connected
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 master - 0 1539719556180 3 connected
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 127.0.0.1:7002 myself,master - 0 0 0 connected
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 master - 0 1539719557188 5 connected
[[email protected] redis]# redis-cli -p 7002 cluster info # 檢視叢集相關的資訊
cluster_state:fail # 叢集狀態為失敗
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6 # 叢集中有6個已知節點
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:0
cluster_stats_messages_sent:507
cluster_stats_messages_received:507
3.5 在192.168.81.101虛擬機器上檢視cluster的資訊
[[email protected] config]# redis-cli -p 7000 cluster nodes # 檢視cluster的節點資訊,6個節點的資訊都顯示出來,且都為master
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 master - 0 1539690828453 3 connected
45d79e730593df3216a4236a19c21ff601949449 192.168.81.100:7001 master - 0 1539690825432 1 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 192.168.81.100:7000 master - 0 1539690826437 2 connected
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 192.168.81.100:7002 master - 0 1539690827448 0 connected
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 192.168.81.101:7001 master - 0 1539690827950 4 connected
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 myself,master - 0 0 5 connected
[[email protected] config]# redis-cli -p 7001 cluster nodes # 檢視cluster的節點資訊,6個節點的資訊都顯示出來,且都為master
53911425f4479ce4f054b8b714f4700715f0dc64 127.0.0.1:7002 master - 0 1539690840955 3 connected
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 master - 0 1539690842968 5 connected
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 192.168.81.101:7001 myself,master - 0 0 4 connected
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 192.168.81.100:7002 master - 0 1539690844979 0 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 192.168.81.100:7000 master - 0 1539690841960 2 connected
45d79e730593df3216a4236a19c21ff601949449 192.168.81.100:7001 master - 0 1539690843977 1 connected
[[email protected] config]# redis-cli -p 7002 cluster nodes # 檢視cluster的節點資訊,6個節點的資訊都顯示出來,且都為master
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 127.0.0.1:7001 master - 0 1539690847798 4 connected
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 myself,master - 0 0 3 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 192.168.81.100:7000 master - 0 1539690846795 2 connected
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 192.168.81.100:7002 master - 0 1539690843775 0 connected
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 master - 0 1539690844780 5 connected
45d79e730593df3216a4236a19c21ff601949449 192.168.81.100:7001 master - 0 1539690845786 1 connected
[[email protected] config]# redis-cli -p 7002 cluster info # 檢視cluster的資訊
cluster_state:fail # 叢集狀態為失敗狀態
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6 # 叢集中添加了6個節點
cluster_size:0 # 叢集中
cluster_current_epoch:5
cluster_my_epoch:3
cluster_stats_messages_sent:413
cluster_stats_messages_received:413
3.6 在192.168.81.100虛擬機器上編寫指令碼,完成分配slot
[[email protected] ~]# cd /opt/config/
[[email protected] config]# vi add_slots.sh # 編寫add_slots.sh指令碼
#!/bin/bash
start=$1
end=$2
port=$3
for slot in `seq ${start} ${end}`
do
echo "slot:${slot}"
redis-cli -p ${port} cluster addslots ${slot}
done
[[email protected] config]# sh add_slots.sh 0 5461 7000 # 執行add_slots.sh指令碼,把0到5461號槽分配給192.168.81.100:7000的redis server節點
slot:0
OK
slot:1
OK
slot:2
OK
slot:3
OK
slot:4
OK
slot:5
OK
...中間省略
slot:5459
OK
slot:5460
OK
slot:5461
OK
[[email protected] config]# redis-cli -p 7000 cluster info # 檢視cluster叢集的資訊
cluster_state:ok # 叢集狀態為OK狀態
cluster_slots_assigned:5462 # 叢集中已經有5462個數據槽
cluster_slots_ok:5462 # 5462個槽的狀態為0K狀態
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6 # 叢集添加了6個節點
cluster_size:1 # 叢集的大小為1個
cluster_current_epoch:5
cluster_my_epoch:2
cluster_stats_messages_sent:2237
cluster_stats_messages_received:2237
[[email protected] config]# redis-cli -p 7000 cluster nodes # 檢視叢集中節點的資訊
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 master - 0 1539720483326 5 connected
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 master - 0 1539720484336 3 connected
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 192.168.81.101:7001 master - 0 1539720485346 4 connected
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 master - 0 1539720482314 1 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 myself,master - 0 0 2 connected 0-5461 # 192.168.81.100:7000埠執行的redis server已經分配的槽為0到5461號槽,其他節點還沒有分配槽
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 127.0.0.1:7002 master - 0 1539720486351 0 connected
[[email protected] config]# redis-cli -p 7002 cluster nodes
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 master - 0 1539720507768 1 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 master - 0 1539720509782 2 connected 0-5461
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 192.168.81.101:7001 master - 0 1539720510789 4 connected
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 master - 0 1539720508776 3 connected
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 127.0.0.1:7002 myself,master - 0 0 0 connected
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 master - 0 1539720511799 5 connected
[[email protected] config]# sh add_slots.sh 5462 10922 7001 # 執行add_slots.sh指令碼,把5462號到10922號槽分配給7001埠執行的redis server
[[email protected] config]# sh add_slots.sh 10923 16383 7002 # 執行add_slots.sh指令碼,把10923號到16383號槽分配給7002埠執行的redis server
[[email protected] config]# redis-cli -p 7000 cluster nodes # 檢視叢集的節點資訊
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 master - 0 1539720810075 5 connected
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 master - 0 1539720807558 3 connected
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 192.168.81.101:7001 master - 0 1539720804033 4 connected
45d79e730593df3216a4236a19c21ff601949449 127.0.0.1:7001 master - 0 1539720809067 1 connected 5462-10922 # 7001埠執行的redis server的槽編號為5462號到10922號槽
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 127.0.0.1:7000 myself,master - 0 0 2 connected 0-5461 # 7000埠執行的redis server的槽編號為0號到5461號槽
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 127.0.0.1:7002 master - 0 1539720808061 0 connected 10923-16383 # 7002埠執行的redis server的槽編號為10922號到16383號槽
[[email protected] config]# redis-cli -p 7000 cluster info # 檢視叢集的資訊
cluster_state:ok # 叢集狀態為ok狀態
cluster_slots_assigned:16384 # 叢集已經被分配16384個槽
cluster_slots_ok:16384 # 叢集中狀態為ok的槽有16384個
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6 # 叢集有6個節點
cluster_size:3 # 叢集大小為3個
cluster_current_epoch:5
cluster_my_epoch:2
cluster_stats_messages_sent:2950
cluster_stats_messages_received:2950
[[email protected] config]# redis-cli -p 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:5
cluster_my_epoch:1
cluster_stats_messages_sent:3158
cluster_stats_messages_received:3158
[[email protected] config]# redis-cli -p 7002 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:5
cluster_my_epoch:0
cluster_stats_messages_sent:2828
cluster_stats_messages_received:2828
[[email protected] config]# redis-cli -p 7000
127.0.0.1:7000> set hello world # 成功向叢集中寫入資料
OK
127.0.0.1:7000> config get cluster* # 獲取叢集的配置資訊
1) "cluster-node-timeout"
2) "15000"
3) "cluster-migration-barrier"
4) "1"
5) "cluster-slave-validity-factor"
6) "10"
7) "cluster-require-full-coverage"
8) "no"
127.0.0.1:7000> exit
3.7 在192.168.81.101虛擬機器上操作
[[email protected] config]# redis-cli -p 7002 cluster nodes # 檢視叢集的節點資訊,都是master
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 127.0.0.1:7001 master - 0 1539693824725 4 connected
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 myself,master - 0 0 3 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 192.168.81.100:7000 master - 0 1539693826743 2 connected 0-5461
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 192.168.81.100:7002 master - 0 1539693829260 0 connected 10923-16383
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 master - 0 1539693830266 5 connected
45d79e730593df3216a4236a19c21ff601949449 192.168.81.100:7001 master - 0 1539693828252 1 connected 5462-10922
[[email protected] config]# redis-cli -p 7000 cluster replicate d20aa403c5d7d9507adcc4ef6132c14f3c9486ed # 使192.168.81.101:7000埠執行的redis server做為192.168.81.100:7000埠執行的redis server的master
OK
[[email protected] config]# redis-cli -p 7001 cluster replicate 45d79e730593df3216a4236a19c21ff601949449
OK
[[email protected] config]# redis-cli -p 7002 cluster replicate e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a
OK
[[email protected] config]# redis-cli -p 7002 cluster nodes # 檢視叢集的節點資訊
bb88f0996503bac3b222b9cc4ec25139ad34ed3a 127.0.0.1:7001 slave 45d79e730593df3216a4236a19c21ff601949449 0 1539693962338 4 connected
53911425f4479ce4f054b8b714f4700715f0dc64 192.168.81.101:7002 myself,slave e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 0 0 3 connected
d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 192.168.81.100:7000 master - 0 1539693958297 2 connected 0-5461
e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a 192.168.81.100:7002 master - 0 1539693960324 0 connected 10923-16383
c96589b19b8ff5d9d286d470b8efff9a540726d8 192.168.81.101:7000 slave d20aa403c5d7d9507adcc4ef6132c14f3c9486ed 0 1539693961329 5 connected
45d79e730593df3216a4236a19c21ff601949449 192.168.81.100:7001 master - 0 1539693963347 1 connected 5462-10922
[[email protected] config]# redis-cli -p 7002 cluster slots # 檢視叢集的槽與節點的關係
1) 1) (integer) 0
2) (integer) 5461
3) 1) "192.168.81.100"
2) (integer) 7000
3) "d20aa403c5d7d9507adcc4ef6132c14f3c9486ed"
4) 1) "192.168.81.101"
2) (integer) 7000
3) "c96589b19b8ff5d9d286d470b8efff9a540726d8"
2) 1) (integer) 10923
2) (integer) 16383
3) 1) "192.168.81.100"
2) (integer) 7002
3) "e5ea887c98e79ef2b2205d6cc1d7ac5cfe936d9a"
4) 1) "192.168.81.101"
2) (integer) 7002
3) "53911425f4479ce4f054b8b714f4700715f0dc64"
3) 1) (integer) 5462
2) (integer) 10922
3) 1) "192.168.81.100"
2) (integer) 7001
3) "45d79e730593df3216a4236a19c21ff601949449"
4) 1) "127.0.0.1"
2) (integer) 7001
3) "bb88f0996503bac3b222b9cc4ec25139ad34ed3a"
3.8 向叢集中寫入資料
[[email protected] config]# redis-cli -c -p 7000
127.0.0.1:7000> set hello world
OK
127.0.0.1:7000> exit
4.優點
可以深入理解Redis Cluster架構
5.缺點
容易出差錯,生產環境中不建議使用