docker+jenkins+git持续部署实践

来源:互联网 发布:c语言的main函数是什么 编辑:程序博客网 时间:2024/05/22 06:27

以前搭建spring-cloud的时候 看到别人代码里面总有Dockerfile,然后了解到Docker容器的概念,玩了两个星期玩不下去了,就放下了,无意间在网上看到一篇文章Docker持续部署,就想着自己也搭建一套玩玩。

流程

网上那篇文章介绍的流程涉及到多个docker镜像,我这里为了入门就简单化了。

这里写图片描述

自动化部署效果就是: 你修改了本地的代码 然后push到git上面,git的webhook功能会给jenkins post一个请求,jenkins接收到请求后,就进行打包部署操作,细节下面会说。

docker部署jenkins

docker 怎么安装使用可以参考以前博客。 
docker启动:

<code class="hljs nginx has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">sudo</span> route del -net <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">172.16.0.0</span> netmask <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255.240.0.0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs bash has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> service docker start</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

镜像地址:https://hub.docker.com/jenkins/ 
jenkins docker官网有提供image的 直接pull下来就可以了。 
jenkisn安装步骤: 
拉取镜像

<code class="hljs css has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">docker</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">pull</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">jenkins</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:latest</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

这种方式启动会存储数据,-t 标示以后台模式运行,-p标示容器和宿主服务器之间的开放端口号 -v表示需要将本地哪个目录挂载到容器中,格式:-v <宿主机目录>:<容器目录> 
运行容器

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">docker run <span class="hljs-attribute" style="box-sizing: border-box;">-t</span>  <span class="hljs-attribute" style="box-sizing: border-box;">-p</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8080</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8080</span> <span class="hljs-attribute" style="box-sizing: border-box;">-p</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8083</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8083</span> <span class="hljs-attribute" style="box-sizing: border-box;">-p</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50000</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50000</span>  <span class="hljs-attribute" style="box-sizing: border-box;">-v</span> /<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">var</span>/jenkins_home:/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">var</span>/jenkins_home jenkins</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

运行成功之后,我们输入http://ip:8080/ 就可以访问到了

jenkins的注意事项

  1. 权限问题,具体细节可以百度,我们可以先开发用户注册功能,然后注册一个用户,然后给注册的用户开放所有的权限,匿名的用户权限勾掉。需要注意一点的就是 git的webhok功能也需要权限,所以我们配置安全矩阵的时候要开发匿名用户的访问job的权限,不然git的webhok功能无法使用 
    这里写图片描述

  2. 系统配置 
    在全局属性里面新增:BUILD_ID=DONTKILLME 这个键值对非常重要,如果不配置 无论你怎么运行Java -jar  *.jar & 项目都无法后台运行,因为一旦jenkins 部署成功执行完shell 就会把其他的进程都干掉。 
    maven jdk都要配置下

  3. 插件安装 
    git/Publish over SSH 都要按照然后配置
  4. 项目配置

    这里写图片描述 
    这里写图片描述

  5. shell 主要实现kill 原有的java进程和运行新的jar
<code class="hljs perl has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">projectName=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"dockerjenkins.jar"</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#kill</span>ps -ef | <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">grep</span> -v <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'grep'</span> | <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">grep</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'dockerjenkins.jar'</span> | awk <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'{print $2}'</span>| xargs <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">kill</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9</span>ps -ef|<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">grep</span> javaecho <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"start <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">${projectName}</span>"</span>java  -jar <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$WORKSPACE</span>/target/<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${projectName}</span>  &</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

git webhoks

git项目地址:https://github.com/ggj2010/dockerjenkins.git 
webhoks配置: 
这个url 和jenkins配置项目的地址一致 可以看前面截图触发远程构建 
这里写图片描述

参考网址:http://www.thinksaas.cn/group/topic/378471/

0 0