欢迎阅读!

潇湘夜雨

当前位置: 主页 > 数据库 >

redis主从同步和sentinel高可用

时间:2017-05-16 19:58来源:潇湘夜雨 作者:华嵩阳 点击:
环境介绍 系统:CentOS release 6.8 (Final) 主机:master:192.168.198.128 slave:192.168.198.129 redis: redis-3.2.8 一、redis安装 说明:master和slave主机都要先安装redis 1、下载安装redis wget http://download.r
 
环境介绍
系统:CentOS release 6.8 (Final)
主机:master:192.168.198.128
 slave:192.168.198.129
redis: redis-3.2.8
 
一、redis安装
 
说明:master和slave主机都要先安装redis
 
1、下载安装redis
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -zxvf redis-3.2.8.tar.gz 
cd redis-3.2.8
make PREFIX=/usr/local/redis install
 
安装redis时遇到zmalloc.h:50:31: 错误:jemalloc/jemalloc.h:没有那个文件或目录
解决办法
make MALLOC=libc
 
[root@localhost redis-3.2.8]# cp redis.conf sentinel.conf /usr/local/redis/
 
[root@localhost ~]# mkdir /var/log/redis #创建redis日志目录
[root@localhost ~]# mkdir -p /data/redis #创建redis数据目录
 
2、修改配置文件
 
[root@localhost ~]# vim /usr/local/redis/redis.conf 
bind 0.0.0.0
protected-mode no #这里一定要修改,否则不能登录
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
 
3、配置redis的环境变量
 
[root@localhost redis]# vim /etc/profile.d/redis.sh
 
export PATH=$PATH:/usr/local/redis/bin
[root@localhost redis]# source /etc/profile
 
注意:环境变量配置非必须,如果不配环境变量,使用redis命令是需用绝对路径。
 
启动redis
[root@localhost redis]# redis-server redis.conf 
[root@localhost redis]# netstat -tnlp|grep redis
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      11490/redis-server  
 
[root@localhost redis]# tail /var/log/redis/redis.log #查看启动日志
 
11490:M 11 May 14:50:44.457 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
11490:M 11 May 14:50:44.457 # Server started, Redis version 3.2.8
11490:M 11 May 14:50:44.457 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
11490:M 11 May 14:50:44.457 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
11490:M 11 May 14:50:44.457 * The server is now ready to accept connections on port 6379
注意:日志中有一些告警,可以忽略,也可以根据提示设置内核参数
 
4、登录测试
[root@localhost redis]# redis-cli #登录redis,可以加-h参数制定主机
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set nu 123
OK
127.0.0.1:6379> save #保存数据镜像
OK
127.0.0.1:6379> get nu
"123"
127.0.0.1:6379> exit
[root@localhost redis]# ls -lh /data/redis/ #查看数据镜像文件
总用量 4.0K
-rw-r--r-- 1 root root 87 5月  11 14:57 dump.rdb
 
 
[root@localhost redis]# redis-cli shutdown #关闭redis服务
[root@localhost redis]# netstat -tnlp|grep redis
[root@localhost redis]# tail /var/log/redis/redis.log 
11490:M 11 May 15:00:43.443 # User requested shutdown...
11490:M 11 May 15:00:43.443 * Saving the final RDB snapshot before exiting.
11490:M 11 May 15:00:43.447 * DB saved on disk
11490:M 11 May 15:00:43.447 * Removing the pid file.
11490:M 11 May 15:00:43.447 # Redis is now ready to exit, bye bye...
 
5、添加redis服务项
 
vim /etc/init.d/redis-server
 
 
#!/bin/sh
#
# redis start up the redis server daemon
#
# chkconfig: 345 99 99
# description: redis service in /etc/init.d/redis 
#            chkconfig --add redis or chkconfig --list redis 
#            service redis start  or  service redis stop
# processname: redis-server
# config: /etc/redis.conf
# 注意不同位置要修改变量
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
 
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/redis.conf"
#make sure some dir exist
if [ ! -d /data/redis ] ;then
mkdir -p /data/redis
mkdir -p /var/log/redis
 
fi
 
case "$1" in
status)
ps -ef|grep redis|grep  6379
;;
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
 echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
 do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
 restart|force-reload)
${0} stop
${0} start
;;
 *)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
 
esac
 
 
[root@localhost redis]# chmod +x /etc/init.d/redis-server #执行权限
[root@localhost redis]# chkconfig --add redis-server
[root@localhost redis]# chkconfig redis-server on
[root@localhost redis]# service redis-server start
Starting Redis server...
Redis is running...
[root@localhost redis]# service redis-server status #查看状态
12024 ?        00:00:00 redis-server
12034 pts/0    00:00:00 redis-server
 
 
二、redis主从同步
 
1、更改slave服务器的配置文件
[root@localhost ~]# redis-cli -h 192.168.198.129 #为配置主从的状态
192.168.198.129:6379> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
 
 
[root@localhost ~]# vim /usr/local/redis/redis.conf
添加主从的配置:
slaveof 192.168.198.128 6379
 
保存重启
 
192.168.198.129:6379> info replication
# Replication
role:slave #已经变成slave
master_host:192.168.198.128
master_port:6379
master_link_status:down #主服务器down状态,因为master为启动
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_secon
 
启动master上的redis
 
192.168.198.129:6379> info replication
# Replication
role:slave
master_host:192.168.198.128
master_port:6379
master_link_status:up #主服务器up状态
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:1
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
 
192.168.198.129:6379> set slave 45
(error) READONLY You can't write against a read only slave.
注意:redis主从同步时,salve端时不能写的。
 
2、同步测试
 
1>在master写入数据
[root@localhost redis]# redis-cli -h 192.168.198.128
192.168.198.128:6379> keys *
1) "nu"
192.168.198.128:6379> set master 128
OK
192.168.198.128:6379> KEYS *
1) "master"
2) "nu"
 
2>在slave上查看数据
192.168.198.129:6379> KEYS *
1) "nu"
2) "master" #数据已经同步过来了
192.168.198.129:6379> get master
"128"
 
 
三、Sentinel - 高可用
 
1. 什么是Sentinel?
 
见名知意,它是Redis提供的哨兵程序,它是分布式程序,可以这样描述它们:
 
哨兵一般是好几个一起站岗,他们共同监视一个Master以及其Slaves。当哨兵看到Master挂掉了,他们就会互相确认有没有看走眼,一旦多数哨兵都说它挂了,那么他们就能得出结论:Master挂了。 
此时第一个发现的Sentinel会负责进行自动故障迁移,它会立即在Slaves中选出一个担任Master,所有Slaves从属于新Master,所有客户端对Master的操作转而到这台新Master上。
Sentinel的主要工作如下:
 
监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
 
提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
 
自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
 
2. 配置Sentinel
 
protected-mode no
daemonize yes
port 26379
dir /tmp
logfile "/var/log/sentinel.log"
sentinel monitor mymaster 192.168.198.128 6379 1 #这里法定票数设为1,因为只有1个sentinel。
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 30000
 
注意:再sentinel的主从同步中,slave的slave-priority最好设置成不一样,否则会造成主从切换不能正常进行。这里slave设置:slave-priority 98
 
 其中:
        port: 指定sentinel的侦听端口(即与redis server或client建立tcp连接的端口)
        monitor: 指定sentinel要monitor的redis实例,包括一个redis实例的别名(alias)及redis实例的ip+port,该行最后的数字2表示
至少2个setinel实例同时检测到redis server异常时,才将redis server的状态判决为real fail。也即,若这里配置为2,但实际部
署中sentinel只部署了1套,则即使redis实例已经挂掉,sentinel也不会给出任何警告。这一点需要特别引起注意。
        down-after-milliseconds: 指定sentinel监控到redis实例持续异常多长时间后,会判决其状态为down。
若实际业务需要sentinel尽快判决出redis实例异常,则该值可适当配小。默认3分钟
        failover-timeout: 若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次
failover失败。这里的切换时间不能设置的太小,最好30s以上,避免引发sentinel的误判,导致整个系统无法选出new master
        parallel-syncs: 指定failover过程中,同时被sentinel reconfigure的最大slave实例数。由于reconfigure过程中,对应的slave
会中断响应客户端请求,故为避免所有的slave同时不可用,该值需适当配小。
        notification-script: 指定sentinel检测到master-name指向的实例异常时,调用的报警脚本。该配置项可选,但线上系统建议配置。
 
哨兵应该被放在独立的服务器上,最好应该有3个哨兵(3台服务器),由于条件限制,可以部署在redis服务器上,但至少要2个以上,否则难以实现高可用。
 
虽然 Redis Sentinel 释出为一个单独的可执行文件 redis-sentinel,但实际上它只是一个运行在特殊模式下的 Redis 服务器, 你可以在启动
一个普通 Redis 服务器时通过给定 --sentinel 选项来启动 Redis Sentinel 。
[root@localhost redis]# redis-sentinel sentinel.conf #启动sentinel方式一
[root@localhost redis]# redis-server sentinel.conf --sentinel #启动sentinel方式二
 
[root@localhost ~]# tail -f /var/log/sentinel.log #sentinel日志
|`-._`-._    `-.__.-'    _.-'_.-'|                                  
|    `-._`-._        _.-'_.-'    |                                  
 `-._    `-._`-.__.-'_.-'    _.-'                                   
 `-._    `-.__.-'    _.-'                                       
 `-._        _.-'                                           
 `-.__.-'                                               
 
2657:X 14 May 11:48:55.450 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2657:X 14 May 11:48:55.450 # Sentinel ID is 05c8cc9f5145eba5760dac85c1d0502add3c25a5 #自动生成id
2657:X 14 May 11:48:55.450 # +monitor master mymaster 192.168.198.128 6379 quorum 1 #监控的主redis
 
3、查看状态 
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.198.128:6379,slaves=1,sentinels=1
 
注意:sentinel默认启用了保护模式,只能通过127.0.0.1:26379查看信息。
 
Sentinel 可接受的命令(官网页面搜索Sentinel API):
 
PING :返回 PONG 。
SENTINEL masters :列出所有被监视的主服务器,以及这些主服务器的当前状态。
SENTINEL master <master name>:特定主服务器的当前状态。
SENTINEL slaves <master name>:列出给定主服务器的所有从服务器,以及这些从服务器的当前状态。
SENTINEL sentinels <master name> Show a list of sentinel instances for this master, and their state.
SENTINEL get-master-addr-by-name <master name> : 返回给定名字的主服务器的 IP 地址和端口号。 如果这个主服务器正在执行故障转移操作, 或者针对这个主服务器的故障转移操作已经完成, 那么这个命令返回新的主服务器的 IP 地址和端口号。
SENTINEL reset <pattern>: 重置所有名字和给定模式 pattern 相匹配的主服务器。 pattern 参数是一个 Glob 风格的模式。 重置操作清楚主服务器目前的所有状态, 包括正在执行中的故障转移, 并移除目前已经发现和关联的, 主服务器的所有从服务器和 Sentinel 。
SENTINEL failover : 当主服务器失效时, 在不询问其他 Sentinel 意见的情况下, 强制开始一次自动故障迁移 (不过发起故障转移的 Sentinel 会向其他 Sentinel 发送一个新的配置,其他 Sentinel 会根据这个配置进行相应的更新)。
 
 
127.0.0.1:26379> sentinel reset mymaster #重置主服务器mymaster状态
 
4、sentinel.conf配置被改变
 
每当一个Sentinel启动后,它就会修改并通知其它Sentinel同时修改自身的sentinel.conf文件,例如:
 
[root@localhost redis]# cat sentinel.conf |grep ^[a-z]
daemonize yes
port 26379
dir "/tmp"
logfile "/var/log/sentinel.log"
pidfile /var/run/sentinel.pid
sentinel myid 05c8cc9f5145eba5760dac85c1d0502add3c25a5 #生成id号
sentinel monitor mymaster 192.168.198.128 6379 1
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 30000
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 192.168.198.129 6379 #发现的slave
sentinel current-epoch 0
 
5、主从故障切换测试
 
1>关闭master
192.168.198.128:6379> SHUTDOWN
not connected> 
 
2>查看sentinel日志
2657:X 14 May 11:58:59.601 # +sdown master mymaster 192.168.198.128 6379 #主观判断master 服务down
2657:X 14 May 11:58:59.601 # +odown master mymaster 192.168.198.128 6379 #quorum 1/1 #客观判断master服务 down
2657:X 14 May 11:58:59.601 # +new-epoch 1
2657:X 14 May 11:58:59.601 # +try-failover master mymaster 192.168.198.128 6379 #启动master状态切换
2657:X 14 May 11:58:59.607 # +vote-for-leader 05c8cc9f5145eba5760dac85c1d0502add3c25a5 1
2657:X 14 May 11:58:59.607 # +elected-leader master mymaster 192.168.198.128 6379
2657:X 14 May 11:58:59.607 # +failover-state-select-slave master mymaster 192.168.198.128 6379 #开始切换
2657:X 14 May 11:58:59.679 # +selected-slave slave 192.168.198.129:6379 192.168.198.129 6379 @ mymaster 192.168.198.128 6379 #开始选举slave中节点
2657:X 14 May 11:58:59.679 * +failover-state-send-slaveof-noone slave 192.168.198.129:6379 192.168.198.129 6379 @ mymaster 192.168.198.128 6379
2657:X 14 May 11:58:59.755 * +failover-state-wait-promotion slave 192.168.198.129:6379 192.168.198.129 6379 @ mymaster 192.168.198.128 6379
2657:X 14 May 11:58:59.831 # +promoted-slave slave 192.168.198.129:6379 192.168.198.129 6379 @ mymaster 192.168.198.128 6379 #提升129节点为主
2657:X 14 May 11:58:59.831 # +failover-state-reconf-slaves master mymaster 192.168.198.128 6379 #更改原master的配置
2657:X 14 May 11:58:59.879 # +failover-end master mymaster 192.168.198.128 6379 #更改配置完成
2657:X 14 May 11:58:59.880 # +switch-master mymaster 192.168.198.128 6379 192.168.198.129 6379 #切换master到129节点
2657:X 14 May 11:58:59.880 * +slave slave 192.168.198.128:6379 192.168.198.128 6379 @ mymaster 192.168.198.129 6379 #通告128节点为slave状态
2657:X 14 May 11:59:02.881 # +sdown slave 192.168.198.128:6379 192.168.198.128 6379 @ mymaster 192.168.198.129 6379
 
 
3>查看slave的状态
 
192.168.198.129:6379> info replication
# Replication
role:master #此时已经变成了master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
 
192.168.198.129:6379> set slave 129 #创建一个key值,测试前面的master恢复后能否同步数据
OK
192.168.198.129:6379> KEYS *
1) "nu"
2) "slave"
 
4>恢复故障的master
 
[root@localhost redis]# service redis-server start #恢复关闭的redis
Starting Redis server...
Redis is running...
[root@localhost redis]# redis-cli -h 192.168.198.128 
192.168.198.128:6379> info replication
# Replication
role:slave #现在已经变成slave状态
master_host:192.168.198.129
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:2082
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.198.128:6379> KEYS *
1) "nu"
2) "slave" #故障后创建的数据也同步过来了
192.168.198.128:6379> get slave
"129"
 
再查看sentinel日志:
2657:X 14 May 12:04:13.389 # -sdown slave 192.168.198.128:6379 192.168.198.128 6379 @ mymaster 192.168.198.129 6379 #-sdown表示主观down状态已经解除
2657:X 14 May 12:04:23.405 * +convert-to-slave slave 192.168.198.128:6379 192.168.198.128 6379 @ mymaster 192.168.198.129 6379 #状态切换成slave,并加入到同步。
 
5、节点扩展
 
随着redis服务的开展,有时需要添加更多的节点来提高服务。新增
的redis节点可以在同一台主机,也可以是单独的主机。下面注意介绍
在同一台主机添加多个redis实例的方法。
 
在129节点上新增redis的slave实例
 
1>创建配置文件
[root@localhost redis]# vim redis2.conf #实际创建去掉#注释,否则会报错。
bind 0.0.0.0
protected-mode no
port 6380 #端口号更改
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6380.pid" #pid文件更改
loglevel notice
logfile "/var/log/redis/redis2.log" #日志文件更改
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump2.rdb" #镜像文件名更改
dir "/data/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 99 #优先级更改
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
slaveof 192.168.198.128 6379
 
2>创建启动脚本
 
[root@localhost init.d]# vim redis-server2 
#!/bin/sh
#
# redis start up the redis server daemon
#
# chkconfig: 345 99 99
# description: redis service in /etc/init.d/redis 
#            chkconfig --add redis or chkconfig --list redis 
#            service redis start  or  service redis stop
# processname: redis-server
# config: /etc/redis.conf
# 注意不同位置要修改变量
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6380
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
 
PIDFILE=/var/run/redis_6380.pid #修改pid文件名
CONF="/usr/local/redis/redis2.conf" #修改配置文件名
#make sure some dir exist
if [ ! -d /data/redis ] ;then
mkdir -p /data/redis
mkdir -p /var/log/redis
 
fi
 
case "$1" in
status)
ps -ef|grep redis|grep  6380
;;
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
 echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
 do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
 restart|force-reload)
${0} stop
${0} start
;;
 *)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
 
esac
 
 
chkconfig redis-server2 on #开机启动
 
[root@localhost init.d]# service redis-server2 start #启动redis2
Starting Redis server...
Redis is running...
[root@localhost init.d]# service redis-server2 status
root      5320     1  0 16:58 ?        00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6380  
[root@localhost init.d]# netstat -tnlp|grep redis #可以看到6380端口
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      3269/redis-server 0 
tcp        0      0 0.0.0.0:6380                0.0.0.0:*                   LISTEN      5138/redis-server 0 
[root@localhost init.d]# redis-cli -h 192.168.198.129 -p 6380
192.168.198.129:6380> info replication
# Replication
role:slave
master_host:192.168.198.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1124977
slave_priority:99
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.198.129:6380> KEYS * #添加的节点还把以前的数据同步了过来
1) "slave"
2) "nu"
192.168.198.129:6380> 
 
192.168.198.128:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.198.129,port=6379,state=online,offset=1132367,lag=1
slave1:ip=192.168.198.129,port=6380,state=online,offset=1132367,lag=1
 
 
2657:X 14 May 16:45:07.825 * +slave slave 192.168.198.129:6380 192.168.198.129 6380 @ mymaster 192.168.198.128 6379
 
按照上述方法在129节点增加一个slave实例
 
添加后查看主master上的信息显示有3个节点
192.168.198.128:6379> info replication
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.198.129,port=6379,state=online,offset=1253955,lag=0
slave1:ip=192.168.198.129,port=6380,state=online,offset=1253955,lag=0
slave2:ip=192.168.198.128,port=6380,state=online,offset=1253955,lag=1
master_repl_offset:1253955
 
注意:当有多个slave节点是,故障切换时,sentinel一般会提升priority值最小的为master
 
6、添加sentinel节点
 
添加sentinel节点,只需要把前面redis的配置文件复制的其他主机上,启动后就会自动加入sentinel集群
也可以自定义sentinel的配置文件,但是master的地址必须和目前sentinel一致,因为实际生产环境中master的地址可能会变化的。
 
2657:X 14 May 17:45:13.917 * +sentinel sentinel 07465298cb62d7d14b4606f04f6fb67d1403ac0d 192.168.198.129 26379 @ mymaster 192.168.198.129 6379
2657:X 14 May 17:45:16.927 # -sdown sentinel 07465298cb62d7d14b4606f04f6fb67d1403ac0d 192.168.198.129 26379 @ mymaster 192.168.198.129 6379
 
注意:sentinel要关闭保护模式:protected-mode no。否则是不能加入sentinel集群的。
 redis和sentinel的监听端口要注意在防火墙上开放,否则相互间不能心跳检测,也就不能主从切换。
 各个redis服务之间的时间要同步,否则不能进行主从切换。
(责任编辑:liangzh)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
推荐内容