Docker 搭建redis哨兵、cluster集群

Docker 搭建redis哨兵、cluster集群

  • Docker搭建哨兵集群
    • linux脚本sentinel.sh
  • Docker搭建cluster集群
    • linux脚本redis-cluster.sh

基于redis5.0版本搭建

Docker搭建哨兵集群

使用创建文件sentinel.sh复制下面代码,执行sh sentinel.sh 挂载目录 slave开始端口 slave结束端口 master端口
创建多个从服务器和一个master服务器,一个哨兵监听主从
实例:sh sentinel.sh /Users/wshihua/code/docker/redis 6370 6375 63001

linux脚本sentinel.sh

#############################redis sentinel集群 搭建脚本###########################
vpath=$1
p1=$2
p2=$3
m1=$4

echo "第一个参数挂载路径"${vpath}"第二个参数从服务端口开始"${p1}"第三个参数从服务端口结束"${p2}"第四个参数主节点端口"${m1}

docker pull redis:5.0.2
docker network create redis-net

mkdir sentinel

mkdir  -p ${vpath}/sentinel/redis-master/conf
cd ${vpath}/sentinel/redis-master/conf
touch redis.conf
echo '
##节点端口
port '${m1}'
##持久化模式
appendonly yes
'>redis.conf

cd ${vpath}

docker stop redis-master
docker rm redis-master
docker run -d --network redis-net --name redis-master \
-v ${vpath}/sentinel/redis-master/data:/data \
-v ${vpath}/sentinel/redis-master/conf/redis.conf:/user/local/redis.conf \
--sysctl net.core.somaxconn=1024 redis:5.0.2 redis-server /user/local/redis.conf \
--appendonly yes --port ${m1}

master_ip=$(docker inspect --format '{{(index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-master"); 
echo 启动redis-master成功 ip=${master_ip}

echo 启动redis从服务,为每个子节点创建文件夹
for port in `seq ${p1} ${p2}`; do \
  mkdir  -p ./sentinel/slave-${port}/conf \
  && mkdir  -p ./sentinel/slave-${port}/data; \
cd ${vpath}/sentinel/slave-${port}/conf 
touch redis.conf
echo '
##节点端口
port '${port}'
##持久化模式
appendonly yes
'>redis.conf

cd ${vpath}
docker stop redis-slave-${port}
docker rm redis-slave-${port}
docker run -d --network redis-net --name redis-slave-${port} -v ${vpath}/sentinel/slave-${port}/data:/data \
-v ${vpath}/sentinel/redis-master/conf/redis.conf:/user/local/redis.conf \
--sysctl net.core.somaxconn=1024 redis:5.0.2 redis-server /user/local/redis.conf \
--appendonly yes --port ${port} --slave-read-only yes --slaveof ${master_ip} ${m1}
done

 mkdir -p ${vpath}/sentinel/sentinel-01/conf
 mkdir -p ${vpath}/sentinel/sentinel-01/data
 cd ${vpath}/sentinel/sentinel-01/conf
 touch sentinel.conf

echo '
port 37801
logfile "sentinel-01.log"
sentinel monitor sentinel-master '${master_ip}' '${m1}' 1
sentinel down-after-milliseconds sentinel-master 30000
sentinel parallel-syncs sentinel-master 1
sentinel failover-timeout sentinel-master 180000
'> ${vpath}/sentinel/sentinel-01/conf/sentinel.conf

docker stop redis-master-sentinel
docker rm redis-master-sentinel

 docker run -p 26379:26379 --network redis-net -v ${vpath}/sentinel/sentinel-01/data:/data -v ${vpath}/sentinel/sentinel-01/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf --name redis-master-sentinel -itd redis:5.0.2 /usr/local/etc/redis/sentinel.conf --sentinel
#############################redis sentinel集群 搭建脚本###########################

Docker搭建cluster集群

实例:sh redis-cluster.sh /Users/wshihua/code/docker/redis 6300 6305

linux脚本redis-cluster.sh

###########redis cluster集群 搭建脚本#########
vpath=$1
p1=$2
p2=$3
echo "第一个参数挂载路径"${vpath}"第二个参数端口开始"${p1}"第三个参数端口结束"${p3}

docker pull redis:5.0.2
docker network create redis-net

##在home 目录下创建 集群配置文件加
mkdir cluster 
 ##在创建目录中
cd cluster  
##创建模版件
touch redis-cluster.tmpl

echo '
##节点端口
port ${PORT}
##cluster集群模式                 
cluster-enabled yes
##集群配置名             
cluster-config-file nodes.conf   
##超时时间  
cluster-node-timeout 5000
##实际为各节点网卡分配ip  先用上网关ip代替 
cluster-announce-ip 172.18.0.1  
##节点映射端口
cluster-announce-port ${PORT}    
##节点总线端
cluster-announce-bus-port 1${PORT}
##持久化模式
appendonly yes
'>redis-cluster.tmpl

echo 为每个节点创建文件夹
for port in `seq ${p1} ${p2}`; do \
  mkdir -p ./${port}/conf \
  && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
  && mkdir -p ./${port}/data; \
done

echo 启动全部容器
for port in `seq ${p1} ${p2}`; do \
  docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
  --privileged=true -v ${vpath}/cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
  --privileged=true -v ${vpath}/cluster/${port}/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

echo 替换端口为docker分配的ip
for port in `seq ${p1} ${p2}`; do \
ip=$(docker inspect --format '{{(index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")
find ./${port}/conf -name 'redis.conf'|xargs perl -pi -e 's 
|172.18.0.1|'$ip'|g'
done

echo 重启全部容器
for port in `seq ${p1} ${p2}`; do \
  docker restart redis-${port};
done

 echo 启动集群
 iplist="";
 for port in `seq ${p1} ${p2}`; do 
 ip=$(docker inspect --format '{{(index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}"):${port}; 
 iplist=${iplist}" "${ip}
 done
 echo ${iplist}
 sh="/usr/local/bin/redis-cli --cluster create --cluster-replicas 1 ${iplist}"
 echo ${sh}
 echo yes | docker run -i --rm --net redis-net redis:5.0.2 sh -c "${sh}"

##########redis集群 搭建脚本########

你可能感兴趣的:(docker,cluster,sentinel)