Centos6.6部署Redis集群

Centos6.6部署Redis集群 1
环境准备 1
环境安装redis 1
安装ruby 2
配置redis主从环境 3
部署redis sentinel服务器 5
集群使用 13
当前集群环境说明 13
测试功能点 14
测试点1: 14
测试点2: 14
测试点3: 14
测试点4: 15

环境准备
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 否则创建集群时无法访,和单机集群有区别

daemonize yes #redis后台运行

pidfile /var/run/redis_7001.pid #pidfile文件对应7001-7003

cluster-enabled yes #开启集群

cluster-config-file nodes_7001.conf #保存节点配置,自动创建,自动更新对应7001-7003

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
如类似下图的显示则表示集群启动成功

以上若报错,参考此教程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文件,参考下面的,只需要修改端口号即可

# 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
#

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:7001
192.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 slave
redis2 7003 为 master 7004 slave
redis3 7005 为 master 7006 slave
redis4 启动三个sentinel实例监控主从节点端口分别为 26379 36379 46379
必须有两个以上的sentienl实例认为主节点挂掉,才会启动主从切换
---------------------

posted on 2019-07-15 04:34  激流勇进1  阅读(379)  评论(0编辑  收藏  举报