静态节点就是固定的在一台机器上面,通过一些固定的命令将其启动起来,动态节点就是来了多个任务,可以帮你创建多个agent去跑。而不像静态节点来了多个任务,那么就会等待这个任务队列,然后进行这个构建。这就是静态和动态的区别。
静态节点就是通过java -jar这条命令去启动起来就可以了。
1. 静态节点实践
首先,在Jenkins 中创建一个jnlp类型的agent, 拿到连接所需要的认证和秘钥信息。
jar -jar这条命令后面有很多的参数,不管是配置动态节点还是静态节点,这些参数都会用到。
java -jar agent.jar -jnlpUrl http://192.168.1.200:8080/computer/buildtest01/jenkins-agent.jnlp -secret 047a2627be5ee128c1a93f281051acc7bbbbd5221dcb56ae9fa408a494e72c2f -workDir "/home/jenkins"
1.1 Docker静态节点
启动镜像,传递认证参数。(使用docker的话其实就是使用一个镜像,镜像里面运行了一个agent,然后将这些参数传递进去)
在虚拟机上面下载agent.jar包然后手动执行这些命令就可以了
在命令行中启动节点java -jar agent.jar -jnlpUrl http://139.198.166.235:8080/computer/build%2D02/jenkins-agent.jnlp -secret 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba -workDir "/var/jenkins"
Run from agent command line, with the secret stored in a file:echo 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba > secret-file
java -jar agent.jar -jnlpUrl http://139.198.166.235:8080/computer/build%2D02/jenkins-agent.jnlp -secret @secret-file -workDir "/var/jenkins"
如果是在docker里面就需要找一个agent镜像:inbound-agent镜像 : Docker Hub
## 测试
[root@jenkins-agent ~]# docker run --init jenkins/inbound-agent -url http://139.198.166.235:8080 -workDir=/home/jenkins 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba build-02Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main createEngine
INFO: Setting up agent: build-02
Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener <init>
INFO: Jenkins agent is running in headless mode.
Oct 28, 2021 2:19:25 AM hudson.remoting.Engine startEngine
INFO: Using Remoting version: 4.11
Oct 28, 2021 2:19:25 AM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using /home/jenkins/remoting as a remoting work directory
Oct 28, 2021 2:19:25 AM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to /home/jenkins/remoting
Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Locating server among [http://139.198.166.235:8080/]
Oct 28, 2021 2:19:25 AM org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver resolve
INFO: Remoting server accepts the following protocols: [JNLP4-connect, Ping]
Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Agent discovery successfulAgent address: 139.198.166.235Agent port: 50000Identity: 67:e8:d2:73:d1:cf:01:f6:6c:a4:cb:b3:0f:25:8c:3d
Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Handshaking
Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connecting to 139.198.166.235:50000
Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Trying protocol: JNLP4-connect
Oct 28, 2021 2:19:25 AM org.jenkinsci.remoting.protocol.impl.BIONetworkLayer$Reader run
INFO: Waiting for ProtocolStack to start.
Oct 28, 2021 2:19:26 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Remote identity confirmed: 67:e8:d2:73:d1:cf:01:f6:6c:a4:cb:b3:0f:25:8c:3d
Oct 28, 2021 2:19:26 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connected
## 运行
[root@jenkins-agent ~]# docker run -itd --name build-02 jenkins/inbound-agent -url http://139.198.166.235:8080 -workDir=/home/jenkins 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba build-02
bd2500223dabb45ff41c234a8871651f93b38264458c4d2bed40f821577b8944[root@jenkins-agent ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd2500223dab jenkins/inbound-agent "/usr/local/bin/jenk?? 18 seconds ago Up 18 seconds build-02
这里需要注意,docker的静态agent里面什么都没有,比如构建工具是没有的
[root@jenkins-agent ~]# docker exec -it build-02 bash
jenkins@bd2500223dab:~$ mvn
bash: mvn: command not found
所以解决办法是将agent镜像作为基础镜像,将构建工具打进去,那么你就可以使用了,或者将工具挂载进去。
docker run -itd --name build-02 \
-v /usr/local/apache-maven-3.8.1:/home/jenkins/apache-maven-3.8.1 \
jenkins/inbound-agent -url http://139.198.166.235:8080 -workDir=/home/jenkins 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba build-02jenkins@2a682b01263b:~$ /home/jenkins/apache-maven-3.8.1/bin/mvn -v
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /home/jenkins/apache-maven-3.8.1
Java version: 11.0.12, vendor: Eclipse Foundation, runtime: /opt/java/openjdk
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix"
同理使用docker命令也一样挂载进去。