1.Jenkinsfile

// 需要在jenkins的Credentials设置中配置jenkins-harbor-creds、jenkins-k8s-config参数
pipeline {
    agent any
    environment {
        HARBOR_CREDS = credentials('jenkins-harbor-creds')
        K8S_CONFIG = credentials('jenkins-k8s-config')
       // GIT_TAG = sh(returnStdout: true,script: 'git describe --tags').trim()
        GIT_TAG = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
        //新建的
         mvnHome = tool 'Maven'
    }
    parameters {
        string(name: 'HARBOR_HOST', defaultValue: '192.168.10.146', description: 'harbor仓库地址')
        string(name: 'DOCKER_IMAGE', defaultValue: 'ray-monkey/bond-ray-monkey', description: 'docker镜像名')
        string(name: 'APP_NAME', defaultValue: 'pipeline-demo', description: 'k8s中标签名')
        string(name: 'K8S_NAMESPACE', defaultValue: 'default', description: 'k8s的namespace名称')
    }
    stages {
        stage('Maven Build') {
            when { expression { env.GIT_TAG != null } }
           // agent {
               // docker {
                    //image 'maven:3-jdk-8-alpine'
                   // args '-v $HOME/.m2:/root/.m2'
               // }
           // }
          
            steps {
                //sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
                //sh 'mvn clean package -Dfile.encoding=UTF-8 -DskipTests=true'
                sh "'${mvnHome}/bin/mvn' clean package -Dfile.encoding=UTF-8 -DskipTests=true"
                stash includes: 'target/*.jar', name: 'app'
            }

        }
        stage('Docker Build') {
            when { 
                allOf {
                    expression { env.GIT_TAG != null }
                }
            }
            agent any
            steps {
                unstash 'app'
                sh "docker login -u ${HARBOR_CREDS_USR} -p ${HARBOR_CREDS_PSW} ${params.HARBOR_HOST}"
                sh "docker build --build-arg JAR_FILE=`ls target/*.jar |cut -d '/' -f2` -t ${params.HARBOR_HOST}/${params.DOCKER_IMAGE}:${GIT_TAG} ."
                //  sh "docker build --build-arg JAR_FILE=`bond-ray-monkey.jar` -t ${params.HARBOR_HOST}/${params.DOCKER_IMAGE}:${GIT_TAG} ."
                sh "docker push ${params.HARBOR_HOST}/${params.DOCKER_IMAGE}:${GIT_TAG}"
                sh "docker rmi ${params.HARBOR_HOST}/${params.DOCKER_IMAGE}:${GIT_TAG}"
            }
            
        }
        stage('Deploy') {
            when { 
                allOf {
                    expression { env.GIT_TAG != null }
                }
            }
            agent {
               docker {
                   image 'lwolf/helm-kubectl-docker'
               }
           }
            steps {
                sh "mkdir -p ~/.kube"
                sh "echo -n ${K8S_CONFIG} | base64 -d > ~/.kube/config"
                sh "sed -e 's#{IMAGE_URL}#${params.HARBOR_HOST}/${params.DOCKER_IMAGE}#g;s#{IMAGE_TAG}#${GIT_TAG}#g;s#{APP_NAME}#${params.APP_NAME}#g;s#{SPRING_PROFILE}#dev#g' k8s-deployment.tpl > k8s-deployment.yml"
                sh "kubectl apply -f k8s-deployment.yml --namespace=${params.K8S_NAMESPACE}"
            }
            
        }
        
    }
}
 

 

2. Dockerfile

FROM openjdk:8-jdk-alpine
MAINTAINER caohongshuang
# 构建参数
# ARG JAR_FILE
# ARG WORK_PATH="/opt/demo"
# 环境变量
# ENV JAVA_OPTS="" \
   #JAR_FILE=${JAR_FILE}

# 设置时区
# RUN apk update && apk add ca-certificates && \
#    apk add tzdata && \
#    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
#    echo "Asia/Shanghai" > /etc/timezone

# COPY target/$JAR_FILE $WORK_PATH/
COPY target/bond-ray-monkey.jar app.jar

# WORKDIR $WORK_PATH

# ENTRYPOINT exec java $JAVA_OPTS -jar $JAR_FILE
# CMD java -jar app.jar
EXPOSE 8088
ENTRYPOINT ["java","-jar","/app.jar"]
 

3.k8s-deployment.tpl

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {APP_NAME}-deployment
  labels:
    app: {APP_NAME}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {APP_NAME}
  template:
    metadata:
      labels:
        app: {APP_NAME}
    spec:
      containers:
         - name: {APP_NAME}
           image: {IMAGE_URL}:{IMAGE_TAG}
           ports:
           - containerPort: 40080
           env:
              - name: SPRING_PROFILES_ACTIVE
                value: {SPRING_PROFILE}
      imagePullSecrets:
       - name: myregistrykey

备注:如果发现Pods的状态为ImagePullBackOff, 最终发现是,因为k8s不能够拉取私有仓库的镜像。kubectl create secret docker-registry myregistrykey --docker-server=https://index.docker.io/v1/ --docker-username=wucong60 --docker-password=xxxx--docker-email=xxxx@qq.com 。查看kubectl get secrets 。这样我们就创建好了secret,然后再将这个secret加到yaml文件中。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐