掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
在一臺Linux服務(wù)器上使用docker搭建一個cluster模式的redis集群。三個master節(jié)點,三個slave節(jié)點,六個節(jié)點因為在同一臺服務(wù)器上,所以每個節(jié)點使用不同的端口,端口范圍是6380到6385。

redis cluster集群具有如下幾個特點:
可以選擇指定版本的redis,本文為了方便演示,使用最新版本
docker pull redis
docker network create rediscluster
因為六個節(jié)點監(jiān)聽端口不同,所以配置文件也有區(qū)別,可以使用如下shell腳本生成對應(yīng)配置文件(假如命名為createRedisConf.sh):
#!/bin/sh
for port in $(seq 6380 6385);
do
mkdir -p ~/redisCluster/node-${port}/conf
touch ~/redisCluster/node-${port}/conf/redis.conf
cat << EOF > ~/redisCluster/node-${port}/conf/redis.conf
#節(jié)點端口
port ${port}
#添加訪問認證
requirepass luduoxin
#如果主節(jié)點開啟了訪問認證,從節(jié)點訪問主節(jié)點需要認證
masterauth luduoxin
#保護模式,默認值 yes,即開啟。開啟保護模式以后,需配置 bind ip 或者設(shè)置訪問密碼;關(guān)閉保護模式,外部網(wǎng)絡(luò)可以直接訪問
protected-mode no
#bind 0.0.0.0
#是否以守護線程的方式啟動(后臺啟動),默認 no
daemonize no
#是否開啟 AOF 持久化模式,默認 no
appendonly yes
#是否開啟集群模式,默認 no
cluster-enabled yes
#集群節(jié)點信息文件
cluster-config-file nodes.conf
#群節(jié)點連接超時時間
cluster-node-timeout 5000
#集群節(jié)點 IP,我使用的服務(wù)的ip為172.16.3.110,替換為自己的服務(wù)器的即可
cluster-announce-ip 172.16.3.110
#集群節(jié)點映射端口
cluster-announce-port ${port}
#集群節(jié)點總線端口
cluster-announce-bus-port 1${port}
EOF
done
創(chuàng)建此文件后,先賦予執(zhí)行權(quán)限,然后執(zhí)行生成配置文件
$ chmod 755 ./createRedisConf.sh
$ sh ./createRedisConf.sh
可以使用如下shell腳本快速創(chuàng)建出來(createRedis.sh)。
#!/bin/sh
## 首次創(chuàng)建并運行redis容器
if [ "$1" = "create" ]; then
for port in $(seq 6380 6385);
do
docker run -di --restart always --name redis-${port} --net rediscluster -p ${port}:${port} -p 1${port}:1${port} -v ~/redisCluster/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redisCluster/node-${port}/data:/data redis redis-server /usr/local/etc/redis/redis.conf
done
fi
##停止redis容器
if [ "$1" = "stop" ]; then
for port in $(seq 6380 6385);
do
docker stop redis-${port}
done
fi
##啟動已有的redis容器
if [ "$1" = "start" ]; then
for port in $(seq 6380 6385);
do
docker start redis-${port}
done
fi
## 刪除redis容器
if [ "$1" = "rm" ]; then
for port in $(seq 6380 6385);
do
docker rm redis-${port}
done
fi
創(chuàng)建此文件后,先賦予執(zhí)行權(quán)限,然后執(zhí)行創(chuàng)建出redis服務(wù)
$ chmod 755 ./createRedis.sh
$ sh ./createRedis.sh create
創(chuàng)建完成后,可以使用命令 docker ps -a 查看容器。
選擇一個redis容器,例如選擇redis-6380容器,進入容器。
docker exec -it redis-6380 /bin/bash
在容器里面執(zhí)行如下命令。
redis-cli -a luduoxin --cluster create 172.16.3.110:6380 172.16.3.110:6381 172.16.3.110:6382 172.16.3.110:6383 172.16.3.110:6384 172.16.3.110:6385 --cluster-replicas 1
然后出現(xiàn)如下提示,輸入 yes 繼續(xù)。
Can I set the above configuration ? (type 'yes' to accept):
創(chuàng)建過程如下:
# redis-cli -a luduoxin --cluster create 172.16.3.110:6380 172.16.3.110:6381 172.16.3.110:6382 172.16.3.110:6383 172.16.3.110:6384 172.16.3.110:6385 --cluster-replicas 1
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 172.16.3.110:6384 to 172.16.3.110:6380
Adding replica 172.16.3.110:6385 to 172.16.3.110:6381
Adding replica 172.16.3.110:6383 to 172.16.3.110:6382
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 78891932599b7497c8dd921295ba19eb0f549285 172.16.3.110:6380
slots:[0-5460] (5461 slots) master
M: 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081 172.16.3.110:6381
slots:[5461-10922] (5462 slots) master
M: 254d130ac0f88ec36be9cda27e59500e04c283cc 172.16.3.110:6382
slots:[10923-16383] (5461 slots) master
S: e2875613c12f0754e485e5eb56d968dd78493bae 172.16.3.110:6383
replicates 78891932599b7497c8dd921295ba19eb0f549285
S: a87af69f190a86455864c5ca73fabb60290abd1e 172.16.3.110:6384
replicates 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081
S: 846fa01cecc7fa75fc558eb8fcfb2788abb2a83d 172.16.3.110:6385
replicates 254d130ac0f88ec36be9cda27e59500e04c283cc
Can I set the above configuration? (type 'yes' to accept): 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 172.16.3.110:6380)
M: 78891932599b7497c8dd921295ba19eb0f549285 172.16.3.110:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 254d130ac0f88ec36be9cda27e59500e04c283cc 172.16.3.110:6382
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081 172.16.3.110:6381
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a87af69f190a86455864c5ca73fabb60290abd1e 172.16.3.110:6384
slots: (0 slots) slave
replicates 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081
S: 846fa01cecc7fa75fc558eb8fcfb2788abb2a83d 172.16.3.110:6385
slots: (0 slots) slave
replicates 254d130ac0f88ec36be9cda27e59500e04c283cc
S: e2875613c12f0754e485e5eb56d968dd78493bae 172.16.3.110:6383
slots: (0 slots) slave
replicates 78891932599b7497c8dd921295ba19eb0f549285
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到這里集群就創(chuàng)建成功了。
檢查集群狀態(tài)。
redis-cli -a luduoxin --cluster check 172.16.3.110:6382
連接集群某個節(jié)點。
$ redis-cli -c -a luduoxin -h 172.16.3.110 -p 6382
//查看集群信息
172.16.3.110:6381> cluster info
//查看集群結(jié)點信息
172.16.3.110:6381> cluster nodes
//查看集群的slot分配區(qū)間及對應(yīng)的主從節(jié)點映射關(guān)系
172.16.3.110:6381> cluster slots
本文主要講了如何在一臺Linux服務(wù)器上使用docker搭建一個cluster模式的redis集群,這種方式搭建的集群主要用于測試用途,不建議在生產(chǎn)環(huán)境使用。

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流