共计 7401 个字符,预计需要花费 19 分钟才能阅读完成。
如果让你为开发、测试环境别离搭一套哨兵和集群模式的 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 有很强的掌控力。