redis集群和哨兵搭建

本文围绕实现Redis高可用展开,介绍主从复制和哨兵模式。包含Redis 4.0.10版本的安装,解决测试依赖tcl 8.5的问题,还提及Redis配置,如后台运行、取消保护模式等。同时说明了远程连接问题的解决办法,最后阐述主从和哨兵的配置、启动、状态查看及宕机测试。

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

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

https://blog.youkuaiyun.com/qq_24113267/article/details/79150533

https://blog.youkuaiyun.com/ling811/article/details/53637257

yum -y install gcc

yum -y install wget

cd /usr/local/

wget http://download.redis.io/releases/redis-4.0.10.tar.gz

  1. make之后,会出现一句提示

Hint: To run 'make test' is a good idea ;)

但是不测试,通常是可以使用的。若我们运行make test ,会有如下提示

[devnote@devnote src]$ make test

You need tcl 8.5 or newer in order to run the Redis test

make: ***[test] Error_1

解决办法是用yum安装tcl8.5(或去tcl的官方网站http://www.tcl.tk/下载8.5版本,并参考官网介绍进行安装)

yum install tcl

cd redis-4.0.10

测试能否启动

src/redis-server /usr/local/redis-4.0.10/redis.conf

 

  1. 默认不是后台启动,需要对redis.conf配置。

常用配置如下:

(1) 加 # 注释掉# bind 127.0.0.1(默认只侦听本地回环网络端口)

(2) daemonize no改为daemonize yes (修改为在后台运行,不占用当前窗口)

(3) protected-mode yes改为protected-mode no (取消保护模式)

     2.如果还无法远程连接,可以进行如下操作:

(1) 先确保redis.conf中注释掉bind 127.0.0.1这一行

(2) 检查端口 netstat -tunlp |grep 6379

tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 3227/redis-server *,查看结果也是ok的.

(3) 服务器防火墙的问题,关了防火墙试下:

systemctl stop firewalld.service #我的远程服务器是centos7

补充:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

执行下面这段命令就可以开放6379端口了

firewall-cmd --zone=public --add-port=6379/tcp --permanent

firewall-cmd --reload

firewall-cmd --list-ports

主从配置

[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

[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则表示连接成功

哨兵配置

[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      #日志路径

启动哨兵

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

查看状态

[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掉主节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值