欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

centos6使用docker部署redis主从数据库操作示例

程序员文章站 2022-07-06 17:53:30
本文实例讲述了centos6使用docker部署redis主从数据库操作。分享给大家供大家参考,具体如下:目录结构:/redis /dockerfile /readme /redis-3.2.8.ta...

本文实例讲述了centos6使用docker部署redis主从数据库操作。分享给大家供大家参考,具体如下:

目录结构:

/redis

    /dockerfile

    /readme

    /redis-3.2.8.tar.gz

    /start.sh

dockerfile:

from centos
maintainer qiongtao.li hnatao@126.com
add ./redis-3.2.8.tar.gz /opt
add ./start.sh /opt/start_redis.sh
run echo "asia/shanghai" > /etc/timezone \
 && cp /usr/share/zoneinfo/asia/shanghai /etc/localtime \
 && yum -y install gcc make \
 && ln -s /opt/redis-3.2.8 /opt/redis \
 && cd /opt/redis && make && make install
expose 6379
entrypoint ["sh", "/opt/start_redis.sh"]

start.sh

role=$1
port=6379
password=abc123
redis_conf=/opt/redis/redis.conf
dir=/data/redis
logfile=/data/redis/redis.log
mkdir -p $dir
sed -i "s|bind 127.0.0.1|bind 0.0.0.0|g" $redis_conf
sed -i "s|# requirepass foobared|requirepass ${password}|g" $redis_conf
sed -i "s|dir ./|dir ${dir}|g" $redis_conf
sed -i "s|logfile \"\"|logfile ${logfile}|g" $redis_conf
if [ "$role" == "slave" ]; then
 echo "slave"
 sed -i "s|# slaveof <masterip> <masterport>|slaveof redis-master ${port}|g" $redis_conf
 sed -i "s|# masterauth <master-password>|masterauth ${password}|g" $redis_conf
else
 echo "master"
fi
redis-server $redis_conf

readme

docker rm -f redis-master redis-slave-1 redis-slave-2
docker rmi -f redis
docker build -t redis .
docker run -d --name redis-master redis
docker run -d --name redis-slave-1 --link redis-master:redis-master redis slave
docker run -d --name redis-slave-2 --link redis-master:redis-master redis slave
docker ps -a
docker exec redis-master redis-cli -a abc123 set name hnatao
docker exec redis-master redis-cli -a abc123 get name
docker exec redis-slave-1 redis-cli -a abc123 get name
docker exec redis-slave-2 redis-cli -a abc123 get name
docker exec redis-master grep -e "bind|dir|require|logfile|slaveof|masterauth" /opt/redis/redis.conf
docker exec redis-slave-1 grep -e "bind|dir|require|logfile|slaveof|masterauth" /opt/redis/redis.conf
docker exec redis-slave-2 grep -e "bind|dir|require|logfile|slaveof|masterauth" /opt/redis/redis.conf
docker exec redis-master redis-cli -a abc123 info
docker exec redis-slave-1 redis-cli -a abc123 info
docker exec redis-slave-2 redis-cli -a abc123 info

测试安装部署

cat readme|while read line; do $line; done

希望本文所述对大家docker容器使用有所帮助。