1.服务规划
主机名 | ip | redis port | 角色 | sentinel port |
---|---|---|---|---|
redis-01 | 172.17.0.78 | 6379 | master | 26379 |
Redis-02 | 172.17.0.77 | 6379 | slave | 26379 |
Redis-03 | 172.17.0.76 | 6379 | slave | 26379 |
本文主要介绍sentinel 和主从配置 redis 一主两从集群搭建
2.redis配置主从
只需要在两个从节点执行一下命令
127.0.0.1:6379> slaveof 172.17.0.78 6379
OK
127.0.0.1:6379>
两个均执行完成后可以查看主从复制状态
info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.77,port=6379,state=online,offset=701751,lag=1
slave1:ip=172.17.0.76,port=6379,state=online,offset=701765,lag=0
master_failover_state:no-failover
master_replid:cc2aa24cae22cd9148aa2baaa1439f1c62dac128
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:701765
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:701765
3.sentinel配置
默认redis的源码目录是有sentinel的配置的 我们只需要将配置文件复制到相应目录启动即可
cp /opt/redis-6.2.6/sentinel.conf /usr/local/redis/conf/sentinel_26379.conf
配置文件
port 26379
daemonize yes
#需要改为yes 否则不是后台运行
pidfile "/var/run/redis-sentinel_26379.pid"
logfile "/usr/local/redis/log/sentinel_26379.log"
dir "/tmp"
sentinel monitor mymaster 172.17.0.78 6379 2
sentinel auth-pass mymaster XbNaw1d2kFUw
sentinel config-epoch mymaster 3
sentinel leader-epoch mymaster 4
acllog-max-len 128
sentinel deny-scripts-reconfig yes
sentinel resolve-hostnames no
sentinel announce-hostnames no
# Generated by CONFIG REWRITE
protected-mode no
user default on nopass ~* &* +@all
sentinel myid 173915edb665a620743e0bcd83265e78e74f5838
sentinel current-epoch 3
sentinel known-replica mymaster 172.17.0.77 6379
sentinel known-replica mymaster 172.17.0.76 6379
sentinel known-sentinel mymaster 172.17.0.76 26379 46ee2470e473a0ade94efdac51da190e8019fc9b
sentinel known-sentinel mymaster 172.17.0.77 26379 868edf41daea6b9518660398f8905d2bf1d6d7bc
可以连接sentinel查看状态
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=youpin898master,status=ok,address=172.17.0.78:6379,slaves=2,sentinels=3
至此集群已经搭建完成