redis+sentinel集群部署

一、环境介绍 CentOS7

192.168.1.201 主+Sentinel      redis端口:6379 Sentinel端口:26379
192.168.1.202 从+Sentinel      redis端口:6379 Sentinel端口:26379
192.168.1.203 从+Sentinel      redis端口:6379 Sentinel端口:26379

二、安装redis服务

1、安装依赖(3台)

$ yum -y install gcc*

2、下载redis安装(3台)

$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz

3、解压编译(3台)

$ tar xf redis-5.0.5.tar.gz -C /usr/local/
$ cd /usr/local/redis-5.0.5
$ make

4、创建redis 数据目录 日志目录(3台)

$ mkdir -p /opt/redis/{data,conf,redis-log,sentinel-log}

5、移动配置文件(3台)

$ cd /usr/local/redis-5.0.5/
$ cat redis.conf | grep -v ^#|grep -v ^$ >/opt/redis/conf/redis.conf

6、修改配置文件redis.conf(主)

#修改配置
bind 192.168.1.201
dir "/opt/redis/data"
daemonize yes
logfile "/opt/redis/redis-log/redis.log"
#增加配置
requirepass "123.456.789"
masterauth 123456789

修改配置文件redis.conf(从)

#修改配置
bind 192.168.1.202
dir "/opt/redis/data"
daemonize yes
logfile "/opt/redis/redis-log/redis.log"
#增加配置
requirepass "123.456.789"
masterauth 123456789
slaveof 192.168.1.201 6379
#修改配置
bind 192.168.1.203
dir "/opt/redis/data"
daemonize yes
logfile "/opt/redis/redis-log/redis.log"
#增加配置
requirepass "123.456.789"
masterauth 123456789
slaveof 192.168.1.201 6379

7、把启动文件放在/usr/local/bin方便启动(3台)

$ cp /usr/local/redis-5.0.5/src/redis-server  /usr/local/bin/
$ cp /usr/local/redis-5.0.5/src/redis-cli  /usr/local/bin/

8、启动(3台)

$ redis-server  /opt/redis/conf/redis.conf

9、测试

# 查看集群是否正常
$ redis-cli  -h 192.168.1.201 -p 6379 -a 123456789 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.202,port=6379,state=online,offset=906,lag=0
slave1:ip=192.168.1.203,port=6379,state=online,offset=906,lag=0
master_replid:4ac62473efb8c3767bbd2cd0e1754c4e8d2980d1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:906
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:906
192.168.1.201:6379> AUTH 123.456.789
OK
192.168.1.201:6379> set k1 v1
OK
192.168.1.201:6379> exit
$ redis-cli  -h 192.168.1.202 -p 6379 -a 123456789
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.202:6379> AUTH 123456789
OK
192.168.1.202:6379> get k1
"v1"
192.168.1.202:6379> exit
$ redis-cli  -h 192.168.1.203 -p 6379 -a 123456789
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.203:6379> AUTH 123.456.789
OK
192.168.1.203:6379> get k1
"v1"

三、安装哨兵 Sentinel

1、复制配置文件 拷贝哨兵启动文件到/usr/local/bin(3台)

$ cd /usr/local/redis-5.0.5/
$ cat sentinel.conf | grep -v ^#|grep -v ^$ >/opt/redis/conf/sentinel.conf
$ cd src
$ cp redis-sentinel /usr/local/bin

2、修改配置文件/opt/redis/conf/sentinel.conf(3台一致)

port 26379
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile "/opt/redis/sentinel-log/sentinel.log"
dir /tmp
sentinel monitor mymaster 192.168.1.201 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster 123456789

3、启动哨兵

$ redis-sentinel  /opt/redis/conf/sentinel.conf

4、测试

$ redis-cli  -h 192.168.1.201 -p 6379 -a 123456789 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.202,port=6379,state=online,offset=30335,lag=1
slave1:ip=192.168.1.203,port=6379,state=online,offset=30335,lag=1
master_replid:db7b7b2d05b7bc2e770f5597e31bf9b7274b6add
master_replid2:d8095aad044052ca59d4ecfc54a89911eb17e966
master_repl_offset:30617
second_repl_offset:11834
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:30617

kill掉201的redis服务,可以看到主已经顺利切换到202了

$ redis-cli  -h 192.168.1.202 -p 6379 -a 123456789 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.203,port=6379,state=online,offset=0,lag=0
master_replid:68c6b9e0b0ca4f51a887adb82ecd6e1b36a2276e
master_replid2:db7b7b2d05b7bc2e770f5597e31bf9b7274b6add
master_repl_offset:42413
second_repl_offset:41827
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1448
repl_backlog_histlen:40966

猜你喜欢

转载自www.cnblogs.com/xiaobao2/p/12309497.html