The number of visits to the website has slowly come up. For the performance of the website, we started using Redis as a caching strategy. At first, Redis is a single point, and when a machine rock machine, the service of Redis stops completely, which will affect the normal operation of other services. Crap not much to say, under the use of Redis Sentinel to do a master-slave switch cluster management. Do this cluster management time, looked a lot of information to fully understand, he is how to do.
For the Java client, see:
Http://blog.mkfree.com/posts/52b146e6479e5a64742fddd0
Reference: Http://redis.io/topics/sentinel I also read this article.
Environment configuration:
Because I do not have too many machines this time, I used vagrant to open more than one virtual machine. And then put the environment in place.
For Redis installation Please refer to: Redis Simple Official Script installation method (Linux)
Cluster configuration requires a minimum of three machines, then I have three virtual machines, three virtual machines installed the same Redis environment
IP respectively:
192.168.9.17 (Redis Sentinel cluster monitoring)
192.168.9.18 (Redis Master)
192.168.9.19 (redis slave)
Redis configuration:
Master Redis profile, use the default profile on it, if you need to design additional parameters
From the Redis configuration file, add
#从的redis配置文件, you need to add vim/etc/redis/6379.confslaveof 192.168.9.18 6379
Start Master-Slave Redis
#启动主redis (192.168.9.18)/etc/init.d/redis_6379.conf start# initiated from Redis (192.168.9.19)/etc/init.d/redis_6379.conf start
View Master Redis Information
#查看主redis的信息redis-cli-h 192.168.9.18 Info replication# replicationrole:master #代表192.168.9.18:6379 This redis is the main connected_slaves:1slave0:192.168.9.18,6379,online.
View from Redis information
#查看主redis的信息redis-cli-h 192.168.9.19 Info replication# replicationrole:slave #代表192.168.9.18:6379 This redis is the main master_ host:192.168.9.18master_port:6379master_link_status:upmaster_last_io_seconds_ago:4master_sync_in_progress:0 slave_priority:100slave_read_only:1connected_slaves:0
Configuring the Redis Sentinel Cluster monitoring service 1. Add a redis Sentinel configuration file
vim /etc/redis/sentinel.conf# #redis -0# #sentinel实例之间的通讯端口port 26379# master1sentinel monitor master1 192.168.9.18 6379 1sentinel Down-after-milliseconds master1 5000sentinel failover-timeout master1 900000sentinel can-failover master1 yessentinel parallel-syncs master1 2#master2 Multiple sets of master-slave Redis snooping can be added ....
redis-sentinel sentinel. conf --sentinel
All right, all the environment is set up. Let's start with the formal demo 1. Normal presentation.
Observing Redis Sentinel Log information
650) this.width=650; "style=" border:0px none;vertical-align:middle;width:1022px; "Src=" http://upload.mkfree.com/ Blog/posts/000/000/001/2013-10-14-20-28-50-317.jpg "/>
It is clear here that the Redis has joined the cluster
[4925] Oct 03:42:21.889 * +slave slave 192.168.9.19:6379 192.168.9.19 6379 @ master1 192.168.9.18 6379
Perform the following command to view the Redis master-slave information
[Email protected] vagrant]# redis-cli-h 192.168.9.17-p 26379 info sentinel# sentinelsentinel_masters:1sentinel_tilt:0 sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=master1,status=ok,address= 192.168.9.18:6379,slaves=1,sentinels=1
Then it means everything is fine. Your Redis Sentinel cluster has been configured successfully!
2. Failure Demo
2.1 When the master Redis Server rock machine, what will happen?
Execute the following command to stop using the master Redis service
Redis-cli-h 192.168.9.18-p 6379 shutdown #表示把192.168.9.18 this redis off
650) this.width=650; "style=" border:0px none;vertical-align:middle;width:1022px; "Src=" http://upload.mkfree.com/ Blog/posts/000/000/001/2013-10-14-20-38-16-83.jpg "/>
This picture clearly reflects that Redis Sentinel monitors the Master's Redis service to stop and then automatically switches from Redis to master.
Then execute the following command to view the Redis master-slave information
[[email protected] vagrant]# redis-cli -h 192.168.33.111 -p 26379 info sentinel# sentinelsentinel_masters:1sentinel_tilt:0 sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=master1,status=ok,address= 192.168.9.19:6379,slaves=1,sentinels=1
2.2 When we have discovered that a REDIS has failed and may receive some fault information, what happens when the service is turned off by the Redis recovery service state?
650) this.width=650; "style=" border:0px none;vertical-align:middle;width:1022px; "Src=" http://upload.mkfree.com/ Blog/posts/000/000/001/2013-10-14-20-45-50-965.jpg "/>
The Redis Sentinel Cluster service will re-join the last master Redis service, but instead of the master Redis, he becomes the Reids.
This article from "My Sky" blog, declined reprint!
Cluster Management for Redis master-slave switching