<<INFO
AUTHOR:运维@小兵
DATE:2021-07-08
DESCRIBE:Docker方式安装redis
SYSTEM:CentOS7/RedHat7
WARNING:
MODIFY:
INFO
set -e
source /etc/profile #避免ansible远程执行shell获取环境变量不一致的问题
WORKDIR=$(cd `dirname $0`;pwd) #脚本所在路径
echo "Current Excute: bash ${WORKDIR}/$0 $@"
REDIS_PORT="6379"
REDIS_CONTAINER_NAME="redis"
HARBOR_USER="admin"
HARBOR_PWD="12345"
HARBOR_IP="192.168.1.2"
IMAGE_ADDR="${HARBOR_IP}/redis:6.0"
#环境检查
Check_Env(){
echo "INFO:Begin Check Env..."
local pid=$(lsof -i:${REDIS_PORT} -t)
if ss -nutlp | grep -w ':${REDIS_PORT}\b' &> /dev/null;then
echo "ERROR:${REDIS_PORT} Is Used" && exit 1
fi
if ! command -v docker &> /dev/null;then
echo "ERROR:Please Install Docker" && exit 1
fi
}
#配置docker信任
Config_Docker(){
if ! docker info | grep -w "${HARBOR_IP}" &> /dev/null;then
[[ ! -f /etc/docker/daemon.json.bak ]] && cp /etc/docker/daemon.json{,.bak}
tee /etc/docker/daemon.json << EOF
{
"registry-mirrors":["https://7bezldxe.mirror.aliyuncs.com/"],
"log-driver":"json-file","log-opts":{"max-size":"5m"},
"storage-driver":"overlay2",
"storage-opts":["overlay2.override_kernel_check=true"],
"insecure-registries": ["${HARBOR_IP}","${HARBOR_IP}:80"]
}
EOF
systemctl restart docker
docker info | grep -w "${HARBOR_IP}"
fi
}
#拉取镜像
Pull_Image(){
echo "INFO:Begin Pull Image ${IMAGE_ADDR}..."
Config_Docker
docker login -u ${HARBOR_USER} -p ${HARBOR_PWD} ${HARBOR_IP}
docker pull ${IMAGE_ADDR}
}
#安装redis容器
Install_Redis(){
Check_Env
Pull_Image
echo "INFO:Begin Create Redis Container..."
docker run -d --name=${REDIS_CONTAINER_NAME} --restart unless-stopped \
-p ${REDIS_PORT}:6379 ${IMAGE_ADDR}
sleep 2
if ss -nutlp | grep -w ":${REDIS_PORT}\b" &> /dev/null;then
echo "INFO:${REDIS_CONTAINER_NAME} Container Create Success"
else
echo "ERROR:${REDIS_CONTAINER_NAME} Container Create Fail" && exit 1
fi
}
#卸载REDIS
Uninstall_Redis(){
if docker ps -a | egrep -w "\b${REDIS_CONTAINER_NAME}$" &> /dev/null;then
docker rm -f ${REDIS_CONTAINER_NAME}
echo "INFO:${REDIS_CONTAINER_NAME} Container uninstall Success" && exit 0
else
echo "WARN:${REDIS_CONTAINER_NAME} Container Is Without" && exit 0
fi
}
######################主程序######################
[[ $# -ne 1 ]] && echo "ERROR:Invalid Param!!! eg:bash $0 (install|uninstall)" && exit 1
case $1 in
install)
Install_Redis;;
uninstall)
Uninstall_Redis;;
*)
echo "ERROR:Invalid Param!!! eg:bash $0 install or uninstall" && exit 1
esac