微服务用yml安装系统(第一版)
当用微服务安装系统后,面临服务较多,一个一个安装比较麻烦,是否有统一的脚本可以直接执行安装呢?答案是肯定的:
1、首先介绍一下所有安装脚本,如下图
spd-volume:是各服务外挂的资料卷
common.sh文件主要封装了一些公共的函数,比较docker的安装与检查
daemon.json与dokcer.service文件是安装docker必须的文件
docker-compose.yml文件是安装各服务的yml语法文件
install.conf是配置文件,如服务的端口ip都可在此配置
install.sh程序运行的主命令
2、将所有的安装脚本复制到要安装的linux服务器上面
新建/home/shell/spd-auto文件夹,将所有要安装的文件复制进去
3、修改配置文件,主要是修改如下的镜像的版本,和服务器的ip
4、修改各服务外挂的资料卷文件
以一个文件为例,主要是修改里面的ip地址和数据库连接地址
5、执行命令安装程序
进入到/home/shell/spd-auto文件夹中,执行如下命
./install.sh
如权限不足执行:chmod +x install.sh
如果不出意外,所有的服务都会安装完成
6、还原数据库
注意数据库的名字要与资料卷中的数据库的名称一致。
7、最后将将所有的服务脚本文件代码贴上,有问题请留言,当然脚本还有很多不足的地方,希望大家提出宝贵的建议。
install.sh
#!/bin/bash
set -e
DIR="$(cd "$(dirname "$0")" && pwd)"
source $DIR/common.sh
set +o noglob
item=0
#拷资料卷开始
if [ ! -d "/home/data" ]
then
mkdir /home/data
fi
cp -a ${DIR}/spd-volume /home/data
#拷资料卷结束
workdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $workdir
h2 "[Step $item]: checking if docker is installed ..."; let item+=1
check_docker
h2 "[Step $item]: checking docker-compose is installed ..."; let item+=1
check_dockercompose
h2 "[Step $item]: preparing environment ..."; let item+=1
if [ -f "install.conf" ]
then
cat ${DIR}/install.conf > .env
source ${DIR}/install.conf
source ${DIR}/.env
else
error "install.conf file does not exist"
exit 1
fi
h2 "[Step $item]: loading SPD images ..."; let item+=1
if [ -f spd*.tar ]
then
docker load -i ./spd*.tar
else
docker login -u=${SPD_DOCKER_USER} -p=${SPD_DOCKER_PASSWORD} ${SPD_DOCKER_HOST}
fi
echo ""
if [ -n "$(docker-compose ps -q)" ]
then
note "stopping existing SPD instance ..."
docker-compose down -v
fi
echo ""
h2 "[Step $item]: starting SPD ..."
docker-compose up -d
success $"----SPD has been installed and started successfully.----"
install.conf
# 基础配置 ## 安装路径配置及数据文件默认将安装在 ${SPD_BASE} 目录下 SPD_BASE=/home #环境变量 SPD_ENVIRONMENT=Production # Docker配置 SPD_DOCKER_USER=18963948278 SPD_DOCKER_PASSWORD=Zhengwei0101 SPD_DOCKER_HOST=219.139.241.229:8844 ## 镜像前缀 SPD_IMAGE_PREFIX=219.139.241.229:8844 ## 安装模式 SPD_INSTALL_MODE=allinone ## MeterSphere 主程序的 HTTP 服务监听端口 SPD_SERVER_PORT=8080 # 数据库配置 ## 是否使用外部数据库 SPD_EXTERNAL_MYSQL=false ## 数据库地址 SPD_MYSQL_HOST=192.168.139.129 ## 数据库端口 SPD_MYSQL_PORT=3306 ## 数据库库名 SPD_MYSQL_DB=spd ## 数据库用户名 SPD_MYSQL_USER=root ## 数据库密码 SPD_MYSQL_PASSWORD=123456 # Redis 配置 ## 是否使用外部Redis SPD_EXTERNAL_REDIS=false ## Redis 端口 SPD_REDIS_PORT=6379 ## Redis 密码 SPD_REDIS_PASSWORD=123456 ## Redis地址 SPD_REDIS_HOST=192.168.139.129 # Consul 配置 SPD_CONSUL_PORT=8500 SPD_CONSUL_HOST=192.168.139.129 # minio 配置 SPD_MINIO_PORT=9000 SPD_MINIO_HOST=192.168.139.129 # Identity 配置 SPD_IDENTITY_IMAGE=identity-test:34 SPD_IDENTITY_PORT=8086 SPD_IDENTITY_HOST=192.168.139.129 # auth 配置 SPD_AUTH_IMAGE=auth-test:8 SPD_AUTH_PORT=8087 SPD_AUTH_HOST=192.168.139.129 # file 配置 SPD_FILE_IMAGE=file-test:9 SPD_FILE_PORT=8077 SPD_FILE_HOST=192.168.139.129 # web 配置 SPD_WEB_IMAGE=spd-web-develop:98 SPD_WEB_PORT=8088 SPD_WEB_HOST=192.168.139.129 # gateway 配置 SPD_GATEWAY_IMAGE=gateway-test:25 SPD_GATEWAY_PORT=8089 SPD_GATEWAY_HOST=192.168.139.129 # baseinfo 配置 SPD_BASEINFO_IMAGE=base-develop:29 SPD_BASEINFO_PORT=8085 SPD_BASEINFO_HOST=192.168.139.129 # product 配置 SPD_PRODUCT_IMAGE=product-develop:50 SPD_PRODUCT_PORT=8083 SPD_PRODUCT_HOST=192.168.139.129 # stockmanage 配置 SPD_STOCK_IMAGE=stockmanage-develop:25 SPD_STOCK_PORT=8082 SPD_STOCK_HOST=192.168.139.129 # order 配置 SPD_ORDER_IMAGE=order-develop:109 SPD_ORDER_PORT=8084 SPD_ORDER_HOST=192.168.139.129 # interface 配置 SPD_INTERFACE_IMAGE=interface-develop:22 SPD_INTERFACE_PORT=8078 SPD_INTERFACE_HOST=192.168.139.129 # monthlystatement 配置 SPD_MONTHLYSTATEMENT_IMAGE=monthlystatement-develop:22 SPD_MONTHLYSTATEMENT_PORT=8080 SPD_MONTHLYSTATEMENT_HOST=192.168.139.129 # materialpackage 配置 SPD_MATERIALPACKAGE_IMAGE=materialpackage-develop:28 SPD_MATERIALPACKAGE_PORT=8079 SPD_MATERIALPACKAGE_HOST=192.168.139.129 # reagent 配置 SPD_REAGENT_IMAGE=reagent-develop:21 SPD_REAGENT_PORT=8081 SPD_REAGENT_HOST=192.168.139.129 # drug 配置 SPD_DRUG_IMAGE=drug-develop:21 SPD_DRUG_PORT=8077 SPD_DRUG_HOST=192.168.139.129
docker-compose.yaml文件
version: '2.3' services: redis: image: ${SPD_IMAGE_PREFIX}/rancher/redis:latest container_name: redis ports: - ${SPD_REDIS_PORT}:6379 restart: always cap_drop: - ALL cap_add: - CHOWN - SETGID - SETUID environment: - TZ=Asia/Shanghai command: --requirepass ${SPD_REDIS_PASSWORD} --appendonly yes volumes: - ${SPD_BASE}/data/redis:/data networks: - spd-network mysql: image: ${SPD_IMAGE_PREFIX}/rancher/mysql:8.0 container_name: mysql environment: MYSQL_ROOT_PASSWORD: ${SPD_MYSQL_PASSWORD} ports: - ${SPD_MYSQL_PORT}:3306 command: --lower_case_table_names=1 volumes: - ${SPD_BASE}/data/mysql:/var/lib/mysql networks: - spd-network restart: always consul: image: ${SPD_IMAGE_PREFIX}/rancher/consul:latest container_name: consul ports: - ${SPD_CONSUL_PORT}:8500 environment: - TZ=Asia/Shanghai command: agent -server -bootstrap -ui -client=0.0.0.0 volumes: - ${SPD_BASE}/data/consul/data:/consul/data - ${SPD_BASE}/data/consul/config:/consul/config networks: - spd-network restart: always minio: image: ${SPD_IMAGE_PREFIX}/rancher/minio:RELEASE.2021-06-17T00-10-46Z container_name: minio ports: - ${SPD_MINIO_PORT}:9000 command: server /data environment: - TZ=Asia/Shanghai volumes: - ${SPD_BASE}/data/minio/data:/data - ${SPD_BASE}/minio/config:/root/.minio networks: - spd-network restart: always identity: image: ${SPD_IMAGE_PREFIX}/oms/${SPD_IDENTITY_IMAGE} container_name: spd-identity ports: - ${SPD_IDENTITY_PORT}:8016 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/identity/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/identity/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/identity/Logs:/app/Logs - /usr/share/zoneinfo/Asia:/usr/share/zoneinfo/Asia:ro networks: - spd-network restart: always auth: image: ${SPD_IMAGE_PREFIX}/oms/${SPD_AUTH_IMAGE} container_name: spd-auth ports: - ${SPD_AUTH_PORT}:8015 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/auth/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/auth/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/auth/Logs:/app/Logs networks: - spd-network restart: always web: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_WEB_IMAGE} container_name: spd-web ports: - ${SPD_WEB_PORT}:8020 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/web/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/web/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/web/Logs:/app/Logs - ${SPD_BASE}/data/spd-volume/web/scripts/Config.js:/app/wwwroot/scripts/Config.js - ${SPD_BASE}/data/spd-volume/web/ActiveReportSource/Custom:/app/ActiveReportSource/Custom networks: - spd-network restart: always gateway: image: ${SPD_IMAGE_PREFIX}/oms/${SPD_GATEWAY_IMAGE} container_name: spd-gateway ports: - ${SPD_GATEWAY_PORT}:8083 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/gateway/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/gateway/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/gateway/Logs:/app/Logs networks: - spd-network restart: always file: image: ${SPD_IMAGE_PREFIX}/oms/${SPD_FILE_IMAGE} container_name: file ports: - ${SPD_FILE_PORT}:8018 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/file/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/file/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/file/Logs:/app/Logs networks: - spd-network restart: always baseinfo: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_BASEINFO_IMAGE} container_name: spd-baseinfo ports: - ${SPD_BASEINFO_PORT}:8011 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/baseinfo/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/baseinfo/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/baseinfo/Logs:/app/Logs networks: - spd-network restart: always product: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_PRODUCT_IMAGE} container_name: spd-product ports: - ${SPD_PRODUCT_PORT}:8012 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/product/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/product/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/product/Logs:/app/Logs networks: - spd-network restart: always stockmanage: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_STOCK_IMAGE} container_name: spd-stockmanage ports: - ${SPD_STOCK_PORT}:8023 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/stockmanage/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/stockmanage/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/stockmanage/Logs:/app/Logs networks: - spd-network restart: always order: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_ORDER_IMAGE} container_name: spd-order ports: - ${SPD_ORDER_PORT}:8025 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/order/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/order/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/order/Logs:/app/Logs networks: - spd-network restart: always interface: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_INTERFACE_IMAGE} container_name: spd-interface ports: - ${SPD_INTERFACE_PORT}:8026 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/interface/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/interface/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/interface/Logs:/app/Logs networks: - spd-network restart: always monthlystatement: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_MONTHLYSTATEMENT_IMAGE} container_name: spd-monthlystatement ports: - ${SPD_MONTHLYSTATEMENT_PORT}:8024 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/monthlystatement/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/monthlystatement/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/monthlystatement/Logs:/app/Logs networks: - spd-network restart: always materialpackage: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_MATERIALPACKAGE_IMAGE} container_name: spd-materialpackage ports: - ${SPD_MATERIALPACKAGE_PORT}:8027 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/materialpackage/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/materialpackage/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/materialpackage/Logs:/app/Logs networks: - spd-network restart: always reagent: image: ${SPD_IMAGE_PREFIX}/spd/${SPD_REAGENT_IMAGE} container_name: spd-reagent ports: - ${SPD_REAGENT_PORT}:8029 environment: - TZ=Asia/Shanghai - ASPNETCORE_ENVIRONMENT=${SPD_ENVIRONMENT} volumes: - ${SPD_BASE}/data/spd-volume/reagent/appsettings.json:/app/appsettings.json:ro - ${SPD_BASE}/data/spd-volume/reagent/appsettings.${SPD_ENVIRONMENT}.json:/app/appsettings.${SPD_ENVIRONMENT}.json:ro - ${SPD_BASE}/data/spd-volume/reagent/Logs:/app/Logs networks: - spd-network restart: always networks: spd-network: external: false
daemon.json
{ "insecure-registries": [ "219.139.241.229:8844" ], "registry-mirrors": [ "http://hub-mirror.c.163.com", "https://registry.docker-cn.com", "https://docker.mirrors.ustc.edu.cn" ], "log-driver":"json-file", "log-opts":{"max-size":"500m","max-file":"3"} }
common.sh
#!/bin/bash #docker version: 17.06.0+ #docker-compose version: 1.18.0+ DIR="$(cd "$(dirname "$0")" && pwd)" set +e set -o noglob # # Set Colors # bold=$(tput bold) underline=$(tput sgr 0 1) reset=$(tput sgr0) red=$(tput setaf 1) green=$(tput setaf 76) white=$(tput setaf 7) tan=$(tput setaf 202) blue=$(tput setaf 25) # # Headers and Logging # underline() { printf "${underline}${bold}%s${reset}\n" "$@" } h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@" } h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@" } debug() { printf "${white}%s${reset}\n" "$@" } info() { printf "${white}➜ %s${reset}\n" "$@" } success() { printf "${green}✔ %s${reset}\n" "$@" } error() { printf "${red}✖ %s${reset}\n" "$@" } warn() { printf "${tan}➜ %s${reset}\n" "$@" } bold() { printf "${bold}%s${reset}\n" "$@" } note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@" } set -e function check_docker { if ! docker --version &> /dev/null then install_docker # error "Need to install docker(17.06.0+) first and run this script again." # exit 1 fi # docker has been installed and check its version if [[ $(docker --version) =~ (([0-9]+)\.([0-9]+)([\.0-9]*)) ]] then docker_version=${BASH_REMATCH[1]} docker_version_part1=${BASH_REMATCH[2]} docker_version_part2=${BASH_REMATCH[3]} note "docker version: $docker_version" # the version of docker does not meet the requirement if [ "$docker_version_part1" -lt 17 ] || ([ "$docker_version_part1" -eq 17 ] && [ "$docker_version_part2" -lt 6 ]) then error "Need to upgrade docker package to 17.06.0+." fi else error "Failed to parse docker version." exit 1 fi } function check_dockercompose { if ! docker-compose --version &> /dev/null then install_docker_compose # error "Need to install docker-compose(1.18.0+) by yourself first and run this script again." # exit 1 fi # docker-compose has been installed, check its version if [[ $(docker-compose --version) =~ (([0-9]+)\.([0-9]+)([\.0-9]*)) ]] then docker_compose_version=${BASH_REMATCH[1]} docker_compose_version_part1=${BASH_REMATCH[2]} docker_compose_version_part2=${BASH_REMATCH[3]} note "docker-compose version: $docker_compose_version" # the version of docker-compose does not meet the requirement if [ "$docker_compose_version_part1" -lt 1 ] || ([ "$docker_compose_version_part1" -eq 1 ] && [ "$docker_compose_version_part2" -lt 18 ]) then error "Need to upgrade docker-compose package to 1.18.0+." exit 1 fi else error "Failed to parse docker-compose version." exit 1 fi } # install docker function install_docker { if [ ! -d "/etc/docker/" ]; then mkdir /etc/docker else echo "Folder already exists" fi cp daemon.json /etc/docker/ if [[ -d docker ]]; then info "... offline install docker" cp docker/* /usr/bin/ cp docker.service /usr/lib/systemd/system/ chmod +x /usr/bin/docker* chmod 754 /usr/lib/systemd/system/docker.service else info "... online install docker" curl -fsSL https://get.docker.com -o get-docker.sh 2>&1 | tee -a ${DIR}/install.log sudo sh get-docker.sh --mirror Aliyun 2>&1 | tee -a ${DIR}/install.log fi if [ ! -f "/usr/lib/systemd/system/docker.service" ]; then cp docker.service /usr/lib/systemd/system/ else echo "file already exists" fi info "... start docker" systemctl daemon-reload && systemctl start docker 2>&1 | tee -a ${DIR}/install.log systemctl enable docker docker ps 1>/dev/null 2>/dev/null if [ $? != 0 ];then error "Docker 未正常启动,请先安装并启动 Docker 服务后再次执行本脚本" exit 1 fi } function install_docker_compose { if [[ -d docker ]]; then info "... offline docker-compose" cp docker/bin/docker-compose /usr/bin/ chmod +x /usr/bin/docker-compose else info "... online docker-compose" curl -L https://get.daocloud.io/docker/compose/releases/download/v2.2.3/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose 2>&1 | tee -a ${DIR}/install.log chmod +x /usr/local/bin/docker-compose ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose fi docker-compose version 1>/dev/null 2>/dev/null if [ $? != 0 ];then error "docker-compose 未正常安装,请先安装 docker-compose 后再次执行本脚本" exit 1 fi }
常见问题:
解决方法一(推荐):
安装docker-compose
1、下载安装包:
官网:https://github.com/docker/compose/releases
百度云下载:
链接:https://pan.baidu.com/s/10In9P2lSLuYL3M7dVhARhQ
提取码:bax3
2、移动文件到指定目录并改名
mv docker-compose-linux-x86_64
/usr/local/bin/docker-compose
3、添加可执行权限
chmod
+x
/usr/local/bin/docker-compose
4、命令测试是否安装成功
docker-compose -v
解决方法二:
1、无法创建符号链接"/usr/bin/docker-compose": 文件已存在
下载docker-compose:curl -SL https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
加个执行权限:chmod +x /usr/local/bin/docker-compose