开发离不开项目的部署,运维,因此为了以后的开发便利,我最近学习了下服务的自运维,现在总结一下
参考:https://my.oschina.net/yimingkeji/blog/2878371
技术
Jenkins+docker+git+Gitee 部署SpringBoot项目
环境
- Win10
- 虚拟机:VMware
- 系统:centos7
操作流程
1.安装虚拟机,配置centos系统,联通网络
2.配置Java环境
3.下载,安装,配置jenkins
1)Jenkins的介绍
Jenkins是一个广泛用于持续构建的可视化web工具,可用于自动化与构建、测试、交付或部署软件相关的各种任务。
2)看下Jenkins的工作流程
- 我们在本地开发项目。记得加上dcoker的依赖和Dockerfile的配置(后面说)。然后通过本地git提交到码云(Github)。
- 这时码云上的项目中配置钩子程序(Webhooks)。当我们在本地push的时候触发钩子。
这个URL的地址要触发Jenkis(后面介绍)
- Jenkins接收到通知以后,会调用虚拟机上的git。将远程码云仓库的项目pull到服务器上
- 接下来的工作中心就在Jenkins上。根据配置(后面详细说),将项目用服务器上mvn打成jar包
- 利用docker结合项目的dockerfile,打包成docker镜像,然后启动即可。(具体配置看后面)
3)Jenkins的下载和安装
看参考即可
不同的是我没有安装到tomcat下面,随tomcat启动,而是直接安装linux中,然后设置启动
这可以百度下
4.配置docker
- 首先是插件的安装,可以看参考,会遇到无法连接到外网服务器的问题,百度解决即可
- 进去后,首先全局配置,-》看参考 配置maven、JDK、Docker、git
这些都需要安装到服务器上,git还需生成私钥,公钥。公钥放到码云远程仓库,私钥放到Jenkins上,使得通过Jenkin利用git将代码拉到服务器上。
- 安装docker
- Springboot项目
项目地址:https://gitee.com/yimingkeji/docker-springboot
docker依赖
com.spotify
docker-maven-plugin
0.4.13
${project.artifactId}
${project.basedir}/src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar
Dockerfile内容:
FROM hub.c.163.com/dwyane/openjdk:8
VOLUME /tmp
ADD docker-test-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8081
dockerfile的jar包的名字要和项目的名字保持一致
com.wuhen
docker-test
0.0.1-SNAPSHOT
然后上传到自己的码云远程仓库
5.使用Jenkins部署项目到docker
看参考
其中
第一次执行shell 添加shell:
#!/bin/bash -il
mvn docker:build
echo "当前docker 镜像:"
docker images | grep docker-test
echo "启动容器----->"
docker run --name dockertest -p 8081:8081 -d docker-test
echo "启动服务成功!"
项目构建成功后再次构建shell有所改变
#!/bin/bash -il
# 先删除之前的容器
echo "remobe old container"
docker ps -a | grep dockertest | awk '{print $1}'| xargs docker rm -f
# 删除之前的镜像
echo "romove old image"
docker rmi docker-test
# 构建镜像
mvn docker:build
# 打印当前镜像
echo "current docker images"
docker images | grep docker-test
# 启动容器
echo "start container"
docker run --name dockertest -p 8081:8081 -d docker-test
# 打印当前容器
echo "current container"
docker ps -a | grep dockertest
echo "star service success!"
这个容器端口映射:项目的启动端口好为8081,映射到外部服务器端口为8081(可以固定。也可以随机),所以在外部浏览器访问IP:8081,即映射到项目上,添加路径即可访问
控制台输出
Started by user unknown or anonymous
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/docker-test
> /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> /usr/bin/git config remote.origin.url [email protected]:zhege/docker-test.git # timeout=10
Fetching upstream changes from [email protected]:zhege/docker-test.git
> /usr/bin/git --version # timeout=10
using GIT_SSH to set credentials gitee
> /usr/bin/git fetch --tags --progress [email protected]:zhege/docker-test.git +refs/heads/*:refs/remotes/origin/*
> /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> /usr/bin/git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 8b9c5e5cdda5cf060be846f2c53b9662cd8d1c7f (refs/remotes/origin/master)
> /usr/bin/git config core.sparsecheckout # timeout=10
> /usr/bin/git checkout -f 8b9c5e5cdda5cf060be846f2c53b9662cd8d1c7f
Commit message: "测试0.8"
> /usr/bin/git rev-list --no-walk 8b9c5e5cdda5cf060be846f2c53b9662cd8d1c7f # timeout=10
[docker-test] $ /usr/local/maven/apache-maven/bin/mvn -s /usr/local/maven/apache-maven/conf/settings.xml -gs /usr/local/maven/apache-maven/conf/settings.xml clean install -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.wuhen:docker-test >------------------------
[INFO] Building docker-test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ docker-test ---
[INFO] Deleting /var/lib/jenkins/workspace/docker-test/target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ docker-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ docker-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /var/lib/jenkins/workspace/docker-test/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ docker-test ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ docker-test ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ docker-test ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ docker-test ---
[INFO] Building jar: /var/lib/jenkins/workspace/docker-test/target/docker-test-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.1.RELEASE:repackage (repackage) @ docker-test ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ docker-test ---
[INFO] Installing /var/lib/jenkins/workspace/docker-test/target/docker-test-0.0.1-SNAPSHOT.jar to /root/.m2/repository/com/wuhen/docker-test/0.0.1-SNAPSHOT/docker-test-0.0.1-SNAPSHOT.jar
[INFO] Installing /var/lib/jenkins/workspace/docker-test/pom.xml to /root/.m2/repository/com/wuhen/docker-test/0.0.1-SNAPSHOT/docker-test-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.245 s
[INFO] Finished at: 2019-11-19T15:24:43+08:00
[INFO] ------------------------------------------------------------------------
[docker-test] $ /bin/bash -il /tmp/jenkins6420905348235517809.sh
bash: no job control in this shell
remobe old container
8ecc1c0ad528
romove old image
Untagged: docker-test:latest
Deleted: sha256:e21b5d37eec2178a134b2b11ff2c520c9e4195da30514fdf3a9c908c7d1923bc
Deleted: sha256:f6687ce9698053b270d31032bba9953d1df257038b356827161a7ef6b3a0d6fa
Deleted: sha256:1770755bad3191affc457705afe435cd5d21a60fadd543a67b4df65de66a5cf7
Deleted: sha256:0a45f6912e8188a2ef73537590483f7284f12856aab29263bb03f9a41f5d86e8
Deleted: sha256:046c6fab78820fb8d39f3c502a882a71841fdbc23ecb18f39bd712e7e9d556bd
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.wuhen:docker-test >------------------------
[INFO] Building docker-test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:build (default-cli) @ docker-test ---
[INFO] Copying /var/lib/jenkins/workspace/docker-test/target/docker-test-0.0.1-SNAPSHOT.jar -> /var/lib/jenkins/workspace/docker-test/target/docker/docker-test-0.0.1-SNAPSHOT.jar
[INFO] Copying /var/lib/jenkins/workspace/docker-test/src/main/docker/Dockerfile -> /var/lib/jenkins/workspace/docker-test/target/docker/Dockerfile
[INFO] Building image docker-test
Step 1/5 : FROM hub.c.163.com/dwyane/openjdk:8
---> 96cddf5ae9f1
Step 2/5 : VOLUME /tmp
---> Running in 486c1231762b
Removing intermediate container 486c1231762b
---> 13ba15843faa
Step 3/5 : ADD docker-test-0.0.1-SNAPSHOT.jar app.jar
---> 728f47311940
Step 4/5 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
---> Running in 5e60e14f90b9
Removing intermediate container 5e60e14f90b9
---> fb9135e0b8d3
Step 5/5 : EXPOSE 8081
---> Running in b37736b77608
Removing intermediate container b37736b77608
---> b245fecbacc9
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built b245fecbacc9
Successfully tagged docker-test:latest
[INFO] Built docker-test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.657 s
[INFO] Finished at: 2019-11-19T15:24:50+08:00
[INFO] ------------------------------------------------------------------------
current docker images
docker-test latest b245fecbacc9 Less than a second ago 658MB
start container
6a5195061803f56761fbb4289c6a6035fd3f91afd1057d9c8051cb69c7ef4a75
current container
6a5195061803 docker-test "java -Djava.securit…" 1 second ago Up Less than a second 0.0.0.0:8081->8081/tcp dockertest
star service success!
Finished: SUCCESS
可以结合参考和我的文章选择自己合适的配置路线,与其说是写总结,也是结合自己弟参考文章的补充