Redis Sentinel 启动脚本
公司项目中使用到了 Redis Sentinel。以前只用过 Reids。所以,也只了解 Redis 的关闭和启动。至于 Redis Sentinel,那就抓瞎了。每次使用,都是先用现查,特别麻烦。这次在查资料时,在 Github Gist 上查到一个非常不错的启动脚本,可以非常方便地解决这个问题。分享给大家,希望对大家有所帮助。另外, 推荐大家认真阅读一下 Redis Sentinel 相关的文档。 废话完毕,上脚本:
01 | #!/bin/bash |
02 | # |
03 | # Starts redis sentinel using start-stop-daemon |
04 | # |
05 | # D瓜哥,http://www.diguage.com/ |
06 | # |
07 | # 2016-02-17 |
08 |
09 | NAME=redis-sentinel |
10 | BIN=/usr/ local /bin/redis-server # --1-- |
11 | CONF=/usr/ local /etc/sentinel.conf |
12 | SENTINEL_PID=/tmp/redis-sentinel.pid |
13 | CMD=$1 |
14 |
15 | start() { |
16 | echo "Starting $NAME ..." |
17 | # exec 2>&1 $BIN $CONF --sentinel | logger -t sentinel & # --2-- |
18 | exec 2>&1 $BIN $CONF --sentinel &>/dev/null & |
19 | echo $! > "${SENTINEL_PID}" ; |
20 | } |
21 |
22 | stop() { |
23 | PID=` cat $SENTINEL_PID` |
24 | echo "Stopping $NAME ($PID) ..." |
25 | kill $PID |
26 | } |
27 |
28 | restart() { |
29 | echo "Restarting $NAME ..." |
30 | stop |
31 | start |
32 | } |
33 |
34 | case "$CMD" in |
35 | start) |
36 | start |
37 | ;; |
38 | stop) |
39 | stop |
40 | ;; |
41 | restart) |
42 | restart |
43 | ;; |
44 | *) |
45 | echo "Usage $0 {start|stop|restart}" |
46 | esac |
- 使用时,请根据实际情况来修改相关命令以及参数的位置。
- 参考的源代码中使用的使用的是这种方式。但是,这种方式在 Mac 下,记录的
PID
是logger
命令的PID
,导致的问题时,执行关闭命令时,不能正常的杀掉 Redis Sentinel 进程。这个问题,还需要再看看有没有更好的解决方案。
这里有几点东西需要说明一下,
-
启动时,加入了参数
&>/dev/null
,导致的结果是所有的日志都直接抛掉了。不方便追查日志。 -
在 Mac 使用 Homebrew 安装完 Reids 后,D瓜哥没有找到关于 Redis Sentinel 的相关配置。这
里给大家提供一个模板。如下:
001 | # port <sentinel-port> |
002 | # The port that this sentinel instance will run on |
003 | port 26379 |
004 |
005 | # sentinel announce-ip <ip> |
006 | # sentinel announce-port <port> |
007 | # |
008 | # The above two configuration directives are useful in environments where, |
009 | # because of NAT, Sentinel is reachable from outside via a non-local address. |
010 | # |
011 | # When announce-ip is provided, the Sentinel will claim the specified IP address |
012 | # in HELLO messages used to gossip its presence, instead of auto-detecting the |
013 | # local address as it usually does. |
014 | # |
015 | # Similarly when announce-port is provided and is valid and non-zero, Sentinel |
016 | # will announce the specified TCP port. |
017 | # |
018 | # The two options don't need to be used together, if only announce-ip is |
019 | # provided, the Sentinel will announce the specified IP and the server port |
020 | # as specified by the "port" option. If only announce-port is provided, the |
021 | # Sentinel will announce the auto-detected local IP and the specified port. |
022 | # |
023 | # Example: |
024 | # |
025 | # sentinel announce-ip 1.2.3.4 |
026 |
027 | # dir <working-directory> |
028 | # Every long running process should have a well-defined working directory. |
029 | # For Redis Sentinel to chdir to /tmp at startup is the simplest thing |
030 | # for the process to don't interfere with administrative tasks such as |
031 | # unmounting filesystems. |
032 | dir /tmp |
033 |
034 | # sentinel monitor <master-name> <ip> <redis-port> <quorum> |
035 | # |
036 | # Tells Sentinel to monitor this master, and to consider it in O_DOWN |
037 | # (Objectively Down) state only if at least <quorum> sentinels agree. |
038 | # |
039 | # Note that whatever is the ODOWN quorum, a Sentinel will require to |
040 | # be elected by the majority of the known Sentinels in order to |
041 | # start a failover, so no failover can be performed in minority. |
042 | # |
043 | # Slaves are auto-discovered, so you don't need to specify slaves in |
044 | # any way. Sentinel itself will rewrite this configuration file adding |
045 | # the slaves using additional configuration options. |
046 | # Also note that the configuration file is rewritten when a |
047 | # slave is promoted to master. |
048 | # |
049 | # Note: master name should not include special characters or spaces. |
050 | # The valid charset is A-z 0-9 and the three characters ".-_". |
051 | sentinel monitor mymaster 127.0.0.1 6379 2 |
052 |
053 | # sentinel auth-pass <master-name> <password> |
054 | # |
055 | # Set the password to use to authenticate with the master and slaves. |
056 | # Useful if there is a password set in the Redis instances to monitor. |
057 | # |
058 | # Note that the master password is also used for slaves, so it is not |
059 | # possible to set a different password in masters and slaves instances |
060 | # if you want to be able to monitor these instances with Sentinel. |
061 | # |
062 | # However you can have Redis instances without the authentication enabled |
063 | # mixed with Redis instances requiring the authentication (as long as the |
064 | # password set is the same for all the instances requiring the password) as |
065 | # the AUTH command will have no effect in Redis instances with authentication |
066 | # switched off. |
067 | # |
068 | # Example: |
069 | # |
070 | # sentinel auth-pass mymaster MySUPER--secret-0123passw0rd |
071 |
072 | # sentinel down-after-milliseconds <master-name> <milliseconds> |
073 | # |
074 | # Number of milliseconds the master (or any attached slave or sentinel) should |
075 | # be unreachable (as in, not acceptable reply to PING, continuously, for the |
076 | # specified period) in order to consider it in S_DOWN state (Subjectively |
077 | # Down). |
078 | # |
079 | # Default is 30 seconds. |
080 | sentinel down-after-milliseconds mymaster 30000 |
081 |
082 | # sentinel parallel-syncs <master-name> <numslaves> |
083 | # |
084 | # How many slaves we can reconfigure to point to the new slave simultaneously |
085 | # during the failover. Use a low number if you use the slaves to serve query |
086 | # to avoid that all the slaves will be unreachable at about the same |
087 | # time while performing the synchronization with the master. |
088 | sentinel parallel-syncs mymaster 1 |
089 |
090 | # sentinel failover-timeout <master-name> <milliseconds> |
091 | # |
092 | # Specifies the failover timeout in milliseconds. It is used in many ways: |
093 | # |
094 | # - The time needed to re-start a failover after a previous failover was |
095 | # already tried against the same master by a given Sentinel, is two |
096 | # times the failover timeout. |
097 | # |
098 | # - The time needed for a slave replicating to a wrong master according |
099 | # to a Sentinel current configuration, to be forced to replicate |
100 | # with the right master, is exactly the failover timeout (counting since |
101 | # the moment a Sentinel detected the misconfiguration). |
102 | # |
103 | # - The time needed to cancel a failover that is already in progress but |
104 | # did not produced any configuration change (SLAVEOF NO ONE yet not |
105 | # acknowledged by the promoted slave). |
106 | # |
107 | # - The maximum time a failover in progress waits for all the slaves to be |
108 | # reconfigured as slaves of the new master. However even after this time |
109 | # the slaves will be reconfigured by the Sentinels anyway, but not with |
110 | # the exact parallel-syncs progression as specified. |
111 | # |
112 | # Default is 3 minutes. |
113 | sentinel failover-timeout mymaster 180000 |
114 |
115 | # SCRIPTS EXECUTION |
116 | # |
117 | # sentinel notification-script and sentinel reconfig-script are used in order |
118 | # to configure scripts that are called to notify the system administrator |
119 | # or to reconfigure clients after a failover. The scripts are executed |
120 | # with the following rules for error handling: |
121 | # |
122 | # If script exits with "1" the execution is retried later (up to a maximum |
123 | # number of times currently set to 10). |
124 | # |
125 | # If script exits with "2" (or an higher value) the script execution is |
126 | # not retried. |
127 | # |
128 | # If script terminates because it receives a signal the behavior is the same |
129 | # as exit code 1. |
130 | # |
131 | # A script has a maximum running time of 60 seconds. After this limit is |
132 | # reached the script is terminated with a SIGKILL and the execution retried. |
133 |
134 | # NOTIFICATION SCRIPT |
135 | # |
136 | # sentinel notification-script <master-name> <script-path> |
137 | # |
138 | # Call the specified notification script for any sentinel event that is |
139 | # generated in the WARNING level (for instance -sdown, -odown, and so forth). |
140 | # This script should notify the system administrator via email, SMS, or any |
141 | # other messaging system, that there is something wrong with the monitored |
142 | # Redis systems. |
143 | # |
144 | # The script is called with just two arguments: the first is the event type |
145 | # and the second the event description. |
146 | # |
147 | # The script must exist and be executable in order for sentinel to start if |
148 | # this option is provided. |
149 | # |
150 | # Example: |
151 | # |
152 | # sentinel notification-script mymaster /var/redis/notify.sh |
153 |
154 | # CLIENTS RECONFIGURATION SCRIPT |
155 | # |
156 | # sentinel client-reconfig-script <master-name> <script-path> |
157 | # |
158 | # When the master changed because of a failover a script can be called in |
159 | # order to perform application-specific tasks to notify the clients that the |
160 | # configuration has changed and the master is at a different address. |
161 | # |
162 | # The following arguments are passed to the script: |
163 | # |
164 | # <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port> |
165 | # |
166 | # <state> is currently always "failover" |
167 | # <role> is either "leader" or "observer" |
168 | # |
169 | # The arguments from-ip, from-port, to-ip, to-port are used to communicate |
170 | # the old address of the master and the new address of the elected slave |
171 | # (now a master). |
172 | # |
173 | # This script should be resistant to multiple invocations. |
174 | # |
175 | # Example: |
176 | # |
177 | # sentinel client-reconfig-script mymaster /var/redis/reconfig.sh |
参考资料
作 者: D瓜哥,https://www.diguage.com/
原文链接:https://wordpress.diguage.com/archives/139.html
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。
原文链接:https://wordpress.diguage.com/archives/139.html
版权声明:非特殊声明均为本站原创作品,转载时请注明作者和原文链接。