以下內(nèi)容操作在阿里云centos服務器:
1.安裝docker
yum install docker
2.拉取Redis鏡像
docker pull redis
3.準備集群文件
mkdir /home/redis-cluster
4.在/home/redis-cluster目錄下面新建Redis配置模板redis-cluster.tmpl內(nèi)容如下:
port ${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 39.10X.XX.XX //每個容器自己的IP(有了ip后再修改)
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
5.容器直接采用的docker網(wǎng)絡創(chuàng)建
docker network create redis-net
可以執(zhí)行命令docker network inspect redis-net
查看網(wǎng)絡情況.
查看到網(wǎng)管地址后,下面每個容器的cluster-announce-ip 可以推斷出來(不知道準不準)
6.在redis-cluster目錄下面生成6個文件夾桑涎,從7000到7005着绷,每個文件夾下包含data和conf文件夾,同時conf里面有redis.conf配置文件
for port in `seq 7000 7005`; do \
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done
7.啟動6個Redis容器,為了獲取容器的ip地址.
for port in `seq 7000 7005`; do \
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
-v /home/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /home/redis-cluster/${port}/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
done
8 查看容器的IP信息
docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-7000"
列出所有容器的IP信息以及端口,作為創(chuàng)建集群的參數(shù)(后面使用)
(for port in `seq 7000 7005`; do \
echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port} ' ' ; \
done)
9.停止并刪除所有容器,
docker stop $(docker ps -q -a)
docker rm $(docker ps -q -a)
10. 修改/home/redis-cluster/700*/conf/redis.conf文件中cluster-announce-ip 為對應的真實ip地址
11. 刪除/home/redis-cluster/700*/conf/data/下面的內(nèi)容
12 重新執(zhí)行第7步,啟動6個容器.
13 進入某個容器創(chuàng)建集群
docker exec -it redis-7000 bash
redis-cli --cluster create 172.19.0.2:7000 172.19.0.3:7001 172.19.0.4:7002 172.19.0.5:7003 172.19.0.6:7004 172.19.0.7:7005 --cluster-replicas 1
14創(chuàng)建密碼
分別連接每個Redis容器設置密碼:redis-cli -p 7000
config set masterauth passwordstr
config set requirepass passwordstr
config rewrite
15連接集群
redis-cli -p 7000 -c -a passwordstr