redis主从架构与redis+sentinel 哨兵机制架
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.log8 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.1bind 0.0.0.0protected-mode noport 26379daemonize yesloglevel noticelogfile "/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 REWRITEsentinel down-after-milliseconds mymaster 6000sentinel auth-pass mymaster xxxsentinel config-epoch mymaster 0sentinel leader-epoch mymaster 1sentinel known-slave mymaster 10.80.85.21 6379sentinel known-sentinel mymaster 10.80.85.21 26379 c3002e02c6a2d0041afd2569f3fd1985bc38993esentinel current-epoch 1
[root@prd3-redis01-10-183 conf]# cat sentinel.conf //这里面都是我们自己指定的,他其他的文件内容,都是他自己生成的# Example sentinel.conf# bind 127.0.0.1 192.168.1.1bind 0.0.0.0protected-mode noport 26379daemonize yesloglevel noticelogfile "/ivargo/log/sentinel.log"pidfile "/ivargo/app/sentinel/26379.pid"dir "/tmp"sentinel monitor mymaster 192.168.10.183 6379 2sentinel auth-pass mymaster XXX184上的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.1bind 0.0.0.0protected-mode noport 26379daemonize yesloglevel noticelogfile "/ivargo/log/sentinel.log"pidfile "/ivargo/app/sentinel/26379.pid"dir "/tmp"sentinel myid 6075d58bdc7c2602f98d90c0aa48470c4dbb50d9sentinel deny-scripts-reconfig yes# Generated by CONFIG REWRITEsentinel monitor mymaster 192.168.10.183 6379 2sentinel auth-pass mymaster XXXsentinel config-epoch mymaster 0sentinel leader-epoch mymaster 0sentinel known-slave mymaster 192.168.10.184 6379sentinel known-sentinel mymaster 192.168.10.184 26379 039bd11a572245b6c16c6e204523d781ffb6ba4csentinel current-epoch 0查看不同点我们检查[root@prd3-redis01-10-183 conf]# redis-cli -h 192.168.10.183 -p 26379 info # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0: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 # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0: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# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0: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 # Replicationrole:slavemaster_host:192.168.10.184master_port:6379[root@prd3-redis02-10-184 conf]# redis-cli -h 192.168.10.184 -p 6379 -a XXX info # Replicationrole:masterconnected_slaves:1slave0:ip=192.168.10.183,port=6379,state=online,offset=169470,lag=1以上实现了redis的sentinel机制 也就是 HA机制
其实对于现上环境的话,当redis发生了主从切换的时候,这个比如程序调用的ip就会改变,写两个ip和两个sentinel地址显然是不合理的,我们可以实现vip机制自动切换,来实现程序调用只需要一个ip,相关内容,请期待。