Press "Enter" to skip to content

利用docker实现redis 集群搭建

一、环境介绍:centos 7 阿里云服务器一台。 二、实现思路:
  1. 利用Dockerfile构建redis镜像
  2. 利用docker-compose编排所需的docker,然后批量生成docker容器
  3. 配置集群
三、集群配置
  1. 配置文件修改,(已经在创建容器之前修改好了)
  2. 节点握手
  3. 配置主从
  4. 分配数据槽
四、节点规划 容器名称 | 容器IP地址 | 映射端口号 | 服务运行模式
—|—|—|—|—
Redis-master|172.50.0.2|6391-> 6391|master
Redis-master2 |172.50.0.3 |6392-> 6392 |master
Redis-master3 |172.50.0.4 |6393 -> 6393 |master
redis-slave |172.30.0.2 |6394 -> 6394 |Slave
redis-slave2 |172.30.0.3 |6395 -> 6395 |Slave
redis-slave3 |172.30.0.4 |6396 -> 6396 |Slave
五、 实现过程
  1. 创建Dockerfile文件,文件内容如下
FROM centos:latest
MAINTAINER qw "ssf"
RUN groupadd -r redis && useradd  -r -g redis redis
RUN  yum -y update &&  yum -y install epel-release \
&&   yum -y install redis  && yum -y install wget \
&&   yum -y install net-tools \
&&   yum -y install  ruby && yum  -y install  rubygems
RUN wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem  &&  gem install -l ./redis-3.2.1.gem \
&&  rm -f redis-3.2.1.gem
COPY  ./config/redis-trib.rb  /usr/bin
COPY  ./config/redis.sh       /usr/bin
RUN  mkdir -p /config  && chmod  775  /usr/bin/redis.sh
  1. Dockerfile存放目录为/home/docker/,进入该文件夹并且执行命令创建自定义docker镜像。
cd /home/docker
docker build -t redis .
  1. 编排docker-compose.yaml文件。说明:在创建docker-compose文件之前已经创建了两个网络mynetwork50和mynetwork30,主节点使用mynetwork50,从节点使用mynetwork30。并且设置了共享卷,把文件共享到docker下。
version: "3.6"
services:
  redis-master1:
     image: redis-cluster
     container_name: redis-master1
     working_dir: /config
     environment:
       - PORT=6391
     ports:
       - "6391:6391"
       - "16391:16391"
     stdin_open: true
     networks:
        mynetwork50:
          ipv4_address: 172.50.0.2
     tty: true
     privileged: true
     volumes: ["/home/docker/config:/config"]
     entrypoint:
       - /bin/bash
       - redis.sh
  redis-master2:
       image: redis-cluster
       working_dir: /config
       container_name: redis-master2
       environment:
              - PORT=6392
       networks:
          mynetwork50:
             ipv4_address: 172.50.0.3
       ports:
         - "6392:6392"
         - "16392:16392"
       stdin_open: true
       tty: true
       privileged: true
       volumes: ["/home/docker/config:/config"]
       entrypoint:
         - /bin/bash
         - redis.sh
  redis-master3:
       image: redis-cluster
       container_name: redis-master3
       working_dir: /config
       environment:
              - PORT=6393
       networks:
          mynetwork50:
            ipv4_address: 172.50.0.4
       ports:
         - "6393:6393"
         - "16393:16393"
       stdin_open: true
       tty: true
       privileged: true
       volumes: ["/home/docker/config:/config"]
       entrypoint:
         - /bin/bash
         - redis.sh
  redis-slave1:
       image: redis-cluster
       container_name: redis-slave1
       working_dir: /config
       environment:
            - PORT=6394
       networks:
          mynetwork30:
             ipv4_address: 172.30.0.2
       ports:
         - "6394:6394"
         - "16394:16394"
       stdin_open: true
       tty: true
       privileged: true
       volumes: ["/home/docker/config:/config"]
       entrypoint:
         - /bin/bash
         - redis.sh
  redis-salve2:
       image: redis-cluster
       working_dir: /config
       container_name: redis-salve2
       environment:
             - PORT=6395
       ports:
         - "6395:6395"
         - "16395:16395"
       stdin_open: true
       networks:
          mynetwork30:
              ipv4_address: 172.30.0.3
       tty: true
       privileged: true
       volumes: ["/home/docker/config:/config"]
       entrypoint:
         - /bin/bash
         - redis.sh
  redis-salve3:
       image: redis-cluster
       container_name: redis-slave3
       working_dir: /config
       environment:
          - PORT=6396
       ports:
         - "6396:6396"
         - "16396:16396"
       stdin_open: true
       networks:
          mynetwork30:
            ipv4_address: 172.30.0.4
       tty: true
       privileged: true
       volumes: ["/home/docker/config:/config"]
       entrypoint:
         - /bin/bash
         - redis.sh
networks:
  mynetwork50:
    external: true 
  mynetwork30:
    external: true
  1. 快速创建出6个redis容器。
docker-compose up -d
  1. 节点握手。进入任意一个redis容器,然后进入redis。
redis-cli -p 6391
开始握手各个节点
127.0.0.1:6391> cluster meet 47.91.209.221 6392
OK
127.0.0.1:6391> cluster nodes
b0f8d7592cfa8919596ef361dca1c4c02aeea82e 172.50.0.2:6391 myself,master - 0 0 0 connected
6a1b290fc6ddee0fd24dad721980a3ac6fc13fb3 47.91.209.221:6392 master - 0 1545113754100 1 connected
127.0.0.1:6391> cluster meet 47.91.209.221 6393
OK
127.0.0.1:6391> cluster meet 47.91.209.221 6394
OK
127.0.0.1:6391> cluster meet 47.91.209.221 6395
OK
127.0.0.1:6391> cluster meet 47.91.209.221 6396
OK
127.0.0.1:6391> cluster nodes
a63119615bba7776e163a6eb2b1ca7b8ff8a354d 47.91.209.221:6394 master - 0 1545113804719 3 connected
95bddfda92195da5e49a8c468f680949813e9405 47.91.209.221:6393 master - 0 1545113802715 2 connected
b0f8d7592cfa8919596ef361dca1c4c02aeea82e 172.50.0.2:6391 myself,master - 0 0 0 connected
87ff89c62d0cb71abba71d84ae1550c879f90de6 47.91.209.221:6395 master - 0 1545113805220 4 connected
6a1b290fc6ddee0fd24dad721980a3ac6fc13fb3 47.91.209.221:6392 master - 0 1545113806222 1 connected
6a59850c223eab56298ae10c4c3b3d134a2a2f69 47.91.209.221:6396 master - 0 1545113807224 5 connected
127.0.0.1:6391> exit
  1. 设置主从。以此进入6394、6395、 6395三个端口节点进行配置
redis-cli -h 47.91.209.221 -p 6395
cluster replicate 6a1b290fc6ddee0fd24dad721980a3ac6fc13fb3
以上代码,可以把6395节点作为6392的从节点,其他两个节点以此类推。
  1. 分配槽点。
[root@148df83d86a4 config]# redis-cli -h 47.91.209.221  -p 6391  cluster addslots {0..5461}
OK

[root@148df83d86a4 config]

redis-cli -h 47.91.209.221 -p 6392 cluster addslots {5462..10922}

OK

[root@148df83d86a4 config]

redis-cli -h 47.91.209.221 -p 6393 cluster addslots {10923..16383}

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注