如果让你为开发、测试环境分别搭一套哨兵和集群模式的 redis,你最快需要多久,或许你需要一天?2 小时?事实是可以更短。 是的,你已经猜到了,用 docker 部署,真的只需要十几分钟。
一.准备工作#
拉取 redis 镜像#
运行如下命令:
Copy Line-numbers language-nonedocker pull redis
复制代码
该命令拉取的镜像是官方镜像,当然你可以搜索其他的镜像,这里不做深入查看镜像情况:
二.部署 redis 哨兵主从模式#
什么是哨兵模式?--请自行百度
1、什么是 docker compose?#
Docker Compose 可以理解为将多个容器运行的方式和配置固化下来!
就拿最简单的例子来说吧,如果我们要为我们的应用容器准备一个 MySQL 容器和一个 Redis 容器,那么在每次启动时,我们先要将 MySQL 容器和 Redis 容器启动起来,再将应用容器运行起来。这其中还不要忘了在创建应用容器时将容器网络连接到 MySQL 容器和 Redis 容器上,以便应用连接上它们并进行数据交换。
这还不够,如果我们还对容器进行了各种配置,我们最好还得将容器创建和配置的命令保存下来,以便下次可以直接使用。
针对这种情况,我们就不得不引出在我们开发中最常使用的多容器定义和运行软件,也就是 Docker Compose 了。
2、编写 reids 主从 docker-compose.yml#
Copy Line-numbers language-ymlversion: '3.7'
services:
master:
image: redis
container_name: redis-master
restart: always
command: redis-server --requirepass redispwd --appendonly yes
ports:
- 6379:6379
volumes:
- ./data1:/data
slave1:
image: redis
container_name: redis-slave-1
restart: always
command: redis-server --slaveof redis-master 6379 --requirepass redispwd --masterauth redispwd --appendonly yes
ports:
- 6380:6379
volumes:
- ./data2:/data
slave2:
image: redis
container_name: redis-slave-2
restart: always
command: redis-server --slaveof redis-master 6379 --requirepass redispwd --masterauth redispwd --appendonly yes
ports:
- 6381:6379
volumes:
- ./data3:/data
复制代码
名词解释:
3、启动主从 redis#
进入 redis 对应的 docker-compose.yml 的目录,执行命令:
Copy Line-numbers language-nonedocker-compose up -d
复制代码
-d 表示后台运行使用命令 docker ps 命令查看启动结果:
出现截图所示,表示运行成功
4.编写哨兵 docker-compose.yml#
Copy Line-numbers language-ymlversion: '3.7'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
restart: always
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
restart: always
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
default:
external:
name: redis_default
复制代码
5.编写哨兵 sentinel.conf#
Copy Line-numbers language-none# 自定义集群名,其中172.19.0.3 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
port 26379
dir /tmp
sentinel monitor mymaster 172.19.0.3 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster redispwd
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
复制代码
将上述文件分别拷贝 3 份分别命名为 sentinel1.conf、sentinel2.conf、sentinel3.conf 与 docker-compose.yml 中的配置文件对应,然后放置和哨兵的 docker-compose.yml 在同一目录
6.启动哨兵#
进入哨兵 docker-compose.yml 所在目录,执行命令:
Copy Line-numbers language-nonedocker-compose up -d
复制代码
查看容器,可以看到哨兵和主从 redis 都起来了
6.1 哨兵启动日志
上述日志中可以看出,哨兵监听 master 和 slave 节点
6.2 关掉 master 节点
通过命令停止 redis 的 master 节点
Copy Line-numbers language-nonedocker stop redis-master
复制代码
通过上述日志,我们可以看到 sdown,odown,他们是什么意思呢?sdown 是主观宕机,就一个哨兵如果自己觉得一个 master 宕机了,那么就是主观宕机 odown 是客观宕机,如果 quorum 数量的哨兵都觉得一个 master 宕机了,那么就是客观宕机然后就是开始选举,从日志可以看出两个哨兵选择了同一个 slave 节点,这时候满足了我们配置最小投票数,那么这台 slave 就被选为新的 master。
6.3 重开 master 节点
上述日志表明哨兵检测到原 master 重新启动,将原 master 节点变成新 master 的从节点
三.部署 redis 集群模式#
1、创建目录和文件#
Copy Line-numbers language-none├── docker-compose.yml
├── redis-6371
│ ├── conf
│ │ └── redis.conf
│ └── data
├── redis-6372
│ ├── conf
│ │ └── redis.conf
│ └── data
├── redis-6373
│ ├── conf
│ │ └── redis.conf
│ └── data
├── redis-6374
│ ├── conf
│ │ └── redis.conf
│ └── data
├── redis-6375
│ ├── conf
│ │ └── redis.conf
│ └── data
└── redis-6376
├── conf
│ └── redis.conf
└── data
复制代码
2、redis.conf 配置文件#
Copy Line-numbers language-noneport 6371
cluster-enabled yes
cluster-config-file nodes-6371.conf
cluster-node-timeout 5000
appendonly yes
protected-mode no
requirepass 1234
masterauth 1234
cluster-announce-ip 10.12.12.10 # 这里是宿主机IP
cluster-announce-port 6371
cluster-announce-bus-port 16371
复制代码
每个节点的配置只需改变端口。
3、docker-compose 配置文件#
Copy Line-numbers language-noneversion: "3"
# 定义服务,可以多个
services:
redis-6371: # 服务名称
image: redis # 创建容器时所需的镜像
container_name: redis-6371 # 容器名称
restart: always # 容器总是重新启动
volumes: # 数据卷,目录挂载
- ./redis-6371/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6371/data:/data
ports:
- 6371:6371
- 16371:16371
command:
redis-server /usr/local/etc/redis/redis.conf
redis-6372:
image: redis
container_name: redis-6372
volumes:
- ./redis-6372/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6372/data:/data
ports:
- 6372:6372
- 16372:16372
command:
redis-server /usr/local/etc/redis/redis.conf
redis-6373:
image: redis
container_name: redis-6373
volumes:
- ./redis-6373/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6373/data:/data
ports:
- 6373:6373
- 16373:16373
command:
redis-server /usr/local/etc/redis/redis.conf
redis-6374:
image: redis
container_name: redis-6374
restart: always
volumes:
- ./redis-6374/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6374/data:/data
ports:
- 6374:6374
- 16374:16374
command:
redis-server /usr/local/etc/redis/redis.conf
redis-6375:
image: redis
container_name: redis-6375
volumes:
- ./redis-6375/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6375/data:/data
ports:
- 6375:6375
- 16375:16375
command:
redis-server /usr/local/etc/redis/redis.conf
redis-6376:
image: redis
container_name: redis-6376
volumes:
- ./redis-6376/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./redis-6376/data:/data
ports:
- 6376:6376
- 16376:16376
command:
redis-server /usr/local/etc/redis/redis.conf
复制代码
编写完成后使用 docker-compose up -d 启动容器 ,这里没有使用主机模式(host),而是使用 NAT 模式,因为主机模式可能导致外部客户端无法连接。
4、进入容器,创建集群#
上面只是启动了 6 个 Redis 实例,并没有构建成 Cluster 集群。执行 docker exec -it redis-6371 bash 进入一个 Redis 节点容器,随便哪个都行。继续执行以下命令创建集群:
Copy Line-numbers language-none# 集群创建命令
redis-cli -a 1234 --cluster create 10.35.30.39:6371 10.35.30.39:6372 10.35.30.39:6373 10.35.30.39:6374 10.35.30.39:6375 10.35.30.39:6376 --cluster-replicas 1
# 执行过后会有以下输出
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.35.30.39:6375 to 10.35.30.39:6371
Adding replica 10.35.30.39:6376 to 10.35.30.39:6372
Adding replica 10.35.30.39:6374 to 10.35.30.39:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: e9a35d6a9d203830556de89f06a3be2e2ab4eee1 10.35.30.39:6371
slots:[0-5460] (5461 slots) master
M: 0c8755144fe6a200a46716371495b04f8ab9d4c8 10.35.30.39:6372
slots:[5461-10922] (5462 slots) master
M: fcb83b0097d2a0a87a76c0d782de12147bc86291 10.35.30.39:6373
slots:[10923-16383] (5461 slots) master
S: b9819797e98fcd49f263cec1f77563537709bcb8 10.35.30.39:6374
replicates fcb83b0097d2a0a87a76c0d782de12147bc86291
S: f4660f264f12786d81bcf0b18bc7287947ec8a1b 10.35.30.39:6375
replicates e9a35d6a9d203830556de89f06a3be2e2ab4eee1
S: d2b9f265ef7dbb4a612275def57a9cc24eb2fd5d 10.35.30.39:6376
replicates 0c8755144fe6a200a46716371495b04f8ab9d4c8
Can I set the above configuration? (type 'yes' to accept): yes # 这里输入 yes 并回车 确认节点 主从身份 以及 哈希槽的分配
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 10.35.30.39:6371)
M: e9a35d6a9d203830556de89f06a3be2e2ab4eee1 10.35.30.39:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 0c8755144fe6a200a46716371495b04f8ab9d4c8 10.35.30.39:6372
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b9819797e98fcd49f263cec1f77563537709bcb8 10.35.30.39:6374
slots: (0 slots) slave
replicates fcb83b0097d2a0a87a76c0d782de12147bc86291
M: fcb83b0097d2a0a87a76c0d782de12147bc86291 10.35.30.39:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: f4660f264f12786d81bcf0b18bc7287947ec8a1b 10.35.30.39:6375
slots: (0 slots) slave
replicates e9a35d6a9d203830556de89f06a3be2e2ab4eee1
S: d2b9f265ef7dbb4a612275def57a9cc24eb2fd5d 10.35.30.39:6376
slots: (0 slots) slave
replicates 0c8755144fe6a200a46716371495b04f8ab9d4c8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
复制代码
看到上面的输出即为 Cluster 集群配置完成。且为 3 主 3 从。
总结:#
以上就是通过 docker compose 方式部署哨兵模式和集群模式的全过程,redis 部署在 docker 中,适用于本地、开发、测试等环境,生产环境请慎用,除非你对 docker 有很强的掌控力。
来源;https://www.cnblogs.com/coderaniu/p/15352323.html
评论