redis主从架构与redis+sentinel哨兵机制架
redis的搭建过程,请参考 https://blog.51cto.com/12445535/2385106
十余年的大洼网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整大洼建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“大洼网站设计”,“大洼网站推广”以来,每个客户项目都认真落实执行。
接下来,我们再找一台服务器,进行安装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,相关内容,请期待。
文章标题:redis主从架构与redis+sentinel哨兵机制架
链接分享:http://scjbc.cn/article/gjdoip.html