如果让你为开发、测试环境分别搭一套哨兵和集群模式的redis,你最快需要多久,或许你需要一天?2小时?事实是可以更短。 是的,你已经猜到了,用docker部署,真的只需要十几分钟。
一.准备工作
拉取redis镜像
运行如下命令:
docker pull redis
该命令拉取的镜像是官方镜像,当然你可以搜索其他的镜像,这里不做深入
查看镜像情况:
二.部署redis哨兵主从模式
什么是哨兵模式?--请自行百度
1、什么是docker compose?
Docker Compose 可以理解为将多个容器运行的方式和配置固化下来!
就拿最简单的例子来说吧,如果我们要为我们的应用容器准备一个 MySQL 容器和一个 Redis 容器,那么在每次启动时,我们先要将 MySQL 容器和 Redis 容器启动起来,再将应用容器运行起来。这其中还不要忘了在创建应用容器时将容器网络连接到 MySQL 容器和 Redis 容器上,以便应用连接上它们并进行数据交换。
这还不够,如果我们还对容器进行了各种配置,我们最好还得将容器创建和配置的命令保存下来,以便下次可以直接使用。
针对这种情况,我们就不得不引出在我们开发中最常使用的多容器定义和运行软件,也就是 Docker Compose 了。
2、编写reids主从docker-compose.yml
version: "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的目录,执行命令:
docker-compose up -d
-d表示后台运行
使用命令docker ps命令查看启动结果:
出现截图所示,表示运行成功
4.编写哨兵docker-compose.yml
version: "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
# 自定义集群名,其中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所在目录,执行命令:
docker-compose up -d
查看容器,可以看到哨兵和主从redis都起来了
6.1哨兵启动日志
上述日志中可以看出,哨兵监听master和slave节点
6.2关掉master节点
通过命令停止redis的master节点
docker stop redis-master
通过上述日志,我们可以看到sdown,odown,他们是什么意思呢?
sdown是主观宕机,就一个哨兵如果自己觉得一个master宕机了,那么就是主观宕机
odown是客观宕机,如果quorum数量的哨兵都觉得一个master宕机了,那么就是客观宕机
然后就是开始选举,从日志可以看出两个哨兵选择了同一个slave节点,这时候满足了我们配置最小投票数,那么这台slave就被选为新的master。
6.3重开master节点
上述日志表明哨兵检测到原master重新启动,将原master节点变成新master的从节点
三.部署redis集群模式
1、创建目录和文件
├── 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 配置文件
port 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 配置文件
version: "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 节点容器,随便哪个都行。
继续执行以下命令创建集群:
# 集群创建命令 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有很强的掌控力。
到此这篇关于5分钟教你实现用docker搭建Redis集群模式和哨兵模式的文章就介绍到这了,更多相关docker搭建Redis集群模式和哨兵模式内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/coderaniu/p/15352323.html