相关文章
企业实战(23)基于Docker平台部署Jenkins中国定制版(1)
添加node节点前配置
开始添加node节点
在添加node节点时,填好“名称”、“描述”、“执行器数量”、“远程工作目录”、“标签”、“用法(一般默认)”、“启动方式(根据情况)”,完成后保存。
这时node1节点还是未在线状态,点击“node1”名称会有节点连接Jenkins的方法。
注意:
这里必须用--user root
用户进入容器才可以创建工作目录,默认进入是jenkins
用户,会提示权限不足。
[root@localhost ~]# docker ps -a //容器运行时需要映射代理连接端口50000 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1dad787d6f46 myjenkins:v1 "/sbin/tini -- /usr/…" 38 minutes ago Up 11 minutes 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp myjenkins [root@localhost ~]# docker exec -it --user root jenkins /bin/bash root@367402ec96bf:/# mkdir -p /opt/jenkins //创建工作目录 root@367402ec96bf:/# cd /opt/jenkins/ root@367402ec96bf:/opt/jenkins# wget http://192.168.1.8:8080/jnlpJars/agent.jar root@367402ec96bf:/opt/jenkins# ls agent.jar root@367402ec96bf:/opt/jenkins# java -jar agent.jar -jnlpUrl http://192.168.1.8:8080/computer/node1/slave-agent.jnlp -secret 90e0e5a0d61f67d551d029a22584632fb96f314acf1f3789372adbb747e81b11 -workDir "/opt/jenkins" ...... Nov 16, 2020 6:19:14 AM hudson.remoting.jnlp.Main$CuiListener status INFO: Trying protocol: JNLP4-connect Nov 16, 2020 6:19:14 AM hudson.remoting.jnlp.Main$CuiListener status INFO: Remote identity confirmed: 33:34:ca:16:46:58:d8:12:94:38:4b:eb:ee:33:a9:4c Nov 16, 2020 6:19:14 AM hudson.remoting.jnlp.Main$CuiListener status INFO: Connected //连接成功
`报错`
如果在容器中下载好agent.jar后,执行节点连接命令的时候出现下面的错误:
WARNING: Connection refused (Connection refused)
root@367402ec96bf:/opt/jenkins# java -jar agent.jar -jnlpUrl http://192.168.1.8:8080/computer/node1/slave-agent.jnlp -secret 5cb49ff710c0dde742725fcb82dc2e8ccf4f612819e2b6889cfcb93207fc734a -workDir "/opt/jenkins" ...... WARNING: Connection refused (Connection refused) Nov 16, 2020 3:57:42 AM hudson.remoting.jnlp.Main$CuiListener error SEVERE: http://192.168.1.8:8080/ provided port:50000 is not reachable java.io.IOException: http://192.168.1.8:8080/ provided port:50000 is not reachable at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:314) at hudson.remoting.Engine.innerRun(Engine.java:693) at hudson.remoting.Engine.run(Engine.java:518)
解决:
1.查看正在运行的容器是否已经映射了前面在Jenkins管理界面设置中的“全局安全配置”里配置的代理指定端口(详情见顶图)
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1dad787d6f46 myjenkins:v1 "/sbin/tini -- /usr/…" 38 minutes ago Up 11 minutes 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp myjenkins
2.没有映射代理端口需要重新映射才能正常连接,有下面两种方法:
- 2.1 `将当前已经运行的容器通过docker commit命令提交为一个镜像,然后重新执行docker run命令添加完整的端口映射。`(推荐)`
[root@localhost ~]# docker stop jenkins [root@localhost ~]# docker commit -m "my jenkins" 367402ec96bf myjenkins:v1 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE myjenkins v1 5349fdcc8533 7 minutes ago 662MB jenkinszh/jenkins-zh latest 2cb4cce6db0a 5 months ago 656MB [root@localhost ~]# docker run -itd --name=myjenkins -p 8080:8080 -p 50000:50000 myjenkins:v1 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1dad787d6f46 myjenkins:v1 "/sbin/tini -- /usr/…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp myjenkins
进入浏览器访问Jenkins页面,重新完成入门步骤后进入Jenkins管理界面首页,按照本教程从头开始进行“添加node节点前配置”、“开始添加node节点”步骤,进行至“节点连接Jenkins方式”时,按照下面步骤重新操作即可。
[root@localhost ~]# docker exec -it --user root myjenkins /bin/bash root@1dad787d6f46:/# cd /opt/jenkins/ root@1dad787d6f46:/opt/jenkins# rm -rf * //删除之前的文件 root@1dad787d6f46:/opt/jenkins# wget http://192.168.1.8:8080/jnlpJars/agent.jar //重新复制连接代理的agent.jar链接 root@1dad787d6f46:/opt/jenkins# ls agent.jar root@1dad787d6f46:/opt/jenkins# java -jar agent.jar -jnlpUrl http://192.168.1.8:8080/computer/node1/slave-agent.jnlp -secret 90e0e5a0d61f67d551d029a22584632fb96f314acf1f3789372adbb747e81b11 -workDir "/opt/jenkins" ...... INFO: Agent discovery successful Agent address: 192.168.1.8 Agent port: 50000 Identity: 33:34:ca:16:46:58:d8:12:94:38:4b:eb:ee:33:a9:4c Nov 16, 2020 6:19:14 AM hudson.remoting.jnlp.Main$CuiListener status INFO: Handshaking Nov 16, 2020 6:19:14 AM hudson.remoting.jnlp.Main$CuiListener status INFO: Connecting to 192.168.1.8:50000 Nov 16, 2020 6:19:14 AM hudson.remoting.jnlp.Main$CuiListener status INFO: Trying protocol: JNLP4-connect Nov 16, 2020 6:19:14 AM hudson.remoting.jnlp.Main$CuiListener status INFO: Remote identity confirmed: 33:34:ca:16:46:58:d8:12:94:38:4b:eb:ee:33:a9:4c Nov 16, 2020 6:19:14 AM hudson.remoting.jnlp.Main$CuiListener status INFO: Connected
- 2.2 `通过配置文件修改已经运行的docker容器端口映射`
优化
在上面代理连接Jenkins的时候,可以看到我们是直接通过执行命令连接,这种方式会一直占用系统终端,这时我们可以通过编写一个脚本来实现不占用终端一直运行脚本达到代理连接Jenkins的效果。(在容器中可以通过宿主机写好脚本后,与jenkins容器共享脚本目录,这样容器中也可以直接访问脚本目录执行脚本)
nohup
不挂断地运行命令 &
在后台运行 (一般这两个一起用)
nohup command &
[root@localhost jenkins]# vim start.sh #!/bin/bash nohup java -jar agent.jar -jnlpUrl http://192.168.1.8:8080/computer/node1/slave-agent.jnlp -secret 90e0e5a0d61f67d551d029a22584632fb96f314acf1f3789372adbb747e81b11 -workDir "/opt/jenkins" & (可以通过commit新镜像重新执行docker run 映射端口跟共享目录) # sh start.sh # ps -aux | grep start.sh //通过ps查看后台运行的脚本 root 15269 0.0 0.0 112720 996 pts/0 S+ 15:13 0:00 grep --color=auto start.sh # tail -f nohup.out //查看脚本后台执行启动日志 十一月 16, 2020 3:10:11 下午 hudson.remoting.jnlp.Main$CuiListener status 信息: Handshaking 十一月 16, 2020 3:10:11 下午 hudson.remoting.jnlp.Main$CuiListener status 信息: Connecting to 192.168.1.8:50000 十一月 16, 2020 3:10:11 下午 hudson.remoting.jnlp.Main$CuiListener status 信息: Trying protocol: JNLP4-connect 十一月 16, 2020 3:10:12 下午 hudson.remoting.jnlp.Main$CuiListener status 信息: Remote identity confirmed: 33:34:ca:16:46:58:d8:12:94:38:4b:eb:ee:33:a9:4c 十一月 16, 2020 3:10:12 下午 hudson.remoting.jnlp.Main$CuiListener status 信息: Connected