DEVOPS技术实践_15:使用Docker作为Jenkins的slave

Stella981
• 阅读 528

前面实验了使用docker搭建一个jenkins,下面实验使用docker作为jenkins的slave节点

1. 环境准备

  • 一个运行Docker的主机或者群集
  • Jenkins应该能访问互联网,方便安装插件。
  • Jenkins服务器能够和GitHub通信。
  • Jenkinss对Java所需要的Git,和Maven配置应该配置好。
  • 一个Jenkins master.

2. 在Docker主机上开启远端的访问API

此处针对Docker 18.03

/etc/docker/daemon.json会被docker.service的配置文件覆盖,直接添加daemon.json不起作用。可以有如下几种设置:

直接编辑配置文件:Centos中docker daemon配置文件在/lib/systemd/system/docker.service,找到以下字段,在后面添加如下,注意,此处不能用”fd://”,否则报错

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375    #添加
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

执行

[root@node6 ~]# systemctl daemon-reload

[root@node6 ~]# systemctl restart docker.service

安装Docker插件

点击【管理Jenkins】–【管理插件】–【可选插件】然后安装此插件

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

3. 配置Docker插件

点击系统管理----->系统设置

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

添加配置

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

然后点击保存

4. 创建Docker镜像

需要创建一个Docker的镜像在Docker主机上,方便我们在创建Jenkins slaves时使用。

查看Docker的镜像

[root@node6 ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jenkins/jenkins     lts                 fac78e370c0b        6 days ago          568MB

拉取ubuntu的镜像

由于拉取的Ubuntu安装git有点问题,重新拉去一个ubuntu16.04的镜像

[root@node6 ~]# docker pull dftbcros.mirror.aliyuncs.com/library/ubuntu:16.04

16.04: Pulling from library/ubuntu
e80174c8b43b: Pull complete 
d1072db285cc: Pull complete 
858453671e67: Pull complete 
3d07b1124f98: Pull complete 
Digest: sha256:bb5b48c7750a6a8775c74bcb601f7e5399135d0a06de004d000e05fd25c1a71c
Status: Downloaded newer image for dftbcros.mirror.aliyuncs.com/library/ubuntu:16.04
dftbcros.mirror.aliyuncs.com/library/ubuntu:16.04 

[root@node6 ~]# docker images

REPOSITORY                                    TAG                 IMAGE ID            CREATED             SIZE
dftbcros.mirror.aliyuncs.com/library/ubuntu   16.04               5f2bf26e3524        3 days ago          123MB
jenkins/jenkins                               lts                 fac78e370c0b        6 days ago          568MB
centos                                        latest              0f3e07c0138f        4 weeks ago         220MB

[root@node6 ~]# docker pull centos

Using default tag: latest
latest: Pulling from library/centos
729ec3a6ada3: Pull complete 
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

执行下面的命令运行Docker容器并在容器中执行命令。

root@37e4f2be0a41:/# apt-get update

Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]                                        
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [985 kB]                                   
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]                                               
Get:5 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]                                             
Get:6 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]                                            
Get:7 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.7 kB]                            
Get:8 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [589 kB]                               
Get:9 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [6281 B]                             
Get:10 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]                                     
Get:11 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]                                       
Get:12 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]                                      
Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [1362 kB]                                   
Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.1 kB]                             
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [993 kB]                                
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [19.3 kB]                             
Get:17 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [7942 B]                                  
Get:18 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [8807 B]                              
Fetched 16.1 MB in 6min 48s (39.5 kB/s)                                                                                
Reading package lists... Done

 安装git

root@37e4f2be0a41:/# apt-get install -y git

添加用户

root@:37e4f2be0a41/# adduser  jenkins
Adding user `jenkins' ...
Adding new group `jenkins' (1000) ...
Adding new user `jenkins' (1000) with group `jenkins' ...
Creating home directory `/home/jenkins' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for jenkins
Enter the new value, or press ENTER for the default
    Full Name []: 123
    Room Number []: 12
    Work Phone []: 312
    Home Phone []: 21
    Other []: 21
Is the information correct? [Y/n] Y

安装SSH server

root@37e4f2be0a41:/# apt-get install openssh-serverd

root@37e4f2be0a41:/# mkdir /var/run/sshd

5. 安装所需的应用

root@37e4f2be0a41:/# apt-get install openjdk-8-jdk -y

root@37e4f2be0a41:/# apt-get install maven

root@37e4f2be0a41:/# exit

[root@node6 ~]# docker ps -a

37e4f2be0a41        5f2bf26e3524          "/bin/bash"              About an hour ago   Exited (0) 20 seconds ago                           sweet_hypatia

6. 保存镜像

[root@node6 ~]#  docker commit b119e7f28b50  maven-build-slave-0.2

sha256:3a444723b0557cf81661b3f95947c3deae8c93008476ac3a86cbb8b7979815ce

[root@node6 ~]# docker images

maven-build-slave-0.2                         latest              3a444723b055        About a minute ago   568MB

7. jenkins配置docker凭据

系统管理----->系统设置

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

创建凭据

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

 配置模板

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

 保存

8. jenkins测试

使用这个项目

DEVOPS技术实践_15:使用Docker作为Jenkins的slave

pipeline语法

node('docker') {
    // some block
    stage('Preparation') {
    // some block
    //step
    git 'https://github.com/jglick/simple-maven-project-with-tests.git'
}
    stage('Build') {
    // some block
    //step
    withMaven(maven: 'M3') {
    // some block
    sh label: '', script: 'mvn -Dmaven.test.failuer.ignore clean install package'
}
}
}

构建即可

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这