使用docker本地搭建Redis集群

1,769 阅读4分钟

前言

最近在学习Redis相关知识,想实践一把,奈何巧妇难为无米之炊,又不太舍得买服务器(穷),只能本地自己搭建一个Redis集群了,标准三主三从.主要是分享搭建过程以及本地如何访问.

如有错误,欢迎指正.

docker 安装 redis

docker pull redis:latest
docker images

image.png

配置文件 redis.conf

这里主要是用到的参数,真实参数远远不止这些,用时再加,如需详细了解,点击 链接

# 端口号
port 6379
# 关闭保护模式
protected-mode no
# 关闭守护进程
daemonize no
# 开启持久化
appendonly yes
# 开启 cluster集群
cluster-enabled yes
# 集群配置文件
cluster-config-file nodes.conf
# 集群超时 
cluster-node-timeout 15000
# 注意一下,如果需对外提供访问,填写宿主机ip,否则 下面三行注释掉即可
# 集群节点ip 
cluster-announce-ip xxx.xxx.xxx.xxx
# 集群节点映射端口号
cluster-announce-port 6379
# 集群节点总线端口号
cluster-announce-bus-port 16379
# 绑定0.0.0.0
bind 0.0.0.0

桥接模式

默认的bridge网络是无法使用DNS,因为需要支持外网访问,所以需要自定义网络

# 查看网络模式
docker network ls

image.png

# 创建自定义网络模式
docker network create redis-net

image.png

# 查看redis-net网络详情
docker network inspect redis-net

image.png

脚本文件

因为是集群,不想一个一个创建,所以...

run.sh

#!/usr/bin/env bash
set -e

# 脚本当前目录
cPath=$(cd $(dirname "$0") || exit; pwd)

# 根目录
dirPath=$(dirname "$cPath")

# 获取端口
port="$1"
if [[ ! "$port" ]]; then
  port=6379
fi

# 创建数据目录
mkdir -p "$dirPath"/redis/"$port"

# 删除已启动服务
containerId=$(docker ps -a | grep "redis_$port" | awk -F' ' '{print $1}')
if [[ "$containerId" ]]; then
    docker rm -f ${containerId} > /dev/null
fi

# 启动服务
containerName=redis_"$port"
 docker run -itd --privileged=true -p "$port":"$port" -p 1"$port":1"$port" --net redis-net --name ${containerName} \
-v="$dirPath"/redis/"$port"/conf/redis.conf:/etc/redis/redis.conf \
-v="$dirPath"/redis/"$port"/data:/data \
redis \
redis-server /etc/redis/redis.conf > /dev/null

# 获取容器启动状态
status=$(docker inspect -f "{{.State.Running}}" "$containerName")
if [[ "$status" == "true" ]]; then
  echo "container:$containerName - start success"
fi

cluster.sh (集群启动脚本)

#!/usr/bin/env bash
set -e

# 脚本当前目录
cPath=$(cd $(dirname "$0") || exit; pwd)

# 启动集群数量
num="$1"
if [[ ! "$num" ]]; then
  num=6
fi

sPort=6378
for((i=1;i<=$num;i++)); do
    sh ${cPath}/run.sh  $(($sPort+$i))
done

启动脚本

# 在sh目录执行
sh cluster.sh

image.png

目录结构

├─redis                                   
│  ├─6379                                 
│  │  ├─conf                              
│  │  │      redis.conf                                                     
│  │  └─data                              
│  ├─6380                                 
│  │  ├─conf                              
│  │  │      redis.conf                                                     
│  │  └─data                              
│  ├─6381                                 
│  │  ├─conf                              
│  │  │      redis.conf                                                    
│  │  └─data                              
│  ├─6382                                 
│  │  ├─conf                              
│  │  │      redis.conf                                                     
│  │  └─data                              
│  ├─6383                                 
│  │  ├─conf                              
│  │  │      redis.conf                                                      
│  │  └─data                              
│  └─6384                                 
│      ├─conf                             
│      │      redis.conf                                                    
│      └─data                             
└─sh                                      
        cluster.sh                        
        run.sh                            

redis 配置 redis.conf

将配置文件在每一个 redis.conf 配置一下,大概结构是这个样子

# 在 sh目录执行的
cat ../redis/63{79..84}/conf/redis.conf

结果

port 6379
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.31.41
cluster-announce-port 6379
cluster-announce-bus-port 16379
bind 0.0.0.0

port 6380
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.31.41
cluster-announce-port 6380
cluster-announce-bus-port 16380
bind 0.0.0.0

port 6381
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.31.41
cluster-announce-port 6381
cluster-announce-bus-port 16381
bind 0.0.0.0

port 6382
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.31.41
cluster-announce-port 6382
cluster-announce-bus-port 16382
bind 0.0.0.0

port 6383
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.31.41
cluster-announce-port 6383
cluster-announce-bus-port 16383
bind 0.0.0.0

port 6384
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.31.41
cluster-announce-port 6384
cluster-announce-bus-port 16384
bind 0.0.0.0

因为开启了集群,再重新执行一遍 cluster.sh

# 在sh目录执行
sh cluster.sh

查看容器是否创建成功

docker ps

image.png

查看节点分配的ip信息

docker network inspect redis-net | grep -i -E "name|ipv4address"

image.png

创建集群

进入任意容器节点

 docker exec -it redis_6379 /bin/bash

创建集群

redis-cli  --cluster create 172.18.0.2:6379, 172.18.0.3:6380, 172.18.0.4:6381, 172.18.0.5:6382, 172.18.0.6:6383, 172.18.0.7:6384 --cluster-replicas 1

image.png

是否平均分配槽位

image.png

yes

image.png

到这里可以说Redis集群搭建成功了

验证

验证集群是否创建成功

# 开启集群模式进入
redis-cli -c

执行命令: cluster info

image.png

看到cluster_state:ok 真ok了

执行命令: cluster nodes

image.png

三主三从,perfect!!!

使用数据验证

image.png

验证外网能否访问

这里以redisson 为例,原因是最近在看redisson源码

  1. 使用idea快速构建一个Spring boot 项目
  2. 引入一个最新redisson依赖
<!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.16.1</version>
</dependency>
  1. 源码
public static void main(String[] args) {
    Config config = new Config();
    config.useClusterServers()
            .addNodeAddress("redis://192.168.31.41:6379")
            .addNodeAddress("redis://192.168.31.41:6380")
            .addNodeAddress("redis://192.168.31.41:6381")
            .addNodeAddress("redis://192.168.31.41:6382")
            .addNodeAddress("redis://192.168.31.41:6383")
            .addNodeAddress("redis://192.168.31.41:6384");

    RedissonClient redisson = Redisson.create(config);

    System.out.println(redisson);
}

4.执行

image.png

5.debug

image.png

end

end.png