合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
>[info] redis集群部署实战 1. 创建一个 `redis` 网卡 ``` docker network create redis --subnet 192.168.0.0/16 docker network inspect redis ``` ![](https://img.kancloud.cn/a1/6c/a16cc2e61bcac04211353fc7cc61576e_877x376.png) 2. 使用 `shell` 脚本创建配置 ``` # 注:0.0.0.0 变成 ... 和 192.169..1 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 192.168.0.1${port} cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes EOF done ``` ![](https://img.kancloud.cn/f1/ac/f1ac1d57d79b1d9c20a0059c101a7e0f_723x364.png) 3. 启动6个容器 ```cnosole 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 192.168.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 例: 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 192.168.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6372:6379 -p 16372:16379 --name redis-2 -v /mydata/redis/node-2/data:/data -v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 192.168.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6373:6379 -p 16373:16379 --name redis-3 -v /mydata/redis/node-3/data:/data -v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 192.168.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6374:6379 -p 16374:16379 --name redis-4 -v /mydata/redis/node-4/data:/data -v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 192.168.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6375:6379 -p 16375:16379 --name redis-5 -v /mydata/redis/node-5/data:/data -v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 192.168.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6376:6379 -p 16376:16379 --name redis-6 -v /mydata/redis/node-6/data:/data -v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 192.168.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf ``` ![](https://img.kancloud.cn/7a/0a/7a0a0cb6556c82122acbbb824132fd9d_1132x357.png) 4. 创建集群 ```console # 1. 进入 redis1 创建集群 docker exec -it redis-1 /bin/sh # 2. 创建集群 redis-cli --cluster create 192.168.0.11:6379 192.168.0.12:6379 192.168.0.13:6379 192.168.0.14:6379 192.168.0.15:6379 192.168.0.16:6379 --cluster-replicas 1 ``` ![](https://img.kancloud.cn/54/8d/548d2d1a9e724a5dd88e873de20a1069_873x576.png) 5. 连接集群 ```console # 进入 redis1 连接集群 redis-cli -c ``` ```console # 查看集群信息 cluster info ``` ![](https://img.kancloud.cn/9d/f1/9df17845dc38f5799dc259696b1af3c3_882x363.png) ```console # 查看节点信息 cluster nodes ``` ![](https://img.kancloud.cn/9d/5b/9d5be25ca5cb3ae3649aca4df61fda8e_1144x253.png) 6. 示例 ```console # 设置一个值(存储在 13 上) set a b ``` ![](https://img.kancloud.cn/69/de/69deee82068f8eea5e07fb0f1d208def_650x112.png) ```console # 停止掉 13 容器 docker stop redis-3 ``` ![](https://img.kancloud.cn/48/16/481628e73e15985d4b00294473379c8b_1125x437.png) ```console # 获取a的值(发现在 14 上面) get a ``` ![](https://img.kancloud.cn/d4/09/d4096365a148d833893e267358c11189_894x175.png) ```console # 再次获取集群节点信息(发现 13 已经挂掉 并且 14 代替了13 成为 master) cluster nodes ``` ![](https://img.kancloud.cn/59/19/5919aab90711323a5216ec0c1ecd0fef_1146x263.png) ```console # 重新启动 13 容器发现 变成了 slave docker start redis-3 cluster nodes ``` ![](https://img.kancloud.cn/87/d7/87d722675fea7a852fe67df15377f31e_1154x292.png)