linux虚拟机搭建哨兵模式
Redis 搭建了主从复制以后,虽然减轻了服务器读的压力🍐,但并没有减轻服务器读的压力
主从复制模式也存在一些问题:
- 由于在操作的时候,所有的数据都是写在主服务器上,然后又主服务器异步将命令发送给slave完成书的同步,这恶鬼操作会有一定的延时,尤其是业务特别繁忙的时候或者slave过多的时候胡更加严重。
- 当主服务器宕机之后,就没有办法提供服务的。
哨兵模式(sentine)
优点:
- 主服务器出现故障之后,我们可以挑选一个slave发送slaveof no ine 让这个slave变成主服务器,并且让他的slave继续跟随新的主服务器
- 我们可以根据日志去修复之前故障的主服务器,当修复之后,我们启动服务后,将其变成slave继续提供服务
**注意:**哨兵需要投票,所以建议哨兵的数量是奇数,以防万一一直出现平票的问题。
- 哨兵模式失败,更改了配置文件,所以需要回复下配置文件
- 哨兵模式要求主从之间的密码必须一致,否则切换主机会失败
配置哨兵模式:
(注:此次搭建的哨兵模式是在一台虚拟机中实现的,如果要在多台虚拟中实现,只需将配置文件放在对应的虚拟机中即可)
将redis按转目录下的redis.conf 复制三份,分别命名为
redis6380.conf redis6381.conf redis6382.conf
需要修改的内容如下:
-
redis6380.conf
bind 0.0.0.0 # (配置文件69行) protected-mode:no # (配置文件88行) prot 6380 # (配置文件92行) pidfile /var/run/redis_6380.pid # (配置文件158行) masterauth 123456 # (配置文件293行)
-
redis6381.conf
bind 0.0.0.0 # (配置文件69行) protected-mode:no # (配置文件88行) prot 6381 # (配置文件92行) pidfile /var/run/redis_6381.pid # (配置文件158行) slaveof 127.0.0.1 6380 # (配置文件21行) replicaof 192.168.218.130 6380 # (配置文件286行) masterauth 123456 # (配置文件293行)
-
redis6382.conf
bind 0.0.0.0 # (配置文件69行) protected-mode:no # (配置文件88行) prot 6382 # (配置文件92行) pidfile /var/run/redis_6382.pid # (配置文件158行) slaveof 127.0.0.1 6380 # (配置文件21行) replicaof 192.168.218.130 6380 # (配置文件286行) masterauth 123456 # (配置文件293行)
-
redis安装目录下的
sentine.conf
配置文件修改内容如下:(此内容添加在配置文件任意行都可以)
sentinel monitor master 192.168.218.130 6380 1 #(配置文件67行) sentinel auth-pass master 123456 #(配置文件86行)
测试服务
配置完以后启动服务:
[root@Test1 redis-5.0.3]# src/redis-server redis6380.conf
[root@Test1 redis-5.0.3]# src/redis-server redis6381.conf
[root@Test1 redis-5.0.3]# src/redis-server redis6382.conf
# 启动哨兵对服务进行监听
[root@Test1 redis-5.0.3]# src/redis-sentinel sentinel.conf
逐个进入redis客户端查看redis服务器状态:
[root@Test1 ~]# /usr/local/redis-5.0.3/src/redis-cli -p 6380 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> info replication # 进入服务 查看主机的橘色
# Replication
role:master # 查看当前主机的角色
connected_slaves:2
...
[root@Test1 ~]# /usr/local/redis-5.0.3/src/redis-cli -p 6381 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6381> info replication
# Replication
role:slave # 查看当前主机的角色
master_host:192.168.218.130
master_port:6380
第三台也是如此
这样就配置完成了