环境准备: CentOS7

三台虚拟机:因为redis集群高可用,这里配置六个节点。每台部署两个节点。ip和端口分别为:

192.168.1.103:6379

192.168.1.103:6380

192.168.1.106:6379

192.168.1.106:6380

192.168.1.107:6379

192.168.1.107:6380

1. docker安装


2. 拉取redis镜像

# 没有写版本默认拉取最新的
docker pull redis

3. redis环境准备(这个步骤三台操作都是一样的,这里以192.168.1.107为例。)

    3.1 创建redis配置目录以及映射数据圈目录,用于备份同步和启动docker容器

# 创建目录
mkdir -p /opt/redis/redis-6379/conf
mkdir -p /opt/redis/redis-6379/data

    3.2 redis配置文件,实际生产可以下载redis源码中的redis.conf,由于这里只是搭建测试,直接配置写简单能运行起来的参数

# 到配置目录下
cd /opt/redis/redis-6379/conf
vim ./redis.conf
# 写入以下内容
port 6379 #端口
cluster-enabled yes #开启集群模式
cluster-config-file nodes.conf #集群节点信息文件
cluster-node-timeout 5000 #超时时间
cluster-announce-ip 192.168.1.107 #部署redis主机上对应的ip
cluster-announce-port 6379 #集群映射端口
cluster-announce-bus-port 16379 #集群总线端口
appendonly yes #指令追加持久化方式

    3.3 docker-redis容器启动脚本

# cd到redis-6379目录下
cd /opt/redis/redis-6379
# 编辑容器启动脚本
vim ./redis-6379-start.sh
# 写入以下内容
#!/bin/bash
docker run -d -ti \
--privileged=true -v /opt/redis/redis-6379/conf/redis.conf:/usr/local/etc/redis/redis.conf \
--privileged=true -v /opt/redis/redis-6379/data:/data \
--restart always --name redis-6379 --net host \
--sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf
# 修改脚本执行权限
chmod a+u ./redis-6379-start.sh

   3.4 执行启动redis-6379容器

./redis-6379-start.sh

    这里192.168.1.107的redis-6379已经启动了,我们只需要修改下配置,启动本地的第二台redis-6380

    3.5 复制修改启动redis-6380

# cd到/opt/redis目录,直接复制redis-6379
cd /opt/redis
cp -r ./redis-6379 ./redis-6380
# 直接修改redis.conf文件配置项
port 6380 #端口
cluster-announce-ip 192.168.1.107 #在其他主机配置时才需要更换
cluster-announce-port 6380#集群映射端口
cluster-announce-bus-port 16380 #集群总线端口
# 修改启动脚本
直接将6379替换成6380即可,其他的不需要更改

    3.6 启动redis-6380

# 修改下脚本名称
mv ./redis-6379-start.sh ./redis-6380-start.sh
# 启动
./redis-6380-start.sh

4. 按照192.168.1.107的步骤,在192.168.1.103和192.168.1.106上同样操作。

5. 以上步骤已经全部启动redis服务,但是还没有真正开始集群模式。接下来随便进入一台redis容器,这里仍以192.168.1.107为例操作,开启集群模式。

# 进入redis-6379容器并分配终端
docker exec -it redis-6379 bash
# 执行命令开启集群模式
redis-cli --cluster create 192.168.1.107:6379 192.168.1.107:6380 192.168.1.103:6379 192.168.1.103:6380 192.168.1.106:6379 192.168.1.106:6380 --cluster-replicas 1

   命令显示过程(主要是分配主从节点和哈希槽的一个过程,中途输入yes即可):

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.103:6380 to 192.168.1.107:6379
Adding replica 192.168.1.106:6380 to 192.168.1.103:6379
Adding replica 192.168.1.107:6380 to 192.168.1.106:6379
M: e8aef9ed8e05b38f8bc65879bd30a825b306c7b6 192.168.1.107:6379
   slots:[0-5460] (5461 slots) master
S: 66559e06fefa1e6aa4c73f6141addc750df16406 192.168.1.107:6380
   replicates 3baa5bc431925f1099561bc82355c992e19c42f8
M: d7d899e9ad644e86641f82bb97adf2f7f7ff8214 192.168.1.103:6379
   slots:[5461-10922] (5462 slots) master
S: 7ec346484effbe562ea0bf3e40ed9e9538b973b7 192.168.1.103:6380
   replicates e8aef9ed8e05b38f8bc65879bd30a825b306c7b6
M: 3baa5bc431925f1099561bc82355c992e19c42f8 192.168.1.106:6379
   slots:[10923-16383] (5461 slots) master
S: fe233c5c139ea7bb7c2b91c94b8efd6d6f592096 192.168.1.106:6380
   replicates d7d899e9ad644e86641f82bb97adf2f7f7ff8214
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.1.107:6379)
M: e8aef9ed8e05b38f8bc65879bd30a825b306c7b6 192.168.1.107:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: d7d899e9ad644e86641f82bb97adf2f7f7ff8214 192.168.1.103:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 7ec346484effbe562ea0bf3e40ed9e9538b973b7 192.168.1.103:6380
   slots: (0 slots) slave
   replicates e8aef9ed8e05b38f8bc65879bd30a825b306c7b6
S: 66559e06fefa1e6aa4c73f6141addc750df16406 192.168.1.107:6380
   slots: (0 slots) slave
   replicates 3baa5bc431925f1099561bc82355c992e19c42f8
M: 3baa5bc431925f1099561bc82355c992e19c42f8 192.168.1.106:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: fe233c5c139ea7bb7c2b91c94b8efd6d6f592096 192.168.1.106:6380
   slots: (0 slots) slave
   replicates d7d899e9ad644e86641f82bb97adf2f7f7ff8214
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

6. 测试

    看到set key如果有key能够重定向存储到其他节点证明成功了,注意这里要卡其redis客户端要加 -c 进入集群模式,否则当一个key需要存储到其他节点时,会报错无法移动到其他节点上

[root@localhost redis-6380]# docker exec -it redis-6379 redis-cli -c
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name whf
-> Redirected to slot [5798] located at 192.168.1.103:6379
OK
192.168.1.103:6379> set age 19
-> Redirected to slot [741] located at 192.168.1.107:6379
OK