几个常用一键部署脚本
Posted 草根追逐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了几个常用一键部署脚本相关的知识,希望对你有一定的参考价值。
离线安装docker-compose
#!/bin/bash
1.关闭selinux
echo "关闭selinux..."
sleep 2
sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
setenforce 0
2.解压缩docker和docker-file
提前下载好二进制docker相关包
https://download.docker.com/linux/static/stable/x86_64/
echo "解压缩docker和docker-file..."
sleep 2
tar xvf docker-20.10.6.tgz
mv docker/* /usr/bin/
3.配置国内仓库及中间件仓库
echo "配置国内仓库及中间件仓库..."
sleep 2
[ ! -f /etc/docker ] && mkdir /etc/docker
tee /etc/docker/daemon.json <<-EOF
"registry-mirrors": [
"https://5sr5uink.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
],
"insecure-registries": ["harbor.xxx.com"]
EOF
4.配置docker开机自启动
echo "配置docker开机自启动..."
sleep 2
tee /etc/systemd/system/docker.service <<-EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd --selinux-enabled=false
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start docker
systemctl enable docker
5.拷贝docker-compose文件
提前下载好docker-compose文件
curl -L "https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/bin/docker-compose
echo "拷贝docker-compose文件..."
sleep 2
cp docker-compose /usr/bin/docker-compose
chmod +x /usr/bin/docker-compose
6.查看docker-compose版本
echo "查看docker-compose版本###..."
sleep 2
docker-compose version
中间件一键部署
#!/bin/ba#sh
1.定义默认配置变量
echo "请替换相关服务的IP地址和Gateway地址..."
sleep 2
FDFS_HOST=10.10.10.203
mysql_HOST=10.10.10.203
MYSQL_PORT=3306
GATEWAY_IP=10.10.10.203
echo "当前网关IP地址为: $GATEWAY_IP"
echo "当前文件系统主机IP为: $FDFS_HOST"
echo "当前nacos连接的数据库IP和端口为: $MYSQL_HOST:$MYSQL_PORT"
echo "预配置的网关地址为: $GATEWAY_IP"
echo "请输入【是】或【不是】..."
read -p "请确认是否已经修改完相应的配置": YES
if [ $YES == 是 ];then
echo "确认配置,执行下面步骤..."
else
echo "没有确认配置,无法执行下面步骤,即将退出,请再次确认配置..."
exit 1
fi
2.定义检测函数
check()
if [ $? -eq 0 ];then
echo "this step is right, continue..."
sleep 1
else
echo "this step has some problem,please check it..."
sleep 1
exit 1
fi
3.定义中间件路径
PUB_PATH=/deploy/sorts
echo "请确认公共路径是不是$PUB_PATH,如果不是请修改..."
read -p "请输入【是】或【不是】": YES
if [ $YES == 是 ];then
echo "默认路径,不需要修改..."
else
read -p "请输入你的真实公共路径": NEW_PATH
PUB_PATH=$NEW_PATH
fi
if [ ! -e $PUB_PATH ];then
mkdir -p $PUB_PATH
fi
4.解压配置文件包
echo "部署开始..."
echo "解压配置文件包..."
sleep 2
tar zxf middleware.tar.gz -C $PUB_PATH
check
5.部署Mysql
echo "部署Mysql..."
sleep 2
MYSQL_BASE=$PUB_PATH/mysql_5.7.34
if [ ! -e $MYSQL_BASE ];then
mkdir -p $MYSQL_BASE
fi
cd $MYSQL_BASE
chmod -R 777 $MYSQL_BASE/log
sh up.sh
check
6.创建数据库
echo "创建数据库..."
sleep 2
CONTAINER_ID=`docker ps |grep mysql|cut -d " " -f 1`
docker cp init/grant.sql $CONTAINER_ID:/tmp
docker cp init/nacos_config.sql $CONTAINER_ID:/tmp
docker cp init/apaas_business.sql $CONTAINER_ID:/tmp
docker cp init/apaas_platform.sql $CONTAINER_ID:/tmp
docker cp init/apaas.sql $CONTAINER_ID:/tmp
docker cp init/gateway.sql $CONTAINER_ID:/tmp
docker-compose exec mysql mysql -uroot -p123456 -e "source /tmp/grant.sql;"
docker-compose exec mysql mysql -uroot -p123456 -e "use nacos_config;source /tmp/nacos_config.sql;"
docker-compose exec mysql mysql -uroot -p123456 -e "use nacos_config;source /tmp/apaas.sql;"
docker exec -i $CONTAINER_ID mysql -uroot -p123456 apaas_business <./init/apaas_business.sql
docker exec -i $CONTAINER_ID mysql -uroot -p123456 apaas_platform <./init/apaas_platform.sql
docker-compose exec mysql mysql -uroot -p123456 -e "source /tmp/gateway.sql;"
check
7.部署nacos
echo "部署nacos..."
sleep 2
NACOS_BASE=$PUB_PATH/nacos
if [ ! -e $NACOS_BASE ];then
mkdir -p $NACOS_BASE
fi
cd $NACOS_BASE
sed -i "s/[0-9]\\1,3\\\\.[0-9]\\1,3\\\\.[0-9]\\1,3\\\\.[0-9]\\1,3\\/$MYSQL_HOST:MYSQL_PORT/g" env/nacos.env
sh up.sh
check
8.部署RabbitMQ
echo "部署RabbitMQ..."
sleep 2
MQ_BASE=$PUB_PATH/rabbitmq
if [ ! -e $MQ_BASE ];then
mkdir -p $MQ_BASE
fi
cd $MQ_BASE
sh up.sh
check
9.创建用户和虚拟主机
echo "创建用户和虚拟主机..."
sleep 2
docker-compose exec rabbitmq sh /init/create.#sh
10.部署Redis
echo "部署Redis..."
sleep 2
REDIS_BASE=$PUB_PATH/redis
if [ ! -e $REDIS_BASE ];then
mkdir -p $REDIS_BASE
fi
cd $REDIS_BASE
sh up.sh
check
11.部署Fastdfs
echo "部署Fastdfs..."
sleep 2
FAST_BASE=$PUB_PATH/fastdfs
if [ ! -e $FAST_BASE ];then
mkdir -p $FAST_BASE
fi
cd $FAST_BASE
sed -i "s/[0-9]\\1,3\\\\.[0-9]\\1,3\\\\.[0-9]\\1,3\\\\.[0-9]\\1,3\\/$FDFS_HOST/g" docker-compose.yml
sh up.sh
check
12.部署nginx
echo "部署Nginx..."
sleep 2
NGINX_BASE=$PUB_PATH/nginx
if [ ! -e $NGINX_BASE ];then
mkdir -p $NGINX_BASE
fi
cd $NGINX_BASE
sh up.sh
check
tar zxvf data.tar.gz -C ./
sed -i "s/[0-9]\\1,3\\\\.[0-9]\\1,3\\\\.[0-9]\\1,3\\\\.[0-9]\\1,3\\/$GATEWAY_IP/g" data/config.js
check
13结束提示语
sleep 2
echo "所有中间件均已部署完成,请检查相应的日志..."
部署应用
#!/bin/bash
1.定义应用路径
PUB_PATH=/deploy/apps
echo "请确认公共路径是不是$PUB_PATH,如果不是请修改..."
echo "请输入【是】或【不是】..."
read -p "请确认是或不是": YES
if [ $YES == 是 ];then
echo "默认路径,不需要修改..."
else
read -p "请输入你的真实公共路径": NEW_PATH
PUB_PATH=$NEW_PATH
fi
APAAS_PATH=$PUB_PATH/apaas
SRM_PATH=$PUB_PATH/srm
if [ ! -e $APAAS_PATH ];then
mkdir -p $APAAS_PATH
fi
if [ ! -e $SRM_PATH ];then
mkdir -p $SRM_PATH
fi
2.解压应用压缩包
sleep 2
echo "开始解压应用压缩包..."
tar zxf app.tar.gz -C /deploy/apps
3.定义默认配置变量
SRV_IP=10.10.10.203
MQ_IP=10.10.10.203
MQ_USERNAME=apaas
MQ_PASS=apaas
MQ_VHOST=/apaas
REDIS_HOST=10.10.10.203
FDFS_SRV=10.10.10.203:22122
NACOS_HOST=10.10.10.203:8848
NACOS_NS=apaas
NACOS_GROUP=apaas
BUSINESS_DB=apaas_business
BUSINESS_USER=apaas_business
BUSINESS_PASS=apaas_business
PLATFORM_DB=apaas_platform
PLATFORM_USER=apaas_platform
PLATFORM_PASS=apaas_platform
XMX=500M
XMN=250M
MYSQL_HOST=10.10.10.203
MYSQL_PORT=3306
echo "请确认是否已经修改完相应的配置..."
sleep 2
echo "应用所在主机IP: $SRV_IP"
echo "数据库地址和端口: $MYSQL_HOST:$MYSQL_PORT"
echo "平台库名: $PLATFORM_DB"
echo "平台库用户名: $PLATFORM_USER"
echo "平台库密码: $PLATFORM_PASS"
echo "业务库名: $BUSINESS_DB"
echo "业务库用户名: $BUSINESS_USER"
echo "业务库密码: $BUSINESS_PASS"
echo "MQ主机地址: $MQ_IP"
echo "MQ用户: $MQ_USERNAME"
echo "MQ虚拟主机名: $MQ_VHOST"
echo "MQ密码: $MQ_PASS"
echo "Redis主机地址: $REDIS_HOST"
echo "文件系统主机地址: $FDFS_SRV"
echo "Nacos主机主机: $NACOS_HOST"
echo "Nacos命名空间: $NACOS_NS"
echo "Nacos组: $NACOS_GROUP"
echo "应用jvm配置: Xmx和Xms值:$XMX Xmn值:$XMN"
echo "请输入【是】或【不是】..."
read -p "请确认是否已经修改完相应的配置": YES
if [ $YES == 是 ];then
echo "确认配置,执行下面步骤..."
else
echo "没有确认配置,无法执行下面步骤,即将退出,请再次确认配置..."
exit 1
fi
4.定义检测函数
check()
if [ $? -eq 0 ];then
echo "this step is right, continue..."
sleep 1
else
echo "this step has some problem,please check it..."
sleep 1
exit 1
fi
5.定义替换函数
config()
sed -i s/IP_ADDR=.*/IP_ADDR=$SRV_IP/g start.*.sh
sed -i s/SPRING_RABBITMQ_HOST=.*/SPRING_RABBITMQ_HOST=$MQ_IP/g start.*.sh
sed -i s/SPRING_RABBITMQ_USERNAME=.*/SPRING_RABBITMQ_USERNAME=$MQ_USERNAME/g start.*.sh
sed -i s/RABBITMQ_PASSWORD=.*/RABBITMQ_PASSWORD=$MQ_PASS/g start.*.sh
sed -i s@RABBITMQ_VIRTUAL_HOST=.*@RABBITMQ_VIRTUAL_HOST=$MQ_VHOST@g start.*.sh
sed -i s/REDIS_HOST=.*/REDIS_HOST=$REDIS_HOST/g start.*.sh
sed -i s/SPRING_REDIS_DATABASE=.*/SPRING_REDIS_DATABASE=0/g start.*.sh
sed -i s/FILE_TRACKER_SERVER=.*/FILE_TRACKER_SERVER=$FDFS_SRV/g start.*.sh
sed -i s/NACOS_CONFIG_SERVER_ADDR=.*/NACOS_CONFIG_SERVER_ADDR=$NACOS_HOST/g start.*.sh
sed -i s/NACOS_CONFIG_NAMESPACE=.*/NACOS_CONFIG_NAMESPACE=$NACOS_NS/g start.*.sh
sed -i s/NACOS_NAMESPACE=.*/NACOS_NAMESPACE=$NACOS_NS/g start.*.sh
sed -i s/NACOS_CONFIG_GROUP=.*/NACOS_CONFIG_GROUP=$NACOS_GROUP/g start.*.sh
sed -i s/NACOS_SERVER_ADDR=.*/NACOS_SERVER_ADDR=$NACOS_HOST/g start.*.sh
sed -i s/NACOS_GROUP=.*/NACOS_GROUP=$NACOS_GROUP/g start.*.sh
sed -ri s/\\-m +[0-9]3M +\\-c/\\-m $XMX -n $XMN -c/g start.*.sh
sed -i s/10.10.10.230:13306/$MYSQL_HOST:$MYSQL_PORT/g */conf/dataSource-dev.xml
sed -i s@"<user>apaas_business</user>"@"<user>$BUSINESS_USER</user>"@g */conf/dataSource-dev.xml
sed -i s@"<password>5rXvFqG6</password>"@"<password>$BUSINESS_PASS</password>"@g */conf/dataSource-dev.xml
sed -i s@"<user>apaas_platform</user>"@"<user>$PLATFORM_USER</user>"@g */conf/dataSource-dev.xml
sed -i s@"<password>jmSYRBZ8</password>"@"<password>$PLATFORM_PASS</password>"@g */conf/dataSource-dev.xml
6.替换apaas默认值
echo "替换apaas的默认配置值..."
sleep 2
cd $APAAS_PATH
config
check
7.替换srm默认值
echo "替换srm的默认配置值..."
sleep 2
cd $SRM_PATH
config
check
8.防火墙开放端口
echo "配置防火墙开放相应的端口..."
sleep 2
firewall-cmd --permanent --zone=public --add-port=5100/tcp
firewall-cmd --permanent --zone=public --add-port=5200/tcp
firewall-cmd --permanent --zone=public --add-port=5300/tcp
firewall-cmd --permanent --zone=public --add-port=5305/tcp
firewall-cmd --permanent --zone=public --add-port=5501-5505/tcp
firewall-cmd --permanent --zone=public --add-port=5507-5509/tcp
firewall-cmd --permanent --zone=public --add-port=5512/tcp
firewall-cmd --reload
9.启动平台
echo "启动apaas平台..."
sleep 2
cd $APAAS_PATH
sh start.sh
check
10.启动srm
echo "启动srm应用..."
sleep 2
cd $SRM_PATH
sh start.sh
check
###11.安装结束提示语###
sleep 2
echo "应用已安装并已启动,启动过程比较慢,请耐心等待,并请查看日志..."
以上是关于几个常用一键部署脚本的主要内容,如果未能解决你的问题,请参考以下文章