redis主从配置

redis需要掌握的知识架构:
这里写图片描述

一:安装
依赖:yum install -y gcc


[root@server1 /]# tar zxf redis-3.0.2.tar.gz  -C /usr/local/
[root@server1 /]# cd /usr/local/
[root@server1 /]# cd redis-3.0.2/
[root@server1 /]# make
[root@server1 redis-3.0.2]# cd utils/
[root@server1 utils]# ls
build-static-symbols.tcl  install_server.sh  redis_init_script.tpl
cluster_fail_time.tcl     lru                redis-sha1.rb
create-cluster            mkrelease.sh       speed-regression.tcl
generate-command-help.rb  redis-copy.rb      whatisdoing.sh
hyperloglog               redis_init_script
[root@server1 utils]# sh install_server.sh   ##执行脚本建立配置文件
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
[root@server1 redis-3.0.2]# make install
cd src && make install
make[1]: Entering directory `/usr/local/redis-3.0.2/src'

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/redis-3.0.2/src'
[root@server1 redis-3.0.2]# 

二:主从配置

主从配置主要是一主多从,从机的slaveof指向主,
主:
[root@server1 utils]# vim /etc/redis/6379.conf
bind 0.0.0.0
从:

[root@server3 utils]# vim /etc/redis/6379.conf
bind 0.0.0.0

slaveof 172.25.67.1 6379
三:主从基础上的高可用
如上,redis主从复制已经完成,当主机宕机的时候,我们必须从从机里面选出一个主机来接替原先的主机;

[root@server1 redis-3.0.2]# cd /usr/local/redis-3.0.2

cp sentinel.conf /etc/redis/
vim /etc/redis/sentinel.conf

sentinel monitor mymaster 172.25.67.1 6379 2##设置master为172.25.671.1   端口是6379  2表示投票数,这个后续具体整理
sentinel down-after-milliseconds mymaster 10000

scp /etc/redis/sentinel.conf server2:/etc/redis/
scp /etc/redis/sentinel.conf server3:/etc/redis/

调用sentinel.conf文件:

redis-sentinel /etc/redis/sentinel.conf 
[root@server1 local]# redis-sentinel /etc/redis/sentinel.conf 
4136:X 23 Oct 14:24:38.501 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 4136
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

4136:X 23 Oct 14:24:38.502 # Sentinel runid is c94ac1b9e8edc4c1b00b701876d08e9c591b0434
4136:X 23 Oct 14:24:38.502 # +monitor master mymaster 172.25.67.1 6379 quorum 2
4136:X 23 Oct 14:24:47.545 * +sentinel sentinel 172.25.67.2:26379 172.25.67.2 26379 @ mymaster 172.25.67.1 6379
4136:X 23 Oct 14:25:18.943 * +sentinel sentinel 172.25.67.3:26379 172.25.67.3 26379 @ mymaster 172.25.67.1 6379

如上,我们同时可以看到监控信息,master为主;
另外,我们在master宕机的时候,可以观察到根据配置文件写的当投票到点后可以下线然后系统通过算法选举出新的master

[root@server1 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped





4181:X 23 Oct 16:37:26.560 # +sdown master mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:26.650 # +odown master mymaster 172.25.67.1 6379 #quorum 2/2
4181:X 23 Oct 16:37:26.650 # +new-epoch 1
4181:X 23 Oct 16:37:26.650 # +try-failover master mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:26.744 # +vote-for-leader 6f74369215d0eff2157affeff65aa39aefccc2a8 1
4181:X 23 Oct 16:37:26.745 # 172.25.67.2:26379 voted for 8e76169705ca62ba5f0c319aebece01005bfaf39 1
4181:X 23 Oct 16:37:26.844 # 172.25.67.3:26379 voted for 6f74369215d0eff2157affeff65aa39aefccc2a8 1
4181:X 23 Oct 16:37:26.871 # +elected-leader master mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:26.872 # +failover-state-select-slave master mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:26.943 # +selected-slave slave 172.25.67.3:6379 172.25.67.3 6379 @ mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:26.943 * +failover-state-send-slaveof-noone slave 172.25.67.3:6379 172.25.67.3 6379 @ mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:27.005 * +failover-state-wait-promotion slave 172.25.67.3:6379 172.25.67.3 6379 @ mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:27.819 # +promoted-slave slave 172.25.67.3:6379 172.25.67.3 6379 @ mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:27.819 # +failover-state-reconf-slaves master mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:27.842 * +slave-reconf-sent slave 172.25.67.2:6379 172.25.67.2 6379 @ mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:28.825 * +slave-reconf-inprog slave 172.25.67.2:6379 172.25.67.2 6379 @ mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:28.825 * +slave-reconf-done slave 172.25.67.2:6379 172.25.67.2 6379 @ mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:28.879 # -odown master mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:28.879 # +failover-end master mymaster 172.25.67.1 6379
4181:X 23 Oct 16:37:28.879 # +switch-master mymaster 172.25.67.1 6379 172.25.67.3 6379
4181:X 23 Oct 16:37:28.879 * +slave slave 172.25.67.2:6379 172.25.67.2 6379 @ mymaster 172.25.67.3 6379
4181:X 23 Oct 16:37:28.879 * +slave slave 172.25.67.1:6379 172.25.67.1 6379 @ mymaster 172.25.67.3 6379
4181:X 23 Oct 16:37:38.889 # +sdown slave 172.25.67.1:6379 172.25.67.1 6379 @ mymaster 172.25.67.3 6379

可以看到已经实现了主从切换;

sentinel文件信息:

1.sentinel实例之间的通讯端口

port 26379

2.sentinel需要监控的master信息:
sentinel monitor
应该小于集群中slave的个数,只有当至少个sentinel实例提交”master失效” ,才会认为master为O_DWON(“客观”失效)
sentinel monitor master 127.0.0.1 6379 2

3.设置认证密码
sentinel auth-pass def_master 012_345^678-90

4.master被当前sentinel实例认定为“失效”的间隔时间。默认为30秒
sentinel down-after-milliseconds
如果当前sentinel与master直接的通讯中,在指定时间内没有响应或者响应错误代码,那么当前sentinel就认为master失效(SDOWN,“主观”失效)
sentinel down-after-milliseconds def_master 30000

5.当前sentinel实例是否允许实施“failover”(故障转移)
no表示当前sentinel为“观察者”(只参与”投票”.不参与实施failover),全局中至少有一个为yes
sentinel can-failover def_master yes

6.当新master产生时,同时进行slaveof到新master并进行“SYNC”的slave个数。
默认为1,建议保持默认值
在salve执行salveof与同步时,将会终止客户端请求。此值较大,意味着“集群”终止客户端请求的时间总和和较大。
此值较小,意味着“集群”在故障转移期间,多个salve向客户端提供服务时仍然使用旧数据。
sentinel parallel-syncs def_master 1

7.failover过期时间
当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel将会认为此次failover失败
sentinel failover-timeout def_master 900000

8.当failover时,可以指定一个“通知”脚本用来告知系统管理员,当前集群的情况。
脚本被允许执行的最大时间为60秒,如果超时,脚本将会被终止(KILL)
脚本执行的结果:
1 -> 稍后重试,最大重试次数为10;
2 -> 执行结束,无需重试
sentinel notification-script mymaster /var/redis/notify.sh

9.failover之后重配置客户端,执行脚本时会传递大量参数,请参考相关文档
sentinel client-reconfig-script

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值