您的位置:首页 > 数据库 > Redis

redis集群和哨兵搭建

2019-05-29 15:09 176 查看

为了实现redis高可用,考虑主从复制,哨兵模式

安装和开机自启省略本文版本为4.0.10,可参考

https://www.geek-share.com/detail/2727783003.html

https://www.geek-share.com/detail/2692689282.html

主从配置

[code][root@localhost redis]# cd /usr/local/redis
[root@localhost redis]# vi redis.conf

主的配置
# Redis使用后台模式
daemonize yes

# 关闭保护模式
protected-mode no

# 开启远程访问
bind 0.0.0.0

# 修改启动端口为6379
port 6379

从的配置
# Redis使用后台模式
daemonize yes

# 关闭保护模式
protected-mode no

# 开启远程访问
bind 0.0.0.0

# 修改启动端口为6379
port 6379

#配置主的地址端口
slaveof 192.168.255.111 6379

#配置主的密码(如果有)
masterauth 123456

启动redis

[code][root@localhost redis]# src/redis-server ./redis.conf

#查看主从节点信息
[root@localhost redis]# src/redis-cli
127.0.0.1:6379> info replication
主节点信息:
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.255.113,port=6379,state=online,offset=2563538,lag=1
slave1:ip=192.168.255.112,port=6379,state=online,offset=2563538,lag=0
master_replid:58abe37473712e06efca0f4d598de56792b42953
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2563538
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1514963
repl_backlog_histlen:1048576

次节点信息
# Replication
role:slave
master_host:192.168.255.111
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:2577991
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:58abe37473712e06efca0f4d598de56792b42953
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2577991
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1529416
repl_backlog_histlen:1048576

master_link_status:up 为up则表示连接成功

哨兵配置

[code][root@localhost redis]# vi sentinel.conf
3台实例均修改如下:
#本机地址
port 26379
bind 192.168.255.111   #本机地址
dir /usr/local/redis/tmp
sentinel monitor mymaster 192.168.1.132 6379 2  #mymaster是集群的名称可自定义,IP地为集群中master的地址,注意与bind的区别 6379表示端口 2表示 需要多少哨兵同意才能执行故障转移操作
sentinel down-after-milliseconds mymaster 30000  #超过多少毫秒跟一个redis实例断了连接,哨兵就可能认为这个redis实例挂了
sentinel failover-timeout mymaster 60000  #failover转移时间,超出此时间认为master转移失效,重新开始转移
sentinel parallel-syncs mymaster 1          #新的master别切换之后,同时有多少个slave被切换到去连接新master,重新做同步,数字越低,花费的时间越多
protected-mode no                       #关闭安全模式,否则会报错
sentinel auth-pass mymaster redis-pass   #如果集群设置了密码,需要添加
daemonize yes            #后台进程
logfile /usr/local/redis/logs/sentinal.log      #日志路径

启动哨兵

[code]#设置为后台启动
[root@localhost redis]# src/redis-sentinel ./sentinel.conf &

查看状态

[code][root@redis-master 26379]# redis-cli -h 192.168.255.111 -p 26379
192.168.255.112:26379>  sentinel master mymaster #查看master的信息
192.168.255.112:26379> SENTINEL slaves mymaster  #查看slave的信息
192.168.255.112:26379> SENTINEL sentinels mymaster  #查看sentinel的信息,本机sentinel信息不显示
192.168.255.112:26379> SENTINEL get-master-addr-by-name mymaster #查看redis集群master的IP地址和端口

测试宕机

kill掉主节点

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: