一、搭建基本主从环境
1、传输安装包到本机
mysql@cq02-pay-app03.cq02:~/wangjingwei/redis1$ scp -r m1-dba-pay-lcsandbox-dbproxy-01.m1:/home/mysql/wangjingwei/redis/redis .
mysql@cq02-pay-app03.cq02:~/wangjingwei/redis1/redis$ ls
nutcracker redis sentinel #安装包里有三个小安装包,分别是proxy(代理)、redis(非关系型数据库)、sentinel(哨兵)
看一下它的目录结构:
目录结构redis
2、创建数据目录、日志目录
mysql@cq02-pay-app03.cq02:~/wangjingwei/redis1/redis/redis/data$ mkdir redis_7008
mysql@cq02-pay-app03.cq02:~/wangjingwei/redis1/redis/redis/data$ mkdir redis_7009
mysql@cq02-pay-app03.cq02:~/wangjingwei/redis1/redis/redis/log$ touch redis_7008.log
mysql@cq02-pay-app03.cq02:~/wangjingwei/redis1/redis/redis/log$ touch redis_7009.log
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/data$ mkdir redis_7008
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/data$ mkdir redis_7009
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/log$ touch redis_7008.log
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/log$ touch redis_7009.log
3、修改配置文件
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/conf$ cp redis_7000.conf redis_7008.conf
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/conf$ vim redis_7008.conf
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/conf$ cp redis_7000.conf redis_7009.conf
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/conf$ vim redis_7009.conf
…
主要修改的是log的位置、data的位置、端口号。
4、启动redis服务
mysql@cq02-pay-app03.cq02:~/wangjingwei/redis/redis1/redis/redis$ ./bin/redis-server ./conf/redis_7008.conf
mysql@cq02-pay-app03.cq02:~/wangjingwei/redis/redis1/redis/redis$ ./bin/redis-server ./conf/redis_7009.conf
…
5、进入从库redis实例,设置主从关系
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/bin$ ./redis-cli -p 7008
redis 127.0.0.1:7008> slaveof 10.58.57.36 7008
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/redis/bin$ ./redis-cli -p 7009
redis 127.0.0.1:7009> slaveof 10.58.57.36 7009
用info命令查看以下主从信息。
三、配置sentinel
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/sentinel$ vim sentinel_test.conf
# Example sentinel.conf
# port <sentinel-port>
# The port that this sentinel instance will run on
port 24153
daemonize yes
logfile /home/mysql/wangjingwei/redis/redis1/redis/redis/log/sentinel_24153.log
tcp-keepalive 60
sentinel monitor bdrp_fsg_plus_jrmall_new-server1 10.58.57.36 7008 1
sentinel down-after-milliseconds bdrp_fsg_plus_jrmall_new-server1 30000
sentinel can-failover bdrp_fsg_plus_jrmall_new-server1 yes
sentinel parallel-syncs bdrp_fsg_plus_jrmall_new-server1 1
sentinel failover-timeout bdrp_fsg_plus_jrmall_new-server1 90000
sentinel monitor bdrp_fsg_plus_jrmall_new-server2 10.58.57.36 7009 1
sentinel down-after-milliseconds bdrp_fsg_plus_jrmall_new-server2 30000
sentinel can-failover bdrp_fsg_plus_jrmall_new-server2 yes
sentinel parallel-syncs bdrp_fsg_plus_jrmall_new-server2 1
sentinel failover-timeout bdrp_fsg_plus_jrmall_new-server2 90000
修改机器名(在proxy的配置文件里边看)、ip、端口
启动sentinel服务。
./redis-sentinel sentinel_test.conf
四、配置proxy
1、修改配置文件
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/nutcracker/conf$ vim nutcracker_9002.yml
bdrp_wallet_app:
listen: 0.0.0.0:9002
hash: fnv1a_64
distribution: modula
preconnect: true
hash_tag: “{}”
auto_eject_hosts: false
redis: true
backlog: 512
client_connections: 0
server_connections: 1
server_retry_timeout: 2000
server_failure_limit: 2
servers:
- server1
10.58.57.36:7008:1 - server2
10.58.57.36:7009:1
2、启动nutcracker服务
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/nutcracker$ ./bin/nutcracker -c conf/nutcracker_9002.yml -d -v 6 -o log/nutcracker_9002.log -S 26379 -A 10.42.198.13 -m 16384 -L log/nutcracker_slow_9000.log -s 22222 #要提前看一下这个端口号有没有被占用
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/nutcracker$ ps ux | grep nut
mysql 11951 0.0 0.0 16580 772 ? Sl 20:23 0:00 ./bin/nutcracker -c conf/nutcracker_9002.yml -d -v 6 -o log/nutcracker_9002.log -S 24153 -A 10.42.198.13 -m 16384 -L log/nutcracker_slow_9000.log -s 22222
mysql 15564 0.0 0.0 53852 796 pts/3 S+ 20:23 0:00 grep nut
-c:指定配置文件路径
-d:以守护进程进行
-v:设置日志级别
-o:设置日志输出路径
-S:设置和sentinel连接的端口
-A:设置sentinel连接的ip
-m:设置mbuf块大小
-s:设置状态监控端口
-L:设置慢日志路径
五、模拟故障
1、查看proxy里边主机的信息
mysql@m1-dba-pay-lcsandbox-dbproxy-01.m1.baidu.com:~/wangjingwei/redis/redis1/redis/nutcracker/conf$ vim nutcracker_9002.yml
bdrp_wallet_app:
listen: 0.0.0.0:9000
hash: fnv1a_64
distribution: modula
preconnect: true
hash_tag: “{}”
auto_eject_hosts: false
redis: true
backlog: 512
client_connections: 0
server_connections: 1
server_retry_timeout: 2000
server_failure_limit: 2
servers:
- server1
10.58.57.36:7008:1 - server2
10.58.57.36:7009:1
2、看一下从库redis里边的replaction信息
redis 127.0.0.1:7008> info replication
Replication
role:slave
master_host:10.58.57.36
master_port:7008
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1025198
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:200000000
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
redis 127.0.0.1:7009> info replication
# Replication
role:slave
master_host:10.58.57.36
master_port:7009
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1028072
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:200000000
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
3、看一下sentinel里边的主库信息
redis 127.0.0.1:24153> SENTINEL get-master-addr-by-name bdrp_fsg_plus_jrmall_new-server1
- “10.58.57.36”
- “7008”
redis 127.0.0.1:24153> SENTINEL get-master-addr-by-name bdrp_fsg_plus_jrmall_new-server2 - “10.58.57.36”
- “7009”
4、模拟故障
(1)被动
redis-cli -h 10.42.198.13 -p 7008 DEBUG sleep 30 #表示把10.42.198.13这台redis 停止30秒
如果检查Sentinel日志,您应该能够看到很多操作:
每个Sentinel都会检测到主服务器因+sdown事件而关闭。
此事件后来升级为+odown,这意味着多个Sentinels同意主服务器无法访问的事实。
Sentinels投票选择将启动第一次故障转移尝试的Sentinel。
发生故障转移。
如果你再问一下当前的主地址是什么mymaster,最终我们这次应该得到一个不同的回复:
127.0.0.1:5000> SENTINEL get-master-addr-by-name server1
- “10.58.57.36”
- “7008”
(2)主动
SENTINEL failover XXX-server1