Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Cơ Sở Dữ Liệu

Redis Sentinel: High-Availability đơn giản cho Redis Deployment

Redis Sentinel cung cấp simple và automatic high availability (HA) solution cho Redis. Nếu familiar với MongoDB elections, không too far off. Khởi đầu: given master replicating to N slaves. Từ đó, Sentinel daemons running, trên application servers hoặc servers Redis running on. Chúng keep track master's health.

Nếu Sentinel detect master non-responsive, broadcast SDOWN (Subjectively down) message to other sentinels. Sau đó, once quorum reached master down, broadcast ODOWN (Objectively down), và new master elected. Vì need quorum (majority) sentinels agree reach ODOWN state, always best practice có odd number Sentinels running để avoid ties.

Note: highly recommended dùng Redis version 2.8 branch hoặc higher cho best performance với Sentinel.

How it works

Sentinels handle failover bằng re-writing config files của Redis instances running. Go through scenario:

Master "A" replicating to slaves "B" và "C". Ba Sentinels (s1, s2, s3) running on app servers, write to Redis. Lúc này "A" current master goes offline. Sentinels all see "A" offline, send SDOWN messages to each other. Chúng all agree "A" down, so "A" set ODOWN status. Từ đây, election happens see who most ahead, case này "B" chosen as new master.

Config file cho "B" set so that no longer slave of anyone. Meanwhile, config file cho "C" rewritten so no longer slave of "A" mà là "B." Từ đây, everything continues normal. Should "A" come back online, Sentinels recognize this, rewrite config file for "A" to be slave of "B," since "B" current master.

Configuration

Configuring Sentinels không hard như nghĩ. Một trong difficult things: choosing where place Sentinel processes. I personally recommend running them on app servers if possible. Presumably nếu setting this up, concerned about write availability to master. As such, Sentinels provide insight whether application server can talk to master. Welcome run Sentinels on Redis instance servers too.

Start configuration step, reference example file here. Example sentinel.conf found with Redis 2.8.4 on Ubuntu 14.04, work với any 2.8.x version. I've added two lines top that I like use in practice:

daemonize yes
logfile /var/log/redis/redis-sentinel.log

Puts sentinel process daemonize mode, logs all messages to log file instead of stdout.

Nhiều configurable options, most commented well. However, focus on just two.

Most important: telling Sentinels where current master resides. Referenced in line:

sentinel monitor mymaster 127.0.0.1 6379 2

Tells Sentinel monitor "mymaster" (arbitrary name, feel free name as see fit) given IP on given port, và how many Sentinels required meet quorum for failover (minimum 2). Parts likely want change: IP address master và port nếu not standard 6379.

Next, may want change line:

sentinel down-after-milliseconds mymaster 30000

Amount of time sentinel wait before declare master SDOWN. Default 30 seconds, I typically lower to 10 seconds. Don't reduce too low; otherwise may have issues failovers happening too often.

Feel free look at other options. One may interest many users: notification script, nếu like keep track failovers when they happen.

Once sentinel.conf configured as see fit, start daemon:

redis-server /path/to/sentinel.conf --sentinel

Testing Failover

Once all sentinels online, possible do dry run failover make sure all configured correctly.

First thing: connect to Sentinel via redis-cli:

redis-cli -p 26379

Nếu want receive info about sentinel, run command:

127.0.0.1:26379> INFO

Gives info: who current master, how many slaves, how many sentinels monitoring it.

Test failover, execute:

127.0.0.1:26379> SENTINEL failover mymaster

Force ODOWN on current master và cause failover happen. Shortly after, run "INFO" again, should now see new master listed.

Conclusion

Hopefully helpful demystify Redis và Sentinel. Questions, feel free post below!