Docker 搭建 redis 集群
本文搭建的集群一共有三个小集群,即3个master节点,每个master节点有2两个slave节点
拉取redis镜像
docker pull redis
初始化swarm 集群
docker swarm init
创建通信网络
docker network create -d overlay redis-net
配置docker-compose
version: '3'
services:
redis-1:
image: redis
command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes --cluster-enabled yes
volumes:
- ./redis1.conf:/usr/local/etc/redis/redis.conf
ports:
- "6379:6379"
networks:
redis-net:
aliases:
- redis-1
deploy:
replicas: 1
placement:
constraints:
- node.labels.type == redis
redis-2:
image: redis
command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes --cluster-enabled yes
volumes:
- ./redis2.conf:/usr/local/etc/redis/redis.conf
ports:
- "6380:6379"
networks:
redis-net:
aliases:
- redis-2
deploy:
replicas: 1
placement:
constraints:
- node.labels.type == redis
redis-3:
image: redis
command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes --cluster-enabled yes
volumes:
- ./redis3.conf:/usr/local/etc/redis/redis.conf
ports:
- "6381:6379"
networks:
redis-net:
aliases:
- redis-3
deploy:
replicas: 1
placement:
constraints:
- node.labels.type == redis
redis-4:
image: redis
command: redis-server /usr/local/etc/redis/redis-slave.conf --appendonly yes --cluster-enabled yes
volumes:
- ./redis-slave.conf:/usr/local/etc/redis/redis-slave.conf
networks:
redis-net:
aliases:
- redis-4
deploy:
replicas: 2
placement:
constraints:
- node.labels.type == redis
redis-5:
image: redis
command: redis-server /usr/local/etc/redis/redis-slave.conf --appendonly yes --cluster-enabled yes
volumes:
- ./redis-slave.conf:/usr/local/etc/redis/redis-slave.conf
networks:
redis-net:
aliases:
- redis-5
deploy:
replicas: 2
placement:
constraints:
- node.labels.type == redis
redis-6:
image: redis
command: redis-server /usr/local/etc/redis/redis-slave.conf --appendonly yes --cluster-enabled yes
volumes:
- ./redis-slave.conf:/usr/local/etc/redis/redis-slave.conf
networks:
redis-net:
aliases:
- redis-6
deploy:
replicas: 2
placement:
constraints:
- node.labels.type == redis
networks:
redis-net:
attachable: true
redis 配置文件
配置文件的放在上面docker-compose.yml的同级目录下
bind 0.0.0.0
protected-mode no
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
部署 Docker stack
命令的作用是使用 Docker Compose 部署一个 Docker stack。它会根据 docker-compose.yml 文件中定义的服务和配置信息,在 Docker swarm 集群中启动一个 Docker stack,包含了多个服务。在这个例子中,我们使用它来启动一个 Redis 集群,其中包含了多个 Redis 实例的容器。
docker stack deploy -c docker-compose.yml redis-cluster
启动redis容器
docker-compose up
获取各个redis容器的ip
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
结果:
构建集群
## 先进入一个容器内部
docker exec -it 1333df3ca93c1cbab0a3e2ad60a8cdb2fa651025c1d7d6dfec351bca26b465e3 bash
## 构建集群 后面的ip和端口是上面获取到的ip信息
redis-cli --cluster create --cluster-replicas 2 192.168.0.2:6379 192.168.0.3:6379 192.168.0.4:6379 192.168.0.5:6379 192.168.0.6:6379 192.168.0.7:6379 192.168.0.8:6379 192.168.0.9:6379 192.168.0.10:6379
查看集群信息
先连接任何一个redis客户端
然后执行命令:
cluster info
如果搭建成功,结果如下
查看节点信息
cluster nodes