集群部署实例
话不多说,直接上代码…
# 创建桥接 网卡 docker network create redis --subnet 172.38.0.0/16 # 查看所有 网卡 docker network ls # 查看网卡详情 docker network inspect 网卡名称 # 通过脚本创建6个Redis配置 for port in $(seq 1 6); \ do \ mkdir -p /mydata/redis/node-${port}/conf touch /mydata/redis/node-${port}/conf/redis.conf cat << EOF >/mydata/redis/node-${port}/conf/redis.conf port 6379 bind 0.0.0.0 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 172.38.0.1${port} cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes EOF done # 启动容器 for port in $(seq 1 6); \ do \ docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \ -v /mydata/redis/node-${port}/data:/data \ -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf done docker run -p 6371:6379 -p 16371:16379 --name redis-1 \ -v /mydata/redis/node-1/data:/data \ -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf # 创建集群 docker exec -it redis-1 /bin/sh redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1 # 连接集群 redis-cli -c cluster nodes
1.镜像
https://hub.docker.com/_/redis
2.部署
2.1 创建专用网络
# 创建桥接网卡 docker network create redis --subnet 172.81.0.0/16 # 查看所有 网卡 docker network ls # 查看网卡详情 docker network inspect NETWORKID [ { "Name": "redis", "Id": "79a62c1104195f5f077665fcdd8d4e1c92697af7db227c1fd1a92219f22c2501", "Created": "2023-05-05T10:38:24.604493511+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.81.0.0/16", "Gateway": "172.81.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ]
2.2 生成配置文件
# 通过脚本创建6个Redis配置 for port in $(seq 1 6); \ do \ mkdir -p /mydata/redis/node-${port}/conf touch /mydata/redis/node-${port}/conf/redis.conf cat << EOF >/mydata/redis/node-${port}/conf/redis.conf port 6379 bind 0.0.0.0 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 172.81.0.1${port} cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes EOF done
2.3 容器启动及验证
# 启动容器 for port in $(seq 1 6); \ do \ docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \ -v /mydata/redis/node-${port}/data:/data \ -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.81.0.1${port} redis:5.0.14 redis-server /etc/redis/redis.conf done
2.4 创建集群
# 创建集群 docker exec -it redis-1 /bin/sh cd /usr/local/bin redis-cli --cluster create 172.81.0.11:6371 172.81.0.12:6372 172.81.0.13:6373 172.81.0.14:6374 172.81.0.15:6375 172.81.0.16:6376 --cluster-replicas 1 cd /usr/local/bin redis-cli --cluster create 172.81.0.11:6379 172.81.0.12:6379 172.81.0.13:6379 172.81.0.14:6379 172.81.0.15:6379 172.81.0.16:6379 --cluster-replicas 1 # 连接集群 cd /usr/local/redis-5.0.14 ./bin/redis-cli -p 6371 -c cluster nodes
2.5 删除容器及配置文件
for port in $(seq 1 6); \ do \ docker stop redis-${port} done docker rm $(docker ps -qa) rm -rf ./redis-docker
3.测试
./bin/redis-cli -c -p 6379 --intrinsic-latency 10 ./bin/redis-benchmark -h 172.81.0.1 -p 6379 -q