Docker搭建Redis集群

92 阅读2分钟

Docker搭建Redis集群

一 、搭建Redis集群的优点

先说说单个redis的缺点:
1、单个redis具有不稳定性。当redis服务死机了或者redis服务被kill掉了,就没有可用的redis服务了。
2、单个redis的读写能力是有限的。
再根据单个redis服务的缺点谈谈redis集群的优点:
搭建redis集群一般都基于redis主从复制,即有一到多台Master服务、一到多台Slave服务。当redis服务死机了或者redis服务被kill掉了,Slave服务就会顶替原来的Master服务继续照常运行,并且数据不会丢失。
因为redis集群使得redis服务变多了,所以也提高了redis的读写能力。

二 、Redis文件配置

1、基本环境说明

2、创建一个网卡

#创建redis网卡,后面配置redis集群的时候可以指定配置IP(不创建网卡系统会自动分配IP 不会产生影响)
docker network create redis --subnet 172.12.0.0/16

3、使用docker拉取redis镜像

root@LAPTOP-CHOA4ICV:/home# docker pull redis
root@LAPTOP-CHOA4ICV:/home# docker images

4、配置6个redis服务

因为是6个redis服务,单个配起来的话很麻烦,所以我选择使用shell脚本进行配置,redis_config.sh 脚本代码如下:

#!/bin/bash

for port in $(seq 1 6); \
do \
mkdir -p /home/redis/node0${port}/conf
touch /home/redis/node0${port}/conf/redis.conf
cat << EOF >/home/redis/node0${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file node.conf
cluster-node-timeout 5000
cluster-announce-ip 172.12.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
  • cluster-enabled yes 开启集群功能
  • cluster-node-timeout 集群节点的超时时限
  • cluster-announce-ip 集群节点IP
  • cluster-announce-port 集群节点映射端口
  • cluster-announce-bus-port 集群节点总线端口

运行配置脚本:

chmod +x redis_config.sh
./redis_config.sh

查看当前目录结构:

#如果没有 tree 命令先安装 
apt-get install tree
root@LAPTOP-CHOA4ICV:/home/# tree
.
├── redis
│   ├── node01
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   │       ├── appendonlydir
│   │       │   ├── appendonly.aof.1.base.rdb
│   │       │   ├── appendonly.aof.1.incr.aof
│   │       │   └── appendonly.aof.manifest
│   │       └── node.conf
│   ├── node02
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   │       ├── appendonlydir
│   │       │   ├── appendonly.aof.1.base.rdb
│   │       │   ├── appendonly.aof.1.incr.aof
│   │       │   └── appendonly.aof.manifest
│   │       └── node.conf
│   ├── node03
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   │       ├── appendonlydir
│   │       │   ├── appendonly.aof.1.base.rdb
│   │       │   ├── appendonly.aof.1.incr.aof
│   │       │   └── appendonly.aof.manifest
│   │       └── node.conf
│   ├── node04
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   │       ├── appendonlydir
│   │       │   ├── appendonly.aof.2.base.rdb
│   │       │   ├── appendonly.aof.2.incr.aof
│   │       │   └── appendonly.aof.manifest
│   │       ├── dump.rdb
│   │       └── node.conf
│   ├── node05
│   │   ├── conf
│   │   │   └── redis.conf
│   │   └── data
│   │       ├── appendonlydir
│   │       │   ├── appendonly.aof.2.base.rdb
│   │       │   ├── appendonly.aof.2.incr.aof
│   │       │   └── appendonly.aof.manifest
│   │       ├── dump.rdb
│   │       └── node.conf
│   └── node06
│       ├── conf
│       │   └── redis.conf
│       └── data
│           ├── appendonlydir
│           │   ├── appendonly.aof.2.base.rdb
│           │   ├── appendonly.aof.2.incr.aof
│           │   └── appendonly.aof.manifest
│           ├── dump.rdb
│           └── node.conf
├── redis_config.sh
└── redis_start.sh

三、 Redis集群启动

1、启动6个redis服务

同上启动的话也是使用shell脚本来完成,redis_start.sh 脚本如下:

#!/bin/bash

for port in $(seq 1 6); \
do \
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /home/redis/node0${port}/data:/data \
-v /home/redis/node0${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.12.0.1${port} redis redis-server /etc/redis/redis.conf
done
  • -p 端口映射
  • -v 数据卷挂载
  • -d 后台运行
  • –net 指定网络
  • redis-server /etc/redis/redis.conf redis-server指向配置文件启动

运行启动脚本:

chmod +x redis_start.sh
./redis_start.sh

2、创建集群

(1)首先随便进入一个redis容器

docker exec -it redis-1 sh

(2)启动集群服务

redis-cli --cluster create 172.12.0.11:6379 172.12.0.12:6379 172.12.0.13:6379 172.12.0.14:6379 172.12.0.15:6379 172.12.0.16:6379 --cluster-replicas 1

中途手动输入yes,看到这个就代表集群启动成功啦!!!

四 、测试Redis集群服务

#redis-cli -c 为使用集群 不加-c则是单个redis服务
redis-cli -c

#查看nodes
127.0.0.1:6379> cluster nodes

使用get set测试

set name SummerGao
get name

发现是由 172.12.0.12 处理的 get set 命令也就是 redis-2 ,此时我将 redis-2 容器停止,看从服务器是否能顶替 redis-2。

docker stop redis-2

再使用 get name 命令看是否能返回 SummerGao ?

发现依然返回 SummerGao,但此次是由172.12.0.16执行的get命令,再查看nodes。

此时发现redis-2节点已经fail ,原来的redis-6节点也从slave变成了master。证实了集群的高可用性!!!