当前位置: 代码迷 >> 综合 >> Jenkins Docker agent 动态节点实践
  详细解决方案

Jenkins Docker agent 动态节点实践

热度:60   发布时间:2023-09-30 11:57:27.0

动态节点实践

方式1: 启动容器进行构建(无需连接master)  Docker 动态节点


 docker插件: Docker | Jenkins plugin

项目地址: GitHub - jenkinsci/docker-plugin: Jenkins Cloud Plugin for Docker

Jenkins Docker agent 动态节点实践

Jenkins动态slave的配置,需要Jenkins调用Docker的接口完成的。我们需要开启Docker远程访问

(安装好上面的插件需要给docker开启远程访问,因为需要调用docker的API,所以需要暴露出去,所以要开启远程访问)

CentOS

编辑/usr/lib/systemd/system/docker.service

[root@jenkins-agent ~]# vim /usr/lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375

当我们把上面的配置完成后,可以通过Curl命令进行基本的测试API。出现一下信息表示成功开启。

[root@jenkins-agent ~]# systemctl daemon-reload
[root@jenkins-agent ~]# systemctl restart docker
[root@jenkins-agent ~]# curl -XGET http://127.0.0.1:2375/version
{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"20.10.8","Details":{"ApiVersion":"1.41","Arch":"amd64","BuildTime":"2021-07-30T19:54:13.000000000+00:00","Experimental":"false","GitCommit":"75249d8","GoVersion":"go1.16.6","KernelVersion":"3.10.0-693.el7.x86_64","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.4.9","Details":{"GitCommit":"e25210fe30a0a703442421b0f60afac609f950a3"}},{"Name":"runc","Version":"1.0.1","Details":{"GitCommit":"v1.0.1-0-g4144b63"}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":"de40ad0"}}],"Version":"20.10.8","ApiVersion":"1.41","MinAPIVersion":"1.12","GitCommit":"75249d8","GoVersion":"go1.16.6","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-693.el7.x86_64","BuildTime":"2021-07-30T19:54:13.000000000+00:00"}
dockerNode(dockerHost: 'tcp://192.168.100.3:2375', image: 'maven:3.8.1-openjdk-8-slim') {// some block
}

 pipeline里面直接将上面的写到script里面

dockerNode(dockerHost: 'tcp://192.168.100.3:2375', image: 'maven:3.8.1-openjdk-8-slim') {sh "mvn -v"sh "sleep 5"
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] dockerNode (hide)
Launching new docker node based on maven:3.8.1-openjdk-8-slim
Waiting for node to be online ...
Node docker-00000ijb7n1l4 is online.
[Pipeline] {
[Pipeline] sh
+ mvn -v
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /usr/share/maven
Java version: 1.8.0_292, vendor: Oracle Corporation, runtime: /usr/local/openjdk-8/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix"
[Pipeline] sh
+ sleep 5
[Pipeline] }
Terminating docker node ...
Disconnected computer for node 'docker-00000ijb7n1l4'.
Removed Node for node 'docker-00000ijb7n1l4'.
[Pipeline] // dockerNode
[Pipeline] End of Pipeline
Finished: SUCCESS

上面就是一个容器,一站到底,全部的逻辑流程全部一个docker容器实现。 

方式2: 使用CLoud


配置云: 节点管理-> CLoud-> Docker

Docker Cloud details信息填写:

  • Docker Host URI  Docker主机信息(需要开启Docker配置)
    • tcp://192.168.1.200:2375
    • unix:///var/run/docker.sock
  • Connection Timeout  连接超时时间
  • Read Timeout  读操作超时时间  (调大些,容易出现超时的情况)
  • Enabled  是否启用?默认否
  • Error Duration 错误的持续时间 默认300 5分钟
  • Container Cap 容器数量 负值或零,或2147483647都意味着“无限制” ,默认值100。

Jenkins Docker agent 动态节点实践

Docker Agent templates

  • Labels 节点标签
  • Enabled 是否启动 默认否
  • Name 节点名称
  • Docker Image 镜像标签
  • Remote File System Root  远程文件系统根目录
  • 用法 自定义指定项目运行
  • Connect method 连接方式
    • JNLP 推荐
      • User  运行用户
      • Jenkins URL jenkins地址
      • Idle timeout 空闲时间多少秒后杀死slave

    • SSH
    • Docker Container
  • Pull strategy 镜像下载策略
  • Pull timeout 镜像下载超时时间  单位秒
  • Instance Capacity 实例数量

Jenkins Docker agent 动态节点实践

Jenkins Docker agent 动态节点实践 

node("dockercloud"){echo "Hello world!"sh "sleep 20"
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Still waiting to schedule task
All nodes of label ‘dockercloud’ are offline
Running on dockercloud-00001cdwvoxgj on docker in /workspace/docker-agent
[Pipeline] {
[Pipeline] echo
Hello world!
[Pipeline] sh
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

 

Jenkins Docker agent 动态节点实践

  相关解决方案