jenkins通过ssh方式连接执行机

连接过程

  • master通过ssh协议访问slave的sshd服务,先建立ssh连接
  • 检查slave中的java程序及版本
  • 拷贝最新的remoting.jar到slave中,如果slave中已经存在remoting.jar,则校验master与slave中jar的md5值是否一致,如果不一致则更新。
  • 启动命令建立master与slave通信的专用通道,后续master向slave下发命令都是通过该通道进行。

slave中的连接日志保存在 ${slave_workspace}/remoting/logs 目录下,每次master与slave的连接都会生成一个 remoting.log.x 的日志文件
master中的连接日志保存在 ${master_jenkins_home}/logs/slaves/${slave_name}/slave.log.x

master上的连接日志如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
SSHLauncher{host='192.168.43.150', port=22, credentialsId='test', jvmOptions='', javaPath='', prefixStartSlaveCmd='', suffixStartSlaveCmd='', launchTimeoutSeconds=210, maxNumRetries=10, retryWaitTime=15, sshHostKeyVerificationStrategy=hudson.plugins.sshslaves.verifiers.NonVerifyingKeyVerificationStrategy, tcpNoDelay=true, trackCredentials=true}
[11/23/19 17:28:18] [SSH] Opening SSH connection to 192.168.43.150:22.
[11/23/19 17:28:18] [SSH] WARNING: SSH Host Keys are not being verified. Man-in-the-middle attacks may be possible against this connection.
[11/23/19 17:28:24] [SSH] 认证成功。
[11/23/19 17:28:24] [SSH] The remote user's environment is:
BASH=/usr/bin/bash
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_EXECUTION_STRING=set
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
DIRSTACK=()
EUID=0
GROUPS=()
HOME=/root
HOSTNAME=localhost.localdomain
HOSTTYPE=x86_64
IFS=$' \t\n'
LANG=zh_CN.UTF-8
LESSOPEN='||/usr/bin/lesspipe.sh %s'
LOGNAME=root
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/mail/root
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
PIPESTATUS=([0]="0")
PPID=18696
PS4='+ '
PWD=/root
SELINUX_LEVEL_REQUESTED=
SELINUX_ROLE_REQUESTED=
SELINUX_USE_CURRENT_RANGE=
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=1
SSH_CLIENT='192.168.43.150 53796 22'
SSH_CONNECTION='192.168.43.150 53796 192.168.43.150 22'
TERM=dumb
UID=0
USER=root
XDG_RUNTIME_DIR=/run/user/0
XDG_SESSION_ID=21
_=/etc/bashrc
[11/23/19 17:28:24] [SSH] Checking java version of /data/workspace/jdk/bin/java
Couldn't figure out the Java version of /data/workspace/jdk/bin/java
bash: /data/workspace/jdk/bin/java: 没有那个文件或目录

[11/23/19 17:28:24] [SSH] Checking java version of java
[11/23/19 17:28:24] [SSH] java -version returned 11.0.3.
[11/23/19 17:28:24] [SSH] Starting sftp client.
[11/23/19 17:28:24] [SSH] 正在拷贝最新版本的 remoting.jar...
[11/23/19 17:28:24] [SSH] Copied 875,406 bytes.
Expanded the channel window size to 4MB
[11/23/19 17:28:24] [SSH] Starting agent process: cd "/data/workspace" && java -jar remoting.jar -workDir /data/workspace -jar-cache /data/workspace/remoting/jarCache
11月 23, 2019 5:28:26 下午 org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
信息: Using /data/workspace/remoting as a remoting work directory
11月 23, 2019 5:28:26 下午 org.jenkinsci.remoting.engine.WorkDirManager setupLogging
信息: Both error and output logs will be printed to /data/workspace/remoting
<===[JENKINS REMOTING CAPACITY]===>channel started
Remoting version: 3.35
This is a Unix agent
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by jenkins.slaves.StandardOutputSwapper$ChannelSwapper to constructor java.io.FileDescriptor(int)
WARNING: Please consider reporting this to the maintainers of jenkins.slaves.StandardOutputSwapper$ChannelSwapper
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Evacuated stdout
Agent successfully connected and online

重连方法

修改了slave的某些配置(如修改了tmpdir)后需要重新建立slave与master的连接,通过master上的Mark this node temporarily offline按钮时实现不了重连功能的。

  • 通过 Disconnect 按钮进行断链,然后重新 Launch agent
  • 重启执行机,执行机重启后,master会自动重连
  • 执行Delete Agent删掉该执行机后重新添加该执行机
  • 到执行机上杀掉对应进程,然后再master中再Launch agent

    1
    2
    3
    4
    5
    6
    7
    [root@localhost ~]# ps -ef |grep remoting.jar
    root 8015 7860 0 17:16 ? 00:00:00 bash -c cd "/data/workspace" && java -jar remoting.jar -workDir /data/workspace -jar-cache /data/workspace/remoting/jarCache
    root 8020 8015 5 17:16 ? 00:00:08 java -jar remoting.jar -workDir /data/workspace -jar-cache /data/workspace/remoting/jarCache
    root 10188 5056 0 17:19 pts/0 00:00:00 grep --color=auto remoting.jar
    [root@localhost ~]# kill -9 8015
    [root@localhost ~]# ps -ef |grep remoting.jar
    root 10552 5056 0 17:19 pts/0 00:00:00 grep --color=auto remoting.jar