docker 简单redis主从、哨兵、集群快速搭建

主从模式
目录结构
.
├── docker-compose.yml    
├── master
│   ├── Dockerfile
│   └── redis.conf
├── slave1
│   ├── Dockerfile
│   └── redis.conf
└── slave2
    ├── Dockerfile
    └── redis.conf
  1. 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

  1. 修改主从 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
  1. 修改 主redis配置 vim master/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
  1. 修改 从redis配置 vim slave1/redis.conf , vim slave2/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
replicaof master 6379
  1. 启动 docker-compose up --build -d
  2. 测试, 连接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哨兵模式
目录结构
├── 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
  1. 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
  1. 修改主从 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
  1. 修改 主redis配置 vim master/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
  1. 修改 从redis配置 vim slave1/redis.conf , vim slave2/redis.conf
bind 0.0.0.0
daemonize no
pidfile /var/run/redis.pid
replicaof master 6379
  1. 修改哨兵 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
  1. 修改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
  1. 启动 docker-compose up --build -d
  2. 测试: 执行 docker stop "redis主容器ID",过30s查看另外2个从redis, 输入 info Replication ,观察节点信息
docker stop 3a860979a9ec
//从节点变成主节点
Connected.
local_slave_1:0>info Replication
# Replication
role:master
connected_slaves:1


集群模式

目录结构
$ 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
  1. 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
  1. 修改主从 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
  1. 修改 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
  1. 启动
docker-compose up --build -d
  1. 查看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                                                                    
  1. 查看redis集群容器的ip地址
docker inspect --format='{{.Name}} - {{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) | grep redis_cluster
//输出结果
/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
  1. 设置redis集群
  • 随便进入一台redis容器
docker exec -it de46397b5623 /bin/bash
  • 使用redis-cli 创建集群
/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
  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
  1. 其他:如果要使用 php 的 RedisCluster, node节点配置 需要和redis-cli --cluster create ip:port 设置的保持一致

你可能感兴趣的:(docker 简单redis主从、哨兵、集群快速搭建)