环境安装
linux :centos 7
docker :19.03.3
docker-compose:1.24.1
创建目录和编排文件
1.在工作目录下创建目录redis
2.然后分别创建文件夹 6000-6005(以节点端口号命名的)
3.创建 docker-compose.yaml 文件
4.在各个端口目录下创建data目录和redis.conf配置文件
创建完成目录结构如下:
redis目录结构
各个端口目录结构,比如6000目录下
编写redis.conf 配置文件
#端口
port 6000
##run in background 必须是no
daemonize no
##enable redis-cluster
cluster-enabled yes
##cluster node config file name
cluster-config-file nodes_6000.conf
## timeout
tcp-keepalive 300
timeout 0
##aof
appendonly yes
#当daemonize = yes时候才会生效
pidfile /var/run/redis_6000.pid
#log
#日志等级
loglevel notice
#日志文件存放路径
logfile "redis_6000.log"
#集群对外ip
cluster-announce-ip 机器ip
每个节点的配置只需改变端口。
docker-compose.yaml 配置文件
version: '3.4'
services:
node1:
image: redis:5.0.5
container_name: redis-node1
restart: always
ports:
- 6000:6000
- 16000:16000
volumes:
- /home/gwms/redis/6000/data:/data
- /home/gwms/redis/6000:/usr/local/etc/redis
command:
redis-server /usr/local/etc/redis/redis.conf
node2:
image: redis:5.0.5
container_name: redis-node2
restart: always
ports:
- 6001:6001
- 16001:16001
volumes:
- /home/gwms/redis/6001/data:/data
- /home/gwms/redis/6001:/usr/local/etc/redis
command:
redis-server /usr/local/etc/redis/redis.conf
node3:
image: redis:5.0.5
container_name: redis-node3
restart: always
ports:
- 6002:6002
- 16002:16002
volumes:
- /home/gwms/redis/6002/data:/data
- /home/gwms/redis/6002:/usr/local/etc/redis
command:
redis-server /usr/local/etc/redis/redis.conf
node4:
image: redis:5.0.5
container_name: redis-node4
restart: always
ports:
- 6003:6003
- 16003:16003
volumes:
- /home/gwms/redis/6003/data:/data
- /home/gwms/redis/6003:/usr/local/etc/redis
command:
redis-server /usr/local/etc/redis/redis.conf
node5:
image: redis:5.0.5
container_name: redis-node5
restart: always
ports:
- 6004:6004
- 16004:16004
volumes:
- /home/gwms/redis/6004/data:/data
- /home/gwms/redis/6004:/usr/local/etc/redis
command:
redis-server /usr/local/etc/redis/redis.conf
node6:
image: redis:5.0.5
container_name: redis-node6
restart: always
ports:
- 6005:6005
- 16005:16005
volumes:
- /home/gwms/redis/6005/data:/data
- /home/gwms/redis/6005:/usr/local/etc/redis
command:
redis-server /usr/local/etc/redis/redis.conf
编写完成后,在redis目录下 执行 docker-compose up -d
启动容器。然后通过docker ps 命令查看redis进程是否正常,我在安装时候遇到一些坑,下文我会详细描述。
状态是up 就是启动正常了。
创建集群
目前只是启动了六个实例,并没有组成集群。
进入任意一个Redis节点容器,命令如下:
docker exec -it redis-node1 bash
继续执行以下命令创建集群:
redis-cli --cluster create ip:6000 ip:6001 ip:6002 ip:6003 ip:6004 ip:6005 --cluster-replicas 1
其中ip 为服务器ip
命令执行结果如下:遇到yes 选择yes。
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.35.30.39:6375 to 10.35.30.39:6371
Adding replica 10.35.30.39:6376 to 10.35.30.39:6372
Adding replica 10.35.30.39:6374 to 10.35.30.39:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: e9a35d6a9d203830556de89f06a3be2e2ab4eee1 10.35.30.39:6371
slots:[0-5460] (5461 slots) master
M: 0c8755144fe6a200a46716371495b04f8ab9d4c8 10.35.30.39:6372
slots:[5461-10922] (5462 slots) master
M: fcb83b0097d2a0a87a76c0d782de12147bc86291 10.35.30.39:6373
slots:[10923-16383] (5461 slots) master
S: b9819797e98fcd49f263cec1f77563537709bcb8 10.35.30.39:6374
replicates fcb83b0097d2a0a87a76c0d782de12147bc86291
S: f4660f264f12786d81bcf0b18bc7287947ec8a1b 10.35.30.39:6375
replicates e9a35d6a9d203830556de89f06a3be2e2ab4eee1
S: d2b9f265ef7dbb4a612275def57a9cc24eb2fd5d 10.35.30.39:6376
replicates 0c8755144fe6a200a46716371495b04f8ab9d4c8
Can I set the above configuration? (type 'yes' to accept): yes # 这里输入 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 10.35.30.39:6371)
M: e9a35d6a9d203830556de89f06a3be2e2ab4eee1 10.35.30.39:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 0c8755144fe6a200a46716371495b04f8ab9d4c8 10.35.30.39:6372
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b9819797e98fcd49f263cec1f77563537709bcb8 10.35.30.39:6374
slots: (0 slots) slave
replicates fcb83b0097d2a0a87a76c0d782de12147bc86291
M: fcb83b0097d2a0a87a76c0d782de12147bc86291 10.35.30.39:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: f4660f264f12786d81bcf0b18bc7287947ec8a1b 10.35.30.39:6375
slots: (0 slots) slave
replicates e9a35d6a9d203830556de89f06a3be2e2ab4eee1
S: d2b9f265ef7dbb4a612275def57a9cc24eb2fd5d 10.35.30.39:6376
slots: (0 slots) slave
replicates 0c8755144fe6a200a46716371495b04f8ab9d4c8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到目前为止集群就搭建完毕了,进入任意一个容器执行如下命令,查看集群状态
1.docker exec -it redis-node1 /bin/bash
2.redis-cli -p 6000 -h 173.17.0.1 cluster info
此时集群状态是 ok ,节点数也是正常的。
遇到的坑
1.redis.conf 配置文件中 daemonize 如果为yes会和 docker-compose.yaml中restart: always配置冲突,导致 执行 docker-compose up -d 命令后通过 docker ps查看redis进程一直在restaring中。
2.redis.conf 配置文件中“cluster-config-file” 和 “logfile”,之前配置了子目录,如下:
cluster-config-file config/nodes_6000.conf
logfile "log/redis_6000.log"
这样也同样会导致,启动时候无限重启的问题,目前我不知道怎么解决,只好去除了子目录。
这个大家有啥解决方案吗,希望大佬们留言告诉我。
去除子目录后,生成的这些文件都在容器的/data目录下,由于我们在docker-compose.yaml中对磁盘做了如下映射:
volumes:
- /home/gwms/redis/6000/data:/data
所以这些生成的文件,我们可以在宿主机中的redis-> 端口文件夹->data中找到。
3.cluster-announce-ip 配置
第一次该配置没有写,结果是集群创建后,节点1 使用的是机器ip,而其他节点使用的是br-2a8667af806这个网卡的ip,导致的问题是,当客户端连接reids服务后,你设置一个key,如果路由到其他节点,此时连接的ip 会变成 该网卡的ip,从而客户端无法连接上,我们期望的是,路由后的节点ip依旧是是机器ip,所以我在redis.conf中加入了该配置,代表各个节点对外的ip地址是多少,从而解决了相关问题。
spring boot 集成redis
spring boot 项目中集成redis集群配置,进行简单的测试。
额外说明
1.restart: always 表示docker重启后,容器跟着一起重启,相当于开机启动。
2.boot 项目如果我们用了连接池,并且我们设置了:“连接池中的最小空闲连接”,那么服务启动完成后短暂延迟会会和各个节点创建 我们设置数量的连接,如果节点最大连接数 小于我们配置数,那么能创建多少创建多少。