CICD 小提示:Jenkins 部署在 Kubernetes 集群中如何暴露服务?
  lqP1GDtwBaW3 2023年12月11日 21 0

本文是  此长文中的部分内容,方便初学者照着操作。

Jenkins 部署

详细步骤略,参考文章:https://md.huanghuanhui.com/RuoYi-Cloud/RuoYi-Cloud.html


服务暴露细节

Jenkins 共需要暴露两个服务到外部,一个是 web 服务,用于管理操作,默认使用 8080 端口,另一个为 Agent 连接的服务,默认使用 50000 端口。

为了保证流水线正常工作,最终 web 服务和 Agent 服务的入口一致,因此暴露方式有下面几种:

  • 使用 Service Type Loadbalancer 同时暴露 web 和 Agent 服务,Loadbalancer IP 作为统一的入口,此时 Jenkins web 使用 HTTP 明文,Agent 可以直接使用 50000 端口
  • 使用 Service Type NodePort 同时暴露 web 和 Agent 服务,任意 Worker IP 作为统一的入口,此时 Jenkins web 和 Agent 服务均得使用 30000+ 的随机或者指定端口,其中 Web 依然是 HTTP 明文
  • Agent 服务使用 NodePort 发布,Web 使用 Nginx Ingress 暴露,在 Ingress 上做 TLS 卸载,其中 Nginx Ingress Controller 必须也使用 NodePort 或者 Hostport(80+443)进行暴露,外部访问时使用任意 Worker IP 作为入口
  • 和方式 2 类似,使用 NodePort 暴露两个服务,但在外部使用负载均衡器进行负载,本文介绍这种方式

流量拓扑

Client --> 外部 Nginx LB(443 + 50000) --> NodePort(30456+30000) --> Jenkins Pod

NodePort 配置

  • Jenkins Web 8080 转到 30456 端口
  • Jenkins Agent 服务 50000 端口转到 30000 端口
[root@k8s-m01 ~]# kubectl -n jenkins-prod get svc
NAME           TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                          AGE
jenkins-prod   NodePort   10.98.150.143   <none>        8080:30456/TCP,50000:30000/TCP   27d

外部 LB 配置

外部 LB 配置(Web 服务,在 LB 上进行 TLS 卸载):

CICD 小提示:Jenkins 部署在 Kubernetes 集群中如何暴露服务?_Jenkins

CICD 小提示:Jenkins 部署在 Kubernetes 集群中如何暴露服务?_Jenkins_02

使用 Let's Encrypt 的免费证书:

CICD 小提示:Jenkins 部署在 Kubernetes 集群中如何暴露服务?_Jenkins_03

外部 LB 配置(Agent 服务),将 50000 转成 NodePort 30000:

CICD 小提示:Jenkins 部署在 Kubernetes 集群中如何暴露服务?_Jenkins_04

CICD 小提示:Jenkins 部署在 Kubernetes 集群中如何暴露服务?_Jenkins_05

其他 Jenkins 配置

如果环境中想使用非 50000 的其他端口,需要在 Jenkins 的下列位置修改端口(注意:此端口是最终暴露到外面的端口,而非容器端口,容器端口默认 50000 无需修改)

CICD 小提示:Jenkins 部署在 Kubernetes 集群中如何暴露服务?_Jenkins Agent访问问题_06


Agent 连接失败的日志参考 - 无法访问端口

- jnlp -- terminated (0)
-----Logs-------------
INFO: Could not locate server among [https://jenkins.halfcoffee.com/]; waiting 10 seconds before retry
java.io.IOException: https://jenkins.halfcoffee.com/ provided port:50000 is not reachable on host jenkins.halfcoffee.com
	at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:304)
	at hudson.remoting.Engine.innerRun(Engine.java:809)
	at hudson.remoting.Engine.run(Engine.java:563)

Dec 07, 2023 6:26:26 AM hudson.remoting.Launcher$CuiListener status
INFO: Locating server among [https://jenkins.halfcoffee.com/]
Dec 07, 2023 6:26:26 AM org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver resolve
INFO: Remoting server accepts the following protocols: [Ping]
Dec 07, 2023 6:26:26 AM hudson.remoting.Launcher$CuiListener status
INFO: Agent discovery successful
  Agent address: jenkins.halfcoffee.com
  Agent port:    50000
  Identity:      75:c6:a2:4c:4f:20:87:8b:ae:59:50:86:57:63:04:c3
Dec 07, 2023 6:26:26 AM hudson.remoting.Launcher$CuiListener status
INFO: Handshaking
Dec 07, 2023 6:26:26 AM hudson.remoting.Launcher$CuiListener status
INFO: Connecting to jenkins.halfcoffee.com:50000
Dec 07, 2023 6:26:26 AM hudson.remoting.Launcher$CuiListener status
INFO: Server reports protocol JNLP4-connect-proxy not supported, skipping
Dec 07, 2023 6:26:26 AM hudson.remoting.Launcher$CuiListener status
INFO: Server reports protocol JNLP4-connect not supported, skipping
Dec 07, 2023 6:26:26 AM hudson.remoting.Launcher$CuiListener status
INFO: reconnect rejected, sleeping 10s: 
java.lang.Exception: The server rejected the connection: None of the protocols are enabled
	at hudson.remoting.Engine.onConnectionRejected(Engine.java:949)
	at hudson.remoting.Engine.innerRun(Engine.java:898)
	at hudson.remoting.Engine.run(Engine.java:563)

如果 Agent 连接成功,在 Jenkins 中不会有 jnlp 相关的错误。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月11日 0

暂无评论

lqP1GDtwBaW3