此例子采用单台服务器举例,如果多台服务器可以分别配置到对应服务器
1. redis 安装(redis 安装网上有大把例子这里就不做介绍)
2. 创建一个文件夹来存放redis 配置文件
命令:mkdir -p /var/redis/data/
3. 创建 6380、6381、6382 文件夹
命令: mkdir 6380 6381 8382
4. 配置 6380 的redis.conf (默认为master)
命令:vi /var/redis/data/6380/redis-6380.conf
port 6380
bind 192.168.1.19
requirepass andy1818
masterauth andy1818
appendonly yes
daemonize yes
logfile "6380.log"
dbfilename "dump-6380.rdb"
dir "/var/redis/data/6380"
5. 配置 6381 的redis.conf
命令:vi /var/redis/data/6381/redis-6381.conf
port 6381
bind 192.168.1.19
requirepass andy1818
masterauth andy1818
appendonly yes
daemonize yes
logfile "6381.log"
dbfilename "dump-6381.rdb"
dir "/var/redis/data/6381"
slaveof 192.168.1.19 6380
6. 配置 6382 的redis.conf
命令:vi /var/redis/data/6382/redis-6382.conf
port 6382
bind 192.168.1.19
requirepass andy1818
masterauth andy1818
appendonly yes
daemonize yes
logfile "6382.log"
dbfilename "dump-6382.rdb"
dir "/var/redis/data/6382"
slaveof 192.168.1.19 6380
7. 启动redis
命令:/usr/bin/redis-server /var/redis/data/6380/redis-6380.conf
/usr/bin/redis-server /var/redis/data/6381/redis-6381.conf
/usr/bin/redis-server /var/redis/data/6382/redis-6382.conf
8. 这样redis 集群就部署好了,下面我们部署哨兵(redis sentinel)。
命令: mkdir 28379 26380 26381
9. 配置 26379 的redis.conf
命令:vi /var/redis/data/redis-sentinel-26379.conf
port 26379
bind 192.168.1.19
dir "/var/redis/data/26379"
logfile "26379.log"
sentinel myid d8d089ec0d7ad1055bb9062212c11f3a305d942b
sentinel monitor mymaster 192.168.1.19 6380 2
protected-mode no
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster andy1818
daemonize yes
10. 配置 26380 的redis.conf
命令:vi /var/redis/data/26380/redis-sentinel-26380.conf
port 26380
bind 192.168.1.19
dir "/var/redis/data/26380"
logfile "26380.log"
sentinel myid 419aa7a2b9e923fd5fdfe764f4962d7ecccfd059
sentinel monitor mymaster 192.168.1.19 6380 2
protected-mode no
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster andy1818
daemonize yes
11. 配置 6382 的redis.conf
命令:vi /var/redis/data/26381/redis-sentinel-6381.conf
port 26381
bind 192.168.1.19
dir "/var/redis/data/26381"
logfile "26381.log"
sentinel myid cd1760e36f47bdfc7b090dbbec59f28cb83c910b
sentinel monitor mymaster 192.168.1.19 6380 2
protected-mode no
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster andy1818
daemonize yes
12 . 启动redis-sentinel
命令: /usr/bin/redis-sentinel /var/redis/data/26379/redis-sentinel-26379.conf
/usr/bin/redis-sentinel /var/redis/data/26380/redis-sentinel-26380.conf
/usr/bin/redis-sentinel /var/redis/data/26381/redis-sentinel-26381.conf
13 . redis哨兵部署完成,下面介绍一些redis 及哨兵相关的命令
# 登录redis
redis-cli -h 192.168.1.19 -p 6380
redis-cli -h 192.168.1.19 -p 6381
redis-cli -h 192.168.1.19 -p 6382
#登录redis-sentinel
redis-cli -h 192.168.1.19 -p 26379
redis-cli -h 192.168.1.19 -p 26380
redis-cli -h 192.168.1.19 -p 26381
# 查看redis身份信息
info replication
# 查看redis-sentinel
SENTINEL masters