1、安装redis
yum install -y gcc-c++ tcl
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar -xf redis-4.0.11.tar.gz -C /usr/local
mv /usr/local/redis-4.0.11 /usr/local/redis
make
make test
make install
redis-server --version
cp /usr/local/redis/redis.conf /etc/redis/
cp /usr/local/redis/sentinel.conf /etc/redis/
2、搭建主从
| 主机 | redis | sentinel |
|---|---|---|
| 192.168.47.15(主) | 9400 | 29400 |
| 192.168.47.25(从) | 9401 | 29401 |
| 192.168.47.35(从) | 9402 | 29402 |
主redis.conf:
bind 0.0.0.0
protected-mode no # 这里一定要改成no
port 9400
daemonize yes
logfile “/var/log/redis/redis.log”
pidfile “/var/run/redis_9400.pid”
dbfilename “dump.rdb”
dir “/var/lib/redis”
requirepass “123456”
masterauth “123456”
…
从redis.conf
bind 0.0.0.0
protected-mode no
port 9401
daemonize yes
logfile “/var/log/redis/redis.log”
pidfile “/var/run/redis_9401.pid”
dbfilename “dump.rdb”
dir “/var/lib/redis”
requirepass “123456”
masterauth “123456”
slaveof 192.168.47.15 9400
…
3、哨兵配置
主sentinel.conf
port 29400
protected-mode no
daemonize yes
logfile “/var/log/redis/sentinel.log”
dir “/var/lib/redis”
sentinel monitor mymaster 192.168.47.15 9400 2
sentinel down-after-milliseconds mymaster 3000
sentinel auth-pass mymaster 123456
…
从sentinel.conf
port 29401
protected-mode no
daemonize yes
logfile “/var/log/redis/sentinel.log”
dir “/var/lib/redis”
sentinel monitor mymaster 192.168.47.15 9400 2
sentinel down-after-milliseconds mymaster 3000
sentinel auth-pass mymaster 123456
…
4、测试
redis-server /etc/redis/redis.conf
redis-sentinel /etc/redis/sentinel.conf
登录
redis-cli -p 9400
auth 123456
info replication
可以发现role为master,有两个从节点,主可以读写,从只能读。
shutdown # 停止主redis
查看从节点sentinel.log会发现哨兵已经发现主停掉了,并投票选举出了新的主。
原来主再启动,会变成从角色。
5、注意
-
所有配置文件都要设置protected-mode no
protected-mode 是3.2 之后加入的新特性,为了禁止公网访问redis cache,加强redis安全的。保护模式如果开启只接受回环地址的ipv4和ipv6地址链接,拒绝外部链接,如果哨兵配置开启会拒绝其他sentinel的连接,导致哨兵配置无法生效。 -
如果不设密码,所有redis.conf都不要设置requirepass和masterauth,sentinel.conf都不要设置auth-path
-
如果设置密码,所有redis.conf都要设置requirepass和masterauth,sentinel.conf都要设置auth-path
-
以上一定要注意,不然故障转移会出现问题。
本文详细介绍了Redis的安装步骤,包括依赖包安装、源码下载与编译,以及如何搭建主从复制和哨兵集群。通过配置redis.conf和sentinel.conf文件,实现了数据同步与故障转移,确保了系统的高可用性和数据一致性。
1583

被折叠的 条评论
为什么被折叠?



