Redis sentinel主从切换环境配置及服务器搭建
1.下载安装redis(v2.8以上支持,建议使用3.X以上版本):http://redis.io/download
解压缩,拷贝文件夹到指定目录下,我本机安装在/usr/local/redis-3.0.7
进入该目录下/usr/local/redis-3.0.7执行以下命令
编译测试:sudo make test
编译安装:sudo make install
等安装完毕,执行命令:./bin/redis-server
这时候界面会出现下图:
2.接下来我们就要开始配置单机多个实例,实现主-从-sentinel模式。
(1)规划并建立目录以存放redis的配置文件或是redis数据,
- mkdir /etc/redis ——redis的配置目录
- mkdir /var/redis ——redis的存放数据的目录
- mkdir /var/redis/log ——redis的日志文件的存放目录
- mkdir /var/redis/run ——redis的pid文件的存放目录
- mkdir /var/redis/redis_6379——redis实例的工作目录
- mkdir /var/redis/redis_6380——redis实例的工作目录
(2)从redis安装目录下分别复制两个配置文件redis.conf、sentinel.conf 到/etc/redis目录下:
# cp redis.conf /etc/redis/redis_6379.conf
# cp redis.conf /etc/redis/redis_6380.conf
# cp sentinel.conf /etc/redis/sentinel_26379.conf
(3)接下来,我们依次修改配置文件redis_6379.conf 和redis_6380.conf
修改配置文件,修改的有以下内容:
- Set daemonize to yes (by default it is set to no).
- Set the pidfile to /var/redis/run/redis_6379.pid (modify the port if needed).
- Change the port accordingly. In our example it is not needed as the default port is already 6379.
- Set your preferred loglevel.
- Set the logfile to /var/redis/log/redis_6379.log
- Set the dir to /var/redis/6379 (very important step!)
- #配置开始
# By default Redis does not run as a daemon. Use 'yes' if you need it. - # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
- daemonize yes
- # When running daemonized, Redis writes a pid file in /var/run/redis.pid by
- # default. You can specify a custom pid file location here.
- pidfile /var/redis/run/redis_6379.pid
- # Accept connections on the specified port, default is 6379.
- # If port 0 is specified Redis will not listen on a TCP socket.
- port 6379
- # Specify the server verbosity level.
- # This can be one of:
- # debug (a lot of information, useful for development/testing)
- # verbose (many rarely useful info, but not a mess like the debug level)
- # notice (moderately verbose, what you want in production probably)
- # warning (only very important / critical messages are logged)
- loglevel notice
- # Specify the log file name. Also the empty string can be used to force
- # Redis to log on the standard output. Note that if you use standard
- # output for logging but daemonize, logs will be sent to /dev/null
- logfile /var/redis/log/redis_6379.log
- # The working directory.
- # The DB will be written inside this directory, with the filename specified
- # above using the 'dbfilename' configuration directive.
- # The Append Only File will also be created inside this directory.
- # Note that you must specify a directory here, not a file name.
- dir /var/redis/redis_6379
同理,redis_6380.conf只需把上面的6379替换成6380即可。
(4)配置redis实例启动脚本
复制redis_init_script脚本放到/etc/init.d目录中,
[root@changle redis-3.0.7]# cp utils/redis_init_script /etc/init.d/redis_6379
[root@changle redis-3.0.7]# cp utils/redis_init_script /etc/init.d/redis_6380
然后打开脚本,/etc/init.d/redis_6379
[root@changle redis-3.0.7]# vi /etc/init.d/redis_6379
修改文件内容如下(redis_6380同样如此):
REDISPORT=6379
EXEC=/usr/local/ redis-3.0.7/bin/redis-server
CLIEXEC=/usr/local/redis-3.0.7/bin/redis-cli
PIDFILE=/var/redis/run/redis_${REDISPORT}.pid
CONF=/etc/redis/${REDISPORT}.conf
:wq!保存启动看看。
下面开始启动redis 6379,配置的端口为6379,如下,
[root@changle redis-3.0.7]# /etc/init.d/redis_6379 start Starting Redis server...
启动redis 6380也是一样
[root@changle redis-3.0.7]# /etc/init.d/redis_6380 start Starting Redis server...
(4)6379和6380都能正常启动后,我们就可以配置主从关系了,这时候我们修改6380的配置文件redis_6380.conf
追加内容:slaveof 127.0.0.1 6379
配置6380的master是6379.
再次重启6379和6380,在登陆redis-cli客户端输入info观看:
6379显示的信息如下:
6380显示的信息如下:
可以看到,6380从属于6379实例。咱们的主从已经搭建完毕。
(5)下面开始配置sentinel_26379.conf
vim /etc/redis/sentinel.conf
##redis-0 ##sentinel实例之间的通讯端口
port 26379
#master1
sentinel monitor master1 192.168.9.18 6379 1
sentinel down-after-milliseconds master1 3000
sentinel failover-timeout master1 900000
sentinel can-failover master1 yes
sentinel parallel-syncs master1 2
:wq!保存退出。
启动sentinel,观察控制台,打印输出是否如下:
已经正确监视6379master实例了。