使用docker搭建本地集群Redis服务(参考:https://www.cnblogs.com/slowbirdoflsh/p/11633113.html)
环境配置
# 拉取redis镜像
docker pull redis
# 查看镜像详情
docker inspect redis
创建redis集群网络
docker network create redis-cluster-net
集群挂在目录配置
1. 开启6个节点, 3对主从节点搭建集群
2. 开放7000-7005端口, 根据端口创建6个目录, 每个目录下创建data目录和配置文件redis-{port}.conf
3. 配置模板文件redis-cluster.tmpl
# 基本配置 ## 开放端口 port ${port} ## 不作为守护进程 daemonize no ## 启用aof持久化模式 appendonly yes # 集群配置 ## 开启集群配置 cluster-enabled yes ## 存放集群节点的配置文件 系统自动建立 cluster-config-file nodes-${port}.conf ## 节点连接超时时间 cluster-node-timeout 50000 ## 实际为各节点网卡分配ip cluster-announce-ip ${ip} ## 节点映射端口 cluster-announce-port ${port} ## 节点总线端口 cluster-announce-bus-port 1${port} cluster-slave-validity-factor 10 cluster-migration-barrier 1
初始化redis集群目录脚本(init.sh):
#!/bin/bash dir_redis_cluster='/home/lanlang/redis/redis-cluster' gateway='172.18.0.1' idx=1 for port in `seq 7000 7005`; do mkdir -p ${dir_redis_cluster}/${port}/data; idx=$(($idx+1)); port=${port} ip=`echo ${gateway} | sed "s/1$/$idx/g"` envsubst < ${dir_redis_cluster}/redis-cluster.tmpl > ${dir_redis_cluster}/${port}/redis-${port}.conf done
执行init.sh之后创建目录
查看生成的redis配置文件:
集群搭建
启动脚本(start.sh):
#!/bin/bash dir_redis_cluster='/home/lanlang/redis/redis-cluster' for port in `seq 7000 7005`; do docker run --name redis-${port} --net redis-cluster-net -d -p ${port}:${port} -p 1${port}:1${port} -v ${dir_redis_cluster}/${port}/data:/data -v ${dir_redis_cluster}/${port}/redis-${port}.conf:/usr/local/etc/redis/redis.conf redis redis-server /usr/local/etc/redis/redis.conf done
执行完成:
查看启动的服务:
docker ps
查看集群功能是否开启:
docker exec -it redis-7000 redis-cli -p 7000 info cluster
节点连接:
docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.3 7001 docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.4 7002 docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.5 7003 docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.6 7004 docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.7 7005
查看节点并进行主从配置:
docker exec -it redis-7000 redis-cli -p 7000 cluster nodes
配置7001节点为7000节点的从节点 docker exec -it redis-7001 redis-cli -p 7001 cluster replicate bbf710160c18a6267428714e887b33a04e557235 配置7003节点为7002节点的从节点 docker exec -it redis-7003 redis-cli -p 7003 cluster replicate 0c29ac82d4dbb68d48909906a902719cf05ca191 配置7005节点为7004节点的从节点 redis-cluster]# docker exec -it redis-7005 redis-cli -p 7005 cluster replicate c813df11ff91f18c49dd0931b4d5dcca3c43df9f
查看节点:
docker exec -it redis-7000 redis-cli -p 7000 cluster nodes
slots分配:
将16384个槽分配到3个主节点中每个节点平均分5461个槽 docker exec -it redis-7000 redis-cli -p 7000 cluster addslots {0..5460} # 7002 5461~10920 docker exec -it redis-7002 redis-cli -p 7002 cluster addslots {5461..10920} # 7004 10920~16383 docker exec -it redis-7004 redis-cli -p 7004 cluster addslots {10921..16383}
查看集群情况:
docker exec -it redis-7000 redis-cli -p 7000 cluster info
查看槽点情况:
docker exec -it redis-7000 redis-cli -p 7000 cluster slots
测试:
docker exec -it redis-7000 redis-cli -c -p 7000
常见问题:
1. 在启动redis服务的时候,WARNING: IPv4 forwarding is disabled. Networking will not work
vim /etc/sysctl.conf 新增 net.ipv4.ip_forward=1 重启network systemctl restart network 查看 sysctl net.ipv4.ip_forward