From e88bfd3461a31b8b8acabfd80abd5322b22e8650 Mon Sep 17 00:00:00 2001 From: somunslotus Date: Wed, 12 Jun 2024 17:06:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=84=9A=E6=9C=ACbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- k8s/AAdeploy.sh | 142 ------------------------------------- k8s/build_and_deploy.sh | 8 +-- k8s/deploy.sh | 153 +++++++++++++++++++++------------------- 3 files changed, 84 insertions(+), 219 deletions(-) delete mode 100644 k8s/AAdeploy.sh diff --git a/k8s/AAdeploy.sh b/k8s/AAdeploy.sh deleted file mode 100644 index e1b6dbfe..00000000 --- a/k8s/AAdeploy.sh +++ /dev/null @@ -1,142 +0,0 @@ -#!/bin/bash - -# 记录开始时间 -start=$(date +%s) -# 默认参数 -service="manage-front" -env="dev" - -show_help() { - echo "Usage: $0 [-s service] [-e environment]" - echo - echo "Options:" - echo " -s Service to deploy (manage-front, manage, front, all default: manage-front)" - echo " -e Environment (e.g., dev, test, default: dev)" - echo " -h Show this help message" -} - -# 解析命令行参数 -while getopts "s:e:h" opt; do - case $opt in - s) service=$OPTARG ;; - e) env=$OPTARG ;; - h) show_help; exit 0 ;; - \?) echo "Invalid option -$OPTARG" >&2; exit 1 ;; - esac -done - -echo "Deploy service: $service, environment: $env" - -# 根据环境设置 IP 地址 -if [ "$env" == "dev" ]; then - remote_ip="172.20.32.181" -elif [ "$env" == "test" ]; then - remote_ip="172.20.32.185" -else - echo "Invalid environment - $env" - exit 1 -fi - -baseDir=/home/somuns/ci4s -tag=$(date +'%Y%m%d%H%M') -remote_deploy_dir=/home/deploy/manage-platform - -# 构建镜像函数 -build_image() { - local dockerfile=$1 - local image=$2 - cd ${baseDir}/k8s/dockerfiles - docker build -t ${image} -f ${dockerfile} . - if [ $? -ne 0 ]; then - echo "Build ${image} image fail" - exit 1 - fi - docker push ${image} -} - -# 复制和替换 YAML 文件函数 -prepare_yaml() { - local yaml_file=$1 - local image=$2 - - placeholder="\${${yaml_file%.yaml}-image}" - cd ${baseDir}/k8s/template-yaml - cp -rf ${yaml_file} deploy/ - cd deploy/ - sed -i "s|${placeholder}|${image}|g" ${yaml_file} - if [ $? -ne 0 ]; then - echo "Replace ${image} image fail" - exit 1 - fi - - # 建立远程目录并备份文件 - ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${yaml_file} ]; then mv ${remote_deploy_dir}/${yaml_file} ${remote_deploy_dir}/${yaml_file}.bak; fi" - if [ $? -ne 0 ]; then - echo "Failed to create remote directory or backup ${yaml_file}" - exit 1 - else - echo "Successfully created remote directory and backup ${yaml_file}" - fi - - scp ${baseDir}/k8s/template-yaml/deploy/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file} - if [ $? -ne 0 ]; then - echo "Failed to copy ${yaml_file}" - exit 1 - else - echo "Successfully copied ${yaml_file}" - fi -} - -# 部署服务函数 -deploy_service() { - local yaml_file=$1 - ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${yaml_file}" - if [ $? -ne 0 ]; then - echo "Failed to deploy ${yaml_file}" - exit 1 - else - echo "Successfully deployed ${yaml_file}" - fi -} - -deploy_nacos() { - local yaml_file=$1 - scp ${baseDir}/k8s/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file} - deploy_service -} - -build_and_deploy() { - local dockerfile=$1 - local image=$2 - local yaml_file=$3 - - build_image ${dockerfile} ${image} - prepare_yaml ${yaml_file} ${image} - deploy_service ${yaml_file} -} - -# 构建和部署 manage 服务 -if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then - build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml" -fi - - -# 构建和部署 front 服务 -if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then - build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml" -fi - - -if [ "$service" == "all" ]; then - build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml" - build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml" - build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}" "k8s-5auth.yaml" - build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}" "k8s-4gateway.yaml" - build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}" "k8s-6system.yaml" - deploy_nacos "k8s-3nacos.yaml" -fi - - -# 记录结束时间 -end=$(date +%s) -echo "部署成功, 耗时: $((end-start))秒" diff --git a/k8s/build_and_deploy.sh b/k8s/build_and_deploy.sh index 4c6ecaef..0f7ebbcb 100644 --- a/k8s/build_and_deploy.sh +++ b/k8s/build_and_deploy.sh @@ -19,9 +19,9 @@ show_help() { echo "Usage: $0 [-b branch] [-s service] [-e environment]" echo echo "Options:" - echo " -b Branch to deploy" - echo " -s Service to deploy (manage-front, manage, front)" - echo " -e Environment (e.g., dev, prod)" + echo " -b Branch to deploy, default: master" + echo " -s Service to deploy (manage-front, manage, front, all, default: manage-front)" + echo " -e Environment (e.g., dev, test, default: dev)" echo " -h Show this help message" } @@ -46,7 +46,7 @@ echo "build success" # 部署 echo "start deploy" -sh ${baseDir}/k8s/AAdeploy.sh -s ${service} -e ${env} +sh ${baseDir}/k8s/deploy.sh -s ${service} -e ${env} if [ $? -ne 0 ]; then echo "Deploy failed" exit 1 diff --git a/k8s/deploy.sh b/k8s/deploy.sh index 899e45dc..c9d296f9 100644 --- a/k8s/deploy.sh +++ b/k8s/deploy.sh @@ -10,7 +10,7 @@ show_help() { echo "Usage: $0 [-s service] [-e environment]" echo echo "Options:" - echo " -s Service to deploy (manage-front, manage, front, all ,default: manage-front)" + echo " -s Service to deploy (manage-front, manage, front, all default: manage-front)" echo " -e Environment (e.g., dev, test, default: dev)" echo " -h Show this help message" } @@ -25,7 +25,8 @@ while getopts "s:e:h" opt; do esac done -echo "deploy service: $service, env: $env" +echo "Deploy service: $service, environment: $env" + # 根据环境设置 IP 地址 if [ "$env" == "dev" ]; then remote_ip="172.20.32.181" @@ -37,102 +38,108 @@ else fi baseDir=/home/somuns/ci4s -cd ${baseDir}/k8s/dockerfiles -mkdir -p ${baseDir}/k8s/template-yaml/deploy - tag=$(date +'%Y%m%d%H%M') -managent=172.20.32.187/ci4s/managent:${tag} -front=172.20.32.187/ci4s/ci4s-front:${tag} - remote_deploy_dir=/home/deploy/manage-platform -manager_yaml=k8s-7management.yaml -front_yaml=k8s-12front.yaml -# 构建 manage 镜像 -if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then +# 构建镜像函数 +build_image() { + local dockerfile=$1 + local image=$2 cd ${baseDir}/k8s/dockerfiles - docker build -t ${managent} -f managent-dockerfile . - if [ "$?" -ne "0" ]; then - echo "build ${managent} image fail" + docker build -t ${image} -f ${dockerfile} . + if [ $? -ne 0 ]; then + echo "Build ${image} image fail" exit 1 fi + docker push ${image} +} + +# 复制和替换 YAML 文件函数 +prepare_yaml() { + local yaml_file=$1 + local image=$2 + placeholder="\${${yaml_file%.yaml}-image}" cd ${baseDir}/k8s/template-yaml - # 复制 YAML 文件 - cp -rf ${manager_yaml} deploy/ - # 镜像替换 + cp -rf ${yaml_file} deploy/ cd deploy/ - placeholder="\${k8s-7management-image}" - #sed -i "s#managenent-image#${managent}#g" ${manager_yaml} - sed -i "s#$placeholder#${managent}#g" ${manager_yaml} - if [ "$?" -ne "0" ];then - echo "replace ${managent} image fail" - exit 3 + sed -i "s|${placeholder}|${image}|g" ${yaml_file} + if [ $? -ne 0 ]; then + echo "Replace ${image} image fail" + exit 1 fi - # 推送镜像 - docker push ${managent} - # 部署服务 - ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${manager_yaml} ]; then mv ${remote_deploy_dir}/${manager_yaml} ${remote_deploy_dir}/k8s-7management.yaml.bak; fi" - scp ${baseDir}/k8s/template-yaml/deploy/${manager_yaml} root@$remote_ip:${remote_deploy_dir}/${manager_yaml} + + # 建立远程目录并备份文件 + ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${yaml_file} ]; then mv ${remote_deploy_dir}/${yaml_file} ${remote_deploy_dir}/${yaml_file}.bak; fi" if [ $? -ne 0 ]; then - echo "Failed to copy ${managent} yaml file" + echo "Failed to create remote directory or backup ${yaml_file}" exit 1 else - echo "Successfully copied ${managent} yaml file" + echo "Successfully created remote directory and backup ${yaml_file}" fi - ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${manager_yaml}" + + scp ${baseDir}/k8s/template-yaml/deploy/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file} if [ $? -ne 0 ]; then - echo "Failed to deploy ${managent} image" + echo "Failed to copy ${yaml_file}" exit 1 else - echo "Successfully deployed ${managent} image" + echo "Successfully copied ${yaml_file}" fi -fi - +} -# 构建 front 镜像 -if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then - cd ${baseDir}/k8s/dockerfiles - docker build -t ${front} -f nginx-dockerfile . - if [ "$?" -ne "0" ];then - echo "build ${front} image fail" - exit 2 +# 部署服务函数 +deploy_service() { + local yaml_file=$1 + ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${yaml_file}" + if [ $? -ne 0 ]; then + echo "Failed to deploy ${yaml_file}" + exit 1 + else + echo "Successfully deployed ${yaml_file}" fi +} - # 复制 YAML 文件 - cd ${baseDir}/k8s/template-yaml - cp -rf ${front_yaml} deploy/ +deploy_nacos() { + local yaml_file=$1 + scp ${baseDir}/k8s/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file} + deploy_service ${yaml_file} +} - # 镜像替换 - cd deploy/ - placeholder="\${k8s-12front-image}" - #sed -i "s#front-image#${front}#g" ${front_yaml} - sed -i "s#$placeholder#${front}#g" ${front_yaml} - if [ "$?" -ne "0" ];then - echo "replace ${front} image fail" - exit 4 - fi +build_and_deploy() { + local dockerfile=$1 + local image=$2 + local yaml_file=$3 + + build_image ${dockerfile} ${image} + prepare_yaml ${yaml_file} ${image} + deploy_service ${yaml_file} +} + +# 构建和部署 manage 服务 +if [ "$service" == "manage-front" ] || [ "$service" == "manage" ]; then + build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml" +fi - # 镜像推送 - docker push ${front} +# 构建和部署 front 服务 +if [ "$service" == "manage-front" ] || [ "$service" == "front" ]; then + build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml" +fi - #部署服务 - ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${front_yaml} ]; then mv ${remote_deploy_dir}/${front_yaml} ${remote_deploy_dir}/k8s-12front.yaml.bak; fi" - scp ${baseDir}/k8s/template-yaml/deploy/${front_yaml} root@$remote_ip:${remote_deploy_dir}/${front_yaml} - if [ $? -ne 0 ]; then - echo "Failed to copy ${managent} yaml file" - exit 1 - else - echo "Successfully copied ${managent} yaml file" - fi - ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${front_yaml}" - if [ $? -ne 0 ]; then - echo "Failed to deploy ${front} image" - exit 1 - else - echo "Successfully deployed ${front} image" - fi + +if [ "$service" == "all" ]; then + #部署前端 + build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}" "k8s-12front.yaml" + #部署管理平台 + build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}" "k8s-7management.yaml" + #部署认证中心 + build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}" "k8s-5auth.yaml" + #部署网关 + build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}" "k8s-4gateway.yaml" + #部署系统服务 + build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}" "k8s-6system.yaml" + #部署配置中心 + deploy_nacos "k8s-3nacos.yaml" fi