在docker中部署redis主从配置
环境说明:
- 阿里云服务器
- Ubuntu 16.04
- docker
最终生成目录如下:
root@iZ282zj76jqZ:/docker/redis# tree
.
├── redis6379
│ ├── appendonly.aof
│ ├── dump.rdb
│ ├── red2.so
│ └── redis.conf
├── redis6380
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── redis.conf
├── redis6381
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── redis.conf
├── redis6382
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── redis.conf
├── redis.conf
├── sentinel
│ └── sentinel.conf
└── sentinel.conf
1、拉取Redis镜像
docker pull redis
2、配置Redis启动配置文件,此处我创建一个专用目录,存放Redis相关数据及配置
mkdir /docker/redis
在此目录下下载服务启动配置文件
wget http://download.redis.io/redis-stable/redis.conf
下载后,进入文件修改下面3个参数(在非编辑状态下,采用 /bind 来搜索字符串位置 )
vim /docker/redis/redis.conf
# 修改下面三个配置参数 #bind 127.0.0.1 #如果bind选项为空的话,则允许所有来自于可用网络接口的连接 protected-mode no #保护模式,若为yes,只允许本地客户端连接 appendonly yes #开启后,Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件,每次启动时Redis都会先把这个文件的数据读入内存里
3、在此目录下新建Master(redis6379)文件夹,并复制上述conf文件至文件夹中, Master的 conf 文件路径为
/docker/redis/redis6379/redis.conf
4、利用 conf 创建 Master 容器
# --restart always (docker启动时,本镜像也自动启动) # --name 容器名为 redis-6379 # -p 映射本地端口 6379 至容器端口 6379 # -v 绑定Master目录 /docker/redis/redis6379 映射至 docker 中的 /data # -d 后台运行容器,并返回容器ID # 指定镜像 redis # 利用 conf 启动服务 redis-server /data/redis.conf docker run --restart always --name redis-6379 -p 6379:6379 -v /docker/redis/redis6379:/data -d redis redis-server /data/redis.conf
5、查看容器启动情况
root@iZ282zj76jqZ:/docker/redis/redis6379# docker ps |grep redis-6379 792067d225e6 redis "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:6379->6379/tcp redis-6379
查看 Master 容器的IP,用于配置 Salve 中的参数
# 查看 Master 容器 IP,
# docker inspect redis-6379 查看 NetworkSettings 配置中 IPAdress docker inspect --format='{{.NetworkSettings.IPAddress}}' redis-6379 # 192.168.0.3
6、创建 Salve 文件夹(redis6380 、redis6381 、 redis6382),复制上述 redis.conf 文件分别至三个夹中,平修改配置
vim /docker/redis/redis.conf # 修改下面四个配置参数 #bind 127.0.0.1 #如果bind选项为空的话,则允许所有来自于可用网络接口的连接 protected-mode no #保护模式,若为yes,只允许本地客户端连接 appendonly yes #开启后,Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件,每次启动时Redis都会先把这个文件的数据读入内存里 # replicaof <master ip> <master port> replicaof 192.168.0.3 6379 #Redis主机(Master)IP 端口
启动三个 Salve 服务
docker run --restart always --name redis-6380 -p 6380:6379 -v /docker/redis/redis6380:/data -d redis redis-server /data/redis.conf docker run --restart always --name redis-6381 -p 6381:6379 -v /docker/redis/redis6381:/data -d redis redis-server /data/redis.conf docker run --restart always --name redis-6382 -p 6382:6379 -v /docker/redis/redis6382:/data -d redis redis-server /data/redis.conf
7、查看容器服务
root@iZ282zj76jqZ:/docker/redis# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 43488a8c668c redis "docker-entrypoint.s…" 3 seconds ago Up 1 second 0.0.0.0:6382->6379/tcp redis-6382 ca3caaa77f4a redis "docker-entrypoint.s…" 2 hours ago Up About an hour 0.0.0.0:6381->6379/tcp redis-6381 792067d225e6 redis "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:6379->6379/tcp redis-6379 979318c633be redis "docker-entrypoint.s…" 2 hours ago Up About an hour 0.0.0.0:6380->6379/tcp redis-6380
1)进入 Master 容器操作 Redis
root@iZ282zj76jqZ:/docker/redis# docker exec -it redis-6379 redis-cli info replication # Replication role:master connected_slaves:3 slave0:ip=192.168.0.4,port=6379,state=online,offset=812,lag=0 slave1:ip=192.168.0.5,port=6379,state=online,offset=812,lag=0 slave2:ip=192.168.0.6,port=6379,state=online,offset=812,lag=0 master_replid:97bf611eb2ac66774c98f9e1d6303906de0e72ca master_replid2:0000000000000000000000000000000000000000 master_repl_offset:812 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:812
2)进入Salve 容器操作Redis
root@iZ282zj76jqZ:/docker/redis# docker exec -it redis-6380 redis-cli info replication # Replication role:slave master_host:192.168.0.3 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:742 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:97bf611eb2ac66774c98f9e1d6303906de0e72ca master_replid2:0000000000000000000000000000000000000000 master_repl_offset:742 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:742
8、也可以直接用docker命令操作Redis
# -it 为容器重新分配一个伪输入终端,以交互模式运行容器 # 容器名 redis-6379 # redis-cli set Key Value docker exec -it redis-6379 redis-cli set name tomkluas
9、设置哨兵,配置哨兵文件
创建 sentinel.conf 配置文件
# 主节点信息,格式:sentinel <master-name> <ip> <redis-port> <quorum>; # <master-name> 自定义主节点名称; # <ip> <redis-port> 主节点的ip和端口; # <quorum> 多少个主节点检测到主节点有问题就进行故障转移 sentinel monitor mygroup 192.168.0.3 6379 1 port 26379
然后在启动一个容器监听服务 (应该启动3个哨兵,此处为一个,其他同样启动)
docker run --restart always --name sentinel-0 -p 26379:26379 -v /docker/redis/sentinel0:/data -d redis redis-sentinel /data/sentinel.conf
root@iZ282zj76jqZ:/docker/redis# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9ef307e6254b redis "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 6379/tcp, 0.0.0.0:26379->26379/tcp sentinel-1
root@iZ282zj76jqZ:/docker/redis# docker logs sentinel-0 1:X 12 Apr 2020 12:53:47.120 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:X 12 Apr 2020 12:53:47.120 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=1, just started 1:X 12 Apr 2020 12:53:47.120 # Configuration loaded 1:X 12 Apr 2020 12:53:47.121 * Running mode=sentinel, port=26379. 1:X 12 Apr 2020 12:53:47.121 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:X 12 Apr 2020 12:53:47.123 # Sentinel ID is 35ac9959501bbaf6fd40743be0b62e8af6547fec 1:X 12 Apr 2020 12:53:47.123 # +monitor master mygroup 192.168.0.3 6379 quorum 1 1:X 12 Apr 2020 12:53:47.124 * +slave slave 192.168.0.4:6379 192.168.0.4 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:53:47.133 * +slave slave 192.168.0.5:6379 192.168.0.5 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:53:47.135 * +slave slave 192.168.0.6:6379 192.168.0.6 6379 @ mygroup 192.168.0.3 6379
10、测试哨兵监听, 主动停止Master
docker stop redis-6379
查看新的集群情况
root@iZ282zj76jqZ:/docker/redis# docker exec -it redis-6382 redis-cli info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.0.5,port=6379,state=online,offset=22842,lag=0 slave1:ip=192.168.0.4,port=6379,state=online,offset=22706,lag=0 master_replid:9f4af9b24738305e99aa3fe6b8062e3cabb531f9 master_replid2:97bf611eb2ac66774c98f9e1d6303906de0e72ca master_repl_offset:22842 second_repl_offset:16152 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:393 repl_backlog_histlen:22450
查看哨兵log
root@iZ282zj76jqZ:/docker/redis# docker logs sentinel-0 1:X 12 Apr 2020 12:53:47.120 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:X 12 Apr 2020 12:53:47.120 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=1, just started 1:X 12 Apr 2020 12:53:47.120 # Configuration loaded 1:X 12 Apr 2020 12:53:47.121 * Running mode=sentinel, port=26379. 1:X 12 Apr 2020 12:53:47.121 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:X 12 Apr 2020 12:53:47.123 # Sentinel ID is 35ac9959501bbaf6fd40743be0b62e8af6547fec 1:X 12 Apr 2020 12:53:47.123 # +monitor master mygroup 192.168.0.3 6379 quorum 1 1:X 12 Apr 2020 12:53:47.124 * +slave slave 192.168.0.4:6379 192.168.0.4 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:53:47.133 * +slave slave 192.168.0.5:6379 192.168.0.5 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:53:47.135 * +slave slave 192.168.0.6:6379 192.168.0.6 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:52.477 # +sdown master mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:52.477 # +odown master mygroup 192.168.0.3 6379 #quorum 1/1 1:X 12 Apr 2020 12:57:52.477 # +new-epoch 1 1:X 12 Apr 2020 12:57:52.477 # +try-failover master mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:52.498 # +vote-for-leader 35ac9959501bbaf6fd40743be0b62e8af6547fec 1 1:X 12 Apr 2020 12:57:52.498 # +elected-leader master mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:52.498 # +failover-state-select-slave master mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:52.582 # +selected-slave slave 192.168.0.6:6379 192.168.0.6 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:52.582 * +failover-state-send-slaveof-noone slave 192.168.0.6:6379 192.168.0.6 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:52.648 * +failover-state-wait-promotion slave 192.168.0.6:6379 192.168.0.6 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:53.321 # +promoted-slave slave 192.168.0.6:6379 192.168.0.6 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:53.321 # +failover-state-reconf-slaves master mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:53.368 * +slave-reconf-sent slave 192.168.0.5:6379 192.168.0.5 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:54.365 * +slave-reconf-inprog slave 192.168.0.5:6379 192.168.0.5 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:54.365 * +slave-reconf-done slave 192.168.0.5:6379 192.168.0.5 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:54.418 * +slave-reconf-sent slave 192.168.0.4:6379 192.168.0.4 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:55.399 * +slave-reconf-inprog slave 192.168.0.4:6379 192.168.0.4 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:55.399 * +slave-reconf-done slave 192.168.0.4:6379 192.168.0.4 6379 @ mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:55.488 # +failover-end master mygroup 192.168.0.3 6379 1:X 12 Apr 2020 12:57:55.488 # +switch-master mygroup 192.168.0.3 6379 192.168.0.6 6379 # 切换Master 1:X 12 Apr 2020 12:57:55.489 * +slave slave 192.168.0.5:6379 192.168.0.5 6379 @ mygroup 192.168.0.6 6379 1:X 12 Apr 2020 12:57:55.489 * +slave slave 192.168.0.4:6379 192.168.0.4 6379 @ mygroup 192.168.0.6 6379 1:X 12 Apr 2020 12:57:55.489 * +slave slave 192.168.0.3:6379 192.168.0.3 6379 @ mygroup 192.168.0.6 6379 1:X 12 Apr 2020 12:58:25.545 # +sdown slave 192.168.0.3:6379 192.168.0.3 6379 @ mygroup 192.168.0.6 6379