Redis High Availability: Redis Sentinel master-slave replication failover

Source: Internet
Author: User
Tags auth bind failover redis port number

Redis Sentinel provides a high level of availability for Redis to monitor, notify, and fail over replication clusters.


Just right there are two servers for Centos 6.4, which has Redis 3.2 and Redis master-slave replication installed.

Server name: Centos222, ip:192.168.1.222, master-slave role: Master

Server name: Centos224, ip:192.168.1.224, master-slave role: Slave


On the basis of replication, Redis Sentinel is now configured with the following basic structure.



# 222 Configuring Redis Boot Parameters

[root@centos222 ~]# vi/etc/redis/6379.conf

bind 127.0.0.1 192.168.1.222	#redis服务器地址
Port 26379			# Sentinel Port number
daemonize Yes		#后台启动
protected-mode no	#关闭保护模式, prohibit remote direct access
Requirepass 654321	# Local access requires a password
masterauth 654321	#备端同步访问需要密码
slave-read-only Yes	#备端只读

# 224 Configuring Redis Boot Parameters

[root@centos224 ~]# vi/etc/redis/6379.conf

bind 127.0.0.1 192.168.1.224	#redis服务器地址
Port 26379			# Sentinel Port number
daemonize Yes		#后台启动
slaveof 192.168.1.222 6379		#设置为192.168.1.222 6379 from the library
Protected-mode no	#关闭保护模式, disable remote direct access to
requirepass 654321	#本地访问需要密码
masterauth 654321	# Backup sync access requires password
slave-read-only Yes	#备端只读

# Configure Redis Sentinel to configure monitoring for the primary instance only. Sentinel can obtain information from the instance through the master instance.

# Basic parameter description:

Port 26379	#Sentinel Service port
Sentinel monitor Myredis 192.168.1.xxx 6379 1	#监控 People group name/ip/Port/ The number of failure judgements (which are different from the end of the judgment is considered master disconnected)
Sentinel down-after-milliseconds Myredis 10000	#ping more than 10000 milliseconds is considered an outage
Sentinel failover-timeout Myredis 900000		#主从切换超过 30000 ms failed
Sentinel Can-failover myredis Yes				# Whether to allow failover after master disconnect
Sentinel Parallel-syncs Myredis 1				#从端继续同步新master的数量


# 222 Redis Sentinel Configuration

[root@centos222 ~]# vi/etc/redis/sentinel.conf

#ip 192.168.1.222
port 26379
bind 0.0.0.0
daemonize Yes
logfile "/var/log/sentinel_log.log"
#myredis222
Sentinel Monitor myredis222 192.168.1.222 6379 1
Sentinel Down-after-milliseconds myredis222 10000
Sentinel failover-timeout myredis222 900000
Sentinel Parallel-syncs myredis222 1
Sentinel auth-pass myredis222 654321
#myredis224
Sentinel Monitor myredis224 192.168.1.224 6379 1
Sentinel down-after-milliseconds myredis224 10000
Sentinel Failover-timeout myredis224 900000
Sentinel Parallel-syncs myredis224 1
Sentinel Auth-pass myredis224 654321

# 224 Redis Sentinel Configuration

[root@centos224 ~]#  vi/etc/redis/sentinel.conf

#ip 192.168.1.224
port 26379
bind 0.0.0.0
Daemonize Yes
logfile "/var/log/sentinel_log.log"
#myredis222
Sentinel Monitor myredis222 192.168.1.222 6379 1
Sentinel down-after-milliseconds myredis222 10000
Sentinel failover-timeout myredis222 900000
Sentinel Parallel-syncs myredis222 1
Sentinel auth-pass myredis222 654321
#myredis224
Sentinel Monitor myredis224 192.168.1.224 6379 1
Sentinel down-after-milliseconds myredis224 10000
Sentinel Failover-timeout myredis224 900000
Sentinel Parallel-syncs myredis224 1
Sentinel Auth-pass myredis224 654321

# Server 222 and 224 Firewall add inbound rules

Iptables-i input-p TCP--dport 26379-j ACCEPT

# Two methods of launching Sentinel service:

redis-sentinel/etc/redis/sentinel.conf
redis-server/etc/redis/sentinel.conf--sentinel

# 222 Launch Sentinel Service

[root@centos222 ~]# redis-sentinel/etc/redis/sentinel.conf
[root@centos222 ~]# Cat/var/log/sentinel_log.log
24630:x 18:00:50.496 # Sentinel ID is b0c01ccf191b07bc7f8f42c395b1f45ec3849258 24630:x-
Nov 18:00:50.49 6 # +monitor Master myredis224 192.168.1.224 6379 quorum 1
24630:x Nov 18:00:50.496 # +monitor Master myredis222 19 2.168.1.222 6379 Quorum 1
24630:x Nov 18:00:50.497 * +slave slave 192.168.1.224:6379 192.168.1.224 6379 @ myredis22 2 192.168.1.222 6379

# 224 Launch Sentinel Service

[root@centos224 ~]# redis-sentinel/etc/redis/sentinel.conf
[root@centos224 ~]# Cat/var/log/sentinel_log.log
29098:x 07:00:33.795 # Sentinel ID is 2303678ad7c6df83ce9c938ed9341fc33170f843 29098:x-
Nov 07:00:33.79 5 # +monitor Master myredis222 192.168.1.222 6379 quorum 1
29098:x Nov 07:00:33.795 # +monitor Master myredis224 19 2.168.1.224 6379 Quorum 1
29098:x Nov 07:00:33.797 * +slave slave 192.168.1.224:6379 192.168.1.224 6379 @ myredis22 2 192.168.1.222 6379
29098:x Nov 07:00:34.560 * +sentinel Sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis222 192.168.1.222 6379
29098:x Nov 07:00:34.706 * +sentinel Sentinel B0C01CCF191B07BC7 f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis224 192.168.1.224 6379

# A message similar to the above indicates success.

# Connect Sentinel, View information (take 224 client connections as an example)

[root@centos224 ~]# redis-cli-p 26379
127.0.0.1:26379> Info Sentinel
# Sentinel
sentinel_masters:2
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_ failure_flags:0
master0:name=myredis222,status=ok,address=192.168.1.222:6379,slaves=1,sentinels=2
master1:name=myredis224,status=odown,address=192.168.1.224:6379,slaves=0,sentinels=2

# You can see Sentinel two with 2 master, where 192.168.1.22 has 1 slave. At this point, connect to 222 and 224 respectively to view the replication information.


# in 222 server connection Redis:redis role for Master

[root@centos222 ~]# redis-cli
127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.224,port=6379,state=online,offset= 2088480,lag=1
master_repl_offset:2088623
repl_backlog_active:1
repl_backlog_size:1048576
repl _backlog_first_byte_offset:1040048
repl_backlog_histlen:1048576

# in 224 server connection Redis:redis role for slave

[root@centos224 ~]# redis-cli
127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:192.168.1.222
master_port:6379
master_link_status:up
Master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:2087322
slave_priority :
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# no hurry, first introduce Redis Sentinel related commands

127.0.0.1:26379> Sentinel Masters			#列出所有master状态信息
127.0.0.1:26379> Sentinel Master myredis222	# List A master status information (myredis222 as an example)
127.0.0.1:26379> SENTINEL Slaves myredis222 # List all slave and status information for a master (myredis222 for example)
127.0.0.1:26379> SENTINEL Sentinels myredis222	# List A master's sentinels (myredis222 for example)
127.0.0.1:26379> SENTINEL get-master-addr-by-name myredis222 # Get its IP and port via a master (myredis222 as an example)
127.0.0.1:26379> SENTINEL ckquorum myredis222	# Check for a master to reach the polling and failover conditions (myredis222 as an example)
127.0.0.1:26379> Sentinel flushconfig			#强制重写 Sentinel configuration to a disk file
127.0.0.1:26379> SENTINEL failover myredis222	#这个操作就注意了. Forces a failover. Operate with caution ...

# Now test time-out failover, the server is considered to be disconnected because the profile setting Down-after-milliseconds to 10 seconds cannot be connected.
# Set the connection here for 15 seconds to see if the master-slave replication role can fail over.

[root@centos222 ~]# redis-cli-p 6379-a 654321 DEBUG sleep
OK

After the execution finishes, review the replication status.

# 222 characters changed from master to slave

127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:192.168.1.224
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:2422339
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1357484
repl_backlog_histlen:1048576

# 224 characters changed from slave to master

[root@centos224 ~]# redis-cli
127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.222,port=6379,state=online,offset= 2396492,lag=1
master_repl_offset:2396637
repl_backlog_active:1
repl_backlog_size:1048576
repl _backlog_first_byte_offset:2384854
repl_backlog_histlen:11784

Auto-Switch ... Get and set synchronization are normal, and 224 becomes a read-only instance. Toggle success ....


# now try forcing failover. The master is transferred to myredis222.

127.0.0.1:26379> SENTINEL Failover myredis222

The master-slave switchover is equally successful. The system is normal. The test ends here. The principle of the online search bar.



Reference: Redis Sentinel Documentation

Redis high-availability deployment and monitoring

Redis Sentinel Master-Slave Switching (failover) solution, detailed configuration




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.