漫漫长路,其修远兮!
分类: NOSQL
2015-11-27 11:16:14
一.测试服务器信息
Ip地址 |
os版本 |
安装应用 |
端口 |
内存大小 |
192.168.6.85 |
Centos6.5 |
Redis3.0.5/sentinel |
6380 |
100M |
192.168.6.67 |
Centos6.5 |
Redis3.0.5/sentinel |
6381 |
100M |
192.168.6.155 |
Centos6.5 |
Redis3.0.5 |
6382 |
100M |
软件下载地址:
二.redis3.0.5安装
1.对应安装的脚本
install_redis_3.0.sh
初始化脚本
bash open_redis.sh r1 6380 104857600
R1:应用名称,6380为端口号,104857600 为分配内存大小
启动关闭
启动:/data/app/redis/bin/redis-server /data/app/redis/conf/r1_6380.cnf
关闭:redis-cli -h 127.0.0.1 -p 6379 shutdown,kill -9 PID
配置文件模板
daemonize yes
pidfile /data/app/redis/run/redis_6380.pid
port 6380
tcp-backlog 511
bind 0.0.0.0
timeout 300
tcp-keepalive 0
loglevel notice
logfile /data/app/redis/log/redis_6380.log
databases 16
################################ SNAPSHOTTING ################################
#save 900 1
#save 300 10
#save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6380.rdb
dir /data/app/redis/dump
################################# REPLICATION #################################
slave-serve-stale-data yes
slave-read-only no
repl-disable-tcp-nodelay no
slave-priority 100
################################### LIMITS ####################################
maxmemory 104857600
maxclients 10000
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
############################## APPEND ONLY MODE ###############################
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
############################### ADVANCED CONFIG ###############################
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
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
三.建立主从关系
在192.168.6.67,192.168.6.155上执行
slaveof 192.168.6.85 6380 建立了一主2从的关系
四.Sentinel配置
1.sentinel配置文件
port 26380
dir /data/app/redis/tmp
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 30000 30s内mymaster没有响应,就任务sdown
sentinel parallel-syncs mymaster 1-----------failover后同时有几台同步新主库的数据
sentinel failover-timeout mymaster 180000---------180s后mymaster还没有恢复正常就执行failover
2.启动sentinel
在85机器上启动
/data/app/redis/bin/redis-sentinel /data/app/redis/conf/s1_26380.conf &
输出日志
20966:X 04 Nov 15:18:35.643 # Sentinel runid is c46c702bac3471b70d779b6ceec52a59c3f17484
20966:X 04 Nov 15:18:35.643 # +monitor master mymaster 192.168.6.85 6380 quorum 2
20966:X 04 Nov 15:18:36.644 * +slave slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.85 6380
20966:X 04 Nov 15:18:36.646 * +slave slave 192.168.6.67:6381 192.168.6.67 6381 @ mymaster 192.168.6.85 6380
在155机器上启动
/data/app/redis/bin/redis-sentinel /data/app/redis/conf/s3_26382.conf &
输出日志
4817:X 04 Nov 15:22:00.289 # Sentinel runid is 70d6d7ac8ea8d9c44dcce251014f2ea009129263
4817:X 04 Nov 15:22:00.289 # +monitor master mymaster 192.168.6.85 6380 quorum 2
4817:X 04 Nov 15:22:01.288 * +slave slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:22:01.289 * +slave slave 192.168.6.67:6381 192.168.6.67 6381 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:22:01.664 * +sentinel sentinel 192.168.6.85:26380 192.168.6.85 26380 @ mymaster 192.168.6.85 6380
检查sentinel是否正常
redis-cli -h 192.168.6.82 -p 26380 info Sentinel
redis-cli -h 192.168.6.155 -p 26382 info Sentinel
对应的sentinel配置文件变成如下
port 26382
dir "/data/app/redis/tmp"
sentinel monitor mymaster 192.168.6.85 6380 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 15000
# Generated by CONFIG REWRITE------------自己新增的配置项
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 192.168.6.67 6381
sentinel known-slave mymaster 192.168.6.155 6382
sentinel known-sentinel mymaster 192.168.6.85 26380 ab375bfeabdf083197e3ca6ec3e0ba922033a3fc
sentinel current-epoch 0
五.故障切换
1.主库故障
4817:X 04 Nov 15:32:56.981 # +sdown master mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:57.044 # +odown master mymaster 192.168.6.85 6380 #quorum 2/2
4817:X 04 Nov 15:32:57.044 # +new-epoch 1
4817:X 04 Nov 15:32:57.044 # +try-failover master mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:57.045 # +vote-for-leader 70d6d7ac8ea8d9c44dcce251014f2ea009129263 1
4817:X 04 Nov 15:32:57.048 # 192.168.6.85:26380 voted for 70d6d7ac8ea8d9c44dcce251014f2ea009129263 1
4817:X 04 Nov 15:32:57.145 # +elected-leader master mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:57.145 # +failover-state-select-slave master mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:57.204 # +selected-slave slave 192.168.6.67:6381 192.168.6.67 6381 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:57.204 * +failover-state-send-slaveof-noone slave 192.168.6.67:6381 192.168.6.67 6381 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:57.287 * +failover-state-wait-promotion slave 192.168.6.67:6381 192.168.6.67 6381 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:58.105 # +promoted-slave slave 192.168.6.67:6381 192.168.6.67 6381 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:58.105 # +failover-state-reconf-slaves master mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:58.193 * +slave-reconf-sent slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:59.147 * +slave-reconf-inprog slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:32:59.218 # -odown master mymaster 192.168.6.85 6380
4817:X 04 Nov 15:33:00.195 * +slave-reconf-done slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.85 6380
4817:X 04 Nov 15:33:00.246 # +failover-end master mymaster 192.168.6.85 6380
4817:X 04 Nov 15:33:00.246 # +switch-master mymaster 192.168.6.85 6380 192.168.6.67 6381
4817:X 04 Nov 15:33:00.246 * +slave slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.67 6381
4817:X 04 Nov 15:33:00.247 * +slave slave 192.168.6.85:6380 192.168.6.85 6380 @ mymaster 192.168.6.67 6381
4817:X 04 Nov 15:33:05.275 # +sdown slave 192.168.6.85:6380 192.168.6.85 6380 @ mymaster 192.168.6.67 6381
2.老主库恢复
20971:X 04 Nov 16:39:42.110 # -sdown slave 192.168.6.85:6380 192.168.6.85 6380 @ mymaster 192.168.6.67 6381
20971:X 04 Nov 16:39:52.053 * +convert-to-slave slave 192.168.6.85:6380 192.168.6.85 6380 @ mymaster 192.168.6.67 6381
3.从库故障
4817:X 04 Nov 16:49:14.883 # +sdown slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.67 6381
4.从库重新恢复
20971:X 04 Nov 16:56:45.102 * +reboot slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.67 6381
20971:X 04 Nov 16:56:45.153 # -sdown slave 192.168.6.155:6382 192.168.6.155 6382 @ mymaster 192.168.6.67 6381
5.sentinel故障
20971:X 04 Nov 16:59:14.967 # +sdown sentinel 192.168.6.155:26382 192.168.6.155 26382 @ mymaster 192.168.6.67 6381
6.sentinel恢复
20971:X 04 Nov 17:01:20.358 # -sdown sentinel 192.168.6.155:26382 192.168.6.155 26382 @ mymaster 192.168.6.67 6381
20971:X 04 Nov 17:01:22.421 * -dup-sentinel master mymaster 192.168.6.67 6381 #duplicate of 192.168.6.155:26382 or 0fc732fa78975c6e3d7be0b8bcb76677159009a9
20971:X 04 Nov 17:01:22.421 * +sentinel sentinel 192.168.6.155:26382 192.168.6.155 26382 @ mymaster 192.168.6.67 6381
六.常见的命令
1.sentinel常用的命令
PING 返回 PONG
SENTINEL master 列出主服务器的信息
SENTINE slaves mymaster 列出mymaster主服务器的所有从服务器信息
SENTINEL get-master-addr-by-name mymaster 返回主服务器的ip和端口
SENTINEL failover mymaster 强制故障转移
SENTINEL MONITOR
SENTINEL remove mymaster 移除掉mymaster主结点监控
sentinel reset mymaster --其他sentinel可以reset刚刚设置结点刷新信息
2.常用命令
PING 返回 PONG
Config get appendonly
Config set appendonly yes
Config rewrite
SLAVEOF NO ONE 断开主从关系
SLOWLOG GET --获取慢日志
SLOWLOG LEN --查看慢日志的条数
SLOWLOG RESET --清空慢日志
CLIENT LIST --------查看当前的连接
CLIENT KILL ip:port -------杀掉连接
CLIENT KILL TYPE [normal|slave|pubsub]
DBSIZE------当前数据库的 key 的数量