主從模式
目錄結(jié)構(gòu)
.
├── docker-compose.yml
├── master
│ ├── Dockerfile
│ └── redis.conf
├── slave1
│ ├── Dockerfile
│ └── redis.conf
└── slave2
├── Dockerfile
└── redis.conf
- vim docker-compose.yml
version: "3"
networks:
net-redis:
driver: bridge
services:
master:
build:
context: "./master"
ports:
- 6300:6379
networks:
- net-redis
slave1:
build:
context: "./slave1"
ports:
- 6301:6379
depends_on:
- master
networks:
- net-redis
slave2:
build:
context: "./slave2"
ports:
- 6302:6379
depends_on:
- master
networks:
- net-redis
- 修改主從 Dockerfile
- vim master/Dockerfile
- vim slave1/Dockerfile
- vim slave2/Dockerfile
FROM redis:5.0.4
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
EXPOSE 6379
- 修改 主redis配置 vim master/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
- 修改 從redis配置 vim slave1/redis.conf , vim slave2/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
replicaof master 6379
- 啟動 docker-compose up --build -d
- 測試, 連接redids, 查看主從信息
local_master:0>info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.25.0.4,port=6379,state=online,offset=308,lag=1
slave1:ip=172.25.0.3,port=6379,state=online,offset=308,lag=1
master_replid:6a4caf243618274f89de2bd0e4c734627fe00fd2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:308
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:308
local_master:0>
redis哨兵模式
目錄結(jié)構(gòu)
├── docker-compose.yml
├── master
│ ├── Dockerfile
│ └── redis.conf
├── sentinel1
│ ├── Dockerfile
│ └── sentinel.conf
├── sentinel2
│ ├── Dockerfile
│ └── sentinel.conf
├── sentinel3
│ ├── Dockerfile
│ └── sentinel.conf
├── slave1
│ ├── Dockerfile
│ └── redis.conf
└── slave2
├── Dockerfile
└── redis.conf
- vim docker-compose.yml
version: "3"
networks:
net-redis:
driver: bridge
services:
master:
build:
context: "./master"
ports:
- 6300:6379
networks:
- net-redis
slave1:
build:
context: "./slave1"
ports:
- 6301:6379
depends_on:
- master
networks:
- net-redis
slave2:
build:
context: "./slave2"
ports:
- 6302:6379
depends_on:
- master
networks:
- net-redis
sentinel1:
build:
context: "./sentinel1"
ports:
- 26301:26379
networks:
- net-redis
sentinel2:
build:
context: "./sentinel2"
ports:
- 26302:26379
networks:
- net-redis
sentinel3:
build:
context: "./sentinel3"
ports:
- 26303:26379
networks:
- net-redis
- 修改主從 Dockerfile
- vim master/Dockerfile
- vim slave1/Dockerfile
- vim slave2/Dockerfile
FROM redis:5.0.4
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
EXPOSE 6379
- 修改 主redis配置 vim master/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
- 修改 從redis配置 vim slave1/redis.conf , vim slave2/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
replicaof master 6379
- 修改哨兵 dockerfile
- vim sentinel1/Dockerfile
- vim sentinel2/Dockerfile
- vim sentinel3/Dockerfile
FROM redis:5.0.4
COPY sentinel.conf /usr/local/etc/redis/sentinel.conf
CMD [ "redis-sentinel", "/usr/local/etc/redis/sentinel.conf" ]
EXPOSE 26379
- 修改sentinel配置
- vim sentinel1/sentinel.conf
- vim sentinel2/sentinel.conf
- vim sentinel3/sentinel.conf
logfile "sentinel.log"
sentinel monitor redis_master master 6379 2
port 26379
- 啟動 docker-compose up --build -d
- 測試: 執(zhí)行 docker stop "redis主容器ID"闯冷,過30s查看另外2個從redis砂心, 輸入 info Replication ,觀察節(jié)點(diǎn)信息
docker stop 3a860979a9ec
//從節(jié)點(diǎn)變成主節(jié)點(diǎn)
Connected.
local_slave_1:0>info Replication
# Replication
role:master
connected_slaves:1
集群模式
目錄結(jié)構(gòu)
$ tree ~/Documents/redis_cluster
├── docker-compose.yml
├── node1
│ ├── Dockerfile
│ └── redis.conf
├── node2
│ ├── Dockerfile
│ └── redis.conf
├── node3
│ ├── Dockerfile
│ └── redis.conf
├── node4
│ ├── Dockerfile
│ └── redis.conf
├── node5
│ ├── Dockerfile
│ └── redis.conf
├── node6
│ ├── Dockerfile
│ └── redis.conf
└── readme.md
- vim docker-compose.yml
version: "3"
networks:
net-redis-cluster:
driver: bridge
services:
node1:
build:
context: "./node1"
ports:
- 7001:6379
networks:
- net-redis-cluster
node2:
build:
context: "./node1"
ports:
- 7002:6379
networks:
- net-redis-cluster
node3:
build:
context: "./node3"
ports:
- 7003:6379
networks:
- net-redis-cluster
node4:
build:
context: "./node4"
ports:
- 7004:6379
networks:
- net-redis-cluster
node5:
build:
context: "./node5"
ports:
- 7005:6379
networks:
- net-redis-cluster
node6:
build:
context: "./node6"
ports:
- 7006:6379
networks:
- net-redis-cluster
- 修改主從 Dockerfile
- vim node1/Dockerfile
....
....
- vim node6/Dockerfile
FROM redis:5.0.4
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
EXPOSE 6379
- 修改 redis配置
- vim node1/redis.conf
....
....
- vim node6/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 10000
- 啟動
docker-compose up --build -d
- 查看redis容器啟動情況
$ docker ps -a | grep redis
de46397b5623 redis_cluster_node3 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:7003->6379/tcp redis_cluster_node3_1
7a23e523bbef redis_cluster_node6 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:7006->6379/tcp redis_cluster_node6_1
1e319c55aead redis_cluster_node5 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:7005->6379/tcp redis_cluster_node5_1
6ca111dadb5f redis_cluster_node2 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:7002->6379/tcp redis_cluster_node2_1
46dadac7c8ab redis_cluster_node1 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:7001->6379/tcp redis_cluster_node1_1
f5d642bde5fc redis_cluster_node4 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:7004->6379/tcp redis_cluster_node4_1
- 查看redis集群容器的ip地址
docker inspect --format='{{.Name}} - {{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) | grep redis_cluster
//輸出結(jié)果
/redis_cluster_node4_1 - 172.23.0.7
/redis_cluster_node3_1 - 172.23.0.6
/redis_cluster_node1_1 - 172.23.0.5
/redis_cluster_node6_1 - 172.23.0.4
/redis_cluster_node5_1 - 172.23.0.3
/redis_cluster_node2_1 - 172.23.0.2
- 設(shè)置redis集群
docker exec -it de46397b5623 /bin/bash
/usr/local/bin/redis-cli --cluster create 172.23.0.5:6379 172.23.0.2:6379 172.23.0.6:6379 172.23.0.7:6379 172.23.0.3:6379 172.23.0.4:6379 -cluster-replicas 1
- 測試
root@de46397b5623:/data# /usr/local/bin/redis-cli -c -h 172.23.0.3
172.23.0.3:6379>
172.23.0.3:6379> set key1 1
-> Redirected to slot [9189] located at 172.23.0.7:6379
OK
- 其他:如果要使用 php 的 RedisCluster, node節(jié)點(diǎn)配置 需要和redis-cli --cluster create ip:port 設(shè)置的保持一致