简单介绍:
1.应用场景:主要是针对海量数据+高并发+高可用的场景
2.好处:
1)将数据自动切分到多个节点的能力。
2)当集群中的一部分节点失效或者无法进行通讯时, 仍然可以继续处理命令请求的能力,拥有自动故障转移的能力。
3.分区采用:虚拟槽分区
搭建:
1)准备节点。 2)节点握手。 3)分配槽。
准备节点:
创建redis dockerfile镜像文件
FROM ruby:2.5-alpine
RUN sed -i ‘s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g’ /etc/apk/repositories
&& apk add gcc g++ libc-dev wget vim openssl-dev make linux-headers bash
&& rm -rf /var/cache/apk/*
RUN wget https://rubygems.org/downloads/redis-4.0.2.gem && gem install -l ./redis-4.0.2.gem
&& rm -f redis-4.0.2.gem
#通过选择更小的镜像,删除不必要文件清理不必要的安装缓存,从而瘦身镜像
#创建相关目录能够看到日志信息跟数据跟配置文件
RUN mkdir -p /usr/src/redis
&& mkdir -p /usr/src/redis/data
&& mkdir -p /usr/src/redis/conf
&& mkdir -p /usr/src/sh
&& mkdir -p /usr/src/redis/log
RUN wget -O /usr/src/redis/redis-4.0.11.tar.gz “http://download.redis.io/releases/redis-4.0.11.tar.gz”
&& tar -xzf /usr/src/redis/redis-4.0.11.tar.gz -C /usr/src/redis
&& cp /usr/src/redis/redis-4.0.11/src/redis-trib.rb /usr/src/redis/
&& rm -rf /usr/src/redis/redis-4.0.11.tar.tgz
RUN cd /usr/src/redis/redis-4.0.11 && make && make PREFIX=/usr/local/redis install
&& ln -s /usr/local/redis/bin/* /usr/local/bin/ && rm -rf /usr/src/redis/redis-4.0.11
COPY ./redis/sh/cluster.sh /usr/src/sh
COPY ./redis/sh/redis-trib.rb /usr/src/sh/redis-trib.rb
RUN chmod +x /usr/src/sh/cluster.sh && chmod +x /usr/src/sh/redis-trib.rb
创建镜像
docker build -t redis-cluster .
使用docker-composer编排
docker-composer.yaml文件如下
version: “3.6”
services: #服务
master-1:
image: redis-cluster
container_name: cluster-1
environment:
- REALIP=192.168.80.128
- PORT=6390
- PORT2=16390 #集群开放的端口
networks:
redis-network: #网络名称
ipv4_address: 192.168.1.176 #设置ip地址
working_dir: /usr/src/redis #工作目录
stdin_open: true
tty: true
ports: - “6390:6379”
- “16390:16379”
volumes: - /home/docker/clustre/redis/master-1:/usr/src/redis
entrypoint: - /bin/bash
- /usr/src/sh/cluster.sh
master-2:
image: redis-cluster
container_name: cluster-2
environment: - REALIP=192.168.80.128
- PORT=6391
- PORT2=16391
networks:
redis-network: #网络名称
ipv4_address: 192.168.1.177 #设置ip地址
working_dir: /usr/src/redis #工作目录
stdin_open: true
tty: true
ports: - “6391:6379”
- “16391:16379”
volumes: - /home/docker/clustre/redis/master-2:/usr/src/redis
entrypoint: - /bin/bash
- /usr/src/sh/cluster.sh
master-3:
image: redis-cluster
container_name: cluster-3
environment: - REALIP=192.168.80.128
- PORT=6392
- PORT2=16392
networks:
redis-network: #网络名称
ipv4_address: 192.168.1.178 #设置ip地址
working_dir: /usr/src/redis #工作目录
stdin_open: true
tty: true
ports: - “6392:6379”
- “16392:16379”
volumes: - /home/docker/clustre/redis/master-3:/usr/src/redis
entrypoint: - /bin/bash
- /usr/src/sh/cluster.sh
master-4:
image: redis-cluster
container_name: cluster-7
environment: - REALIP=192.168.80.128
- PORT=6396
- PORT2=16396
networks:
redis-network: #网络名称
ipv4_address: 192.168.1.179 #设置ip地址
working_dir: /usr/src/redis #工作目录
stdin_open: true
tty: true
ports: - “6396:6379”
- “16396:16379”
volumes: - /home/docker/clustre/redis/master-4:/usr/src/redis
entrypoint: - /bin/bash
- /usr/src/sh/cluster.sh
slave-1:
image: redis-cluster
container_name: cluster-4
environment: - REALIP=192.168.80.128
- PORT=6393
- PORT2=16393
networks:
redis-network: #网络名称
ipv4_address: 192.168.1.180 #设置ip地址
working_dir: /usr/src/redis #工作目录
stdin_open: true
tty: true
ports: - “6393:6379”
- “16393:16379”
volumes: - /home/docker/clustre/redis/slave-1:/usr/src/redis
entrypoint: - /bin/sh
- /usr/src/sh/cluster.sh
slave-2:
image: redis-cluster
container_name: cluster-5
environment: - REALIP=192.168.80.128
- PORT=6394
- PORT2=16394
networks:
redis-network: #网络名称
ipv4_address: 192.168.1.181 #设置ip地址
working_dir: /usr/src/redis #工作目录
stdin_open: true
tty: true
ports: - “6394:6379”
- “16394:16379”
volumes: - /home/docker/clustre/redis/slave-2:/usr/src/redis
entrypoint: - /bin/bash
- /usr/src/sh/cluster.sh
slave-3:
image: redis-cluster
container_name: cluster-6
environment: - REALIP=192.168.80.182
- PORT=6395
- PORT2=16395
networks:
redis-network: #网络名称
ipv4_address: 192.168.1.25 #设置ip地址
working_dir: /usr/src/redis #工作目录
stdin_open: true
tty: true
ports: - “6395:6379”
- “16395:16379”
volumes: - /home/docker/clustre/redis/slave-3:/usr/src/redis
entrypoint: - /bin/bash
- /usr/src/sh/cluster.sh
slave-4:
image: redis-cluster
container_name: cluster-8
environment: - REALIP=192.168.80.128
- PORT=6398
- PORT2=16398
networks:
redis-network: #网络名称
ipv4_address: 192.168.1.183 #设置ip地址
working_dir: /usr/src/redis #工作目录
stdin_open: true
tty: true
ports: - “6398:6379”
- “16398:16379”
volumes: - /home/docker/clustre/redis/slave-4:/usr/src/redis
entrypoint: - /bin/bash
- /usr/src/sh/cluster.sh
#引用外部预先定义好的网段
networks:
redis-network:
external:
name: redis-network
配置集群配置文件
开启集群 cluster-enabled yes
cluster-config-file “nodes-6379.conf”
运行
docker-compose up -d
创建集群节点
[root@localhost clustre]# docker-compose ps
cluster-1 /bin/bash /usr/src/sh/clus … Up 0.0.0.0:16390->16379/tcp, 0.0.0.0:6390->6379/tcp
cluster-2 /bin/bash /usr/src/sh/clus … Up 0.0.0.0:16391->16379/tcp, 0.0.0.0:6391->6379/tcp
cluster-3 /bin/bash /usr/src/sh/clus … Up 0.0.0.0:16392->16379/tcp, 0.0.0.0:6392->6379/tcp
cluster-4 /bin/sh /usr/src/sh/cluster.sh Up 0.0.0.0:16393->16379/tcp, 0.0.0.0:6393->6379/tcp
cluster-5 /bin/bash /usr/src/sh/clus … Up 0.0.0.0:16394->16379/tcp, 0.0.0.0:6394->6379/tcp
cluster-6 /bin/bash /usr/src/sh/clus … Up 0.0.0.0:16395->16379/tcp, 0.0.0.0:6395->6379/tcp
cluster-7 /bin/bash /usr/src/sh/clus … Up 0.0.0.0:16396->16379/tcp, 0.0.0.0:6396->6379/tcp
cluster-8 /bin/bash /usr/src/sh/clus … Up 0.0.0.0:16398->16379/tcp, 0.0.0.0:6398->6379/tcp
节点握手
在一个节点上依次将其余节点全部握手
docker exec -it cluster-1 bash
cluster meet 192.168.80.128 6391
依次进行握手
cluster nodes 查看集群连接信息
分配槽
通过 cluster addslots 命令为节点分配槽
redis-cli -h 192.168.80.128 -p 6390 -a sixstar cluster addslots {0…4461}
redis-cli -h 192.168.80.128 -p 6391 -a sixstar cluster addslots {4462…8462}
redis-cli -h 192.168.80.128 -p 6392 -a sixstar cluster addslots {8463…12463}
redis-cli -h 192.168.80.128 -p 6396 -a sixstar cluster addslots {12464…16383}
测试:
bash-5.0# redis-cli -a sixstar -c
127.0.0.1:6379> set redis redis
-> Redirected to slot [9169] located at 192.168.80.128:6392
根据key计算出不通的值放入不通的槽
集群搭建完毕