Centos6.6部署Redis集羣 1
環境準備 1
環境安裝redis 1
安裝ruby 2
配置redis主從環境 3
部署redis sentinel服務器 5
集羣使用 13
當前集羣環境說明 13
測試功能點 14
測試點1: 14
測試點2: 14
測試點3: 14
測試點4: 15node
環境準備
Centos6.6虛擬機4臺,redis3.2.4版本源碼
環境安裝redis
須要root用戶
tar -zxvf redis-3.2.4.tar.gz
cd redis-3.2.4
make
make install
若中間沒有出錯,則此時redis基本功能已安裝完畢,在4臺虛擬機上重複此操做。
安裝ruby
從ruby官網獲取大於2.2版本以上的ruby源碼,下載到環境上。
安裝依賴包
yum install openssl-devel zlib-devel
解壓ruby源碼
cd ruby-2.3.7
./configure
make
make install
cd /usr/local/bin
./gem install redis #安裝redis遠程通信組件
若是出現失敗通常會說是缺乏zlib或者openssl
若是缺乏zlib,安裝zlib-devel以後
cd ruby-2.3.7
cd ext/zlib
ruby ./extconf.rb
若成功生成Makefile
make
make install
同理,若openssl報錯則
cd ext/openssl
ruby ./extconf.rb
make
make install
以後在回到/use/local/bin
執行./gem install redis
配置redis主從環境
首先咱們在192.168.1.233虛擬機裏建立2個節點,端口分別是7001,7002
cd /home/redis1
[root@localhost ~]# mkdir redis_cluster
[root@localhost ~]# cd redis_cluster/
[root@localhost redis_cluster]# mkdir 7001 7002
同理咱們在redis2 redis3虛擬機裏分別建立2個節點,端口分別是7003,7004,7005,7006
分別修改7001-7006下的配置文件
[root@localhost ~] vi redis_cluster/7001/redis.conf
bind 192.168.0.164 #默認ip爲127.0.0.1 須要改成其餘節點機器可訪問的ip 不然建立集羣時沒法訪,和單機集羣有區別redis
daemonize yes #redis後臺運行ruby
pidfile /var/run/redis_7001.pid #pidfile文件對應7001-7003服務器
cluster-enabled yes #開啓集羣app
cluster-config-file nodes_7001.conf #保存節點配置,自動建立,自動更新對應7001-7003ide
cluster-node-timeout 5000 #集羣超時時間,節點超過這個時間沒反應就判定是宕機
其餘幾臺節點把7001換成700x便可。
在三臺虛擬機上分別啓動全部服務節點測試
redis1
redis-server /home/redis1/redis_cluster/7001/redis.conf
redis-server /home/redis1/redis_cluster/7002/redis.conf
redis2
redis-server /home/redis2/redis_cluster/7003/redis.conf
redis-server /home/redis2/redis_cluster/7004/redis.conf
redis3
redis-server /home/redis3/redis_cluster/7005/redis.conf
redis-server /home/redis3/redis_cluster/7006/redis.conf
關閉全部機器防火牆
service iptables stop
將redis1做爲控制節點,從redis源碼目錄中找到
redis-trib.rb
在src目錄中
cp redis-trib.rb /usr/local/bin
[root@localhost ~]# redis-trib.rb create --replicas 1 192.168.1.233:7001 192.168.1.233:7002 192.168.1.254:7003 192.168.1.254:7004 192.168.1.11:7005 192.168.1.11:7006
如相似下圖的顯示則表示集羣啓動成功ui
以上若報錯,參考此教程https://blog.csdn.net/duguxingfeng/article/details/78918333
部署redis sentinel服務器
將redis4部署成監視節點用的sentinel服務器
cd /home/redis4
mkdir redis-sentinel
cd redis-sentinel
mkdir 26379 36379 46379
從源碼包中
cp sentinel.conf 到上述三個子目錄
修改conf文件,參考下面的,只須要修改端口號便可this
# Example sentinel.conf
# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#spa
protected-mode no
# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379
daemonize yes
# sentinel announce-ip <ip>
# sentinel announce-port <port>
#
# The above two configuration directives are useful in environments where,
# because of NAT, Sentinel is reachable from outside via a non-local address.
#
# When announce-ip is provided, the Sentinel will claim the specified IP address
# in HELLO messages used to gossip its presence, instead of auto-detecting the
# local address as it usually does.
#
# Similarly when announce-port is provided and is valid and non-zero, Sentinel
# will announce the specified TCP port.
#
# The two options don't need to be used together, if only announce-ip is
# provided, the Sentinel will announce the specified IP and the server port
# as specified by the "port" option. If only announce-port is provided, the
# Sentinel will announce the auto-detected local IP and the specified port.
#
# Example:
#
# sentinel announce-ip 1.2.3.4
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir "/var/lib/redis/26379"
logfile "/var/log/redis/sentinel-26379"
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel myid 1b51aae574d721222f63a84c37d83c9361b2f16b
sentinel monitor master3 192.168.1.11 7005 2
sentinel config-epoch master3 0
# sentinel auth-pass <master-name> <password>
#
# Set the password to use to authenticate with the master and slaves.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for slaves, so it is not
# possible to set a different password in masters and slaves instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds>
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is 30 seconds.
sentinel leader-epoch master3 0
sentinel known-slave master3 192.168.1.11 7006
sentinel known-sentinel master3 192.168.1.114 36379 d6c381864488fdb81db2a86b939fac57ba2345bc
# sentinel parallel-syncs <master-name> <numslaves>
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel known-sentinel master3 192.168.1.114 46379 29538ea65975d208004a1dcc85fc2eed8fa71ab5
sentinel monitor master1 192.168.1.233 7001 2
sentinel config-epoch master1 0
# sentinel failover-timeout <master-name> <milliseconds>
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
# already tried against the same master by a given Sentinel, is two
# times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
# to a Sentinel current configuration, to be forced to replicate
# with the right master, is exactly the failover timeout (counting since
# the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
# did not produced any configuration change (SLAVEOF NO ONE yet not
# acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
# reconfigured as slaves of the new master. However even after this time
# the slaves will be reconfigured by the Sentinels anyway, but not with
# the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
sentinel leader-epoch master1 0
sentinel known-slave master1 192.168.1.254 7004
# SCRIPTS EXECUTION
#
# sentinel notification-script and sentinel reconfig-script are used in order
# to configure scripts that are called to notify the system administrator
# or to reconfigure clients after a failover. The scripts are executed
# with the following rules for error handling:
#
# If script exits with "1" the execution is retried later (up to a maximum
# number of times currently set to 10).
#
# If script exits with "2" (or an higher value) the script execution is
# not retried.
#
# If script terminates because it receives a signal the behavior is the same
# as exit code 1.
#
# A script has a maximum running time of 60 seconds. After this limit is
# reached the script is terminated with a SIGKILL and the execution retried.
# NOTIFICATION SCRIPT
#
# sentinel notification-script <master-name> <script-path>
#
# Call the specified notification script for any sentinel event that is
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
# This script should notify the system administrator via email, SMS, or any
# other messaging system, that there is something wrong with the monitored
# Redis systems.
#
# The script is called with just two arguments: the first is the event type
# and the second the event description.
#
# The script must exist and be executable in order for sentinel to start if
# this option is provided.
#
# Example:
#
# sentinel notification-script mymaster /var/redis/notify.sh
# CLIENTS RECONFIGURATION SCRIPT
#
# sentinel client-reconfig-script <master-name> <script-path>
#
# When the master changed because of a failover a script can be called in
# order to perform application-specific tasks to notify the clients that the
# configuration has changed and the master is at a different address.
#
# The following arguments are passed to the script:
#
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
#
# <state> is currently always "failover"
# <role> is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
啓動sentinel服務
cd /usr/local/bin
./redis-sentinel /home/redis4/redis-sentienl/26379/sentinel.conf
./redis-sentinel /home/redis4/redis-sentienl/36379/sentinel.conf
./redis-sentinel /home/redis4/redis-sentienl/46379/sentinel.conf
至此,sentinel監控集羣建立完畢,日誌去配置目錄的/var/log/redis目錄下查看
集羣使用用cli鏈接客戶端redis-cli -h 192.168.1.233 -c -p 7001查看集羣狀態[root@bogon bin]# redis-trib.rb info 192.168.1.233:7001192.168.1.233:7001 (e6d97aef…) -> 0 keys | 5461 slots | 1 slaves.192.168.1.11:7005 (027540bf…) -> 0 keys | 5461 slots | 1 slaves.192.168.1.254:7003 (bcf79a6d…) -> 0 keys | 5462 slots | 1 slaves.[OK] 0 keys in 3 masters.0.0 keys per slot on average.當前集羣環境說明redis1 7001爲master 7002 slaveredis2 7003 爲 master 7004 slaveredis3 7005 爲 master 7006 slaveredis4 啓動三個sentinel實例監控主從節點端口分別爲 26379 36379 46379必須有兩個以上的sentienl實例認爲主節點掛掉,纔會啓動主從切換---------------------