前面实验了使用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】–【管理插件】–【可选插件】然后安装此插件
3. 配置Docker插件
点击系统管理----->系统设置
添加配置
然后点击保存
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的镜像
[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@node6 ~]# docker run -i -t centos /bin/bash
[root@940907eec8ef /]#
5. 安装所需的应用
添加用户
[root@940907eec8ef /]# useradd jenkins [root@940907eec8ef /]# yum -y install passwd [root@940907eec8ef /]# echo 'p@ssw0rd'|passwd --stdin jenkins
[root@940907eec8ef /]# yum update
[root@940907eec8ef /]# yum -y install epel-release
[root@940907eec8ef /]# yum -y install git
[root@940907eec8ef /]# yum -y install java-11-openjdk.x86_64
[root@940907eec8ef /]# yum -y install maven
[root@940907eec8ef /]# exit
[root@node6 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 940907eec8ef centos "/bin/bash" 11 minutes ago Exited (0) 6 seconds ago naughty_shockley 2c3bfd6ee8a2 ubuntu "/bin/bash" 12 minutes ago Exited (1) 11 minutes ago nice_mccarthy 8158ddd397d6 ubuntu "/bin/bash" 31 minutes ago Exited (100) 13 minutes ago determined_sanderson b119e7f28b50 jenkins/jenkins:lts "/sbin/tini -- /usr/…" 2 hours ago Exited (143) 59 minutes ago jenkins_prod
6. 保存镜像
[root@node6 ~]# docker commit 940907eec8ef maven-build-slave-0.2
sha256:248e3759ed26b796cc3a2e7bd00c2b2be4b30f4f294c7f14bcbbc96aa3b9f012
[root@node6 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE maven-build-slave-0.2 latest 248e3759ed26 53 seconds ago 943MB ubuntu latest 775349758637 3 days ago 64.2MB jenkins/jenkins lts fac78e370c0b 6 days ago 568MB centos latest 0f3e07c0138f 4 weeks ago 220MB
7. jenkins配置docker凭据
系统管理----->系统设置
创建凭据
配置模板
保存
暂时配置完成