简介
通过使用 Redis 自带“主从同步+哨兵守护”功能提高Redis稳定性。
主从同步:保障数据主从数据实时同步。
哨兵:实时监控主redis如果故障,将从redis作为主使用。
环境:
- 系统:Centos7/Redhat7
- 服务:Redis4.0.11
- 服务器台数:1台(可多台)
- Redis服务数量:3个
- 192.168.1.100:6381(主)
- 192.168.1.100:6382(从)
- 192.168.1.100:6383(从)
Redis 主从同步
一、Redis主从同步搭建
1、解压缩包
tar -zxvf redis-4.0.11.tar.gz
2、移动到指定目录
mv redis-4.0.11 /usr/local/redis
3、移动到指定目录并编译安装
cd /usr/local/redis make && make install
4、为每个Redis创建各自的目录
mkdir -p /usr/local/redis-ms/6381 mkdir -p /usr/local/redis-ms/6382 mkdir -p /usr/local/redis-ms/6383
5、将配置文件复制到创建目录下
cp /usr/local/redis/redis.conf /usr/local/redis-ms/6381/ cp /usr/local/redis/redis.conf /usr/local/redis-ms/6382/ cp /usr/local/redis/redis.conf /usr/local/redis-ms/6383/
6、redis 6381主 :修改如下配置参数
vim /usr/local/redis-ms/6381/redis.conf
# 任意ip都可以连接 bind 0.0.0.0 # 关闭保护,允许非本地连接 protected-mode no # 端口号 port 6381 # 后台运行 daemonize yes # 开启日志形式 appendonly yes # 进程守护文件,就是存放该进程号相关信息的地方 pidfile "/var/run/redis_6381.pid" # db等相关目录位置 dir /usr/local/redis-ms/6381/ # 缓存库数量 databases 50 # 设置redis密码 requirepass "123456" # 设置主从复制密码 masterauth "123456"
7、redis 6382从 :修改如下配置参数
vim /usr/local/redis-ms/6382/redis.conf
# 任意ip都可以连接 bind 0.0.0.0 # 关闭保护,允许非本地连接 protected-mode no # 端口号 port 6382 # 后台运行 daemonize yes # 开启日志形式 appendonly yes # 进程守护文件,就是存放该进程号相关信息的地方 pidfile "/var/run/redis_6382.pid" # db等相关目录位置 dir /usr/local/redis-ms/6382/ # 缓存库数量 databases 50 # 设置redis密码 requirepass "123456" # 设置主从复制密码 masterauth "123456" # 主信息 slaveof 192.168.1.100 6381
8、redis 6383从 :修改如下配置参数
vim /usr/local/redis-ms/6383/redis.conf
# 任意ip都可以连接 bind 0.0.0.0 # 关闭保护,允许非本地连接 protected-mode no # 端口号 port 6383 # 后台运行 daemonize yes # 开启日志形式 appendonly yes # 进程守护文件,就是存放该进程号相关信息的地方 pidfile "/var/run/redis_6383.pid" # db等相关目录位置 dir /usr/local/redis-ms/6383/ # 缓存库数量 databases 50 # 设置redis密码 requirepass "123456" # 设置主从复制密码 masterauth "123456" # 主信息 slaveof 192.168.1.100 6381
9、启动
/usr/local/redis/src/redis-server /usr/local/redis-ms/6381/redis.conf /usr/local/redis/src/redis-server /usr/local/redis-ms/6382/redis.conf /usr/local/redis/src/redis-server /usr/local/redis-ms/6383/redis.conf
二、测试Redis主从同步
1、查看主从同步状态
/usr/local/redis/src/redis-cli -p 6381 -a 123456 info
# Server redis_version:4.0.11 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:c471ec760376b22e redis_mode:standalone os:Linux 3.10.0-123.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.2 process_id:2788 run_id:3e7540f2dbcf55921c4e6bf5ac6de8f0c0d48625 tcp_port:6381 uptime_in_seconds:3196 uptime_in_days:0 hz:10 lru_clock:15365847 executable:/usr/local/redis/src/redis-server config_file:/usr/local/redis-ms/6381/redis.conf # Clients connected_clients:3 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # Memory used_memory:1960456 used_memory_human:1.87M used_memory_rss:10055680 used_memory_rss_human:9.59M used_memory_peak:2001448 used_memory_peak_human:1.91M used_memory_peak_perc:97.95% used_memory_overhead:1935516 used_memory_startup:786632 used_memory_dataset:24940 used_memory_dataset_perc:2.12% total_system_memory:499048448 total_system_memory_human:475.93M used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:5.13 mem_allocator:jemalloc-4.0.3 active_defrag_running:0 lazyfree_pending_objects:0 # Persistence loading:0 rdb_changes_since_last_save:0 rdb_bgsave_in_progress:0 rdb_last_save_time:1575644640 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:6475776 aof_enabled:1 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok aof_last_cow_size:8507392 aof_current_size:113 aof_base_size:58 aof_pending_rewrite:0 aof_buffer_length:0 aof_rewrite_buffer_length:0 aof_pending_bio_fsync:0 aof_delayed_fsync:0 # Stats total_connections_received:17 total_commands_processed:8041 instantaneous_ops_per_sec:2 total_net_input_bytes:402231 total_net_output_bytes:1561243 instantaneous_input_kbps:0.14 instantaneous_output_kbps:0.18 rejected_connections:0 sync_full:0 sync_partial_ok:1 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:1 keyspace_misses:0 pubsub_channels:1 pubsub_patterns:0 latest_fork_usec:1179 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0 # Replication role:master connected_slaves:1 slave0:ip=192.168.1.100,port=6383,state=online,offset=224730,lag=1 master_replid:ca1e338e226a084983510088b89c15c556e7a945 master_replid2:f5f36c27bd86377158a1f2e85064f55924601b00 master_repl_offset:224871 second_repl_offset:22486 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:224871 # CPU used_cpu_sys:2.09 used_cpu_user:1.02 used_cpu_sys_children:0.13 used_cpu_user_children:0.00 # Cluster cluster_enabled:0 # Keyspace db0:keys=1,expires=0,avg_ttl=0
/usr/local/redis/src/redis-cli -p 6382 -a 123456 info
# Server redis_version:4.0.11 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:c471ec760376b22e redis_mode:standalone os:Linux 3.10.0-123.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.2 process_id:2958 run_id:7907851a00ca97feba2e43c82112dda4ca8ba9b2 tcp_port:6382 uptime_in_seconds:70 uptime_in_days:0 hz:10 lru_clock:15366134 executable:/usr/local/redis/src/redis-server config_file:/usr/local/redis-ms/6382/redis.conf # Clients connected_clients:4 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # Memory used_memory:1960496 used_memory_human:1.87M used_memory_rss:10022912 used_memory_rss_human:9.56M used_memory_peak:2042400 used_memory_peak_human:1.95M used_memory_peak_perc:95.99% used_memory_overhead:1935516 used_memory_startup:786632 used_memory_dataset:24980 used_memory_dataset_perc:2.13% total_system_memory:499048448 total_system_memory_human:475.93M used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:5.11 mem_allocator:jemalloc-4.0.3 active_defrag_running:0 lazyfree_pending_objects:0 # Persistence loading:0 rdb_changes_since_last_save:1 rdb_bgsave_in_progress:0 rdb_last_save_time:1575647152 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:-1 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:0 aof_enabled:1 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:0 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok aof_last_cow_size:8495104 aof_current_size:55 aof_base_size:55 aof_pending_rewrite:0 aof_buffer_length:0 aof_rewrite_buffer_length:0 aof_pending_bio_fsync:0 aof_delayed_fsync:0 # Stats total_connections_received:5 total_commands_processed:161 instantaneous_ops_per_sec:2 total_net_input_bytes:11283 total_net_output_bytes:44463 instantaneous_input_kbps:0.19 instantaneous_output_kbps:0.23 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:1 pubsub_patterns:0 latest_fork_usec:806 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0 # Replication role:slave master_host:192.168.1.100 master_port:6381 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:4901 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:cdfb2756fa43e1d4dfce72986be98de38911dd38 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:4901 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:4901 # CPU used_cpu_sys:0.05 used_cpu_user:0.01 used_cpu_sys_children:0.02 used_cpu_user_children:0.00 # Cluster cluster_enabled:0 # Keyspace db0:keys=1,expires=0,avg_ttl=0
/usr/local/redis/src/redis-cli -p 6383 -a 123456 info
# Server redis_version:4.0.11 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:c471ec760376b22e redis_mode:standalone os:Linux 3.10.0-123.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.2 process_id:2966 run_id:e345097b352029bbe43aeea0ed17a7c7c305ba90 tcp_port:6383 uptime_in_seconds:115 uptime_in_days:0 hz:10 lru_clock:15366180 executable:/usr/local/redis/src/redis-server config_file:/usr/local/redis-ms/6383/redis.conf # Clients connected_clients:4 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # Memory used_memory:1960480 used_memory_human:1.87M used_memory_rss:10027008 used_memory_rss_human:9.56M used_memory_peak:1980560 used_memory_peak_human:1.89M used_memory_peak_perc:98.99% used_memory_overhead:1935516 used_memory_startup:786632 used_memory_dataset:24964 used_memory_dataset_perc:2.13% total_system_memory:499048448 total_system_memory_human:475.93M used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:5.11 mem_allocator:jemalloc-4.0.3 active_defrag_running:0 lazyfree_pending_objects:0 # Persistence loading:0 rdb_changes_since_last_save:1 rdb_bgsave_in_progress:0 rdb_last_save_time:1575647153 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:-1 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:0 aof_enabled:1 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:0 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok aof_last_cow_size:6397952 aof_current_size:55 aof_base_size:55 aof_pending_rewrite:0 aof_buffer_length:0 aof_rewrite_buffer_length:0 aof_pending_bio_fsync:0 aof_delayed_fsync:0 # Stats total_connections_received:4 total_commands_processed:257 instantaneous_ops_per_sec:0 total_net_input_bytes:18203 total_net_output_bytes:65296 instantaneous_input_kbps:0.01 instantaneous_output_kbps:0.03 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:1 pubsub_patterns:0 latest_fork_usec:933 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0 # Replication role:slave master_host:192.168.1.100 master_port:6381 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:8073 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:cdfb2756fa43e1d4dfce72986be98de38911dd38 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:8073 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:8073 # CPU used_cpu_sys:0.12 used_cpu_user:0.01 used_cpu_sys_children:0.01 used_cpu_user_children:0.00 # Cluster cluster_enabled:0 # Keyspace db0:keys=1,expires=0,avg_ttl=0
2、进入主终端创建key
/usr/local/redis/src/redis-cli -p 6381 -a 123456 --raw
127.0.0.1:6381> set name 'xsk' 127.0.0.1:6381> get name
xsk
3、进入从终端查看同步状态
/usr/local/redis/src/redis-cli -p 6382 -a 123456 --raw
127.0.0.1:6381> get name
xsk
三、添加到系统服务
1、创建redis启动脚本存放目录
mkdir /usr/local/redis-ms/script
cd /usr/local/redis-ms/script/
2、创建启动脚本
vim ./start.sh
#!/bin/sh /usr/local/redis/src/redis-server /usr/local/redis-ms/6381/redis.conf /usr/local/redis/src/redis-server /usr/local/redis-ms/6382/redis.conf /usr/local/redis/src/redis-server /usr/local/redis-ms/6383/redis.conf
3、创建停止脚本
vim ./stop.sh
#!/bin/sh /usr/local/redis/src/redis-cli -p 6381 shutdown /usr/local/redis/src/redis-cli -p 6382 shutdown /usr/local/redis/src/redis-cli -p 6383 shutdown
4、创建重启脚本
vim ./restart.sh
#!/bin/sh systemctl stop redis-ms systemctl start redis-ms
5、赋值脚本权限
chmod 755 ./*
6、编写系统服务管理文件
vim /usr/lib/systemd/system/redis-ms.service
[Unit] Description=redis-ms After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/redis-ms/script/start.sh ExecStop=/usr/local/redis-ms/script/stop.sh ExecReload=/usr/local/redis-ms/script/restart.sh [Install] WantedBy=multi-user.target
7、赋值权限
chmod 755 /usr/lib/systemd/system/redis-ms.service
8、重启服务并设置开机自启动
systemctl daemon-reload systemctl enable redis-ms.service systemctl restart redis-ms.service
Redis 哨兵
一、搭建哨兵
1、修改sentinel文件
vim /usr/local/redis/sentinel.conf
# 开启保护模式
protected-mode yes
# 任意IP都可链接
bind 0.0.0.0
# mymaster是Master的名称,192.168.0.100是Master的Ip。 # 1表示确认一个Master为O_DOWN最少需要多少个哨兵认可。 sentinel monitor mymaster 192.168.1.100 6381 1 # redis有登陆密码的话必须配置这项参数,设置密码 sentinel auth-pass mymaster 123456 # (默认30秒)指定Sentinel判定Master断线的时间。(单位为毫秒,判定为主观下线SDOWN) sentinel down-after-milliseconds mymaster 10000 sentinel config-epoch redismaster 3 sentinel leader-epoch redismaster 3
2、启动
/usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf
二、添加系统服务
1、在执行脚本最下行添加命令
vim /usr/local/redis-ms/script/stop.sh
ps -aux | grep sentine |grep -v grep|awk '{print $2}'| xargs -I {} kill -9 {}
2、在执行脚本最下行添加命令
vim /usr/local/redis-ms/script/start.sh
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf &
3、重启redis服务
systemctl daemon-reload
systemctl restart redis-ms.service
三、测试切换
1、关闭master后slave成为master
_._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit .-`` .-```. ```/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26381 | `-._ `._ / _.-' | PID: 3032 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 3032:X 07 Dec 00:20:49.244 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 3032:X 07 Dec 00:20:49.244 # Sentinel ID is 4df58ef409ac6b52991173453736d113bd337f19 3032:X 07 Dec 00:20:49.244 # +monitor master mymaster 192.168.1.100 6381 quorum 1 3032:X 07 Dec 00:22:09.709 # +sdown master mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:09.709 # +odown master mymaster 192.168.1.100 6381 #quorum 1/1 3032:X 07 Dec 00:22:09.709 # +new-epoch 8 3032:X 07 Dec 00:22:09.709 # +try-failover master mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:09.710 # +vote-for-leader 4df58ef409ac6b52991173453736d113bd337f19 8 3032:X 07 Dec 00:22:09.710 # +elected-leader master mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:09.710 # +failover-state-select-slave master mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:09.811 # +selected-slave slave 192.168.1.100:6382 192.168.1.100 6382 @ mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:09.811 * +failover-state-send-slaveof-noone slave 192.168.1.100:6382 192.168.1.100 6382 @ mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:09.878 * +failover-state-wait-promotion slave 192.168.1.100:6382 192.168.1.100 6382 @ mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:10.585 # +promoted-slave slave 192.168.1.100:6382 192.168.1.100 6382 @ mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:10.585 # +failover-state-reconf-slaves master mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:10.661 * +slave-reconf-sent slave 192.168.1.100:6383 192.168.1.100 6383 @ mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:11.624 * +slave-reconf-inprog slave 192.168.1.100:6383 192.168.1.100 6383 @ mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:11.625 * +slave-reconf-done slave 192.168.1.100:6383 192.168.1.100 6383 @ mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:11.676 # +failover-end master mymaster 192.168.1.100 6381 3032:X 07 Dec 00:22:11.676 # +switch-master mymaster 192.168.1.100 6381 192.168.1.100 6382 3032:X 07 Dec 00:22:11.676 * +slave slave 192.168.1.100:6383 192.168.1.100 6383 @ mymaster 192.168.1.100 6382 3032:X 07 Dec 00:22:11.676 * +slave slave 192.168.1.100:6381 192.168.1.100 6381 @ mymaster 192.168.1.100 6382 3032:X 07 Dec 00:22:21.696 # +sdown slave 192.168.1.100:6381 192.168.1.100 6381 @ mymaster 192.168.1.100 6382