温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

redis主从架构与redis+sentinel 哨兵机制架

发布时间:2020-07-28 15:00:51 来源:网络 阅读:295 作者:马吉辉 栏目:数据库

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

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

和上面的方法搭建一个redis
只不过
在从redis中的配置文件中写
[root@prd3-redis02-10-184 conf]# grep slaveof 6379.conf
######## slaveof <masterip> <masterport>
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 <master-name> <password>
# sentinel down-after-milliseconds <master-name> <milliseconds>
# Default is 30 seconds.
sentinel myid ba794535d3a65e14b266e28462fa7c68de609f39
# sentinel parallel-syncs <master-name> <numslaves>
sentinel deny-scripts-reconfig yes
# sentinel failover-timeout <master-name> <milliseconds>
# 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,相关内容,请期待。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI