开始部署之前,建议先按照Ubuntu切换到国内镜像源操作一下,能省下大把宝贵时间!
文章目录
单机部署
我们先从单机部署开始,练练手
sudo apt-get install redis-server
sudo vim /etc/redis/redis.conf
# bind 127.0.0.1
sudo systemctl restart redis-server
# 测试下我们的Redis
redis-cli
127.0.0.1:6379> keys *
1) "1"
哨兵集群部署
Redis
官方还推出了Redis-cluster
集群,不过阿晨还没玩过。后面补上。
下面演示一主二从
的Redis
哨兵模式部署
主机 | ip | redis-server | sentinel |
---|---|---|---|
node01 | 10.10.10.5 | 主 | √ |
node02 | 10.10.10.4 | 从 | √ |
node03 | 10.10.10.6 | 从 | √ |
安装Redis-Server
# 切换到node01
sudo apt-get install redis-server
sudo vim /etc/redis/redis.conf
bind: 10.10.10.5 # 本机内网IP
sudo service redis-server restart
# =====================================================
# 切换到node02
sudo apt-get install redis-server
sudo vim /etc/redis/redis.conf
bind: 10.10.10.4 # 本机内网IP
slaveof 10.10.10.5 # master节点内网IP
sudo service redis-server restart
# =====================================================
# 切换到node03
sudo apt-get install redis-server
sudo vim /etc/redis/redis.conf
bind: 10.10.10.6 # 本机内网IP
slaveof 10.10.10.5 # master节点内网IP
sudo service redis-server restart
# =====================================================
测试主从同步
# 切换到node01
redis-cli -h 10.10.10.5 -p 6379
10.10.10.5:6379>info
....
# Replication
role:master
connected_slaves:2
slave0:ip=10.10.10.4,port=6379,state=online,offset=99,lag=0
slave1:ip=10.10.10.6,port=6379,state=online,offset=99,lag=1
master_repl_offset:99
....
10.10.10.5:6379>set testkey testvalue
OK
10.10.10.5:6379>get testkey
"testvalue"
# =====================================================
# 切换到node02
redis-cli -h 10.10.10.4 -p 6379
10.9.8.203:6379>info
...
# Replication
role:slave
master_host:10.10.10.5
master_port:6379
master_link_status:up
...
10.10.10.4:6379>get testkey
"testvalue"
# =====================================================
# 切换到node03
# 操作和node02一样,不多赘述
配置 Sentinel
一个稳健的
Redis Sentinel
集群,应该使用至少 三个Sentinel
实例,并且保证将这些实例放到 不同的机器 上,甚至不同的 物理区域。
sudo wget http://download.redis.io/redis-stable/sentinel.conf -O /etc/redis/sentinel.conf
sudo chown redis:redis /etc/redis/sentinel.conf
sudo vim /etc/redis/sentinel.conf
sentinel monitor mymaster 10.10.10.5 6379 2 # 修改为你自己的master节点内网IP
sentinel down-after-milliseconds mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
## 自启动配置
sudo vim /etc/redis/sentinel.service
[Unit]
Documentation=http://redis.io/topics/sentinel
[Service]
ExecStart=/usr/bin/redis-server /etc/redis/sentinel.conf --sentinel
User=redis
Group=redis
[Install]
WantedBy=multi-user.target
sudo ln -s /etc/redis/sentinel.service /lib/systemd/system/sentinel.service
sudo systemctl enable sentinel.service
sudo service sentinel start
## node02 node03 sentinel 配置同node01,所有节点配置完成,再继续下一步
配置好
sentinel
之后,redis.conf
和sentinel.conf
都由sentinel
接管;sentinel
监控主节点发生改变的话,会更改对应的配置文件sentinel.conf
和redis.conf
。
测试Sentinel监控、通知、自动故障转移
# 查看所有节点哨兵配置
# node01,node02,node03
redis-cli -h 10.10.10.5 -p 26379
10.10.10.5:26379> info
# Server
redis_version:3.0.6
...
config_file:/etc/redis/sentinel.conf
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=10.10.10.5:6379,slaves=2,sentinels=1
# =====================================================
# 在从节点查看哨兵详情,关注主节点信息
redis-cli -h 10.10.10.4 -p 26379
10.10.10.5:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "10.10.10.5"
5) "port"
6) "6379"
...
# 停止主节点所在redis-server
# 切换到node01
systemctl stop redis-server.service
# 查看从节点的哨兵详情,一般来说,过1分钟~2分钟,会自动选举出新的主节点,例如node03被推举为主节点
# 切换到node02:
redis-cli -h 10.10.10.4 -p 26379
10.10.10.4:26379> info
...
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=10.10.10.6:6379,slaves=2,sentinels=3
# =====================================================
# 切换到node03
redis-cli -h 10.10.10.6 -p 6379
10.10.10.6:6379> info
...
# Replication
role:master
connected_slaves:1
slave0:ip=10.10.10.4,port=6379,state=online,offset=19874,lag=0
master_repl_offset:19874
# =====================================================
# 启动刚才被停止的原主节点redis-server,将作为从节点加入到redis集群
# 切换到node01
systemctl start redis-server
redis-cli -h 10.10.10.5 -p 6379
10.10.10.5:6379> info
...
# Replication
role:slave
master_host:10.10.10.6
master_port:6379
master_link_status:up
...
redis-cli -h 10.10.10.5 -p 26379
10.10.10.5:26379> info
...
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=10.10.10.6:6379,slaves=2,sentinels=3
客户端连接Sentinel
配置完sentinel
,客户端连接方式就改变了,拿Redisson举例,需要增加以下配置,并删除单机模式下spring.redis.host
配置,端口号改成哨兵的端口号
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=10.10.10.4:26379,10.10.10.5:26379,10.10.10.6:26379
引入的jar是
compile("org.redisson:redisson-spring-boot-starter:3.9.1")
配置类所在位置:
org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel
常用命令
sudo systemctl start redis
sudo systemctl enable redis
sudo systemctl restart
sudo systemctl stop redis
常见问题
1、有时可能会遇到关闭或重启不了,这时候可以使用redis-cli
提供的命令行来强制关闭
redis-cli -h 10.10.10.5 -p 6379
10.10.10.5:6379> shutdown nosave
## 更多参考 https://redis.io/commands/SHUTDOWN
2、Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
Redis
被配置为保存数据库快照,但它目前不能持久化到硬盘。
vim /etc/sysctl.conf
## 添加一行
vm.overcommit_memory=1
sudo sysctl -p /etc/sysctl.conf
## 重启所有节点redis-server和sentinel
如果改好后,还不行,就需要查看下Redis
的dump
文件配置是不是被更改了
redis-cli -h 10.10.10.5
10.10.10.5:6379> CONFIG GET dbfilename
1) "dbfilename"
2) ".rdb" ## 默认是dump.rdb
10.10.10.5:6379> CONFIG GET dir
1) "dir"
2) "/var/spool/cron" ## 默认是dump.rdb
以上配置,如果不是自己更改的,则可怀疑是被黑客篡改了
- 检查
Redis
端口是否在公网开放,如果是,立马关闭 - 设置
Redis
访问密码 - 恢复
Redis
默认配置
vim /etc/redis/redis.conf
dbfilename "dump.rdb"
dir "/var/lib/redis"
service redis-server restart
# node01 node02 node03均按此修改并重启
## 了解更多 https://serverfault.com/questions/800295/redis-spontaneously-failed-failed-opening-rdb-for-saving-permission-denied
如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。
我是阿晨,在技术的道路上我们一起砥砺前行!