Redis缓存数据库集群搭建(哨兵模式)

此文章在之前的基础上又添加了哨兵服务来监控redis主从。

**redis哨兵模式主要是通过修改redis和reids哨兵服务的配置文件来切换redis的主从关系。

特别说明:
在做redis哨兵服务配置之前需要做好redis主从配置,有关主从配置请看博主其他文章,链接为https://blog.csdn.net/zeorg/article/details/112759778。

系统环境:centos7(提前关闭防火墙和selinux)

IP节点名称
192.168.182.150master
192.168.182.151node-1
192.168.182.152node-2

1、查看redis哨兵模式配置文件(查看文件可不做)。

[root@master redis]# cat /usr/local/redis/sentinel.conf |grep -v "#"|grep -v "^$" 
port 26379      #哨兵模式启动端口
dir /tmp        #哨兵模式一些存储信息
sentinel monitor mymaster 127.0.0.1 6379 2     ##个人认为是监听本地的端口并且说明有几台哨兵服务器认为他挂掉,那么他就算挂掉,其中mymaster可以自自定义。
sentinel down-after-milliseconds mymaster 30000   ##设置本机redis 多长时间没有响应哨兵服务才认为本机redis挂掉,注意的是mymaster要和上面的值对上,这里30000是30秒的意思。
sentinel parallel-syncs mymaster 1 ##这里就是你的redis挂掉了有多少个从服务器开始同步,这里值越大你同步的就越快,但是相应的要看你的服务器性能(1肯定是没毛病的)。
sentinel failover-timeout mymaster 180000    ##数据同步超时时间,这里设置的是3分钟

2、配置哨兵模式redis。
master节点执行

[root@master redis]# mkdir /usr/local/redis/data

cat /usr/local/redis/sentinel.conf

port 26379
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.182.150 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel auth-pass mymaster password
bind 192.168.182.150 127.0.0.1

3、配置哨兵模式redis。
node-1节点执行

[root@node-1 redis]# mkdir /usr/local/redis/data

cat /usr/local/redis/sentinel.conf

port 26380
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.182.150 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel auth-pass mymaster password
bind 192.168.182.151 127.0.0.1

4、配置哨兵模式redis。
node-2节点执行

[root@node-2 redis]# mkdir /usr/local/redis/data

cat /usr/local/redis/sentinel.conf

port 26381
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.182.150 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel auth-pass mymaster password
bind 192.168.182.152 127.0.0.1

5、启动redis哨兵服务(注意我执行的目录)。
所有节点执行

[root@master redis]# ./bin/redis-sentinel sentinel.conf 
[root@node-1 redis]# ./bin/redis-sentinel sentinel.conf 
[root@node-2 redis]# ./bin/redis-sentinel sentinel.conf 

6、查看哨兵日志(可以看到主从的信息很清晰的显示出来)。

1520:X 27 Jan 22:42:45.457 # Sentinel ID is 7c1c1cf268d0c278b3b65f1020398df78010bd9e
1520:X 27 Jan 22:42:45.457 # +monitor master mymaster 192.168.182.150 6379 quorum 2
1520:X 27 Jan 22:42:53.426 * +sentinel sentinel 18db6235144594c7adc4557445a0c5701bf59120 192.168.182.152 26381 @ mymaster 192.168.182.150 6379
1520:X 27 Jan 22:44:45.869 * +reboot master mymaster 192.168.182.150 6379
1520:X 27 Jan 22:46:03.376 * +sentinel sentinel 34da7d114579feeeea35341f4785b8b2a7fb26a8 192.168.182.151 26380 @ mymaster 192.168.182.150 6379
1520:X 27 Jan 22:46:06.079 * +slave slave 192.168.182.151:6379 192.168.182.151 6379 @ mymaster 192.168.182.150 6379
1520:X 27 Jan 22:46:26.183 * +slave slave 192.168.182.152:6379 192.168.182.152 6379 @ mymaster 192.168.182.150 6379

7、查看redis哨兵服务状态(这里我截取了重要部分)。
master节点执行

[root@master redis]# ./bin/redis-cli -p 26379
127.0.0.1:26379> info
# 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.182.150:6379,slaves=2,sentinels=3

8、查看redis服务状态。
master节点执行

[root@master redis]# ./bin/redis-cli -p 6379 -a password
127.0.0.1:6379> info
# Replication
role:slave
master_host:192.168.182.150
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:118122
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

9、模拟down掉master节点redis。
master节点执行

[root@master redis]# ./bin/redis-cli -p 6379 -a password shutdown

10、再次查看master 哨兵模式日志(可以看到150的主redis已经down了,并且推举152位新的redis主)。
master节点执行

1520:X 27 Jan 22:47:16.867 * +slave slave 192.168.182.151:6379 192.168.182.151 6379 @ mymaster 192.168.182.152 6379
1520:X 27 Jan 22:47:16.867 * +slave slave 192.168.182.150:6379 192.168.182.150 6379 @ mymaster 192.168.182.152 6379
1520:X 27 Jan 22:47:46.909 # +sdown slave 192.168.182.150:6379 192.168.182.150 6379 @ mymaster 192.168.182.152 6379

11、开启master节点redis服务。
master节点执行

[root@master redis]# /usr/local/redis/bin/redis-server ./redis.conf

12、查看master节点哨兵服务日志(可以看到以前150机器已经已经变成152机器的从数据库)。

[root@master redis]# ./bin/redis-cli -p 6379 -a password       
127.0.0.1:6379> info
# Server
redis_version:3.2.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:94c70f4a25ff1708
redis_mode:standalone
os:Linux 3.10.0-693.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:1538
run_id:651e3232fadd723edf4daf54a0ff043d875e0d7e
tcp_port:6379
uptime_in_seconds:351
uptime_in_days:0
hz:10
lru_clock:1146658
executable:/usr/local/redis/bin/redis-server
config_file:/usr/local/redis/./redis.conf

# Clients
connected_clients:8
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:968384
used_memory_human:945.69K
used_memory_rss:7876608
used_memory_rss_human:7.51M
used_memory_peak:1069448
used_memory_peak_human:1.02M
total_system_memory:1023713280
total_system_memory_human:976.29M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:8.13
mem_allocator:jemalloc-4.4.0

# Persistence
loading:0
rdb_changes_since_last_save:1
rdb_bgsave_in_progress:0
rdb_last_save_time:1611759043
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
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

# Stats
total_connections_received:9
total_commands_processed:2158
instantaneous_ops_per_sec:5
total_net_input_bytes:161273
total_net_output_bytes:12844932
instantaneous_input_kbps:0.41
instantaneous_output_kbps:3670.58
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication
role:slave
master_host:192.168.182.152
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:118122
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

# CPU
used_cpu_sys:1.35
used_cpu_user:0.10
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace

redis哨兵配置就写到这里。
本人aws云计算新课程上线
地址:https://edu.51cto.com/sd/b1556

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页