zoukankan      html  css  js  c++  java
  • jenkins+k8s的自动部署脚本

    用了一个很简单的方式,只是把kubernetes的config文件给放到/home/jenkins/.kube/下面了,直接通过集群名字进行发布

    需要注意harbor里面的账号具有删除镜像的权限,这里只保留30个版本的tag,之前的tag做删除处理

    希望能帮到大家,也希望能给予指正,谢谢。

    #!/bin/bash
    CLUSTERLIST="k8s-app-service-prod-default-nodepool-396630973"
    CLUSTERLIST="k8s"
    NAMESPACE="prod"
    SERVICE_NAME="app-service"
    
    IMAGE_USERNAME="cicd_s_xxxx"
    IMAGE_PASSWORD="xxxxxxxxx"
    IMAGE_REGISTRY="harbor.xxxx.top"
    IMAGE_PROJECT="xxxx"
    IMAGE_REPOSITORY="${IMAGE_REGISTRY}/${IMAGE_PROJECT}/${SERVICE_NAME}"
    IMAGE_TAG="${BUILD_NUMBER}"
    IMAGE_OLDTAG=$((${IMAGE_TAG}-30))
    
    BUILD () {
    echo "[INFO]----------------------------------"
    echo "[INFO] Pull the code to build the image"
    echo "[INFO]----------------------------------"
    docker build . -t ${IMAGE_REPOSITORY}:${IMAGE_TAG}
    if [ $? -ne 0 ];then
       echo "[ERROR]+----------------------------------------------------+"
       echo "[ERROR]+     × × × ×  Build image failed  × × × ×    +"
       echo "[ERROR]+----------------------------------------------------+"
       exit 1
    fi
    docker logout ${IMAGE_REGISTRY}
    docker login -u ${IMAGE_USERNAME} -p ${IMAGE_PASSWORD} ${IMAGE_REGISTRY}
    echo "[INFO]------------------------------------------"
    echo "[INFO] Push the built image to ${IMAGE_REGISTRY}"
    echo "[INFO]------------------------------------------"
    docker push ${IMAGE_REPOSITORY}:${IMAGE_TAG}
    if [ $? -ne 0 ];then
       echo "[ERROR]+-------------------------------------------------+"
       echo "[ERROR]+    × × × ×  Push image failed  × × × ×   +"
       echo "[ERROR]+-------------------------------------------------+"
       exit 1
    fi
    }
    
    CLEAN_IMAGE () {
    echo "[INFO]-----------------------------------------------------"
    echo "[INFO] Delete the image ${IMAGE_REPOSITORY}:${IMAGE_OLDTAG}"
    echo "[INFO]-----------------------------------------------------"
    docker rmi -f ${IMAGE_REPOSITORY}:${IMAGE_TAG}
    if [ $? -ne 0 ];then
       echo "[ERROR]+--------------------------------------------------------------------------------------+"
       echo "[ERROR]+    × × × × Delete local image ${IMAGE_REPOSITORY}:${IMAGE_OLDTAG}  failed × × × ×   +"
       echo "[ERROR]+--------------------------------------------------------------------------------------+"
       exit 1
    fi
    if [ ${IMAGE_TAG} -gt 30 ]; then
       echo "[INFO] Delete remote image ${IMAGE_REPOSITORY}:${IMAGE_OLDTAG}."
       curl -u "$IMAGE_USERNAME:$IMAGE_PASSWORD" -X DELETE -H "Content-Type: application/json" -k "https://${IMAGE_REGISTRY}/api/repositories/${IMAGE_PROJECT}/${SERVICE_NAME}/tags/${IMAGE_OLDTAG}"
       if [ $? -ne 0 ];then
       echo "[ERROR]+-------------------------------------------------------------------------------------------+"
       echo "[ERROR]+     × × × ×  Delete remote image ${IMAGE_REPOSITORY}:${IMAGE_OLDTAG} failed  × × × ×     +"
       echo "[ERROR]+-------------------------------------------------------------------------------------------+"
       exit 1
      fi
    elif [ ${IMAGE_TAG} -lt 30 ]; then
       echo "[INFO] ${IMAGE_TAG} less than 30"
    else
       echo "[INFO] ${IMAGE_TAG} equal 30"
    fi
    }
    
    DEPLOY () {
    echo "[INFO]---------------------------------------------------------"
    echo "[INFO] Deploy with the ${IMAGE_REPOSITORY}:${IMAGE_TAG}        "
    echo "[INFO]---------------------------------------------------------"
    for CLUSTER  in `echo "${CLUSTERLIST}"|awk -F, 'BEGIN{OFS=" "}{$1=$1;printf("%s",$0);}'`; do
      echo "[INFO] Remotely update ${SERVICE_NAME} service image to ${IMAGE_TAG} tag"
      echo "[INFO] cluser:${CLUSTER} namespace:${NAMESPACE} service:${SERVICE_NAME} tag:${IMAGE_TAG} start"
      kubectl --context ${CLUSTER} set image deployment/${SERVICE_NAME} ${SERVICE_NAME}=${IMAGE_REPOSITORY}:${IMAGE_TAG} --namespace=${NAMESPACE}
      kubectl --context ${CLUSTER} rollout status deployment ${SERVICE_NAME} --namespace=${NAMESPACE}
      if [ $? -ne 0 ];then
        echo "[ERROR]+---------------------------------------------+"
        echo "[ERROR]+     × × × × Deploy failed × × × ×    +"
        echo "[ERROR]+---------------------------------------------+"
        exit 1
      else
        sleep 10s
        echo "[INFO]kubectl --context ${CLUSTER} get pod,service,ingress,deployment  --namespace=${NAMESPACE}"
        kubectl --context ${CLUSTER} get pod,service,ingress,deployment  --namespace=${NAMESPACE}
        echo "[INFO] cluser:${CLUSTER} namespace:${NAMESPACE} service:${SERVICE_NAME} tag:${IMAGE_TAG}  done"
        echo "[INFO]+------------------------------------------------------+"
        echo "[INFO]+     √ √ √ √  Deploy Successfully  √ √ √ √     +"
        echo "[INFO]+------------------------------------------------------+"
      fi
    done
    }
    
    BUILD
    CLEAN_IMAGE
    DEPLOY
    
  • 相关阅读:
    debian系统完全卸载mysql
    已解决:Linux虚拟机创建后无法切换到root
    已解决:win10 下MarkdownPad2渲染出错:This View has crashed!
    计算机网络---运输层
    计算机网络---网络层
    计算机网络---数据链路层
    计算机网络---概述
    计算机网络---物理层
    go的命令行参数---flag
    Go---第九章:使用共享变量实现并发(小知识点笔记)
  • 原文地址:https://www.cnblogs.com/DevOpsTechLab/p/12906891.html
Copyright © 2011-2022 走看看