docker脚本部署redis

文章目录

      • 脚本简介
      • 脚本注解
      • 执行方式
      • 脚本内容

脚本简介

  1. 基于运维统一脚本中,3、常见服务部署下的数据库服务选项redis
  2. 使用docker环境安装

脚本注解

  1. 该脚本为了在Centos7服务器中docker环境上部署redis(单机版)服务,同时设置root初始密码
  2. 注意:脚本采用模板文件进行配置文件的生成,所有镜像版本差距过大,可能会出现语法错误,不建议修改镜像版本

执行方式

sh start_redis.sh

如图所示,设置默认值,可根据实际情况填写不同的值即可
docker脚本部署redis_第1张图片

脚本内容

#!/bin/bash
#当前版本
#v1.0.1
###################################################################
info(){
  echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[36m$1\033[0m "
}
info1(){
  echo -e "\033[36m $1 \033[0m "
}
info2(){
  echo -e "\033[34m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[35m$1\033[0m "
}
info3(){
 echo -e "\033[36m------------------------------------------------------------------------------\033[0m" 
}
info4(){
  echo -e "\033[32m $1 \033[0m "
}
info5(){
 info1 "==============================================================================" 
}
info6(){
  echo -e "\033[33m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[91m$1\033[0m "
}
info7(){
  echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[32m$1\033[0m "
}

info8(){
  echo -e "\033[31m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[35m$1\033[0m "
}
info9(){
  echo -e "\033[32m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`\033[0m" "\033[36m$1\033[0m "
}

redis_run(){
  local registory=`docker images --format {{.Repository}}:{{.Tag}}|grep ${redis_images}|wc -l`
  local images=`docker images --format {{.Repository}}:{{.Tag}}|grep ${redis_images}`

 ###########################docker mysql##################
 [ ! -d ${redis_HOME} ] && mkdir -p ${redis_HOME}/data
 cd ${redis_HOME}
  info2 "加载配置文件"
 cat > redis.conf <<EOF
 bind 0.0.0.0 
 port 6379
 protected-mode no
 daemonize no     
 databases 16  
 masterauth 123456
 dir  ./      
 appendonly yes 
EOF
 sed -i "s/123456/${redis_password}/g"  redis.conf

 info2 "Pull the image ${redis_images} Wait !!!"
   if [ ${registory} -ne 1 ];then
         docker pull ${redis_images} &> /dev/null
   fi
 [ $? -eq 0 ] && info2 "Pull the image  ${redis_images} success"
 info2 "Start ${redis_name} to the root user"

 if [ `docker ps -a | grep ${redis_name}|wc -l` -eq 1 ];then
     docker rm -f ${redis_name} &>/dev/null
      [ $? -eq 0 ] && info6 "${redis_name} rm success"
     docker run -p ${redis_port}:6379 --name ${redis_name} -v ${redis_HOME}/redis.conf:/etc/redis/redis.conf \
           -v ${redis_HOME}/data:/data -d ${images} redis-server \
             /etc/redis/redis.conf --appendonly yes  &>/dev/null
   else
     docker run -p ${redis_port}:6379 --name ${redis_name} -v ${redis_HOME}/redis.conf:/etc/redis/redis.conf \
           -v ${redis_HOME}/data:/data -d ${images} redis-server \
             /etc/redis/redis.conf --appendonly yes  &>/dev/null
 fi
 [ $? -eq 0 ] && info "start the image ${redis_name} success" ||info6 "start the image ${redis_name} fail"
}

start_redis(){
  info5
  echo ""
 read -p "请输入redis数据持久化存储目录(/opt/redis):" redis_home
 read -p "请输入redis容器启动的容器名称(redis):" redis_name
 read -p "请输入redis容器启动的镜像版本(redis:4.0.9):" redis_version
 read -p "请输入redis容器启动的初始密码(123456):" redis_password
 read -p "请输入redis容器启动的初始端口(6379):" redis_port
 redis_HOME="${redis_home:-/opt/redis}"
 redis_name="${redis_name:-redis}"
 redis_images="${redis_version:-redis:4.0.9}"
 redis_password="${redis_password:-123456}"
 redis_port="${redis_port:-6379}"
 if [ -z ${redis_home} ] && [ -z ${redis_name} ] && [ -z ${redis_version} ]&& [ -z ${redis_password} ]&& [ -z ${redis_port} ];then
   info8 "请根据提示,输入所有的值"
   exit 0
 else
 info8 "第一次使用,选择启动方式必须是: run"
 read -p "选择${redis_name}的启动方式:(run|restart|start|stop|rm):" redis_start_run1
 if [ -z ${redis_start_run1} ];then
       info6 "请重新输入"    
       info5
       Service_database
 else
   case $redis_start_run1 in
     'run')      
         redis_run;;
     'restart')
         docker restart ${redis_name} &>/dev/null
         [ $? -eq 0 ] && info " ${redis_name} Restart successful";;
     'start')
            docker start ${redis_name} &>/dev/null
            [ $? -eq 0 ] &&  info " ${redis_name} Start successful";;
     'stop')
           docker stop ${redis_name} &>/dev/null
           [ $? -eq 0 ] &&   info " ${redis_name} Stop successful";;
     'rm')
              docker rm -f ${redis_name} &>/dev/null
           [ $? -eq 0 ] &&  info " ${redis_name} delete successful";;
      '*')   
              info "脚本执行错误,请检查后继续";;
   esac
  fi
 fi
}
start_redis

你可能感兴趣的:(#,docker应用,docker,redis,运维)