环境准备
系统环境:centos7
k8s集群:master 和 slave
gitlab
jenkins
制作maven3.6-jdk-11镜像
Dockfile配置
FROM maven:3.6-jdk-11 COPY settings.xml /usr/share/maven/conf/
COPY settings.xml /usr/share/maven/conf/:替换容器中的settings.xml文件,使用更改ali源的settings.xml文件
maven的settings文件配置
配置阿里镜像仓库源
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- 本地仓库的位置 --> <localRepository>/root/.m2/repository</localRepository> <!-- Apache Maven 配置 --> <pluginGroups/> <proxies/> <!-- 私服发布的用户名密码 --> <!-- <servers> <server> <id>releases</id> <username>deployment</username> <password>He2019</password> </server> <server> <id>snapshots</id> <username>deployment</username> <password>He2019</password> </server> </servers> --> <!-- 阿里云镜像 --> <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <!-- https://maven.aliyun.com/repository/public/ --> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <!-- 配置 java8, 先从阿里云下载, 没有再去私服下载 --> <!-- 20190929 hepengju 测试结果 影响下载顺序的是profiles标签的配置顺序(后面配置的ali仓库先下载), 而不是activeProfiles的顺序 --> <profiles> <!-- 全局JDK11配置 --> <profile> <id>jdk11</id> <activation> <activeByDefault>true</activeByDefault> <jdk>11</jdk> </activation> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion> </properties> </profile> <!-- Nexus私服配置 第三方jar包下载, 比如oracle的jdbc驱动等 --> <!-- <profile> <id>dev</id> <repositories> <repository> <id>nexus</id> <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>Public Repositories</name> <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url> </pluginRepository> </pluginRepositories> </profile> --> <!-- 阿里云配置 提高国内的jar包下载速度 --> <profile> <id>ali</id> <repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </pluginRepository> </pluginRepositories> </profile> </profiles> <!-- 激活配置 --> <activeProfiles> <activeProfile>jdk11</activeProfile> <!-- <activeProfile>dev</activeProfile> --> <activeProfile>ali</activeProfile> </activeProfiles> </settings>
构建新的镜像
╭─root@k201 ~/mk-maven ╰─# ls Dockerfile settings.xml ╭─root@k201 ~/mk-maven ╰─# docker build -t yfy/maven:3.6-jdk-11 .
创建spring-boot微服务项目
创建spring-boot微服务,并由gitlab进行托管
修改pom.xml文件中的spring-boot-starter-parent 版本以及 jdk 的版本
jenkins创建流水线
脚本内容
pipeline agent kubernetes //yamlFile 'KubernetesPod.yaml' cloud 'kubernetes' yaml '''---apiVersion v1 kind Pod metadata namespace devops spec nodeSelector kubernetes.io/os linux restartPolicy Never serviceAccountName jenkins containersname"docker" image"docker:latest" imagePullPolicy"IfNotPresent" resources limits requests command cat ttytrue volumeMountsmountPath /var/run/docker.sock name docker-sock readOnlyfalsename"python3" image"python:3.8.6" imagePullPolicy"IfNotPresent" resources limits requests command cat ttytrue volumeMountsmountPath /var/run/docker.sock name docker-sock readOnlyfalsename"maven36" image"yfy/maven:3.6-jdk-11" imagePullPolicy"IfNotPresent" resources limits requests command cat ttytrue volumeMountsmountPath /root/.m2/repository name maven-repo readOnlyfalse volumeshostPath path /var/run/docker.sock name docker-sock hostPath path /data/devops/jenkins/.m/repository name maven-repo ''' stages stage('checkout') steps checkout([$class'GitSCM' branches name'*/main' extensions userRemoteConfigs credentialsId'gitlab-author' url'http://192.168.110.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git' ) stage('maven3.6') steps container('maven36') sh "mvn -v" sh "mvn clean package -DskipTests=true" // stage('build-allure-results') // steps // container('python3') // sh """ // python --version // pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple // python main.py $ allure_results_path // """ // // // // stage('build-allure-export') // steps // // 通过jenkins的全局工具配置,下载allure工具 // allure includeProperties false jdk'' results path'${allure_results_path}' // // // stage('docker build') // steps // container('docker') // sh """ // docker version // """ // // container('python3') // print('user mutil container...') // // //
在配置文件配置,构建的yfy/maven:3.6-jdk-11的镜像
并设置maven仓库宿主机的存储目录
注意
volumeMountsmountPath /root/.m2/repository name maven-repo readOnlyfalse
mountPath: /root/.m2/repository: 容器中仓库的的地址,此目录可以在settings.xml
文件中进行配置
如图所示
设置maven仓库在宿主机中的目录
/data/devops/jenkins/.m/repository: 这个目录为jenkins在宿主机上的目录,
建议把maven的仓库目录映射到jenkins目录中,方便jenkins迁移,同时把maven仓库的进行统一迁移。
流水线构建
Started by user jenkins Pipeline Start of Pipeline Pipeline podTemplate Pipeline Pipeline node Created Pod kubernetes devops/maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4 Agent maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4 is provisioned from template maven-k8s-spring-boot-demo_11-8pnnw-fcdzv ---apiVersion"v1"kind"Pod"metadata annotations buildUrl"http://192.168.10.201:30080/job/maven-k8s-spring-boot-demo/11/" runUrl"job/maven-k8s-spring-boot-demo/11/" labels jenkins"slave" jenkins/label-digest"a898832bd9447ff86f51e703caa04979f02f781b" jenkins/label"maven-k8s-spring-boot-demo_11-8pnnw" name"maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4" namespace"devops"spec containerscommand"cat" image"docker:latest" imagePullPolicy"IfNotPresent" name"docker" resources limits requests ttytrue volumeMountsmountPath"/var/run/docker.sock" name"docker-sock" readOnlyfalsemountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalsecommand"cat" image"python:3.8.6" imagePullPolicy"IfNotPresent" name"python3" resources limits requests ttytrue volumeMountsmountPath"/var/run/docker.sock" name"docker-sock" readOnlyfalsemountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalsecommand"cat" image"yfy/maven:3.6-jdk-11" imagePullPolicy"IfNotPresent" name"maven36" resources limits requests ttytrue volumeMountsmountPath"/root/.m2/repository" name"maven-repo" readOnlyfalsemountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalseenvname"JENKINS_SECRET" value"********"name"JENKINS_AGENT_NAME" value"maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4"name"JENKINS_NAME" value"maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4"name"JENKINS_AGENT_WORKDIR" value"/home/jenkins/agent"name"JENKINS_URL" value"http://192.168.10.201:30080/" image"jenkins/inbound-agent:4.11-1-jdk11" name"jnlp" resources limits requests memory"256Mi" cpu"100m" volumeMountsmountPath"/home/jenkins/agent" name"workspace-volume" readOnlyfalse nodeSelector kubernetes.io/os"linux" restartPolicy"Never" serviceAccountName"jenkins" volumeshostPath path"/var/run/docker.sock" name"docker-sock"hostPath path"/data/devops/jenkins/.m/repository" name"maven-repo"emptyDir medium"" name"workspace-volume"Running on maven-k8s-spring-boot-demo-11-8pnnw-fcdzv-mkxn4 in /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo Pipeline Pipeline stage Pipeline (checkout) Pipeline checkout The recommended git tool is NONE using credential gitlab-author Cloning the remote Git repository Cloning repository http://192.168.10.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git > git init /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo # timeout=10Fetching upstream changes from http://192.168.10.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git > git --version # timeout=10 > git --version # 'git version 2.30.2'using GIT_ASKPASS to set credentials gitlab-author > git fetch --tags --force --progress -- http://192.168.10.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git config remote.origin.url http://192.168.10.200:8085/yfy-dev/maven-k8s-spring-boot-demo.git # timeout=10 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10Avoid second fetch Checking out Revision 8ee4fae71eb7c9bec5b590ffa5c314301623c2d6 (refs/remotes/origin/main) > git rev-parse refs/remotes/origin/main^# timeout=10 commit > git config core.sparsecheckout # timeout=10 > git checkout -f 8ee4fae71eb7c9bec5b590ffa5c314301623c2d6 # timeout=10Commit message"Update pom.xml"First time build. Skipping changelog. Pipeline Pipeline // stage Pipeline stage Pipeline (maven3.6) Pipeline container Pipeline Pipeline sh + mvn -v Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home /usr/share/maven Java version11.0.10 vendor Oracle Corporation runtime /usr/local/openjdk-11 Default locale en platform encoding UTF-8 OS name"linux" version"3.10.0-1160.71.1.el7.x86_64" arch"amd64" family"unix" Pipeline sh + mvn clean package -DskipTests=true INFO Scanning for projects... INFO INFO --------------------------< com.example:demo >-------------------------- INFO Building demo 0.0.1-SNAPSHOT INFO -------------------------------- jar --------------------------------- INFO INFO --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo --- INFO INFO --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo --- 'UTF-8' encoding to copy filtered resources. INFO Using 'UTF-8' encoding to copy filtered properties files. INFO Using INFO Copying 1 resource INFO Copying 0 resource INFO INFO --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo --- INFO Changes detected - recompiling the module! INFO Compiling 1 source file to /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo/target/classes INFO INFO --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ demo --- 'UTF-8' encoding to copy filtered resources. INFO Using 'UTF-8' encoding to copy filtered properties files. INFO Using INFO skip non existing resourceDirectory /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo/src/test/resources INFO INFO --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo --- INFO Changes detected - recompiling the module! INFO Compiling 1 source file to /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo/target/test-classes INFO INFO --- maven-surefire-plugin:2.22.2:test (default-test) @ demo --- INFO Tests are skipped. INFO INFO --- maven-jar-plugin:3.2.0:jar (default-jar) @ demo --- INFO] Building jar /home/jenkins/agent/workspace/maven-k8s-spring-boot-demo/target/demo-0.0.1-SNAPSHOT.jar INFO INFO --- spring-boot-maven-plugin:2.4.5:repackage (repackage) @ demo --- INFO Replacing main artifact with repackaged archive INFO ------------------------------------------------------------------------ INFO BUILD SUCCESS INFO ------------------------------------------------------------------------ INFO] Total time 5.688 s INFO] Finished at 2022-12-17T09 54 34Z INFO ------------------------------------------------------------------------ Pipeline Pipeline // container Pipeline Pipeline // stage Pipeline Pipeline // node Pipeline Pipeline // podTemplate Pipeline End of Pipeline Finished SUCCESS