redis主从架构与redis+sentinel 哨兵机制架
admin
2023-01-27 23:19:38
0

redis的搭建过程,请参考 https://blog.51cto.com/12445535/2385106

接下来,我们再找一台服务器,进行安装redis 实现redis的主从架构

和上面的方法搭建一个redis
只不过
在从redis中的配置文件中写
[root@prd3-redis02-10-184 conf]# grep slaveof 6379.conf
######## slaveof
slaveof 192.168.10.183 6379 //直接添加这个一行,然后

启动redis
[root@prd3-redis02-10-184 conf]# redis-server /ivargo/app/redis/conf/6379.conf

接下来就是看差距

在主redis可以看到
[root@prd3-redis01-10-183 conf]# redis-cli -a XXX
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379> info

Replication

role:master
connected_slaves:1

在从redis上可以看到
[root@prd3-redis02-10-184 conf]# redis-cli -aXXX
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6379> info
####### Replication
role:slave
master_host:192.168.10.183
master_port:6379

这样redis的主从就搭建好了

搭建完redis后,目录结构

[root@prd3-redis01-10-183 ivargo]# pwd
/ivargo
[root@prd3-redis01-10-183 ivargo]# tree 
.
├── app
│   ├── redis
│   │   ├── 6379.pid
│   │   ├── conf
│   │   │   └── 6379.conf
│   │   ├── data
│   │   │   ├── 6379.rdb
│   │   │   └── appendonly_6379.aof
│   │   └── log
│   └── sentinel
│       └── conf
└── log
    └── 6379.log

8 directories, 5 files
实现redis的sentinel 
[root@prd3-redis01-10-183 conf]# pwd
/ivargo/app/sentinel/conf
[root@prd3-redis01-10-183 conf]# cat sentinel.conf  //这个配置文件是prd3上的sentinel的配置文件,我们需要修改的
# Example sentinel.conf
# bind 127.0.0.1 192.168.1.1
bind 0.0.0.0
protected-mode no
port 26379
daemonize yes
loglevel notice
logfile "/ivargo/log/sentinel.log"
dir "/tmp"
# sentinel auth-pass  
# sentinel down-after-milliseconds  
# Default is 30 seconds.
sentinel myid ba794535d3a65e14b266e28462fa7c68de609f39
# sentinel parallel-syncs  
sentinel deny-scripts-reconfig yes
# sentinel failover-timeout  
# Default is 3 minutes = 180000.
sentinel monitor mymaster 10.80.85.20 6379 2
# Generated by CONFIG REWRITE
sentinel down-after-milliseconds mymaster 6000
sentinel auth-pass mymaster xxx
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 1
sentinel known-slave mymaster 10.80.85.21 6379
sentinel known-sentinel mymaster 10.80.85.21 26379 c3002e02c6a2d0041afd2569f3fd1985bc38993e
sentinel current-epoch 1
[root@prd3-redis01-10-183 conf]# cat sentinel.conf  //这里面都是我们自己指定的,他其他的文件内容,都是他自己生成的
# Example sentinel.conf
# bind 127.0.0.1 192.168.1.1
bind 0.0.0.0
protected-mode no
port 26379
daemonize yes
loglevel notice
logfile "/ivargo/log/sentinel.log"
pidfile "/ivargo/app/sentinel/26379.pid"
dir "/tmp"
sentinel monitor mymaster 192.168.10.183 6379 2
sentinel auth-pass mymaster XXX

184上的sentinel配置文件和183一样的

[root@prd3-redis01-10-183 conf]# redis-sentinel /ivargo/app/sentinel/conf/sentinel.conf
[root@prd3-redis02-10-184 conf]# redis-sentinel /ivargo/app/sentinel/conf/sentinel.conf
[root@prd3-redis01-10-183 conf]# cat sentinel.conf                                        
# Example sentinel.conf
# bind 127.0.0.1 192.168.1.1
bind 0.0.0.0
protected-mode no
port 26379
daemonize yes
loglevel notice
logfile "/ivargo/log/sentinel.log"
pidfile "/ivargo/app/sentinel/26379.pid"
dir "/tmp"
sentinel myid 6075d58bdc7c2602f98d90c0aa48470c4dbb50d9
sentinel deny-scripts-reconfig yes
# Generated by CONFIG REWRITE
sentinel monitor mymaster 192.168.10.183 6379 2
sentinel auth-pass mymaster XXX
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 192.168.10.184 6379
sentinel known-sentinel mymaster 192.168.10.184 26379 039bd11a572245b6c16c6e204523d781ffb6ba4c
sentinel current-epoch 0

查看不同点

我们检查
[root@prd3-redis01-10-183 conf]# redis-cli -h 192.168.10.183 -p 26379 info 
# 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=192.168.10.183:6379,slaves=1,sentinels=2
验证sentinel高可用
[root@prd3-redis01-10-183 redis]# kill `cat 6379.pid`
杀死183的进程
然后在184上看
[root@prd3-redis02-10-184 conf]# redis-cli -h 192.168.10.183 -p 26379 info  
# 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=192.168.10.184:6379,slaves=1,sentinels=2  //主节点变成了184了

在启动183
[root@prd3-redis01-10-183 redis]# redis-server /ivargo/app/redis/conf/6379.conf 
[root@prd3-redis01-10-183 redis]# redis-cli -h 192.168.10.183 -p 26379 info
# 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=192.168.10.184:6379,slaves=1,sentinels=2

然后查看redis 原来的183是主节点,现在是从节点了
[root@prd3-redis01-10-183 redis]# redis-cli -h 192.168.10.183 -p 6379 -a XXX info 
# Replication
role:slave
master_host:192.168.10.184
master_port:6379

[root@prd3-redis02-10-184 conf]# redis-cli -h 192.168.10.184 -p 6379 -a XXX info  
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.10.183,port=6379,state=online,offset=169470,lag=1

以上实现了redis的sentinel机制 也就是 HA机制

其实对于现上环境的话,当redis发生了主从切换的时候,这个比如程序调用的ip就会改变,写两个ip和两个sentinel地址显然是不合理的,我们可以实现vip机制自动切换,来实现程序调用只需要一个ip,相关内容,请期待。

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...