jenkins發佈k8s應用

一.環境準備
k8s jenkins gitee harbor maven已經安裝
資源鏈接:https://gitee.com/jikeli/k8s-demo-docker.git
二.建立流水線node

node {
    env.BUILD_DIR="/root/build-workspace/"
    env.MODULE = "web-demo"
    env.HOST = "k8s.mooc.com"
    stage('Preparation'){
        git 'http://172.16.100.234:8033/root/k8s-demo-docker.git'
    }
    stage('Maven Build'){
        sh "/usr/local/maven/apache-maven-3.6.3/bin/mvn -pl ${MODULE} -am clean package"

    }
    stage('Build Image'){
        sh "/root/script/build-image-web.sh"
    }
    stage('Deploy'){
        sh "/root/script/deploy.sh"
    }
}

三.相關腳本
build-image-web.shgit

#!/bin/bash
if [ "${BUILD_DIR}" == "" ];then
   echo "env 'BUILD_DIR' is not set"
   exit 1
fi
DOCKER_DIR=${BUILD_DIR}/${JOB_NAME}
if [ ! -d ${DOCKER_DIR} ];then
    mkdir -p ${DOCKER_DIR}
fi
echo "docker workspace: ${DOCKER_DIR}"
JENKINS_DIR=${WORKSPACE}/${MODULE}
echo "jenkins workspace: ${JENKINS_DIR}"
if [ ! -f ${JENKINS_DIR}/target/*.war ];then
    echo "target war file not found ${JENKINS_DIR}/target/*.war"
    exit 1
fi
cd ${DOCKER_DIR}
rm -rf *
unzip -oq ${JENKINS_DIR}/target/*.war -d ./ROOT
mv ${JENKINS_DIR}/Dockerfile .
if [ -d ${JENKINS_DIR}/dockerfiles ];then
    mv ${JENKINS_DIR}/dockerfiles .
fi
VERSION=$(date +%Y%m%d%H%M%S)
IMAGE_NAME=172.16.100.234/library/${JOB_NAME}:${VERSION}
echo "${IMAGE_NAME}" > ${WORKSPACE}/IMAGE
echo "building ......."
docker build -t $IMAGE_NAME .
docker push ${IMAGE_NAME}

deploy.shweb

#!/bin/bash
name=${JOB_NAME}
image=$(cat ${WORKSPACE}/IMAGE)
host=${HOST}
echo $name $image $host
rm -f web.yaml
cp $(dirname "${BASH_SOURCE[0]}")/template/web.yaml .
sed -i "s,{{name}},${name},g" web.yaml
sed -i "s,{{image}},${image},g" web.yaml
sed -i "s,{{host}},${host},g" web.yaml
kubectl apply -f web.yaml
###健康檢查
count=60
success=0
IFS=","
sleep 5
while [ ${count} -gt 0 ]
do
    replicas=$(kubectl get deploy ${name} -o go-template='{{.status.replicas}},{{.status.updatedReplicas}},{{.status.readyReplicas}},{{.status.availableReplicas}}')
    echo "replicas: ${replicas}"
    arr=(${replicas})
    if [ "${arr[0]}" == "${arr[1]}" -a "${arr[1]}" == "${arr[2]}" -a "${arr[2]}" == "${arr[3]}" ];then
        echo "health check success"
        success=1
        break
    fi
    ((count--))
    sleep 2
done
if [ ${success} -ne 1 ];then
    echo "health check failed"
fi``

四.ingress域名解析
ip k8s.mooc.com
五.部署效果圖
jenkins發佈k8s應用
六.訪問效果圖
jenkins發佈k8s應用docker

相關文章
相關標籤/搜索