回到目录
将练习jenkins使用pipeline项目,结合k8s发布一个简单的springboot项目
前提:jenkins的环境和k8s环境都已经安装完成,提前准备了gitlab和一个简单的springboot项目
创建一个流水线项目
流水线中选择git,并选择gitlab的test项目,拉取分支为main
后端代码整体结构如下
jenkinsfile
在代码根目录下创建一个Jenkinsfile文件
需要注意的是environment中的value比如gitlab访问凭证gitlab-root一定要和jenkins中配置的凭证名称一致
pipeline {//agent 选择jenkins节点进行构建//any表示任意agent any//环境变量environment {//gitlab访问凭证GIT_CREDENTIAL_ID = 'gitlab-root'//gitlab地址GIT_REPO_URL = '10.190.9.23:28080'//gitlab分组GIT_GROUP = 'devops'//gitlab项目名称GIT_NAME = 'test'//harbor凭证HARBOR_ID = 'harbor-admin'//harbor地址HARBOR_URL = '30.14.181.51:8858'//harbor项目HARBOR_REPO = 'repo'//发送delpoment.yml到k8s服务器上的地址K8S_FILE_PATH = '/usr/local/k8s'//gitlab发送到服务器的目录GITLAB_DEPLOYMENT_FILE = 'deploy'}parameters {//git插件 分支参数gitParameter(branchFilter: '.*',defaultValue: "${env.BRANCH_NAME ?: 'main'}",name: 'BRANCH_NAME',type: 'PT_BRANCH',description: '请选择要发布的分支')//git插件 标签参数gitParameter(branchFilter: '.*',defaultValue: "${env.TAG_NAME ?: 'v:1.0.0'}",name: 'TAG_NAME',type: 'PT_TAG',description: '请选择要发布的标签')}stages {stage("基本信息输出"){steps{echo '选定待发布信息'echo "项目地址 ${GIT_REPO_URL}"echo "项目组 ${GIT_GROUP}"echo "项目名 ${GIT_NAME}"echo "分支 ${BRANCH_NAME}"echo "TAG ${TAG_NAME}"}}stage('拉取gitlab代码') {steps {//拉取gitlab代码,选择分支checkout scmGit(branches: [[name: env.BRANCH_NAME]],extensions: [],userRemoteConfigs: [[credentialsId: env.GIT_CREDENTIAL_ID,url: "http://${env.GIT_REPO_URL}/${env.GIT_GROUP}/${env.GIT_NAME}.git"]])echo '拉取gitlab代码 --SUCCESS'}}stage('Maven构建项目') {steps {//maven构建项目打包//maven命令位置 /var/jenkins_home/apache-maven-3.9.3sh "/var/jenkins_home/apache-maven-3.9.3/bin/mvn clean package -DskipTests"echo 'Maven构建项目 --SUCCESS'}}stage('通过docker制作镜像') {steps {//docker制作镜像//将maven打包的jar移动到docker目录下//使用dockerfile进行构建镜像,镜像名称为 项目名:标签sh """mv target/*.jar docker/docker build -t ${env.GIT_NAME}:${env.TAG_NAME} docker/"""echo '通过docker制作镜像 --SUCCESS'}}stage('推送镜像到harbor') {steps {//使用harbor凭证推送镜像withCredentials([usernamePassword(credentialsId: env.HARBOR_ID,passwordVariable: 'DOCKER_PASSWORD',usernameVariable: 'DOCKER_USERNAME')]) {//打标签为远程仓库标签//登陆到harbor//推送镜像sh """docker tag ${env.GIT_NAME}:${env.TAG_NAME} ${env.HARBOR_URL}/${env.HARBOR_REPO}/${env.GIT_NAME}:${env.TAG_NAME}echo "\$DOCKER_PASSWORD" | docker login -u "\$DOCKER_USERNAME" -p "\$DOCKER_PASSWORD" ${env.HARBOR_URL}docker push ${env.HARBOR_URL}/${env.HARBOR_REPO}/${env.GIT_NAME}:${env.TAG_NAME}"""}echo '推送镜像到harbor --SUCCESS'}}stage('发送k8s部署yml文件至目标服务器') {steps {//使用ssh插件 发送deploy目录下的部署yml文件到目标服务器//须提前配置ssh免密登陆sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s',transfers: [sshTransfer(cleanRemote: false,excludes: '',execCommand: '',execTimeout: 120000,flatten: false,makeEmptyDirs: false,noDefaultExcludes: false,patternSeparator: '[, ]+',remoteDirectory: '',remoteDirectorySDF: false,removePrefix: '',sourceFiles: "${env.GITLAB_DEPLOYMENT_FILE}/*yml")],usePromotionTimestamp: false,useWorkspaceInPromotion: false,verbose: false)])echo '发送yml文件至目标服务器 --SUCCESS'}}stage('远程执行k8s部署yaml命令') {steps {//替换发送过来的部署文件//部署sh """ssh root@10.199.99.200 sed -i'' "s#REGISTRY#${env.HARBOR_URL}#" ${env.K8S_FILE_PATH}/${env.GITLAB_DEPLOYMENT_FILE}/deployment.ymlssh root@10.199.99.200 sed -i'' "s#DOCKERHUB_NAMESPACE#${env.HARBOR_REPO}#" ${env.K8S_FILE_PATH}/${env.GITLAB_DEPLOYMENT_FILE}/deployment.ymlssh root@10.199.99.200 sed -i'' "s#APP_NAME#${env.GIT_NAME}#" ${env.K8S_FILE_PATH}/${env.GITLAB_DEPLOYMENT_FILE}/deployment.ymlssh root@10.199.99.200 sed -i'' "s#BUILD_NUMBER#${env.TAG_NAME}#" /${env.K8S_FILE_PATH}/${env.GITLAB_DEPLOYMENT_FILE}/deployment.ymlssh root@10.199.99.200 kubectl apply -f ${env.K8S_FILE_PATH}/${env.GITLAB_DEPLOYMENT_FILE}/"""echo '远程执行k8s部署yaml命令 --SUCCESS'}}}
}
deployment.yml
说明:
imagePullSecrets:
- name: harbor-secret
因为将镜像上传到私人镜像仓库,所以k8s中拉取镜像时从harbor中拉取,需要进行配置harbor的secret
---
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: mytestcomponent: devopstier: backendname: mytest
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: mytestcomponent: devopstier: backendstrategy:rollingUpdate:maxSurge: 100%maxUnavailable: 100%type: RollingUpdatetemplate:metadata:labels:app: mytestcomponent: devopstier: backendspec:imagePullSecrets:- name: harbor-secretcontainers:- name: mytestimage: REGISTRY/DOCKERHUB_NAMESPACE/APP_NAME:BUILD_NUMBERreadinessProbe:httpGet:path: /testport: 8080timeoutSeconds: 10failureThreshold: 30periodSeconds: 5imagePullPolicy: Alwaysports:- containerPort: 8080protocol: TCPresources:limits:cpu: 300mmemory: 600Mirequests:cpu: 100mmemory: 100MiterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilednsPolicy: ClusterFirstrestartPolicy: AlwaysterminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:labels:app: mytestcomponent: devopsname: mytest
spec:ports:- name: httpport: 8080protocol: TCPtargetPort: 8080selector:app: mytestcomponent: devopstier: backendsessionAffinity: Nonetype: NodePort
dockerfile
用于构建docker镜像
FROM eclipse-temurin:8-jre
LABEL org.opencontainers.image.authors="fooleryang@139.com"
COPY mytest.jar /usr/local/
WORKDIR /usr/local
CMD java -jar mytest.jar
发布
修改代码后提交,版本修改为v6.0.0
在gitlab中新加标签v6.0.0
在jenkins中构建,选择v6.0.0
查看
总结
目前使用了jenkins的pipeline项目,发布项目到k8s中