使用docker redis主从集群

#!/bin/sh

CreateNodeDockerfile()
{
touch Dockerfile
cat > Dockerfile <<EOF
FROM centos:latest
ENV redis_home /usr/local/redis
RUN yum install -y make gcc acl && \\
	mkdir -p \$redis_home && \\
	mkdir -p /var/log/redis && \\
	touch /var/log/redis/redis-server.log
 
ADD redis-3.0.6.tar.gz /
COPY redis.conf \$redis_home

WORKDIR redis-3.0.6
RUN make && \\
	cp -rf src/redis-server \$redis_home 

RUN yum remove -y gcc make
RUN rm -rf redis-3.0.6 

EXPOSE 6379

WORKDIR /usr/local/redis
ENTRYPOINT ["/usr/local/redis/redis-server", "/usr/local/redis/redis.conf"]
EOF
}
 
CreateSentinelDockerfile()
{
touch Dockerfile
cat > Dockerfile <<EOF 
FROM centos:latest

ENV redis_home /usr/local/redis
RUN yum install -y make gcc acl && \\
	mkdir -p \$redis_home
 
ADD redis-3.0.6.tar.gz /
COPY sentinel.conf \$redis_home

WORKDIR redis-3.0.6
RUN make && \\
	cp -rf src/redis-sentinel \$redis_home 

RUN yum remove -y gcc make
RUN rm -rf redis-3.0.6 

WORKDIR /usr/local/redis
ENTRYPOINT ["/usr/local/redis/redis-sentinel", "/usr/local/redis/sentinel.conf"]
EOF
}


#参数不合法
if [ $# -ne 1 ]; then
	echo "argn error" 
	exit 2
fi

if [ $1 -eq 2 ]; then
	docker container stop redis-6379 redis-6380 redis-6381 sentinel1 sentinel2 sentinel3
	docker container rm redis-6379 redis-6380 redis-6381 sentinel1 sentinel2 sentinel3
	exit 0
fi

#删除Dockerfile
if [ -f "Dockerfile" ]; then
	`rm -rf "Dockfile"`
fi

cp redis-3.0.6/sentinel.conf ./ 
[ $? -ne 0 ] && exit 3

cp redis-3.0.6/redis.conf ./ 
[ $? -ne 0 ] && exit 4

if [ $1 -eq 1 ]; then
	sed -i '/^sentinel monitor/c sentinel monitor mymaster 172.17.0.2 6379 2' ./sentinel.conf
else
	#sed -i '/^# slaveof/c slaveof 172.17.0.2 6379' ./redis.conf
	sed -i '/^logfile/c logfile \"/var/log/redis/redis-server.log\"' ./redis.conf
fi

#生产dockerfile并且创建映像
if [[ $1 == 0 ]]; then
	CreateNodeDockerfile
	docker build -t redis-node:1.0 .  
	
	#docker container stop redis-6379 redis-6380 redis-6381
	#docker container rm redis-6379 redis-6380 redis-6381
	docker run --name redis-6379 -p 6379:6379  -d redis-node:1.0
	docker run --name redis-6380 -p 6380:6379  -d redis-node:1.0
	docker run --name redis-6381 -p 6381:6379  -d redis-node:1.0
	redis-3.0.6/src/redis-cli -h 172.17.0.3 slaveof 172.17.0.2 6379
	redis-3.0.6/src/redis-cli -h 172.17.0.4 slaveof 172.17.0.2 6379
else
	CreateSentinelDockerfile
	docker build -t redis-sentinel:1.0 .  
	#docker container stop sentinel1 sentinel2 sentinel3
	#docker container rm sentinel1 sentinel2 sentinel3
	docker run --name sentinel1 -d redis-sentinel:1.0
	docker run --name sentinel2 -d redis-sentinel:1.0
	docker run --name sentinel3 -d redis-sentinel:1.0
fi

  

posted @ 2019-01-08 19:11  buger(八哥)  阅读(311)  评论(0编辑  收藏  举报