Jenkins Docker静态agent节点的构建

网友投稿 1642 2022-11-30

Jenkins docker静态agent节点的构建

Jenkins Docker静态agent节点的构建

静态节点就是固定的在一台机器上面,通过一些固定的命令将其启动起来,动态节点就是来了多个任务,可以帮你创建多个agent去跑。而不像静态节点来了多个任务,那么就会等待这个任务队列,然后进行这个构建。这就是静态和动态的区别。

静态节点就是通过java -jar这条命令去启动起来就可以了。

1. 静态节点实践

首先,在Jenkins 中创建一个jnlp类型的agent, 拿到连接所需要的认证和秘钥信息。

jar -jar这条命令后面有很多的参数,不管是配置动态节点还是静态节点,这些参数都会用到。

java -jar agent.jar -jnlpUrl -secret 047a2627be5ee128c1a93f281051acc7bbbbd5221dcb56ae9fa408a494e72c2f -workDir "/home/jenkins"

1.1 Docker静态节点

启动镜像,传递认证参数。(使用docker的话其实就是使用一个镜像,镜像里面运行了一个agent,然后将这些参数传递进去)

在虚拟机上面-agent.jar包然后手动执行这些命令就可以了

在命令行中启动节点java -jar agent.jar -jnlpUrl -secret 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba -workDir "/var/jenkins"Run from agent command line, with the secret stored in a file:echo 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba > secret-filejava -jar agent.jar -jnlpUrl -secret @secret-file -workDir "/var/jenkins"

如果是在docker里面就需要找一个agent镜像:inbound-agent镜像 : ​​Docker Hub​​

## 测试

[root@jenkins-agent ~]# docker run --init jenkins/inbound-agent -url -workDir=/home/jenkins 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba build-02Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main createEngineINFO: Setting up agent: build-02Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener INFO: Jenkins agent is running in headless mode.Oct 28, 2021 2:19:25 AM hudson.remoting.Engine startEngineINFO: Using Remoting version: 4.11Oct 28, 2021 2:19:25 AM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDirINFO: Using /home/jenkins/remoting as a remoting work directoryOct 28, 2021 2:19:25 AM org.jenkinsci.remoting.engine.WorkDirManager setupLoggingINFO: Both error and output logs will be printed to /home/jenkins/remotingOct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener statusINFO: Locating server among [28, 2021 2:19:25 AM org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver resolveINFO: Remoting server accepts the following protocols: [JNLP4-connect, Ping]Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener statusINFO: Agent discovery successful Agent address: 139.198.166.235 Agent port: 50000 Identity: 67:e8:d2:73:d1:cf:01:f6:6c:a4:cb:b3:0f:25:8c:3dOct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener statusINFO: HandshakingOct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener statusINFO: Connecting to 139.198.166.235:50000Oct 28, 2021 2:19:25 AM hudson.remoting.jnlp.Main$CuiListener statusINFO: Trying protocol: JNLP4-connectOct 28, 2021 2:19:25 AM org.jenkinsci.remoting.protocol.impl.BIONetworkLayer$Reader runINFO: Waiting for ProtocolStack to start.Oct 28, 2021 2:19:26 AM hudson.remoting.jnlp.Main$CuiListener statusINFO: Remote identity confirmed: 67:e8:d2:73:d1:cf:01:f6:6c:a4:cb:b3:0f:25:8c:3dOct 28, 2021 2:19:26 AM hudson.remoting.jnlp.Main$CuiListener statusINFO: Connected

## 运行

[root@jenkins-agent ~]# docker run -itd --name build-02 jenkins/inbound-agent -url -workDir=/home/jenkins 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba build-02bd2500223dabb45ff41c234a8871651f93b38264458c4d2bed40f821577b8944[root@jenkins-agent ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESbd2500223dab 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 bashjenkins@bd2500223dab:~$ mvnbash: 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 -workDir=/home/jenkins 942d7e769a43f65388751ee954af20e90fa0c5f8bdb19e0584db4cc61adab8ba build-02jenkins@2a682b01263b:~$ /home/jenkins/apache-maven-3.8.1/bin/mvn -vApache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)Maven home: /home/jenkins/apache-maven-3.8.1Java version: 11.0.12, vendor: Eclipse Foundation, runtime: /opt/java/openjdkDefault locale: en, platform encoding: UTF-8OS name: "linux", version: "3.10.0-693.el7.x86_64", arch: "amd64", family: "unix"

同理使用docker命令也一样挂载进去。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:@Valid 无法校验List<E>的问题
下一篇:Jenkins Kubernetes静态agent节点的构建
相关文章

 发表评论

暂时没有评论,来抢沙发吧~