「Jenkins / Agent」- 节点管理、添加节点、删除节点、初始化节点

  CREATED BY JENKINSBOT

第一步、添加节点

当「Master 在公网,Agent 在内网」时,Master 无法连接 Agent,我们通过 Agent 主动注册的方式。

节点被动连接:通过 SSH 连接

Master 通过 SSH 主动连接 Agent

节点主动注册:通过 Docker Compose 方式

What’s the docker-compose equivalent of docker run –init? – Stack Overflow
Youtube/9. Connect Master with Slave in Jenkins
jenkins/inbound-agent – Docker Image | Docker Hub

通过 Docker Compose 的原因:我们不想写 systemd Service Unit 文件;需要对 Agent 进行隔离(次要原因);

docker-compose.yaml:

version: '3'

services:
  jenkins-node:
    image: jenkins/inbound-agent
    # init: /usr/libexec/docker-init # Version 2
    init: true
    restart: always
    volumes:
    - ./data/:/data/jenkins-node/
    - ./sbin/:/usr/local/sbin/      # 部分命名需要安装
    command:
    - -url 
    - <the Jenkins root URL>
    - -workDir 
    - /data/jenkins-node/data
    - <Secret> 
    - <NodeName>

第二步、节点初始化(节点环境设置)

Jenkins – Run job when new slave connects – Stack Overflow

针对节点初始化,其本质工作还是安装各种命令及工具,诸如 kubectl, helm, make 等等。

Node and Label parameter

Node and Label parameter | Jenkins plugin
3.3 Run Single Job Parallely in Multiple slaves – Jenkins

通过该插件,Job 将在多个节点上执行,并在节点中完成命令安装。

Startup Trigger

Startup Trigger | Jenkins plugin

Slave SetupPlugin

Slave SetupPlugin | Jenkins plugin

Custom Tools(我们的最终方案)

Custom Tools | Jenkins plugin

针对节点的初始化,其主要工作依旧为安装各种命令,所以我们放弃节点初始化,转而使用 Custom Tools 来让 Job 自己选择要使用的构建工具。

一来,解决工具版本冲突的问题。二来,增加作业的可移植性(与节点所安装工具无关)。

常见问题处理

jenkins Received disconnect from … Closed due to user request.

[JENKINS-54746] Can’t connect via SSH on 1.29.1 – Jenkins Jira

问题描述:通过 SSH 方式,来为 Jenkins 添加 Agent 节点,但提示 jenkins Received disconnect from … Closed due to user request. 错误。

原因分析:SSH Credentials Plugin no longer supports SSH credentials from files on the Jenkins master file system, neither user-specified file paths nor ~/.ssh. Existing SSH credentials of these kinds are migrated to “directly entered” SSH credentials.

解决方案:找对对应的 Credentials 并创新上传 SSH Private Key(此时仅能填写文本,不再能上传文件)。

参考文献

Supervisor and systemd config for jenkins slave
Youtube/9. Connect Master with Slave in Jenkins
Compose file version 3 reference | Docker Documentation