Redis——搭建主从模式 搭建哨兵模式

首先在linux安装好redis,版本如下:

[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# redis-cli -v
redis-cli 4.0.8
[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# redis-server -v
Redis server v=4.0.8 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=6878437d5ce73519

编辑三个redis.conf,master 1, slave 2, 目录如下:

[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# tree /root/data/soft/redis/ins-sentinel/
/root/data/soft/redis/ins-sentinel/
├── 6300
│   ├── dump.rdb
│   ├── logfile.log
│   ├── pidfile.pid
│   └── redis.conf
├── 6301
│   ├── dump.rdb
│   ├── logfile.log
│   ├── pidfile.pid
│   └── redis.conf
└── 6302
    ├── dump.rdb
    ├── logfile.log
    ├── pidfile.pid
    └── redis.conf

3 directories, 12 files
[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# 

端口号分别为 6300  6301  6302

master修改:

bind 0.0.0.0
protected-mode yes
port 6300
pidfile /root/data/soft/redis/ins-sentinel/6300/pidfile.pid
logfile "/root/data/soft/redis/ins-sentinel/6300/logfile.log"
dir /root/data/soft/redis/ins-sentinel/6300/

slave修改,同master,但多一句话:

slaveof 172.31.140.161 6300

分别启动三个实例:

redis-server /root/data/soft/redis/ins-sentinel/6300/redis.conf 
redis-server /root/data/soft/redis/ins-sentinel/6301/redis.conf 
redis-server /root/data/soft/redis/ins-sentinel/6302/redis.conf 

自此一主两从搭建并启动完成,查看主从状态如下:

[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# redis-cli -h 172.31.140.161 -p 6300 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.31.140.161,port=6301,state=online,offset=280,lag=1
slave1:ip=172.31.140.161,port=6302,state=online,offset=280,lag=1
master_replid:02859e4cd236cbc4c3276b5e66016a871ca0010c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:280
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:280
[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# 


[root@izm5e6u39ae8sj3eivv3rvz data]# redis-cli -h 172.31.140.161 -p 6301 info replication
# Replication
role:slave
master_host:172.31.140.161
master_port:6300
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:378
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:02859e4cd236cbc4c3276b5e66016a871ca0010c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:378
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:378
[root@izm5e6u39ae8sj3eivv3rvz data]# 

验证主从同步

在主插入,在从查

[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# redis-cli -h 172.31.140.161 -p 6300 set "name" "zhangsan"
OK
[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# redis-cli -h 172.31.140.161 -p 6300 keys "*"
1) "name"

[root@izm5e6u39ae8sj3eivv3rvz data]# redis-cli -h 172.31.140.161 -p 6301 get "name"
"zhangsan"

[root@izm5e6u39ae8sj3eivv3rvz data]# redis-cli -h 172.31.140.161 -p 6302 get "name"
"zhangsan"

表示插主从查没问题。

 

下面加入哨兵,以实现高可用,主从切换。

编辑哨兵配置文件   sentinel.conf

protected-mode no
daemonize yes
port 16300
dir "/root/data/soft/redis/ins-sentinel/sentinel"
logfile "./sentinel.log"
sentinel monitor mymaster 172.31.140.161 6300 1

启动哨兵

redis-sentinel /root/data/soft/redis/ins-sentinel/sentinel/sentinel_16300.conf

查看哨兵状态

redis-cli -p 16300 info sentinel

[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# redis-cli -p 16300 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=mymaster,status=ok,address=172.31.140.161:6300,slaves=2,sentinels=1
[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# 

停接redis主节点后,再看哨兵状态,显示master节点已切换

[root@izm5eb28tnyvb00rm7as8wz ins-sentinel]# redis-cli -p 16300 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=mymaster,status=ok,address=172.31.140.161:6302,slaves=2,sentinels=1

 

posted @ 2021-05-08 12:49  会飞的斧头  阅读(578)  评论(0编辑  收藏  举报