目录

1. 前期准备

2. 导入镜像

3. 部署redis master脚本

4. 部署redis slave脚本

5. 模板文件

6. 部署redis

7. 基本维护


1. 前期准备

新部署前可以仓库repository下载 redis 镜像,或者从已有部署中的镜像生成文件

# 查看当前本地镜像
docker images
# 生成本镜像文件
docker save <IMAGE ID> –o redis_503.tar

2. 导入镜像

# 在新部署的机器导入镜像
docker load -i redis_503.tar
docker images

3. 部署redis master脚本

redismasterdeploy.sh 文件内容如下

#!/bin/bash

if [ ! -n "$1" ] ;then
    echo "执行方式redis-master-deploy.sh 镜像 redis起始端口号,如 redis-master-deploy.sh a.com:8443/dockerhub/redis:5.0.3 7001"
    exit
fi

if [ ! -n "$2" ] ;then
    echo "执行方式:redis-master-deploy.sh 镜像 redis起始端口号,如 redis-master-deploy.sh a.com:8443/dockerhub/redis:5.0.3 7001"
    exit
fi

image=${1////\/}
port=$2
len=${#port}

for ((var=0; var<$len; var++))
do
   a=${port:$var:1}
   if [ $var -eq 0 ]; then
     sentinel_port=$(($a+1))
   elif [ $var -eq $(($len-1)) ]; then
     sentinel_port=${sentinel_port}1
   else
     sentinel_port=${sentinel_port}0
   fi
done

# redis-server
for i in {1..16}
do
  old_port=$((7000+$i))
  new_port=$(($port + $i -1))
  sed_para=$sed_para"s/${old_port}/${new_port}/g;"
  mkdir -p /usr/local/etc/redis/redis$i/
  cp -f ~/redis.conf.template /usr/local/etc/redis/redis$i/redis.conf
  sed -i 's/7001/'"${new_port}"'/g;s/redis1/redis'"${i}"'/g;' /usr/local/etc/redis/redis$i/redis.conf
done

cp -f ~/docker-compose-server.yml.template /usr/local/etc/redis/docker-compose.yml
sed -e ${sed_para} -i /usr/local/etc/redis/docker-compose.yml
sed -i 's/a.com:8443/dockerhub/redis:5.0.3/'"${image}"'/g;' /usr/local/etc/redis/docker-compose.yml

# sentinel
mkdir -p /usr/local/etc/redis/sentinel/
cp -f ~/sentinel.conf.template /usr/local/etc/redis/sentinel/sentinel.conf
ip=`hostname -I | awk '{print $1}'`
sed -i 's/172.18.29.56/'"${ip}"'/g;s/8001/'"${sentinel_port}"'/g;' /usr/local/etc/redis/sentinel/sentinel.conf
sed -e ${sed_para} -i /usr/local/etc/redis/sentinel/sentinel.conf
cp -f ~/docker-compose-sentinel.yml.template /usr/local/etc/redis/sentinel/docker-compose.yml
sed -i 's/a.com:8443/dockerhub/redis:5.0.3/'"${image}"'/g;s/8001/'"${sentinel_port}"'/g;' /usr/local/etc/redis/sentinel/docker-compose.yml

# start-all
chown -R polkitd:root /usr/local/etc/redis/
/usr/libexec/docker/cli-plugins/docker-compose -f /usr/local/etc/redis/docker-compose.yml up -d
/usr/libexec/docker/cli-plugins/docker-compose -f /usr/local/etc/redis/sentinel/docker-compose.yml up -d

4. 部署redis slave脚本

redis-slavedeploy.sh 文件内容如下

#!/bin/bash

if [ ! -n "$1" ] ;then
    echo "执行方式:redis-master-deploy.sh 镜像 redis起始端口号 master_ip,如 redis-master-deploy.sh a.com:8443/dockerhub/redis:5.0.3 7001 172.18.29.56"
    exit
fi

if [ ! -n "$2" ] ;then
    echo "执行方式:redis-master-deploy.sh 镜像 redis起始端口号 master_ip,如 redis-master-deploy.sh a.com:8443/dockerhub/redis:5.0.3 7001 172.18.29.56"
    exit
fi

if [ ! -n "$3" ] ;then
    echo "执行方式:redis-master-deploy.sh 镜像 redis起始端口号 master_ip,如 redis-master-deploy.sh a.com:8443/dockerhub/redis:5.0.3 7001 172.18.29.56"
    exit
fi

image=${1////\/}
port=$2
len=${#port}

for ((var=0; var<$len; var++))
do
   a=${port:$var:1}
   if [ $var -eq 0 ]; then
     sentinel_port=$(($a+1))
   elif [ $var -eq $(($len-1)) ]; then
     sentinel_port=${sentinel_port}1
   else
     sentinel_port=${sentinel_port}0
   fi
done

# redis-server
for i in {1..16}
do
  old_port=$((7000+$i))
  new_port=$(($port + $i -1))
  sed_para=$sed_para"s/${old_port}/${new_port}/g;"
  mkdir -p /usr/local/etc/redis/redis$i/
  cp -f ~/redis.conf.template /usr/local/etc/redis/redis$i/redis.conf
  sed -i 's/7001/'"${new_port}"'/g;s/redis1/redis'"${i}"'/g;' /usr/local/etc/redis/redis$i/redis.conf
done

cp -f ~/docker-compose-server.yml.template /usr/local/etc/redis/docker-compose.yml
sed -e ${sed_para} -i /usr/local/etc/redis/docker-compose.yml
sed -i 's/a.com:8443/dockerhub/redis:5.0.3/'"${image}"'/g;s/172.18.29.56/'"$3"'/g;' /usr/local/etc/redis/docker-compose.yml

# sentinel
mkdir -p /usr/local/etc/redis/sentinel/
cp -f ~/sentinel.conf.template /usr/local/etc/redis/sentinel/sentinel.conf
# ip=`hostname -I | awk '{print $1}'`
sed -i 's/172.18.29.56/'"$3"'/g;s/8001/'"${sentinel_port}"'/g;' /usr/local/etc/redis/sentinel/sentinel.conf
sed -e ${sed_para} -i /usr/local/etc/redis/sentinel/sentinel.conf
cp -f ~/docker-compose-sentinel.yml.template /usr/local/etc/redis/sentinel/docker-compose.yml
sed -i 's/a.com:8443/dockerhub/redis:5.0.3/'"${image}"'/g;s/8001/'"${sentinel_port}"'/g;' /usr/local/etc/redis/sentinel/docker-compose.yml

# start-all
chown -R polkitd:root /usr/local/etc/redis/
/usr/libexec/docker/cli-plugins/docker-compose -f /usr/local/etc/redis/docker-compose.yml up -d
/usr/libexec/docker/cli-plugins/docker-compose -f /usr/local/etc/redis/sentinel/docker-compose.yml up -d

5. 模板文件

docker-compose-sentinel.yml.template 文件内容如下

version: '3'
services:
  sentinel:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-sentinel
    ports:
      - 8001:8001
    command: redis-sentinel /sentinel/sentinel.conf
    volumes:
      - /usr/local/etc/redis/sentinel/:/sentinel

docker-compose-server.yml.template 文件内容如下

version: '3'
services:
  master1:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master1
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7001:7001
    volumes:
      - /usr/local/etc/redis/redis1/:/redis1
    command: redis-server /redis1/redis.conf
  master2:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master2
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7002:7002
    volumes:
      - /usr/local/etc/redis/redis2/:/redis2
    command: redis-server /redis2/redis.conf
  master3:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master3
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7003:7003
    volumes:
      - /usr/local/etc/redis/redis3/:/redis3
    command: redis-server /redis3/redis.conf
  master4:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master4
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7004:7004
    volumes:
      - /usr/local/etc/redis/redis4/:/redis4
    command: redis-server /redis4/redis.conf
  master5:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master5
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7005:7005
    volumes:
      - /usr/local/etc/redis/redis5/:/redis5
    command: redis-server /redis5/redis.conf
  master6:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master6
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7006:7006
    volumes:
      - /usr/local/etc/redis/redis6/:/redis6
    command: redis-server /redis6/redis.conf
  master7:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master7
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7007:7007
    volumes:
      - /usr/local/etc/redis/redis7/:/redis7
    command: redis-server /redis7/redis.conf
  master8:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master8
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7008:7008
    volumes:
      - /usr/local/etc/redis/redis8/:/redis8
    command: redis-server /redis8/redis.conf
  master9:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master9
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7009:7009
    volumes:
      - /usr/local/etc/redis/redis9/:/redis9
    command: redis-server /redis9/redis.conf
  master10:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master10
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7010:7010
    volumes:
      - /usr/local/etc/redis/redis10/:/redis10
    command: redis-server /redis10/redis.conf
  master11:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master11
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7011:7011
    volumes:
      - /usr/local/etc/redis/redis11/:/redis11
    command: redis-server /redis11/redis.conf
  master12:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master12
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7012:7012
    volumes:
      - /usr/local/etc/redis/redis12/:/redis12
    command: redis-server /redis12/redis.conf
  master13:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master13
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7013:7013
    volumes:
      - /usr/local/etc/redis/redis13/:/redis13
    command: redis-server /redis13/redis.conf
  master14:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master14
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7014:7014
    volumes:
      - /usr/local/etc/redis/redis14/:/redis14
    command: redis-server /redis14/redis.conf
  master15:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master15
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7015:7015
    volumes:
      - /usr/local/etc/redis/redis15/:/redis15
    command: redis-server /redis15/redis.conf
  master16:
    image: a.com:8443/dockerhub/redis:5.0.3
    container_name: redis-master16
    restart: always
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 7016:7016
    volumes:
      - /usr/local/etc/redis/redis16/:/redis16
    command: redis-server /redis16/redis.conf

redis.conf.template 文件内容如下

rename-command flushAll ""
daemonize no
port 7001
dir "/redis1"
pidfile "/redis1/redis.pid"
logfile "/redis1/redis.log"
dbfilename "dump.rdb"
save 900 1
appendonly no
appendfilename "appendonly.aof"
appendfsync always
maxmemory 500mb
maxmemory-policy volatile-lru
maxmemory-samples 3
slowlog-log-slower-than 10000
repl-backlog-size 64mb
timeout 0
repl-timeout 240

requirepass "123456"
masterauth "123456"
protected-mode no

sentinel.conf.template 文件内容如下:

port 8001
protected-mode no
dir "/sentinel"

sentinel monitor redis1 172.18.29.56 7001 2
sentinel auth-pass redis1 123456
sentinel down-after-milliseconds redis1 5000
sentinel failover-timeout redis1 10000

sentinel monitor redis2 172.18.29.56 7002 2
sentinel auth-pass redis2 123456
sentinel down-after-milliseconds redis2 5000
sentinel failover-timeout redis2 10000

sentinel monitor redis3 172.18.29.56 7003 2
sentinel auth-pass redis3 123456
sentinel down-after-milliseconds redis3 5000
sentinel failover-timeout redis3 10000

sentinel monitor redis4 172.18.29.56 7004 2
sentinel auth-pass redis4 123456
sentinel down-after-milliseconds redis4 5000
sentinel failover-timeout redis4 10000

sentinel monitor redis5 172.18.29.56 7005 2
sentinel auth-pass redis5 123456
sentinel down-after-milliseconds redis5 5000
sentinel failover-timeout redis5 10000

sentinel monitor redis6 172.18.29.56 7006 2
sentinel auth-pass redis6 123456
sentinel down-after-milliseconds redis6 5000
sentinel failover-timeout redis6 10000

sentinel monitor redis7 172.18.29.56 7007 2
sentinel auth-pass redis7 123456
sentinel down-after-milliseconds redis7 5000
sentinel failover-timeout redis7 10000

sentinel monitor redis8 172.18.29.56 7008 2
sentinel auth-pass redis8 123456
sentinel down-after-milliseconds redis8 5000
sentinel failover-timeout redis8 10000

sentinel monitor redis9 172.18.29.56 7009 2
sentinel auth-pass redis9 123456
sentinel down-after-milliseconds redis9 5000
sentinel failover-timeout redis9 10000

sentinel monitor redis10 172.18.29.56 7010 2
sentinel auth-pass redis10 123456
sentinel down-after-milliseconds redis10 5000
sentinel failover-timeout redis10 10000

sentinel monitor redis11 172.18.29.56 7011 2
sentinel auth-pass redis11 123456
sentinel down-after-milliseconds redis11 5000
sentinel failover-timeout redis11 10000

sentinel monitor redis12 172.18.29.56 7012 2
sentinel auth-pass redis12 123456
sentinel down-after-milliseconds redis12 5000
sentinel failover-timeout redis12 10000

sentinel monitor redis13 172.18.29.56 7013 2
sentinel auth-pass redis13 123456
sentinel down-after-milliseconds redis13 5000
sentinel failover-timeout redis13 10000

sentinel monitor redis14 172.18.29.56 7014 2
sentinel auth-pass redis14 123456
sentinel down-after-milliseconds redis14 5000
sentinel failover-timeout redis14 10000

sentinel monitor redis15 172.18.29.56 7015 2
sentinel auth-pass redis15 123456
sentinel down-after-milliseconds redis15 5000
sentinel failover-timeout redis15 10000

sentinel monitor redis16 172.18.29.56 7016 2
sentinel auth-pass redis16 123456
sentinel down-after-milliseconds redis16 5000
sentinel failover-timeout redis16 10000

6. 部署redis

# 在 redis master 机器执行
./redis-master-deploy.sh 0f88f9be5839 20001

# 在 redis slave 机器执行
./redis-slave-deploy.sh 0f88f9be5839 20001 172.18.3.15

说明

  1. 一次创建16组redis一主两从实例
  2. 需要模板文件 /root/*.template
  3. image、port为redis-master-deploy.sh命令行参数;image、port、master_ip为redis-slave-deploy.sh命令行参数
  4. port为16个redis-server示例的起始端口例如:port为7001将创建 7001 – 7016 16个实例
  5. 先部署master,再部署slave,主从port参数必须相同,本例同为7001
  6. 哨兵端口位数与port参数相同定义为:第一位为port的第一位加1,最后一位为1,中间位数为0。例如port 参数值为 20010,则哨兵端口为30001;port 参数值为 7001,则哨兵端口为8001。

7. 基本维护

# 查看镜像
docker images
# 查看容器
docker ps
# 查看容器详细信息
docker inspect <CONTAINER ID>
# 重启容器
/usr/libexec/docker/cli-plugins/docker-compose -f /usr/local/etc/redis/docker-compose.yml down
/usr/libexec/docker/cli-plugins/docker-compose -f /usr/local/etc/redis/sentinel/docker-compose.yml down
/usr/libexec/docker/cli-plugins/docker-compose -f /usr/local/etc/redis/docker-compose.yml up -d
/usr/libexec/docker/cli-plugins/docker-compose -f /usr/local/etc/redis/sentinel/docker-compose.yml up -d

原文地址:https://blog.csdn.net/wzy0623/article/details/132005356

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_37962.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注