1 使用docker下载redis镜像,默认下载最redis最新版本,目前版本号为5.0.0,如果需要其他版本请登录https://hub.docker.com/进行搜索
[root@localhost ~]# docker pull redis
2 下载完毕后分别创建/home/docker/redis/data、redis-6379-data、redis-6380-data、redis-6381-data、sentinel-26379-data、sentinel-26380-data、sentinel-26381-data这十个文件夹
[root@localhost ~]# cd /home
[root@localhost home]# mkdir docker
[root@localhost home]# cd docker
[root@localhost docker]# mkdir redis
[root@localhost docker]# cd redis
[root@localhost redis]# mkdir redis-6379-data
[root@localhost redis]# mkdir redis-6380-data
[root@localhost redis]# mkdir redis-6381-data
[root@localhost redis]# mkdir sentinel-26379-data
[root@localhost redis]# mkdir sentinel-26380-data
[root@localhost redis]# mkdir sentinel-26381-data
3 再创建redis-6379.conf、redis-6380.conf、redis-6381.conf这三个自定义redis配置文件,用来配置redis,其中redis-6379.conf是master服务器配置文件,redis-6380.conf、redis-6381.conf 是slave服务器配置文件
[root@localhost redis]# touch redis-6379.conf
[root@localhost redis]# touch redis-6380.conf
[root@localhost redis]# touch redis-6381.conf
4 vi编辑自定义redis配置文件,复制下列代码
4.1 redis-6379.conf文件复制:
port 6379
logfile "redis-6379.log"
dir /data
appendonly yes
appendfilename appendonly.aof
masterauth 123456
requirepass 123456
4.2 redis-6380.conf文件复制:
port 6380
logfile "redis-6380.log"
dir /data
appendonly yes
appendfilename appendonly.aof
slaveof 192.168.43.188 6379
masterauth 123456
requirepass 123456
4.2 redis-6381.conf文件复制:
port 6381
logfile "redis-6381.log"
dir /data
appendonly yes
appendfilename appendonly.aof
slaveof 192.168.43.188 6379
masterauth 123456
requirepass 123456
5 按Esc后,输入wq保存退出
6 启动三个redis容器
[root@localhost redis]# docker run -p 6379:6379 --net=host --restart=always --name redis-6379 -v /home/docker/redis/redis-6379.conf:/etc/redis/redis-6379.conf -v /home/docker/redis/redis-6379-data:/data -d redis redis-server /etc/redis/redis-6379.conf
[root@localhost redis]# docker run -p 6380:6380 --net=host --restart=always --name redis-6380 -v /home/docker/redis/redis-6380.conf:/etc/redis/redis-6380.conf -v /home/docker/redis/redis-6380-data:/data -d redis redis-server /etc/redis/redis-6380.conf
[root@localhost redis]# docker run -p 6381:6381 --net=host --restart=always --name redis-6381 -v /home/docker/redis/redis-6381.conf:/etc/redis/redis-6381.conf -v /home/docker/redis/redis-6381-data:/data -d redis redis-server /etc/redis/redis-6381.conf
6.1 以下为启动命令说明和注意事项,属于延伸阅读,有时间和兴趣可以看一下,不属于配置过程,执行配置请直接跳到步骤 7
6.1.1 命令参数:-v /home/docker/redis/redis-6379-data:/data 让我们回顾一下,刚才在自定义redis配置文件中还指定了dir/data,dir表示数据存储目录,现在回到该命令中来,该命令是将redis容器中,redis配置文件中指定的数据存储目录/data下文件的内容共享到宿主机/home/docker/redis/redis-6379-data目录下
6.1.1.1 为何必须挂载 /data 目录?有状态容器都有数据持久化需求,在容器的生命周期内,数据持久化是持续的,包括容器在被停止后,但当容器被删除后,数据也随之被删除了,因此Docker 采用 volume (卷)的形式来向容器提供持久化存储,如果不设置该命令,数据库中的数据会默认保存在redis容器中的/data目录下,这样当执行 docker rm 容器id/容器name 命令,会丢失数据库中的数据
7 启动redis容器以后查看redis容器状态
[root@localhost redis]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
19fb5e8cd1b0 redis "docker-entrypoint..." 4 seconds ago Up 2 seconds
7.1 如果STATUS状态是Up表示成功启动容器,如果STATUS状态是Exited (1) 或者 Restarting (1) 表示未能正常启动,这时候我们需要查看redis容器日志修改配置文件内容重新运行容器
[root@localhost redis]# docker logs redis-6379|less
7.2 查找出问题后停止redis容器
[root@localhost redis]# docker stop redis-6379
7.3 删除redis容器
[root@localhost redis]# docker rm redis-6379
7.3 重新执行步骤 6
8 启动redis容器后,分别观察三个redis容器内部情况
[root@localhost redis]# docker exec -it redis-6379 /bin/bash
root@zhangqian527halbin:/data# redis-cli
127.0.0.1:6379> auth 123456
ok
127.0.0.1:6379> info replication
8.1 主机显示
# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.1,port=6379,state=online,offset=707,lag=0
slave1:ip=172.17.0.1,port=6379,state=online,offset=707,lag=0
master_replid:db3135b2f4cba8e6c1eb4be290b9dbfc2ec3b6d0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:707
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:707
8.2 两台从机显示
role:slave
master_host:192.168.43.188
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:833
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:db3135b2f4cba8e6c1eb4be290b9dbfc2ec3b6d0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:833
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:833
8.3 看到主机role:master、connected_slaves:2,两台从机role:slave、master_host有地址,可以确认redis主从模式已经建立成功
9 使用RedisDesktopManager连接三个redis进行测试
9.1 右键redis-6379选择Console,控制台输入redis命令
9.2 分别右键redis-6380、redis-6381选择Reload后,可以看到这两个redis自动同步了数据,说明redis主从模式配置成功
10 配置哨兵模式,主机上创建sentinel-26379.conf、sentinel-26380。c哨兵配置文件
[root@localhost redis]# touch sentinel-26379.conf
[root@localhost redis]# touch sentinel-26380.conf
[root@localhost redis]# touch sentinel-26381.conf
11 vi编辑自定义redis配置文件,复制下列代码
复制后一定要对比原文,因为编辑器带格式,复制时容易落下部分内容,复制分后一定要对比原文!!!
4.1 sentinel-26379.conf文件复制:
port 26379
dir "/data"
logfile "sentinel-26379.log"
sentinel monitor mymaster 192.168.43.188 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster 123456
4.2 sentinel-26380.conf文件复制:
port 26380
dir "/data"
logfile "sentinel-26380.log"
sentinel monitor mymaster 192.168.43.188 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster 123456
4.3 sentinel-26381.conf文件复制:
port 26381
dir "/data"
logfile "sentinel-26381.log"
sentinel monitor mymaster 192.168.43.188 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster 123456
12 启动哨兵模式:
[root@local redis]# docker run -p 26379:26379 --restart=always --name sentinel-26379 -v/home/docker/redis/sentinel-26379.conf:/etc/redis/sentinel.conf -v /home/docker/redis/sentinel-26379-data:/data -d redis redis-sentinel /etc/redis/sentinel.conf
[root@local redis]# docker run -p 26380:26380 --restart=always --name sentinel-26380 -v/home/docker/redis/sentinel-26380.conf:/etc/redis/sentinel.conf -v /home/docker/redis/sentinel-26380-data:/data -d redis redis-sentinel /etc/redis/sentinel.conf
[root@local redis]# docker run -p 26381:26381 --restart=always --name sentinel-26381 -v/home/docker/redis/sentinel-26381.conf:/etc/redis/sentinel.conf -v /home/docker/redis/sentinel-26381-data:/data -d redis redis-sentinel /etc/redis/sentinel.conf
13 启动哨兵模式以后查看哨兵容器状态
[root@localhost redis]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
19fb5e8cd1b0 redis "docker-entrypoint..." 4 seconds ago Up 2 seconds
13.1 如果STATUS状态是Up表示成功启动哨兵容器,如果不是请重新执行步骤 7.1
14 至此,哨兵模式已经建立起来,查看哨兵信息
[root@localhost redis]# docker exec -it sentinel-26379 /bin/bash
root@zhangqian527halbin:/data# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.43.188:6379,slaves=2,sentinels=3
15 可以看到,现在端口号为:6379的redis服务器是master服务器,这时我们关闭端口号为6379的redis-6379容器测试哨兵配置是否生效
[root@localhost redis]# docker stop redis-6379
16 然后再查看哨兵信息,哨兵模式下,当master服务器宕机之后,哨兵自动会在从slave redis服务器里面投票选举一个master服务器来,这个master服务器也可以进行读写操作
[root@localhost redis]# docker exec -it sentinel-26379 /bin/bash
root@zhangqian527halbin:/data# redis-cli -p 26379
127.0.0.1:26379> info sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.43.188:6380,slaves=2,sentinels=3
17 可以看到端口号为:6380的redis服务器已经变成master服务器了,说明哨兵配置成功