您好,登錄后才能下訂單哦!
Cluster集群
Redis Cluster是官方提供的高可用方案,一個(gè)去中心化的Redis集群方案,所有的Redis節(jié)點(diǎn)彼此間互聯(lián)(PING-PONG機(jī)制),客戶端直連任意節(jié)點(diǎn)即可連接到Cluster集群,無(wú)需haproxy中間件。
總結(jié):
1、 去中心化,分片存儲(chǔ),把所有的物理節(jié)點(diǎn)映射到[0-16383]共16384個(gè)slot(槽位)上,并不一定平均分配。
2、 Redis集群預(yù)分好16384個(gè)桶,當(dāng)需要在 Redis 集群中放置一個(gè) key-value 時(shí),根據(jù) CRC16(key) mod 16384的值,決定將一個(gè)key放到哪個(gè)桶中。
3、 支持主從機(jī)制,一個(gè)master可以有一個(gè)或多個(gè)從節(jié)點(diǎn)來(lái)保證集群的高可用性。
4、 master宕機(jī)需要經(jīng)過集群中的所有master投票,如果有半數(shù)以上節(jié)點(diǎn)通信超時(shí),則認(rèn)為當(dāng)前master宕掉。
5、 如果Cluster集群中的master沒有slave,那么任意一臺(tái)master宕掉后,集群不可用。
6、 集群如果超多半數(shù)以上master掛掉,無(wú)論是否有slave,集群進(jìn)入fail狀態(tài)。
7、 slave節(jié)點(diǎn)不會(huì)分配槽位
8、 Cluster集群最多支持1000個(gè)節(jié)點(diǎn)
環(huán)境
三臺(tái)虛擬機(jī),啟動(dòng)redis雙實(shí)例,部署三主三從集群;此次是講解最原始的Cluster集群創(chuàng)建,不使用官方的redis.trib.rb腳本創(chuàng)建,這是一個(gè)官方的ruby腳本。因?yàn)樵诠旧a(chǎn)中使用官方的腳本創(chuàng)建三臺(tái)虛擬機(jī)雙實(shí)例的Cluster集群,發(fā)現(xiàn)最后一臺(tái)虛擬機(jī)始終是自己的6379和6380組成一對(duì)主從,即并非三臺(tái)都是互為主從,有一對(duì)主從的是自己的實(shí)例。
172.16.1.7
172.16.1.31
172.16.1.61
#yum安裝redis(需要epel源)
yum install -y redis
#創(chuàng)建多實(shí)例
mkdir -p /data/redis/{6379,6380}/{db,log}
配置文件
[root@web02 6379]# cat /data/redis/6379/redis.conf
bind 172.16.1.7
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /data/redis/6379/redis.pid
loglevel notice
logfile /data/redis/6379/log/redis.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 /data/redis/6379/db
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
#啟動(dòng)redis實(shí)例
redis-server /data/redis/6379/redis.conf
redis-server /data/redis/6380/redis.conf
#配置requirepass認(rèn)證
#臨時(shí)配置(不重啟配置)
redis-cli -h 172.16.1.7 -p 6379
172.16.1.7:6379> config get requirepass
1) "requirepass"
2) ""
172.16.1.7:6379> config set requirepass 123456
OK
172.16.1.7:6379> config get requirepass
(error) NOAUTH Authentication required.
172.16.1.7:6379> auth 123456
OK
172.16.1.7:6379> config get requirepass
1) "requirepass"
2) "123456"
#永久配置,必須修改配置文件
#手動(dòng)修改
requirepass "123456"
#直接保存在命令行中的配置
172.16.1.7:6380> config rewrite
#該命令會(huì)把配置刷到配置文件中(刷到磁盤永久化)
requirepass "123456"
#刷入redis.conf、nodes.conf配置文件,如果是集群的配置需要使用
#cluster saveconfig命令
創(chuàng)建cluster集群
修改配置文件
#每個(gè)cluster節(jié)點(diǎn)都必須啟動(dòng)對(duì)cluster集群的支持
cluster-enabled yes
cluster-config-file nodes-6379.conf #(會(huì)保存在數(shù)據(jù)目錄下)
cluster-node-timeout 15000
#cluster集群中不能有slaveof的定義
slaveof 172.16.1.61 6379
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
Reading the configuration file, at line 57
>>> 'slaveof 172.16.1.61 6379'
slaveof directive not allowed in cluster mode
#查看redis進(jìn)程,是以集群模式啟動(dòng)的
[root@web02 ~]# ps -ef|grep redis
root 1193 1 0 00:08 ? 00:00:01 redis-server 172.16.1.7:6379 [cluster]
root 1212 1 0 00:24 ? 00:00:00 redis-server 172.16.1.7:6380 [cluster]
#此時(shí)集群的狀態(tài)還是fail,因?yàn)檫€沒有把其它的節(jié)點(diǎn)都加進(jìn)來(lái),槽位也還沒有分配
redis-cli -h 172.16.1.7 -p 6379 -a 123456
172.16.1.7:6379> cluster info
cluster_state:fail
cluster_slots_assigned:1
cluster_slots_ok:1
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:1
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
172.16.1.7:6379> cluster nodes
82424bf0ff964ca5486b7f904b95c155e6dceeb1 :6379 myself,master - 0 0 0 connected 15197
#在任意一個(gè)節(jié)點(diǎn)上把所有節(jié)點(diǎn)加入集群中
#把指定的節(jié)點(diǎn)添加到集群中
cluster meet 172.16.1.7 6380
172.16.1.7:6379> cluster nodes
965276bda73d2bce4a44a8fab6e18f676de54f15 172.16.1.61:6380 master - 0 1543768223343 0 connected
56572d4ef3b7e30c30a4956c87d4a6ea0050bef5 172.16.1.31:6379 master - 0 1543768221299 2 connected
673443984a3610fcaa2fcbf3d295f60b2f7f32ae 172.16.1.31:6380 master - 0 1543768222323 3 connected
26d3142fb8766e1cfc01978d01c17e287f4ebd38 172.16.1.61:6379 master - 0 1543768225389 4 connected
85142215ac8c6af9ad0a72fb6ac2a5d2be2608a1 172.16.1.7:6380 master - 0 1543768224368 0 connected
82424bf0ff964ca5486b7f904b95c155e6dceeb1 172.16.1.7:6379 myself,master - 0 0 1 connected 15197
172.16.1.7:6379> cluster info
cluster_state:fail
cluster_slots_assigned:1
cluster_slots_ok:1
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:1
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_sent:96
cluster_stats_messages_received:9
#在slave節(jié)點(diǎn)上指定master節(jié)點(diǎn)
#把當(dāng)前節(jié)點(diǎn)設(shè)置為node_id的slave,即指定當(dāng)前節(jié)點(diǎn)的master節(jié)點(diǎn),在每個(gè)slave節(jié)點(diǎn)上執(zhí)行
[root@web02 ~]# redis-cli -h 172.16.1.7 -p 6380 -a 123456
172.16.1.7:6380> cluster nodes
85142215ac8c6af9ad0a72fb6ac2a5d2be2608a1 172.16.1.7:6380 myself,master - 0 0 5 connected
26d3142fb8766e1cfc01978d01c17e287f4ebd38 172.16.1.61:6379 master - 0 1543768446773 4 connected
82424bf0ff964ca5486b7f904b95c155e6dceeb1 172.16.1.7:6379 master - 0 1543768444120 1 connected 15197
965276bda73d2bce4a44a8fab6e18f676de54f15 172.16.1.61:6380 master - 0 1543768445753 0 connected
56572d4ef3b7e30c30a4956c87d4a6ea0050bef5 172.16.1.31:6379 master - 0 1543768441669 2 connected
673443984a3610fcaa2fcbf3d295f60b2f7f32ae 172.16.1.31:6380 master - 0 1543768444734 3 connected
172.16.1.7:6380> cluster replicate 26d3142fb8766e1cfc01978d01c17e287f4ebd38
OK
#最終結(jié)果
172.16.1.61:6380> cluster nodes
82424bf0ff964ca5486b7f904b95c155e6dceeb1 172.16.1.7:6379 master - 0 1543768678663 1 connected 15197
56572d4ef3b7e30c30a4956c87d4a6ea0050bef5 172.16.1.31:6379 master - 0 1543768682754 2 connected
965276bda73d2bce4a44a8fab6e18f676de54f15 172.16.1.61:6380 myself,slave 56572d4ef3b7e30c30a4956c87d4a6ea0050bef5 0 0 0 connected
85142215ac8c6af9ad0a72fb6ac2a5d2be2608a1 172.16.1.7:6380 slave 26d3142fb8766e1cfc01978d01c17e287f4ebd38 0 1543768683780 5 connected
673443984a3610fcaa2fcbf3d295f60b2f7f32ae 172.16.1.31:6380 slave 82424bf0ff964ca5486b7f904b95c155e6dceeb1 0 1543768679697 3 connected
26d3142fb8766e1cfc01978d01c17e287f4ebd38 172.16.1.61:6379 master - 0 1543768681741 4 connected
分配槽位
#redis3.0.0規(guī)定槽位一共有16384個(gè),我們這里是平均分配到三個(gè)主節(jié)點(diǎn)上。
for i in {0..5460};do redis-cli -h 172.16.1.7 -p 6379 -a 123456 cluster addslots $i >/dev/null;done
for i in {5461..10922};do redis-cli -h 172.16.1.31 -p 6379 -a 123456 cluster addslots $i >/dev/null;done
for i in {10923..16383};do redis-cli -h 172.16.1.61 -p 6379 -a 123456 cluster addslots $i >/dev/null;done
#把集群配置寫入磁盤,每個(gè)節(jié)點(diǎn)都需要執(zhí)行cluster saveconfig
#檢查集群狀態(tài)
172.16.1.7:6379> 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:2972
cluster_stats_messages_received:2972
172.16.1.7:6379> cluster nodes
965276bda73d2bce4a44a8fab6e18f676de54f15 172.16.1.61:6380 slave 56572d4ef3b7e30c30a4956c87d4a6ea0050bef5 0 1543769558241 2 connected
56572d4ef3b7e30c30a4956c87d4a6ea0050bef5 172.16.1.31:6379 master - 0 1543769553136 2 connected 5461-10922
673443984a3610fcaa2fcbf3d295f60b2f7f32ae 172.16.1.31:6380 slave 82424bf0ff964ca5486b7f904b95c155e6dceeb1 0 1543769559263 3 connected
26d3142fb8766e1cfc01978d01c17e287f4ebd38 172.16.1.61:6379 master - 0 1543769556196 4 connected 10923-15196 15198-16383
85142215ac8c6af9ad0a72fb6ac2a5d2be2608a1 172.16.1.7:6380 slave 26d3142fb8766e1cfc01978d01c17e287f4ebd38 0 1543769557220 5 connected
82424bf0ff964ca5486b7f904b95c155e6dceeb1 172.16.1.7:6379 myself,master - 0 0 1 connected 0-5460 15197
#配置集群masterauth認(rèn)證,在集群創(chuàng)建成功之后配置,不然集群會(huì)創(chuàng)建失敗
172.16.1.7:6379> config set masterauth 123456
OK
172.16.1.7:6379> config get masterauth
1) "masterauth"
2) "123456"
172.16.1.7:6379> config rewrite
OK
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。