docker pull redis:5.0.2
docker pull ruby
docker create network redis-net
##查看网卡信息
docker network ls
备注:创建redis-net虚拟网卡 目的是让docker容器能与宿主(centos7)桥接网络 并间接与外界连接
#创建目录
mkdir /usr/local/docker/redis-cluster
#进入创建的目录中
cd /home/redis-cluster
#创建模版文件
touch redis-cluster.tmpl
#编辑模板文件
vim redis-cluster.tmpl
#节点端口 port ${PORT} #保护模式 protected-mode no #cluster集群模式 cluster-enabled yes #集群配置名 cluster-config-file nodes-${PORT}.conf #超时时间 cluster-node-timeout 5000 #实际为各节点网卡分配ip cluster-announce-ip 172.18.148.100 #节点映射端口 cluster-announce-port ${PORT} #节点总线端 cluster-announce-bus-port 1${PORT} #持久化模式 appendonly yes #持久化文件 appendfilename appendonly-${PORT}.aof #数据文件 dbfilename dump-${PORT}.rdb #日志 logfile ${PORT}.log
for port in `seq 6389 6394`; do
mkdir -p ./conf && PORT=${port} envsubst < ./redis-cluster.tmpl > ./conf/redis-${port}.conf && mkdir -p ./data;
done
for port in `seq 6389 6394`; do
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
--privileged=true -v /usr/local/docker/redis-cluster/conf/redis-${port}.conf:
/usr/local/etc/redis/redis.conf \
--privileged=true -v /usr/local/docker/redis-cluster/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis:5.0.2 redis-server /usr/local/etc/redis/redis.conf; done
备注:命令译为 循环6389- 6394 运行redis 容器
docker run 运行
-d 守护进程模式
–restart always 保持容器启动
–name redis-710* 容器起名
–net redis-net 容器使用虚拟网卡
-p 指定宿主机器与容器端口映射 6389:6389
-P 指定宿主机与容器redis总线端口映射 16389:16389
–privileged=true -v /usr/local/docker/redis-cluster/conf/redis-6389.conf:/usr/local/etc/redis/redis.conf
授权将宿主6389节点文件挂载到容器/usr/local/etc/redis/redis.conf 文件中
–privileged=true -v /usr/local/docker/redis-cluster/data:/data 授权将宿主701/data目录挂载到容器/data目录中
–sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; 容器根据挂载的配置文件启动 redis服务端
docker ps
docker network inspect redis-net
“6a8f89a639d310932e3eb955324f3388c490656b1e5dc71352ae79e17d401145”: {
“Name”: “redis-6390”,
“EndpointID”: “c3a02d83c685c86088af06b1c891b7d5bdc8079877009147d5a055f170a1b93a”,
“MacAddress”: “02:42:ac:13:00:03”,
“IPv4Address”: “172.19.0.3/16”,
“IPv6Address”: “”
},
“9f0f24420f87a8bd4c5b15e8f7ac81549608a1858cde443dd6916889cbf2583c”: {
“Name”: “redis-6391”,
“EndpointID”: “15eaeca6d1eafd4f1bba45507b91ed3c366e27021de7dd5b07a205de68996d1”,
“MacAddress”: “02:42:ac:13:00:04”,
“IPv4Address”: “172.19.0.4/16”,
“IPv6Address”: “”
},
“cbacacf9081da8985cc13c7d570531416a6448696ccaf884f3cb72a88c1486a8”: {
“Name”: “redis-6394”,
“EndpointID”: “8eb2163c0b2e6f2704a62b58834f1846f073c154a9b4834a5f1b6db8e3e6a0aa”,
“MacAddress”: “02:42:ac:13:00:07”,
“IPv4Address”: “172.19.0.7/16”,
“IPv6Address”: “”
},
“cd10ae51a5b1a035dc8e5d7a2608e7da43c89e2085291103da8b8195c30bdb97”: {
“Name”: “redis-6389”,
“EndpointID”: “53f3f9205ede2a0ef7e3e67a6c48ff1d700865b44ce8d3df17fd5cc127e4260bc0”,
“MacAddress”: “02:42:ac:13:00:02”,
“IPv4Address”: “172.19.0.2/16”,
“IPv6Address”: “”
},
“ecf8475f639b6d1160c17a60a8d86a5fb6b92ed8f50d85c4fb42ef57a324e05b”: {
“Name”: “redis-6392”,
“EndpointID”: “d1bd466632ee163e789d25a31b970745a54787ae4037dd5af371362fd”,
“MacAddress”: “02:42:ac:13:00:05”,
“IPv4Address”: “172.19.0.5/16”,
“IPv6Address”: “”
},
“f3228d38bdf81f66e2cbc6182c3910909bd5c0df0d163618edd2e37e517edd5c”: {
“Name”: “redis-6393”,
“EndpointID”: “1a7f2913cf1c1f72eb116c8992b179f767ad78197a47f982670f3f683d0c9f4d”,
“MacAddress”: “02:42:ac:13:00:06”,
“IPv4Address”: “172.19.0.6/16”,
“IPv6Address”: “”
}
#编辑ip为查询分配的ip
vim /usr/local/docker/redis-cluster/conf/redis-63*.conf
cluster-announce-ip 172.19.0.2
备注:6个都要修改redis.conf
for port in `seq 7010 7015`; do \
docker stop redis-${port};
docker rm redis-${port};
done
for port in `seq 6389 6394`; do
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
--privileged=true -v /usr/local/docker/redis-cluster/conf/redis-${port}.conf:
/usr/local/etc/redis/redis.conf \
--privileged=true -v /usr/local/docker/redis-cluster/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis:5.0.2 redis-server /usr/local/etc/redis/redis.conf;
done
for port in `seq 6389 6394`; do
echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port}" " ;
done
#登陆其中一个容器
docker exec -it redis-6389 /bin/bash
#在容器中执行
redis-cli --cluster create --cluster-replicas 1 \
"$(for port in `seq 6389 6394`; do \
echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port}" " ; \
done)"
参考:https://blog.csdn.net/qq_36102959/article/details/83303001