目录
前言
写了一个一键在Docker环境下安装基础服务的Linux脚本,实现了一键安装docker的Mysql、Nacos、Redis、Rocketmq、Ftp、nginx这些服务。
一、目录结构及用途
install
|
--install.sh #安装程序脚本
|
--sqlFile #数据库sql文件
|
--updatePass.sql #Mysql更新密码
|
--nacos.sql #nacos服务sql文件
|
--baseService #基础服务
|
--docker #docker服务配置文件+网桥配置
|
--mysql #mysql服务配置文件(用于跳过设置初始密码)
|
--nacos #nacos服务配置文件(设置密码登录)
|
--nginx #nginx服务配置文件(代理实例)
|
--redis #redis服务配置文件
|
--rocketmq #rocketmq服务配置文件
二、安装
2.1、通用脚本
主要写了删除容器和镜像、拉取镜像、创建本地映射目录、判断容器是否启用成功、选择安装服务
# 删除已有容器和镜像
function delExisting(){
id=`docker ps -a | grep $1 | awk '{print $1}'`
if [ "$id" != "" ];then
echo -e "\n发现docker中已存在$1容器,id:$id\n"
echo -e "\n开始删除id=$id 的容器……\n"
docker stop $id
docker rm $id
echo -e "\n删除容器成功!\n"
fi
imageId=`docker images -a | grep $1 | awk '{print $3}'`
if [ "$imageId" != "" ];then
echo -e "\n发现docker中已存在$1镜像,id:$imageId\n"
echo -e "\n开始删除id=$imageId 的镜像……\n"
docker rmi $imageId
echo -e "\n删除镜像成功!\n"
fi
}
function delExisting2(){
id=`docker ps -a | grep $1 | awk '{print $1}'`
if [ "$id" != "" ];then
echo -e "\n发现docker中已存在$1容器,id:$id\n"
echo -e "\n开始删除id=$id 的容器……\n"
docker stop $id
docker rm $id
echo -e "\n删除容器成功!\n"
fi
}
# 输入版本号并拉取镜像
function docker_pull(){
echo -e "\n开始拉取$image:$version镜像,请等待……\n"
if [ "$version" != "" ];then
docker pull $image:$version
else
docker pull $image
fi
if [ $? -eq 0 ];then
echo -e "\n镜像拉取成功!"
else
echo -e "\n镜像拉取失败,请重试或确认 $image:$version 镜像是否存在!"
exit
fi
}
# 创建本地文件映射目录
function create_file_path(){
rm -rf ${applicationPath}/*
if [ "$1" ];then
echo -e "\n==========开始创建映射到本机的目录$@=========="
for i in "$@"
do
mkdir -p ${applicationPath}/$i
ls $applicationPath | grep $i
if [ $? -eq 0 ];then
echo -e "本地文件映射目录$i创建成功!"
chmod -R 777 ${applicationPath}
else
echo -e "本地文件映射目录$i创建失败!"
exit
fi
done
else
mkdir -p ${applicationPath}
fi
}
# 创建本地文件映射目录
function create_file_path_no(){
if [ "$1" ];then
echo -e "\n==========开始创建映射到本机的目录$@=========="
for i in "$@"
do
rm -rf ${applicationPath}/$i
mkdir -p ${applicationPath}/$i
ls $applicationPath | grep $i
if [ $? -eq 0 ];then
echo -e "本地文件映射目录$i创建成功!"
chmod -R 777 ${applicationPath}
else
echo -e "本地文件映射目录$i创建失败!"
exit
fi
done
else
mkdir -p ${applicationPath}
fi
}
# 判断容器是否启动成功
function successOrNot(){
echo -e "docker run命令:\n $dockerRunCmd \n"
if [[ -n $(docker ps -q -f "name=^$application$") ]];then
echo -e "$application 容器启动成功\n"
echo -e "端口号:$port\n"
echo -e "版本:$version\n"
echo -e "容器名称:$application\n"
echo -e "本地映射路径:$applicationPath\n"
echo -e "进入容器使用命令:docker exec -it $application /bin/bash\n"
else
echo "$application 容器启动失败,请检查docker run命令是否有误"
fi
}
while true;do
echo -e "\n============Install一键部署============\n"
echo -e "基础服务--请先安装Docker\n"
echo -e "(0) Docker (1) Mysql\n"
echo -e "(2) Nacos (3) Redis\n"
echo -e "(4) Rocketmq (5) Ftp\n"
echo -e "(6) nginx\n"
echo -e "(9) 一键启动Docker以外的基础服务\n"
echo -e "\n"
echo -e "\n"
echo -e "(99) 退出"
echo -e "\n============Install一键部署============\n"
read -p "请输入要安装服务的编号:" input
case $input in
0)
docker_install
;;
1)
docker_mysql
;;
2)
docker_nacos
;;
3)
docker_redis
;;
4)
docker_rocketmq
;;
5)
docker_ftp
;;
6)
docker_nginx
;;
9)
docker_mysql
docker_nacos
docker_redis
docker_rocketmq
docker_ftp
docker_nginx
;;
99)
echo -e "退出docker部署脚本"
exit
;;
*)
echo "编号输入错误,请重新输入!"
esac
done
2.2、Docker安装
主要步骤:卸载podman->安装启动docker->开启docker api 远程访问->配置仓库->重启docker->开启防火墙端口->配置网桥
# 安装Docker并启动
docker_install(){
echo -e "\n=========================开始安装Docker=========================\n"
echo -e "\n============使用的官方脚本安装,可能因网络问题安装缓慢,请耐心等待============\n"
podman=`ps -ef|grep podman | awk '{print $1}'`
if [ "$podman" != "" ];then
echo -e "\n=========================卸载podman=========================\n"
yum erase podman buildah
echo -e "\n=========================卸载podman成功=========================\n"
fi
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
sudo systemctl start docker
echo -e "\n开始写入配置文件/usr/lib/systemd/system/docker.service……\n"
mkdir -p /usr/lib/systemd/system
cp -f baseService/docker/docker.service /usr/lib/systemd/system/docker.service
echo -e "\n配置文件写入完成!\n"
echo -e "\n开始创建镜像仓库文件/etc/docker/daemon.json...\n"
mkdir -p /etc/docker
cp -f baseService/docker/daemon.json /etc/docker/daemon.json
echo -e "\n创建镜像仓库文件成功!\n\n"
echo -e "\n重启docker服务……\n"
systemctl daemon-reload
systemctl restart docker
echo -e "\n开启防火墙2375端口!\n"
firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload
echo -e "\n创建网桥net 172.18.0.0\n"
docker network create --subnet=172.18.0.0/24 net
echo -e "\n=========================安装Docker结束=========================\n\n"
}
2.3、Mysql安装
主要步骤:删除已有镜像->拉取mysql镜像并启动->修改初始密码->导入nacos数据库->开启端口
# 安装MySql
docker_mysql(){
application="base-mysql"
image="mysql"
applicationPath="/usr/local/mysql"
port="3306"
passwd="demo@2024"
version="5.7"
echo -e "\n=========================开始安装MySql=========================\n"
# 删除已有容器和镜像
delExisting ${application}
# 输入版本号并拉取镜像 docker_pull
# 创建映射目录
create_file_path conf logs data
# 创建并启动docker容器
cp -f baseService/mysql/skipPass.cnf $applicationPath/conf/my.cnf
echo -e "\n开始创建并启动${application}容器……\n"
dockerRunCmd="docker run -itd \
--network=net --ip 172.18.0.2 -p $port:3306 --name $application \
--restart=always \
-e TZ=Asia/Shanghai \
-v ${applicationPath}/data/:/var/lib/mysql \
-v ${applicationPath}/logs/:/logs \
-v ${applicationPath}/conf/:/etc/mysql/conf.d \
-v /etc/localtime:/etc/localtime:ro \
--privileged=true \
-e MYSQL_ROOT_PASSWORD=$passwd \
-d $image:$version"
eval $dockerRunCmd
# 判断容器是否启动成功
successOrNot
echo -e "\n开始导入数据\n"
# 更新密码
docker exec -i $application mysql -uroot < /install/sqlFile/updatePass.sql
cp -f baseService/mysql/my.cnf $applicationPath/conf/my.cnf
docker restart $application
sleep 2
# 导入数据
docker exec -i $application mysql -uroot -p$passwd < /install/sqlFile/nacos.sql
echo -e "\n导入数据结束\n"
echo -e "\n开启防火墙端口3306\n"
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
echo -e "\n=========================安装MySql结束=========================\n\n"
}
2.4、Nacos安装
主要步骤:删除已有镜像->拉取nacos镜像并启动->修改nacos配置文件(开启密码)->重新启动nacos->开启端口
# 安装Nacos
docker_nacos(){
application="base-nacos"
image="nacos/nacos-server"
applicationPath="/usr/local/nacos"
port="8848"
version="v2.2.0"
echo -e "\n=========================开始安装Nacos========================="
echo -e "注:本脚本安装的是nacos单机版,安装集群请自行修改配置文件\n"
# 删除已有容器和镜像
delExisting ${application}
# 输入版本号并拉取镜像 docker_pull
# 创建映射目录
create_file_path conf logs
# 创建配置文件
echo -e "\n开始创建配置文件${applicationPath}/conf/application.properties……\n"
docker run -p 8848:8848 --name $application -d $image:$version
docker cp $application:/home/nacos/logs/ ${applicationPath}/
docker cp $application:/home/nacos/conf/ ${applicationPath}/
# 删除已有容器和镜像
delExisting ${application}
ls ${applicationPath}/conf | grep application.properties
if [ $? -eq 0 ];then
cp -f baseService/nacos/application.properties $applicationPath/conf/application.properties
echo -e "\n配置文件写入完成!\n"
else
echo "\n配置文件创建失败!\n"
exit
fi
echo -e "\n开始创建并启动${application}容器……\n"
if [ "$ip" == "" ];then
ip=`hostname -I | awk '{print $1}'`
echo "\n本机IP地址为$ip(如有多网卡ip可能会不准确,请自行确认)\n"
if [ "$ip" == "" ];then
read -p "查询内网IP失败,请手动输入:" ip
fi
fi
dockerRunCmd="docker run -itd --name $application \
--network=net \
--ip 172.18.0.3 \
-p 7848:7848 \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
--restart=always \
-e TZ=Asia/Shanghai \
-e PREFER_HOST_MODE=$ip \
-e NACOS_SERVER_IP=$ip \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=172.18.0.2 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=demo@2024 \
-v $applicationPath/logs/:/home/nacos/logs \
-v $applicationPath/conf/:/home/nacos/conf \
-d $image:$version "
eval $dockerRunCmd
# 判断容器是否启动成功
successOrNot
echo -e "\n开启防火墙端口8848\n"
firewall-cmd --zone=public --add-port=8848/tcp --permanent
firewall-cmd --reload
echo -e "\n=========================安装Nacos结束=========================\n\n"
}
2.5、Redis安装
主要步骤:删除已有镜像->修改redis配置文件->拉取redis镜像并启动->开启端口
# 安装Redis
docker_redis(){
application="base-redis"
image="redis"
applicationPath="/usr/local/redis"
port="6379"
passwd="demo@2024"
version="6.2.6"
echo -e "\n=========================开始安装Redis=========================\n"
# 删除已有容器和镜像
delExisting ${application}
# 输入版本号并拉取镜像docker_pull
# 创建映射目录
create_file_path data conf
# 将配置写进配置文件
echo -e "\n开始写入配置文件${applicationPath}/conf/redis.conf\n"
cp -f baseService/redis/redis.conf $applicationPath/conf/redis.conf
echo -e "\n配置文件写入完成!\n"
echo -e "\n开始创建并启动${application}容器……\n"
dockerRunCmd="docker run --name $application \
--network=net --ip 172.18.0.6 -p $port:6379 \
--restart always \
-e TZ=Asia/Shanghai \
-v ${applicationPath}/conf/redis.conf:/etc/redis/redis.conf \
-v ${applicationPath}/data:/data \
-d redis:6.2.6 redis-server /etc/redis/redis.conf"
eval $dockerRunCmd
# 判断容器是否启动成功
successOrNot
echo -e "\n开启防火墙端口\n"
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
echo -e "\n=========================安装Redis结束=========================\n\n"
}
2.6、Rocketmq安装
主要步骤:
安装rmqnamesrv:删除已有镜像->拉取镜像并启动->开启端口
安装broker:删除已有镜像->启动镜像->修改替换配置文件->重新启动镜像->开启端口
安装console:删除已有镜像->拉取镜像并启动->开启端口
# 安装 Rocketmq
docker_rocketmq(){
echo -e "\n============RockerMq一键部署============\n"
docker_rocketmq_rmqnamesrv
docker_rocketmq_broker
docker_rocketmq_console
echo -e "\n============RockerMq部署完成============\n\n"
}
# 安装 Rocketmq_rmqnamesrv
docker_rocketmq_rmqnamesrv(){
application="base-rmqnamesrv"
image="rocketmqinc/rocketmq"
applicationPath="/usr/local/rocketmq/data/namesrv"
port="9876"
version="4.4.0"
echo -e "\n=========================开始安装rocketmq-namesrv=========================\n"
# 删除已有容器和镜像
delExisting ${application}
# 输入版本号并拉取镜像
docker_pull
# 创建映射目录
create_file_path store logs
echo -e "\n开始创建并启动容器……\n"
dockerRunCmd="docker run -d\
--network=net \
--ip 172.18.0.8 \
--restart=always \
-e TZ=Asia/Shanghai \
--name $application \
-p $port:9876 \
-v $applicationPath/logs:/root/logs \
-v $applicationPath/store:/root/store \
-e \"MAX_POSSIBLE_HEAP=100000000\" \
$image:$version \
sh mqnamesrv"
eval $dockerRunCmd
# 判断容器是否启动成功
successOrNot
echo -e "\n=========================安装rocketmq-namesrv结束=========================\n\n"
}
# 安装Rocketmq_broker
docker_rocketmq_broker(){
application="base-rmqbroker"
image="rocketmqinc/rocketmq"
applicationPath="/usr/local/rocketmq/data/broker"
local port="10911"
local port1="10909"
local port2="10912"
local namesrvProt="9876"
version=""
echo -e "\n===========安装rocketmq-broker开始===========\n"
# 删除已有容器
delExisting2 ${application}
# 创建映射目录
create_file_path store logs conf
# 将配置写进配置文件
mkdir -p /usr/local/rocketmq/conf
docker run -p $port:10911 --name $application -d $image:$version
docker cp $application:/opt/rocketmq-4.4.0/conf/broker.conf /usr/local/rocketmq/conf
#删除已有容器和镜像
delExisting ${application}
if [ "$ip" == "" ];then
ip=`hostname -I | awk '{print $1}'`
echo "\n本机IP地址为$ip(如有多网卡ip可能会不准确,请自行确认)\n"
if [ "$ip" == "" ];then
read -p "查询内网IP失败,请手动输入:" ip
fi
fi
echo -e "\n开始写入配置/usr/local/rocketmq/conf/broker.conf\n"
mkdir -p $applicationPath
cp -f baseService/rocketmq/broker.conf /usr/local/rocketmq/conf/broker.conf
# 替换配置中的ip
sed -i "s/192.168.101.153/${ip}/g" `grep 192.168.101.153 -rl /usr/local/rocketmq/conf/broker.conf`
sleep 2
echo -e "开始创建并启动容器……\n"
dockerRunCmd="docker run -d \
--network=net \
--ip 172.18.0.9 \
--restart=always \
-e TZ=Asia/Shanghai \
--name $application \
--link rmqnamesrv:namesrv \
-p $port:10911 \
-p $port1:10909 \
-p $port2:10912 \
-v /usr/local/rocketmq/conf/broker.conf:/opt/rocketmq-4.4.0/conf/broker.conf \
-v ${applicationPath}/logs:/root/logs \
-v ${applicationPath}/store:/root/store \
-e "NAMESRV_ADDR=namesrv:$namesrvProt" \
-e "MAX_POSSIBLE_HEAP=200000000" \
$image \
sh mqbroker -c /opt/rocketmq-4.4.0/conf/broker.conf"
eval $dockerRunCmd
# 判断容器是否启动成功
successOrNot
echo -e "\n===========安装rocketmq-broker结束===========\n"
}
# 安装 Rocketmq_console
docker_rocketmq_console(){
application="base-rmqadmin"
image="pangliang/rocketmq-console-ng"
port="10020"
version=""
local namesrvProt="9876"
echo -e "\n===========安装rocketmq-console开始===========\n"
# 删除已有容器和镜像
delExisting ${application}
# 输入版本号并拉取镜像
docker_pull
if [ "$ip" == "" ];then
ip=`hostname -I | awk '{print $1}'`
echo "\n本机IP地址为$ip(自动获取可能会不准确,请自行确认)\n"
if [ "$ip" == "" ];then
read -p "查询IP失败,请手动输入:" ip
fi
fi
echo -e "\n开始创建并启动容器……\n"
dockerRunCmd="docker run -d \
--network=net \
--ip 172.18.0.10 \
--restart=always \
-e TZ=Asia/Shanghai \
--name ${application} \
-e \"JAVA_OPTS=-Drocketmq.namesrv.addr=$ip:$namesrvProt \
-Dcom.rocketmq.sendMessageWithVIPChannel=false\" \
-p $port:8080 \
$image"
eval $dockerRunCmd
# 判断容器是否启动成功
successOrNot
echo -e "\n===========安装rocketmq-console结束===========\n\n"
}
2.8、Ftp安装
主要步骤:删除已有镜像->拉取镜像并启动->开启端口
# 安装 ftp
docker_ftp(){
application="base-vsftpd"
image="fauria/vsftpd"
applicationPath="/home"
port1="21"
port2="20"
port3="21100"
port4="21110"
user="sjz"
passwd="sjz"
echo -e "\n===========安装Ftp开始===========\n"
# 删除已有容器和镜像
delExisting ${application}
# 创建映射目录
create_file_path_no ftp
if [ "$ip" == "" ];then
ip=`hostname -I | awk '{print $1}'`
echo "\n本机IP地址为$ip(如有多网卡ip可能会不准确,请自行确认)\n"
if [ "$ip" == "" ];then
read -p "查询内网IP失败,请手动输入:" ip
fi
fi
echo -e "开始创建并启动容器……\n"
dockerRunCmd="docker run -d --name $application \
--network=net --ip 172.18.0.21 \
-p $port1:21 -p $port2:20 -p $port3-$port4:21100-21110 \
--restart=always \
-v $applicationPath/ftp:/home/vsftpd/sjz \
-e FTP_USER=$user \
-e FTP_PASS=$passwd \
-e PASV_ADDRESS=$ip \
-e PASV_MIN_PORT=$port3 \
-e PASV_MAX_PORT=$port4 \
$image"
eval $dockerRunCmd
# 判断容器是否启动成功
successOrNot
echo -e "\n开启防火墙端口11121、21100-21110、72\n"
firewall-cmd --zone=public --add-port=72/tcp --permanent
firewall-cmd --zone=public --add-port=11121/tcp --permanent
sudo firewall-cmd --permanent --zone=public --add-port=21100-21110/tcp
firewall-cmd --reload
echo -e "\n===========安装Ftp结束===========\n\n"
}
2.7、nginx安装
主要步骤:删除已有镜像->拉取镜像并启动->修改配置文件->重新启动->开启端口
# 安装 nginx
docker_nginx(){
echo -e "\n===========安装nginx开始===========\n"
nginxName="base-nginx"
image="nginx"
applicationPath="/usr/local/nginx"
port="80"
# 删除已有容器和镜像
delExisting ${nginxName}
# 创建映射目录
create_file_path_no log html
# 创建nginx.conf 文件
echo -e "\n开始写入配置文件${applicationPath}/nginx.conf\n"
mkdir -p $applicationPath
cp -f baseService/nginx/nginx.conf ${applicationPath}/nginx.conf
if [ "$ip" == "" ];then
ip=`hostname -I | awk '{print $1}'`
echo "\n本机IP地址为$ip(如有多网卡ip可能会不准确,请自行确认)\n"
if [ "$ip" == "" ];then
read -p "查询内网IP失败,请手动输入:" ip
fi
fi
# 替换nginx.conf 文件中的ip
sed -i "s/192.168.101.153/${ip}/g" `grep 192.168.101.153 -rl ${applicationPath}/nginx.conf`
echo -e "\n写入配置文件完成\n"
sleep 2
echo -e "\n开始创建并启动容器……\n"
dockerRunCmd="docker run -itd --name $nginxName \
--network=net --ip 172.18.0.80
-p $port:80 \
-p 81:81 \
-p 84:84 \
-p 89:89 \
-p 70:70 \
-p 71:71 \
-p 72:72 \
-v $applicationPath/nginx.conf:/etc/nginx/nginx.conf \
-v $applicationPath/html:/etc/nginx/html \
-v $applicationPath/log:/usr/log/nginx \
-v /home/ftp:/usr/share/nginx/html \
$image"
eval $dockerRunCmd
# 判断容器是否启动成功
successOrNot
echo -e "\n开启防火墙端口80\n"
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
docker restart $nginxName
echo -e "\n===========安装nginx结束===========\n\n"
}