1. 程式人生 > 其它 >redis主從複製,哨兵以及叢集搭建部署

redis主從複製,哨兵以及叢集搭建部署

redis主從複製

1.redis支援多例項的功能,一臺機器上,可以執行多個單個的redis資料庫

環境準備,執行3個redis資料庫,達到  1主 2從的配置

主庫  6379.conf 
    port 6379
    daemonize yes
    pidfile /data/6379/redis.pid
    loglevel notice
    logfile "/data/6379/redis.log"
    dbfilename dump.rdb
    dir /data/6379

從庫 6380
    port 6380
    daemonize yes
    pidfile 
/data/6380/redis.pid loglevel notice logfile "/data/6380/redis.log" dbfilename dump.rdb dir /data/6380 slaveof 127.0.0.1 6379 從庫 6381 port 6381 daemonize yes pidfile /data/6381/redis.pid loglevel notice logfile "/data/6381/redis.log" dbfilename dump.rdb dir
/data/6381 slaveof 127.0.0.1 6379 [root@mcw01 ~/msRedis]$ ls [root@mcw01 ~/msRedis]$ tree /data/ /data/ ├── 6379 ├── 6380 └── 6381 3 directories, 0 files [root@mcw01 ~/msRedis]$ vim 6379.conf [root@mcw01 ~/msRedis]$ vim 6380.conf [root@mcw01 ~/msRedis]$ vim 6381.conf [root@mcw01 ~/msRedis]$ cat 6379
.conf port 6379 daemonize yes pidfile /data/6379/redis.pid loglevel notice logfile "/data/6379/redis.log" dbfilename dump.rdb dir /data/6379 [root@mcw01 ~/msRedis]$ cat 6380.conf port 6380 daemonize yes pidfile /data/6380/redis.pid loglevel notice logfile "/data/6380/redis.log" dbfilename dump.rdb dir /data/6380 [root@mcw01 ~/msRedis]$ redis-server 6379.conf [root@mcw01 ~/msRedis]$ redis-server 6380.conf #建立三個配置檔案,並啟動redis服務 [root@mcw01 ~/msRedis]$ redis-server 6381.conf [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis root 25270 1 0 11:43 ? 00:00:00 redis-server *:6379 root 25275 1 0 11:43 ? 00:00:00 redis-server *:6380 root 25280 1 0 11:43 ? 00:00:00 redis-server *:6381 [root@mcw01 ~/msRedis]$ redis-cli 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name1 mcw1 OK 127.0.0.1:6379> keys * 1) "name1" 127.0.0.1:6379> [root@mcw01 ~/msRedis]$ redis-cli -p 6380 #6379上建立的key,在6380和6381上都看不到,三者沒有關係 127.0.0.1:6380> get name1 (nil) 127.0.0.1:6380> keys * (empty list or set) 127.0.0.1:6380> [root@mcw01 ~/msRedis]$ redis-cli -p 6381 127.0.0.1:6381> keys * (empty list or set) 127.0.0.1:6381> [root@mcw01 ~/msRedis]$ redis-cli -p 6379 get name1 #連線進入的後面加命令,像MySQL一樣的免互動執行命令 "mcw1" [root@mcw01 ~/msRedis]$ tree /data/ #檢視目錄結構 /data/ ├── 6379 │ ├── redis.log │ └── redis.pid ├── 6380 │ ├── redis.log │ └── redis.pid └── 6381 ├── redis.log └── redis.pid 3 directories, 6 files [root@mcw01 ~/msRedis]$

2.開啟主從複製功能

redis-cli info   #檢視資料庫資訊,查出所有
redis-cli info replication  #快速檢視複製資訊。只查info裡的Replication部分

在6380 和6381資料庫上 ,配置主從資訊,通過引數形式修改配置,臨時生效,注意要寫入配置檔案 

    redis-cli -p 6380 slaveof 127.0.0.1 6379
    redis-cli -p 6381 slaveof 127.0.0.1 6379


此時檢查6379的複製資訊,以及6380 6381的複製資訊

redis-cli -p 6380 info replication  
redis-cli -p 6381 info replication  


主從複製是 讀寫分離的,master可寫,  slave只讀



[root@mcw01 ~/msRedis]$ redis-cli -p 6379 info
# Server
redis_version:4.0.10
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:279e3e51d6e7969b
redis_mode:standalone
os:Linux 3.10.0-693.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:25270
run_id:29d570833f783120fd9e884ea8d4108384abaeae
tcp_port:6379
uptime_in_seconds:466
uptime_in_days:0
hz:10
lru_clock:2371478
executable:/root/msRedis/redis-server
config_file:/root/msRedis/6379.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:849456
used_memory_human:829.55K
used_memory_rss:7688192
used_memory_rss_human:7.33M
used_memory_peak:849456
used_memory_peak_human:829.55K
used_memory_peak_perc:100.12%
used_memory_overhead:836206
used_memory_startup:786504
used_memory_dataset:13250
used_memory_dataset_perc:21.05%
total_system_memory:1911832576
total_system_memory_human:1.78G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:9.05
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:1
rdb_bgsave_in_progress:0
rdb_last_save_time:1646538180
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0

# Stats
total_connections_received:3
total_commands_processed:5
instantaneous_ops_per_sec:0
total_net_input_bytes:131
total_net_output_bytes:10197
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:1
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:0
master_replid:e3784eb64085052e7107d06d1fc605ae6dbb5b59
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.32
used_cpu_user:0.11
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ redis-cli -p 6379 info replication  #現在6379是master,但是連線的從為0
# Replication
role:master
connected_slaves:0
master_replid:e3784eb64085052e7107d06d1fc605ae6dbb5b59
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@mcw01 ~/msRedis]$ redis-cli -p 6379 info keyspace  #info加上後面這個keyspace,就是info裡被註釋的英文
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
[root@mcw01 ~/msRedis]$ redis-cli -p 6379 info cluster
# Cluster
cluster_enabled:0
[root@mcw01 ~/msRedis]$ 



開啟主從複製功能

[root@mcw01 ~/msRedis]$  redis-cli -p 6380 slaveof 127.0.0.1 6379  #開啟6380為6379的從
OK
[root@mcw01 ~/msRedis]$ redis-cli -p 6381 slaveof 127.0.0.1 6379  #開啟6381為6379的從
OK
[root@mcw01 ~/msRedis]$ redis-cli -p 6379 info replication  #檢視主6379的資訊,角色是主,連線的從有兩個,從0和從1的ip埠等資訊
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=28,lag=0
slave1:ip=127.0.0.1,port=6380,state=online,offset=28,lag=0
master_replid:99b8b1b5d61e13152f4025821626574ed7f92ac9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication  #檢視從6380的複製資訊。角色是從,主的ip埠是什麼,主的連線狀態,是否只讀等資訊
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:56
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:99b8b1b5d61e13152f4025821626574ed7f92ac9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
[root@mcw01 ~/msRedis]$ 



再來檢視剛剛主上建立的資料,現在已經複製到從庫上去了
[root@mcw01 ~/msRedis]$ redis-cli -p 6379
127.0.0.1:6379> keys *
1) "name1"
127.0.0.1:6379> 
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 keys *
(error) ERR wrong number of arguments for 'keys' command
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 
127.0.0.1:6380> keys *
1) "name1"
127.0.0.1:6380> 
[root@mcw01 ~/msRedis]$ redis-cli -p 6381
127.0.0.1:6381> keys *
1) "name1"
127.0.0.1:6381> 
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ redis-cli -p 6379   #在主上建立一個新的資料,也很快同步到從上去了,說明主從複製正常執行
127.0.0.1:6379> set name2 mcw2
OK
127.0.0.1:6379> 
[root@mcw01 ~/msRedis]$ redis-cli -p 6381
127.0.0.1:6381> keys *
1) "name1"
2) "name2"
127.0.0.1:6381> 
127.0.0.1:6381> set name3 mcw3  #然後在從上寫入一個數據,發現從是隻讀的。這也是實現了讀寫分離的
(error) READONLY You can't write against a read only slave.
127.0.0.1:6381> 
[root@mcw01 ~/msRedis]$ 


如果命令列設定了主從複製了,但是沒有寫進配置檔案,需要記得把執行過的命令加進去,這樣即使重啟redis,也會自動載入上。
slaveof  127.0.0.1  6379

3.模擬主從複製故障,手動切換master-slave身份

    1.殺死6379程序 ,幹掉主庫 
    
    2.手動切換 6381為新的主庫,需要先關閉它的從庫身份
    redis-cli -p 6381  slaveof no one 
    
    3.修改6380的新主庫是 6381
    redis-cli -p 6380 slaveof  127.0.0.1 6381
    

1.殺死6379程序 ,幹掉主庫 
[root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
root      25270      1  0 11:43 ?        00:00:02 redis-server *:6379
root      25275      1  0 11:43 ?        00:00:02 redis-server *:6380
root      25280      1  0 11:43 ?        00:00:02 redis-server *:6381
[root@mcw01 ~/msRedis]$ kill 25270
[root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
root      25275      1  0 11:43 ?        00:00:02 redis-server *:6380
root      25280      1  0 11:43 ?        00:00:02 redis-server *:6381
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication  #雖然主庫已經殺掉了,但是現在還是連線的6379
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
..........
[root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
........
[root@mcw01 ~/msRedis]$ 



2.手動切換 6381為新的主庫,需要先關閉它的從庫身份
redis-cli -p 6381  slaveof no one 

[root@mcw01 ~/msRedis]$ redis-cli -p 6381  slaveof no one  #
OK
[root@mcw01 ~/msRedis]$ redis-cli -p 6381  
127.0.0.1:6381> keys *
1) "name1"
2) "name2"
127.0.0.1:6381> set name3 mcw3 #取消它從庫身份,就可以寫入資料了
OK
127.0.0.1:6381> 
[root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication  #檢視自己的複製資訊,是主了,現在還沒有從
# Replication
role:master
connected_slaves:0
........
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication  #檢視6380,現在的從不是新的主6381的從,需要手動修改
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
......
[root@mcw01 ~/msRedis]$ 




3.修改6380的新主庫是 6381
redis-cli -p 6380 slaveof  127.0.0.1 6381


[root@mcw01 ~/msRedis]$ redis-cli -p 6380 slaveof  127.0.0.1 6381
OK
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication  #上面是修改新的主,下面是檢視已經修改成功,連線狀態up
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
.........
[root@mcw01 ~/msRedis]$ redis-cli -p 6380  #從上面可以看到剛剛主上建立的name3了
127.0.0.1:6380> keys *
1) "name2"
2) "name3"
3) "name1"
127.0.0.1:6380> 
[root@mcw01 ~/msRedis]$ redis-cli -p 6381 #主上建立一條資料name4
127.0.0.1:6381> set name4 mcw4
OK
127.0.0.1:6381> 
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 #從上能查到新建的資料集name4.並且從不能寫入資料,是隻讀的。
127.0.0.1:6380> keys *
1) "name2"
2) "name3"
3) "name1"
4) "name4"
127.0.0.1:6380> set name5 mcw5
(error) READONLY You can't write against a read only slave.
127.0.0.1:6380> 
[root@mcw01 ~/msRedis]$ 

哨兵搭建

redis哨兵高可用

redis-sentinel功能

環境準備:redis主從複製叢集部署

三個redis資料庫例項 ,配置好 1主 2從的配置

    6379.conf
        port 6379
        daemonize yes
        logfile "6379.log"
        dbfilename "dump-6379.rdb"
        dir "/var/redis/data/"

    6380.conf 
        port 6380
        daemonize yes
        logfile "6380.log"
        dbfilename "dump-6380.rdb"
        dir "/var/redis/data/"
        slaveof 127.0.0.1 6379

    6381.conf 
        port 6381
        daemonize yes
        logfile "6381.log"
        dbfilename "dump-6381.rdb"
        dir "/var/redis/data/"
        slaveof 127.0.0.1 6379


        
[root@mcw01 ~/msRedis]$ ps -ef|grep redis
root      25275      1  0 11:43 ?        00:00:08 redis-server *:6380
root      25280      1  0 11:43 ?        00:00:07 redis-server *:6381
root      45901  25154  0 13:51 pts/0    00:00:00 grep --color=auto redis
[root@mcw01 ~/msRedis]$ kill 25275
[root@mcw01 ~/msRedis]$ kill 25280
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ ps -ef|grep redis
root      45903  25154  0 13:51 pts/0    00:00:00 grep --color=auto redis
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ ls
6379.conf  6380.conf  6381.conf
[root@mcw01 ~/msRedis]$ vim 6380.conf 
[root@mcw01 ~/msRedis]$ vim 6381.conf 
[root@mcw01 ~/msRedis]$ cat 6379.conf 
port 6379
daemonize yes
pidfile /data/6379/redis.pid
loglevel notice
logfile "/data/6379/redis.log"
dbfilename dump.rdb
dir /data/6379
[root@mcw01 ~/msRedis]$ cat 6380.conf  #我這裡直接寫進配置檔案,這樣用配置檔案啟動redis,就不需要執行相關命令了
port 6380
daemonize yes
pidfile /data/6380/redis.pid
loglevel notice
logfile "/data/6380/redis.log"
dbfilename dump.rdb
dir /data/6380
slaveof 127.0.0.1 6379
[root@mcw01 ~/msRedis]$ redis-server 6379.conf 
[root@mcw01 ~/msRedis]$ redis-server 6380.conf 
[root@mcw01 ~/msRedis]$ redis-server 6381.conf 
[root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis  #啟動三個服務
root      45949      1  0 13:53 ?        00:00:00 redis-server *:6379
root      45954      1  0 13:53 ?        00:00:00 redis-server *:6380
root      45960      1  0 13:53 ?        00:00:00 redis-server *:6381
[root@mcw01 ~/msRedis]$ redis-cli -p 6379 info replication  #檢視主從複製情況,一主兩從正常
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=84,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=84,lag=0
........
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication 
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
.......
[root@mcw01 ~/msRedis]$ 

環境準備:redis三個redis哨兵程序部署

    三個redis哨兵程序,指定好,檢測著誰
    也是準備三個配置檔案,內容如下

    sentinel-26379.conf  
    port 26379  
    dir /var/redis/data/
    logfile "26379.log"

    // 當前Sentinel節點監控 192.168.182.130:6379 這個主節點
    // 2代表判斷主節點失敗至少需要2個Sentinel節點節點同意
    // mymaster是主節點的別名
    sentinel monitor s21ms  0.0.0.0 6379 2

    //每個Sentinel節點都要定期PING命令來判斷Redis資料節點和其餘Sentinel節點是否可達,如果超過30000毫秒30s且沒有回覆,則判定不可達
    sentinel down-after-milliseconds s21ms  20000

    //當Sentinel節點集合對主節點故障判定達成一致時,Sentinel領導者節點會做故障轉移操作,選出新的主節點,
    原來的從節點會向新的主節點發起復制操作,限制每次向新的主節點發起復制操作的從節點個數為1
    sentinel parallel-syncs mymaster 1

    //故障轉移超時時間為180000毫秒
    sentinel failover-timeout mymaster 180000


    #三個哨兵的配置檔案,一模一樣,僅僅是埠的區別  
    #三個哨兵的配置檔案,一模一樣,僅僅是埠的區別  
    #三個哨兵的配置檔案,一模一樣,僅僅是埠的區別  
    #三個哨兵的配置檔案,一模一樣,僅僅是埠的區別  
    sentinel-26380.conf  


    sentinel-26381.conf  



2.分別啟動 三個redis資料庫,  以及三個 哨兵程序 ,注意 ,哨兵第一次啟動後,會修改配置檔案,如果錯了,得刪除配置檔案,重新寫
2.分別啟動 三個redis資料庫,  以及三個 哨兵程序 ,注意 ,哨兵第一次啟動後,會修改配置檔案,如果錯了,得刪除配置檔案,重新寫
2.分別啟動 三個redis資料庫,  以及三個 哨兵程序 ,注意 ,哨兵第一次啟動後,會修改配置檔案,如果錯了,得刪除配置檔案,重新寫
2.分別啟動 三個redis資料庫,  以及三個 哨兵程序 ,注意 ,哨兵第一次啟動後,會修改配置檔案,如果錯了,得刪除配置檔案,重新寫


    配置檔案在這裡,這裡用的是127.0.0.1 
    sentinel-26379.conf 
    port 26379  
    dir /var/redis/data/
    logfile "26379.log"
    sentinel monitor s21ms  127.0.0.1  6379 2
    sentinel down-after-milliseconds s21ms  20000
    sentinel parallel-syncs s21ms 1
    sentinel failover-timeout s21ms 180000
    #加一個後臺執行
    daemonize yes 
        
        #僅僅是埠的不同
    sentinel-26380.conf 




    sentinel-26381.conf 


    #啟動
     1244  redis-sentinel sentinel-26379.conf 
     1245  redis-sentinel sentinel-26380.conf 
     1246  redis-sentinel sentinel-26381.conf 


sentinel-26379.conf 
port 26379  
dir /data/sentinel
logfile "26379.log"
sentinel monitor mcw  127.0.0.1  6379 2
sentinel down-after-milliseconds mcw   20000
sentinel parallel-syncs mcw  1
sentinel failover-timeout mcw  180000
daemonize yes      
     
     
[root@mcw01 ~/msRedis]$ ss -lntup|grep 6379
tcp    LISTEN     0      511       *:6379                  *:*                   users:(("redis-server",pi
tcp    LISTEN     0      511      :::6379                 :::*                   users:(("redis-server",pi
[root@mcw01 ~/msRedis]$ ss -anp|grep 6379
tcp    LISTEN     0      511       *:6379                  *:*                   users:(("redis-server",pi
tcp    ESTAB      0      0      127.0.0.1:6379               127.0.0.1:39093               users:(("redis-
tcp    ESTAB      0      0      127.0.0.1:6379               127.0.0.1:39091               users:(("redis-
tcp    ESTAB      0      0      127.0.0.1:39093              127.0.0.1:6379                users:(("redis-
tcp    ESTAB      0      0      127.0.0.1:39091              127.0.0.1:6379                users:(("redis-
tcp    LISTEN     0      511      :::6379                 :::*                   users:(("redis-server",pi
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ ls /data/63
6379/ 6380/ 6381/ 
[root@mcw01 ~/msRedis]$ mkdir /data/sentinel
[root@mcw01 ~/msRedis]$ vim 26379.conf
[root@mcw01 ~/msRedis]$ sed "s#26379#26380#g" 26379.conf >26380.conf 
[root@mcw01 ~/msRedis]$ sed "s#26379#26381#g" 26379.conf >26381.conf 
[root@mcw01 ~/msRedis]$ cat 26379.conf 
port 26379  
dir /data/sentinel
logfile "26379.log"
sentinel monitor mcw  127.0.0.1  6379 2
sentinel down-after-milliseconds mcw   20000
sentinel parallel-syncs mcw  1
sentinel failover-timeout mcw  180000
daemonize yes      
[root@mcw01 ~/msRedis]$ cat 26380.conf 
port 26380  
dir /data/sentinel
logfile "26380.log"
sentinel monitor mcw  127.0.0.1  6379 2
sentinel down-after-milliseconds mcw   20000
sentinel parallel-syncs mcw  1
sentinel failover-timeout mcw  180000
daemonize yes      
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ ls 
26379.conf  26380.conf  26381.conf  6379.conf  6380.conf  6381.conf
[root@mcw01 ~/msRedis]$ mv 26379.conf  sentinel-26379.conf  #起個方便看的名字把
[root@mcw01 ~/msRedis]$ mv 26380.conf  sentinel-26380.conf
[root@mcw01 ~/msRedis]$ mv 26381.conf  sentinel-26381.conf
[root@mcw01 ~/msRedis]$ ls 
6379.conf  6380.conf  6381.conf  sentinel-26379.conf  sentinel-26380.conf  sentinel-26381.conf
[root@mcw01 ~/msRedis]$ 

報錯:
[root@mcw01 ~/msRedis]$ redis-server sentinel-26379.conf

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 4
>>> 'sentinel monitor mcw  127.0.0.1  6379 2'
sentinel directive while not in sentinel mode
[root@mcw01 ~/msRedis]$   

部署哨兵模式的時候,使用的命令不對造成的,正確的命令如下:
./redis-server.sh sentinel.conf --sentinel

為了避免把後面的--sentinel竟然給忘了類似的事情發生,建議使用redis-sentinel sentinel.conf代替redis-server命令

[root@mcw01 ~/msRedis]$ redis-sentinel sentinel-26379.conf
[root@mcw01 ~/msRedis]$ redis-sentinel sentinel-26380.conf
[root@mcw01 ~/msRedis]$ redis-sentinel sentinel-26381.conf  
[root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis  #啟動了三個哨兵
root      45949      1  0 13:53 ?        00:00:01 redis-server *:6379
root      45954      1  0 13:53 ?        00:00:02 redis-server *:6380
root      45960      1  0 13:53 ?        00:00:02 redis-server *:6381
root      46061      1  1 14:27 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root      46066      1  0 14:28 ?        00:00:00 redis-sentinel *:26380 [sentinel]
root      46071      1  0 14:28 ?        00:00:00 redis-sentinel *:26381 [sentinel]
[root@mcw01 ~/msRedis]$ ls
6379.conf  6380.conf  6381.conf  sentinel-26379.conf  sentinel-26380.conf  sentinel-26381.conf
[root@mcw01 ~/msRedis]$ cat 6379.conf 
port 6379
daemonize yes
pidfile /data/6379/redis.pid
loglevel notice
logfile "/data/6379/redis.log"
dbfilename dump.rdb
dir /data/6379
[root@mcw01 ~/msRedis]$ cat sentinel-26379.conf  #檢視配置,啟動後配置被重寫了,添加了部分配置
port 26379
dir "/data/sentinel"
logfile "26379.log"
sentinel myid 750ea6253069388e5897651086d50af8fc604a7f
sentinel monitor mcw 127.0.0.1 6379 2
sentinel down-after-milliseconds mcw 20000
sentinel config-epoch mcw 0
daemonize yes
# Generated by CONFIG REWRITE
sentinel leader-epoch mcw 0
sentinel known-slave mcw 127.0.0.1 6380
sentinel known-slave mcw 127.0.0.1 6381
sentinel known-sentinel mcw 127.0.0.1 26380 3098a36a80a4d1f1b3f01f9a332c8abdf21ee56a
sentinel known-sentinel mcw 127.0.0.1 26381 50aa98a452708c83334286a9a8e2f06cef1f9fd2
sentinel current-epoch 0
[root@mcw01 ~/msRedis]$ 

3.驗證哨兵是否正常

redis-cli -p 26379 info sentinel

master0:name=s21ms,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
哨兵的名字,狀態,監控的地址,監控的從的數量,以及哨兵的個數。檢視到哨兵的資訊,就說明哨兵的部署是正常的


[root@mcw01 ~/msRedis]$ redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mcw,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
[root@mcw01 ~/msRedis]$ 

4.幹掉主庫 ,檢查主從切換狀態

 1253  kill -9 12749
 1254  ps -ef|grep redis
 1255  redis-cli -p 6380 info replication
 1256  redis-cli -p 6381 info replication
 1257  redis-cli -p 6380 info replication
 1258  redis-cli -p 6381 info replication

 [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
root      45949      1  0 13:53 ?        00:00:02 redis-server *:6379
root      45954      1  0 13:53 ?        00:00:03 redis-server *:6380
root      45960      1  0 13:53 ?        00:00:02 redis-server *:6381
root      46061      1  0 14:27 ?        00:00:02 redis-sentinel *:26379 [sentinel]
root      46066      1  0 14:28 ?        00:00:01 redis-sentinel *:26380 [sentinel]
root      46071      1  0 14:28 ?        00:00:01 redis-sentinel *:26381 [sentinel]
[root@mcw01 ~/msRedis]$ kill 45949  #殺掉主服務
[root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
root      45954      1  0 13:53 ?        00:00:03 redis-server *:6380
root      45960      1  0 13:53 ?        00:00:03 redis-server *:6381
root      46061      1  0 14:27 ?        00:00:02 redis-sentinel *:26379 [sentinel]
root      46066      1  0 14:28 ?        00:00:02 redis-sentinel *:26380 [sentinel]
root      46071      1  0 14:28 ?        00:00:02 redis-sentinel *:26381 [sentinel]
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication  #迅速檢視80和81,發現主從複製連結down了,但是還是指向79的
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
.......
[root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
........
[root@mcw01 ~/msRedis]$ 
[root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication  #過一會再看,大約20秒。發現81已經自動成為主了,80已經成為81的從了,實現了自動故障轉移
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
.......
[root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=166686,lag=0
........
[root@mcw01 ~/msRedis]$ 

redis-cluster搭建

環境準備

1.準備好6個redis節點的配置檔案 

redis-7000.conf 
port 7000
daemonize yes
dir "/opt/redis/data"
logfile "7000.log"
dbfilename "dump-7000.rdb

cluster-enabled yes   #開啟叢集模式
cluster-config-file nodes-7000.conf  #叢集內部的配置檔案
cluster-require-full-coverage no      
    #redis cluster需要16384個slot都正常的時候才能對外提供服務,換句話說,只要任何一個slot異常那麼整個cluster不對外提供服務。 因此生產環境一般為no



#6個配置檔案,僅僅是埠的區別
redis-7000.conf
    port 7000
    daemonize yes
    dir "/opt/redis/data"
    logfile "7000.log"
    dbfilename "dump-7000.rdb"
    cluster-enabled yes
    cluster-config-file nodes-7000.conf
    cluster-require-full-coverage no
    
redis-7001.conf 
redis-7002.conf 
redis-7003.conf 
redis-7004.conf 
redis-7005.conf 


[root@mcw01 ~/msRedis]$ cd cluster/
[root@mcw01 ~/msRedis/cluster]$ ls
7000.conf  7001.conf  7002.conf  7003.conf  7004.conf
[root@mcw01 ~/msRedis/cluster]$ cat 7000.conf 
port 7000
daemonize yes
dir "/opt/redis/data"
logfile "7000.log"
dbfilename "dump-7000.rdb
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-require-full-coverage no
[root@mcw01 ~/msRedis/cluster]$ sed "s#7000#7005#g" 7000.conf >7005.conf
[root@mcw01 ~/msRedis/cluster]$ ls
7000.conf  7001.conf  7002.conf  7003.conf  7004.conf  7005.conf
[root@mcw01 ~/msRedis/cluster]$ cat 7005.conf 
port 7005
daemonize yes
dir "/opt/redis/data"
logfile "7005.log"
dbfilename "dump-7005.rdb
cluster-enabled yes
cluster-config-file nodes-7005.conf
cluster-require-full-coverage no
[root@mcw01 ~/msRedis/cluster]$ 

2.根據配置檔案啟動6個節點

 1288  redis-server 7000.conf 
 1290  redis-server 7001.conf 
 1291  redis-server 7002.conf 
 1292  redis-server 7003.conf 
 1293  redis-server 7004.conf 
 1294  redis-server 7005.conf 

 
 如下,6個叢集的結點  7000-7005已經起來了,並且顯示是cluster
[root@mcw01 ~/msRedis/cluster]$ ls
7000.conf  7001.conf  7002.conf  7003.conf  7004.conf  7005.conf
[root@mcw01 ~/msRedis/cluster]$ mkdir /opt/redis/data
mkdir: cannot create directory ‘/opt/redis/data’: No such file or directory
[root@mcw01 ~/msRedis/cluster]$ mkdir -p  /opt/redis/data
[root@mcw01 ~/msRedis/cluster]$ ps -ef|grep -v grep|grep redis
root      45954      1  0 13:53 ?        00:00:06 redis-server *:6380
root      45960      1  0 13:53 ?        00:00:06 redis-server *:6381
root      46061      1  0 14:27 ?        00:00:07 redis-sentinel *:26379 [sentinel]
root      46066      1  0 14:28 ?        00:00:07 redis-sentinel *:26380 [sentinel]
root      46071      1  0 14:28 ?        00:00:07 redis-sentinel *:26381 [sentinel]
[root@mcw01 ~/msRedis/cluster]$ 
[root@mcw01 ~/msRedis/cluster]$ redis-se
redis-sentinel  redis-server    
[root@mcw01 ~/msRedis/cluster]$ redis-server 7000.conf
[root@mcw01 ~/msRedis/cluster]$ redis-server 7001.conf
[root@mcw01 ~/msRedis/cluster]$ redis-server 7002.conf
[root@mcw01 ~/msRedis/cluster]$ redis-server 7003.conf
[root@mcw01 ~/msRedis/cluster]$ redis-server 7004.conf
[root@mcw01 ~/msRedis/cluster]$ redis-server 7005.conf
[root@mcw01 ~/msRedis/cluster]$ ps -ef|grep -v grep|grep redis
root      45954      1  0 13:53 ?        00:00:06 redis-server *:6380
root      45960      1  0 13:53 ?        00:00:06 redis-server *:6381
root      46061      1  0 14:27 ?        00:00:08 redis-sentinel *:26379 [sentinel]
root      46066      1  0 14:28 ?        00:00:07 redis-sentinel *:26380 [sentinel]
root      46071      1  0 14:28 ?        00:00:07 redis-sentinel *:26381 [sentinel]
root      46293      1  0 15:17 ?        00:00:00 redis-server *:7000 [cluster]
root      46298      1  0 15:17 ?        00:00:00 redis-server *:7001 [cluster]
root      46303      1  0 15:18 ?        00:00:00 redis-server *:7002 [cluster]
root      46308      1  0 15:18 ?        00:00:00 redis-server *:7003 [cluster]
root      46313      1  0 15:18 ?        00:00:00 redis-server *:7004 [cluster]
root      46318      1  1 15:18 ?        00:00:00 redis-server *:7005 [cluster]
[root@mcw01 ~/msRedis/cluster]$ 

3.分配redis slot 槽位

    -    手動寫c語言 分配  
    -   使用ruby大神 寫的一個redis模組,自動分配
現在登入上去,新增一個數據,顯示槽位沒有被提供。意思是,7000這個節點沒有分配槽位,而資料是放到槽位裡的。    
[root@mcw01 ~/msRedis/cluster]$ redis-cli -p 7000
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7000> set name1 mcw1
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:7000> 
[root@mcw01 ~/msRedis/cluster]$ 
    

4.配置ruby指令碼環境

    1.yum安裝最簡單 
        yum install  ruby  
    
    2.自動配置好 PATH環境變數
        ruby和gem的環境變數
    
    3.下載ruby操作redis的模組
        wget http://rubygems.org/downloads/redis-3.3.0.gem   
        
    4.用ruby的包管理工具 gem 安裝這個模組
        
        gem install -l redis-3.3.0.gem
        
        ruby和python一樣的解釋型語言,gem相當於pip
   

[root@mcw01 ~/msRedis/cluster]$ wget http://rubygems.org/downloads/redis-3.3.0.gem  
--2022-03-06 16:10:28--  http://rubygems.org/downloads/redis-3.3.0.gem
Resolving rubygems.org (rubygems.org)... 151.101.193.227, 151.101.1.227, 151.101.65.227, ...
Connecting to rubygems.org (rubygems.org)|151.101.193.227|:80... failed: Connection timed out.
Connecting to rubygems.org (rubygems.org)|151.101.1.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 92160 (90K) [application/octet-stream]
Saving to: ‘redis-3.3.0.gem’

100%[===============================================================>] 92,160      --.-K/s   in 0.08s   

2022-03-06 16:10:31 (1.12 MB/s) - ‘redis-3.3.0.gem’ saved [92160/92160]

[root@mcw01 ~/msRedis/cluster]$ ls
7000.conf  7001.conf  7002.conf  7003.conf  7004.conf  7005.conf  redis-3.3.0.gem
[root@mcw01 ~/msRedis/cluster]$ gem install -l redis-3.3.0.gem
Successfully installed redis-3.3.0
Parsing documentation for redis-3.3.0
Installing ri documentation for redis-3.3.0
1 gem installed

5.通過ruby一鍵分配redis-cluster叢集的槽位

 找到機器上的redis-trib.rb命令,用絕對命令建立 
開啟叢集,分配槽位
/opt/redis-4.0.10/src/redis-trib.rb  create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005


如下已經分配好槽位了
[root@mcw01 ~/msRedis/cluster]$ /opt/redis-4.0.10/src/redis-trib.rb  create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters: #三個主,三個複製的從
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 2d2654b26e43f6245b41dc2cae44f7bb6ec846bb 127.0.0.1:7000  #三個主上分別分配了槽位區間
   slots:0-5460 (5461 slots) master
M: 102d887feee9b5d2db0284ecff83d893f4736aef 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: c2ba927b42526110a7f840643a1c653057c6b811 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: 22bcee4dc0439600aae268b9de093db39d73e2cf 127.0.0.1:7003
   replicates c2ba927b42526110a7f840643a1c653057c6b811
S: fb7ed8949b7ab36351aabe72a9cb1ae8f859d6d8 127.0.0.1:7004
   replicates 2d2654b26e43f6245b41dc2cae44f7bb6ec846bb
S: cc0da6e5df726d65df53849dd92e2edd6ef66760 127.0.0.1:7005
   replicates 102d887feee9b5d2db0284ecff83d893f4736aef
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 127.0.0.1:7000)
M: 2d2654b26e43f6245b41dc2cae44f7bb6ec846bb 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 102d887feee9b5d2db0284ecff83d893f4736aef 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 22bcee4dc0439600aae268b9de093db39d73e2cf 127.0.0.1:7003
   slots: (0 slots) slave
   replicates c2ba927b42526110a7f840643a1c653057c6b811
S: fb7ed8949b7ab36351aabe72a9cb1ae8f859d6d8 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 2d2654b26e43f6245b41dc2cae44f7bb6ec846bb
M: c2ba927b42526110a7f840643a1c653057c6b811 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: cc0da6e5df726d65df53849dd92e2edd6ef66760 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 102d887feee9b5d2db0284ecff83d893f4736aef
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.  #分配槽位成功
[root@mcw01 ~/msRedis/cluster]$ 

6.分配好集群后,可以向叢集內寫入資料了

    redis-cli -c  指定叢集模式,登入資料庫
    
    登入redis寫入資料,發現槽位分配,且重定向之後,叢集搭建成功 

    
叢集模式登入redis。
 [root@mcw01 ~/msRedis/cluster]$ redis-cli -c -p 7000
127.0.0.1:7000> keys * 
(empty list or set)
127.0.0.1:7000> set name1 mcw1  #7000上設定資料,分配的槽位是12933,它在10923-16383之間,也就是7002服務上
-> Redirected to slot [12933] located at 127.0.0.1:7002
OK
127.0.0.1:7002> keys *  #新增之後,自動切換到7002服務了
1) "name1"
127.0.0.1:7002> get name1
"mcw1"
127.0.0.1:7002> 
[root@mcw01 ~/msRedis/cluster]$ redis-cli -c -p 7000  #退出重新登入7000服務
127.0.0.1:7000> keys *  #key不在7000上
(empty list or set)
127.0.0.1:7000> get name1  #7000上能get到其它節點上的資料,資料儲存在其它節點上的槽位,
-> Redirected to slot [12933] located at 127.0.0.1:7002
"mcw1"
127.0.0.1:7002> keys *  #get到之後,命令列切換到這個資料所在節點上
1) "name1"
127.0.0.1:7002> 
[root@mcw01 ~/msRedis/cluster]$