- 安装docker
- 编译redis
- 下载最新5.xredis
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
tar -zxvf redis-5.0.3.tar.gz
cd redis-5.0.3
make
- 获取redis-cli 和redis.conf
- 修redis.conf
#注释掉
#bind 127.0.0.1
#修改为no
protected-mode no
#修改为yes
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
#修改为yes
cluster-enabled yes
- 创建 docker-compose.yml
注意
- docker容器要使用固定ip
- redis集群不支持NAT模式
- 开放两个端口号,集群端口号为redis端口号+10000
- 修改command,执行启动时配置文件
version: '2'
services:
redis1:
image: redis:5.0.3
command: ["redis-server","/usr/local/etc/redis/redis.conf"]
restart: always
container_name: redis1
ports:
- "6301:6379"
- "16301:16379"
volumes:
- /data/redis/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/data1:/data
networks:
redis_net:
ipv4_address: 172.22.0.11
redis2:
image: redis:5.0.3
command: ["redis-server","/usr/local/etc/redis/redis.conf"]
restart: always
container_name: redis2
ports:
- "6302:6379"
- "16302:16379"
volumes:
- /data/redis/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/data2:/data
networks:
redis_net:
ipv4_address: 172.22.0.12
redis3:
image: redis:5.0.3
command: ["redis-server","/usr/local/etc/redis/redis.conf"]
restart: always
container_name: redis3
ports:
- "6303:6379"
- "16303:16379"
volumes:
- /data/redis/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/data3:/data
networks:
redis_net:
ipv4_address: 172.22.0.13
redis4:
image: redis:5.0.3
command: ["redis-server","/usr/local/etc/redis/redis.conf"]
restart: always
container_name: redis4
ports:
- "6304:6379"
- "16304:16379"
volumes:
- /data/redis/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/data4:/data
networks:
redis_net:
ipv4_address: 172.22.0.14
redis5:
image: redis:5.0.3
command: ["redis-server","/usr/local/etc/redis/redis.conf"]
restart: always
container_name: redis5
ports:
- "6305:6379"
- "16305:16379"
volumes:
- /data/redis/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/data5:/data
networks:
redis_net:
ipv4_address: 172.22.0.15
redis6:
image: redis:5.0.3
command: ["redis-server","/usr/local/etc/redis/redis.conf"]
restart: always
container_name: redis6
ports:
- "6306:6379"
- "16306:6379"
volumes:
- /data/redis/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/data6:/data"
networks:
redis_net:
ipv4_address: 172.22.0.16
redis7:
image: redis:5.0.3
command: ["redis-server","/usr/local/etc/redis/redis.conf"]
restart: always
container_name: redis7
ports:
- "6307:6379"
- "16307:16379"
volumes:
- /data/redis/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/data7:/data
networks:
redis_net:
ipv4_address: 172.22.0.17
redis8:
image: redis:5.0.3
command: ["redis-server","/usr/local/etc/redis/redis.conf"]
restart: always
container_name: redis8
ports:
- "6308:6379"
- "16308:16379"
volumes:
- /data/redis/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/data8:/data
networks:
redis_net:
ipv4_address: 172.22.0.18
networks:
redis_net:
ipam:
config:
- subnet: 172.22.0.0/16
gateway: 172.22.0.1
查看容器ip
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
- 创建 集群
cluster-replicas 副本数量
ip要使用容器的ip不能使用映射端口号,redis 目前(5.0.3)不支持NAT集群
redis-cli --cluster create 172.22.0.11:6379 172.22.0.12:6379 172.22.0.13:6379 172.22.0.14:6379 172.22.0.15:6379 172.22.0.16:6379 --cluster-replicas 1
- 查看集群信息
./redis-cli -c -h 127.0.0.1 -p 6301
127.0.0.1:6301> CLUSTER nodes
c263c43e4f4c7b77baa9cad7befcd7d95b59e604 172.22.0.14:6379@16379 slave c87c0343330eaea324e322717093a7472c7a1c61 0 1546923044864 4 connected
91870f32b9697c8176c4a9f73c2ea2b1a0b5ac98 172.22.0.12:6379@16379 master - 0 1546923043861 2 connected 5461-10922
c87c0343330eaea324e322717093a7472c7a1c61 172.22.0.11:6379@16379 myself,master - 0 1546923041000 1 connected 0-5460
ec06c901c1c52140856c9cb6450bb07e601a6dff 172.22.0.15:6379@16379 slave 91870f32b9697c8176c4a9f73c2ea2b1a0b5ac98 0 1546923042858 5 connected
4f9f32bcc9f26da82d04e7f110a994f9f989e390 172.22.0.16:6379@16379 slave de6458c693e2e11014615f22a92b89ead28eb8c5 0 1546923043000 6 connected
de6458c693e2e11014615f22a92b89ead28eb8c5 172.22.0.13:6379@16379 master - 0 1546923041000 3 connected 10923-16383
网友评论