简体   繁体   English

如何在 Kubernetes 上运行 Jenkins 和 Docker?

[英]How to run Jenkins with Docker on Kubernetes?

I'm attempting to execute a Jenkins & Docker CLI container on Kubernetes.我正在尝试在 Kubernetes 上执行 Jenkins 和 Docker CLI 容器。 Here are my steps:这是我的步骤:

I create the pod using:我使用以下方法创建 pod:

kubectl --kubeconfig my-kubeconfig.yml run my-jenkins-pod --image=trion/jenkins-docker-client --restart=Never

Which creates a pod-based on the image https://hub.docker.com/r/trion/jenkins-docker-client它基于图像https://hub.docker.com/r/trion/jenkins-docker-client创建一个 pod

I create the deployment using:我使用以下方法创建部署:

kubectl --kubeconfig my-kubeconfig.yml apply -f /kub/kube

/kub/kube contains jenkins-deployment-yaml which I have configured as: /kub/kube包含我已配置为jenkins-deployment-yaml

apiVersion: v1
kind: Service
metadata:
  name: my-jenkins-pod
spec:
  ports:
    - protocol: "TCP"
      port: 50000
      targetPort: 5001
  selector:
    app: my-jenkins-pod
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-jenkins-pod
spec:
  selector:
    matchLabels:
      app: my-jenkins-pod
  replicas: 1
  template:
    metadata:
      labels:
        app: my-jenkins-pod
    spec:
      containers:
        - name: ml-services
          image: trion/jenkins-docker-client
          ports:
            - containerPort: 5001

To access the Jenkins container I expose the IP using:要访问 Jenkins 容器,我使用以下方法公开 IP:

kubectl --kubeconfig my-kubeconfig.yml expose deployment my-jenkins-pod --type=LoadBalancer --name=my-jenkins-pod-public

To return the IP of the Jenkins and Docker image I use:要返回 Jenkins 和 Docker 图像的 IP,我使用:

kubectl --kubeconfig my-kubeconfig.yml get services my-jenkins-pod-public

Which returns:返回:

NAME                    TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)          AGE
my-jenkins-pod-public   LoadBalancer   9.5.52.28   161.61.222.16   5001:30878/TCP   10m

To test I open the URL at location:为了测试,我在以下位置打开 URL:

http://161.61.222.16:5001/ http://161.61.222.16:5001/

Which returns:返回:

This page isn’t working
161.61.222.16 didn’t send any data.
ERR_EMPTY_RESPONSE

It seems the service has started but the port mappings are incorrect?服务好像已经启动了,但是端口映射不正确?

The log of the pod my-jenkins-pod contains: pod my-jenkins-pod 的日志包含:

Running from: /usr/share/jenkins/jenkins.war webroot: EnvVars.masterEnvVars.get("JENKINS_HOME") 2021-04-03 11:15:42.899+0000 [id=1] INFO org.eclipse.jetty.util.log.Log#initialized: Logging initialized @274ms to org.eclipse.jetty.util.log.JavaUtilLog 2021-04-03 11:15:43.012+0000 [id=1] INFO winstone.Logger#logInternal: Beginning extraction from war file 2021-04-03 11:15:44.369+0000 [id=1] WARNING oejshandler.ContextHandler#setContextPath: Empty contextPath 2021-04-03 11:15:44.416+0000 [id=1] INFO org.eclipse.jetty.server.Server#doStart: jetty-9.4.39.v20210325;运行自:/usr/share/jenkins/jenkins.war webroot:EnvVars.masterEnvVars.get("JENKINS_HOME") 2021-04-03 11:15:42.899+0000 [id=1] INFO org.eclipse.jetty.util .log.Log#initialized:记录已初始化@274ms 到 org.eclipse.jetty.util.log.JavaUtilLog 2021-04-03 11:15:43.012+0000 [id=1] INFO winstone.Logger#logInternal:开始提取战争文件 2021-04-03 11:15:44.369+0000 [id=1] 警告 oejshandler.ContextHandler#setContextPath: Empty contextPath 2021-04-03 11:15:44.416+0000 [id=1] INFO org.Z6B7B655DD22FAA3F10677C512493A8A jetty.server.Server#doStart:jetty-9.4.39.v20210325; built: 2021-03-25T14:42:11.471Z;建成:2021-03-25T14:42:11.471Z; git: 9fc7ca5a922f2a37b84ec9dbc26a5168cee7e667; git:9fc7ca5a922f2a37b84ec9dbc26a5168cee7e667; jvm 1.8.0_282-b08 2021-04-03 11:15:44.653+0000 [id=1] INFO oejwStandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet 2021-04-03 11:15:44.695+0000 [id=1] INFO oejssDefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0 2021-04-03 11:15:44.695+0000 [id=1] INFO oejssDefaultSessionIdManager#doStart: No SessionScavenger set, using defaults 2021-04-03 11:15:44.696+0000 [id=1] INFO oejserver.session.HouseKeeper#startScavenging: node0 Scavenging every 660000ms 2021-04-03 11:15:45.081+0000 [id=1] INFO hudson.WebAppMain#contextInitialized: Jenkins home directory: /var/jenkins_home found at: EnvVars.masterEnvVars.get("JENKINS_HOME") 2021-04-03 11:15:45.203+0000 [id=1] INFO oejshandler.ContextHandler#doStart: Started w.@24f43aa3{Jenkins v2.286,/,fi jvm 1.8.0_282-b08 2021-04-03 11:15:44.653+0000 [id=1] INFO oejwStandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet 2021-04- 03 11:15:44.695+0000 [id=1] 信息 oejssDefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0 2021-04-03 11:15:44.695+0000 [id=1] 信息 oejssDefaultSessionIdManager#doStart: 没有设置 SessionScavenger,使用默认值2021-04-03 11:15:44.696+0000 [id=1] INFO oejserver.session.HouseKeeper#startScavenging: node0 每 660000ms 清理一次 2021-04-03 11:15:45.081+ WebAppMain#contextInitialized:Jenkins 主目录:/var/jenkins_home 位于:EnvVars.masterEnvVars.get("JENKINS_HOME") 2021-04-03 11:15:45.203+0000 [id=1] INFO oejshandler.ContextHandler#doStart: w.@24f43aa3{Jenkins v2.286,/,fi le:///var/jenkins_home/war/,AVAILABLE}{/var/jenkins_home/war} 2021-04-03 11:15:45.241+0000 [id=1] INFO oejserver.AbstractConnector#doStart: Started ServerConnector@4f0f2942{HTTP/1.1, (http/1.1)}{0.0.0.0:8080} 2021-04-03 11:15:45.241+0000 [id=1] INFO org.eclipse.jetty.server.Server#doStart: Started @2616ms 2021-04-03 11:15:45.245+0000 [id=21] INFO winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled 2021-04-03 11:15:46.479+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: Started initialization 2021-04-03 11:15:46.507+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: Listed all plugins 2021-04-03 11:15:47.654+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained: Prepared all plugins 2021-04-03 11:15:47.660+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: Started all plugins 2021-04-03 11:15:47.68 le:///var/jenkins_home/war/,AVAILABLE}{/var/jenkins_home/war} 2021-04-03 11:15:45.241+0000 [id=1] INFO oejserver.AbstractConnector#doStart: 已启动 ServerConnector@4f0f2942 {HTTP/1.1, (http/1.1)}{0.0.0.0:8080} 2021-04-03 11:15:45.241+0000 [id=1] INFO org.eclipse.jetty.server.Server#doStart: 开始@ 2616 毫秒 2021-04-03 11:15:45.245+0000 [id=21] INFO winstone.Logger#logInternal: Winstone Servlet 引擎正在运行:controlPort=disabled 2021-04-03 11:15:46.479+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: Started initialization 2021-04-03 11:15:46.507+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: Listed all plugins 2021-04-03 11:15:47.654+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained: Prepared all plugins 2021-04-03 11:15:47.660+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: Started all plugins 2021-04-03 11 :15:47.68 0+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained: Augmented all extensions 2021-04-03 11:15:48.620+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: System config loaded 2021-04-03 11:15:48.621+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: System config adapted 2021-04-03 11:15:48.621+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained: Loaded all jobs 2021-04-03 11:15:48.622+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated 2021-04-03 11:15:48.704+0000 [id=40] INFO hudson.model.AsyncPeriodicWork#lambda$doRun$0: Started Download metadata 2021-04-03 11:15:48.722+0000 [id=40] INFO hudson.util.Retrier#start: Attempt #1 to do the action check updates server 2021-04-03 11:15:49.340+0000 [id=26] INFO jenkins.install.SetupWizard 0+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained: Augmented all extensions 2021-04-03 11:15:48.620+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: System config loaded 2021-04 -03 11:15:48.621+0000 [id=26] INFO jenkins.InitReactorRunner$1#onAttained: System config adapted 2021-04-03 11:15:48.621+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained:已加载所有作业 2021-04-03 11:15:48.622+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained:所有作业的配置已更新 2021-04-03 11:15:48.704+0000 [id=40] INFO hudson.model.AsyncPeriodicWork#lambda$doRun$0: 开始下载元数据 2021-04-03 11:15:48.722+0000 [id=40] INFO hudson.util.Retrier#start: 尝试 #1 进行操作检查更新服务器 2021-04-03 11:15:49.340+0000 [id=26] 信息 jenkins.install.SetupWizard #init: #在里面:



************************************************************* Jenkins initial setup is required. ****************************************************** *********** Jenkins 需要初始设置。 An admin user has been created and a password generated.已创建管理员用户并生成密码。 Please use the following password to proceed to installation: ab5dbf74145c405fb5a33456d4b97436 This may also be found at: /var/jenkins_home/secrets/initialAdminPassword请使用以下密码继续安装:ab5dbf74145c405fb5a33456d4b97436 这也可以在以下位置找到:/var/jenkins_home/secrets/initialAdminPassword



************************************************************* 2021-04-03 11:16:08.107+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained: Completed initialization 2021-04-03 11:16:08.115+0000 [id=20] INFO hudson.WebAppMain$3#run: Jenkins is fully up and running 2021-04-03 11:16:08.331+0000 [id=40] INFO hmDownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller 2021-04-03 11:16:08.332+0000 [id=40] INFO hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1 2021-04-03 11:16:08.334+0000 [id=40] INFO hudson.model.AsyncPeriodicWork#lambda$doRun$0: Finished Download metadata. ****************************************************** *********** 2021-04-03 11:16:08.107+0000 [id=27] INFO jenkins.InitReactorRunner$1#onAttained:完成初始化 2021-04-03 11:16:08.115+0000 [id=20] INFO hudson.WebAppMain$3#run: Jenkins 完全启动并运行 2021-04-03 11:16:08.331+0000 [id=40] INFO hmDownloadService$Downloadable#load: 获取 hudson 的更新数据文件.tasks.Maven.MavenInstaller 2021-04-03 11:16:08.332+0000 [id=40] INFO hudson.util.Retrier#start:在尝试 #1 2021-04-03 11 时成功执行了操作检查更新服务器:16:08.334+0000 [id=40] INFO hudson.model.AsyncPeriodicWork#lambda$doRun$0: 完成下载元数据。 19,626 ms 19,626 毫秒

Is Jenkins server is started at port 8080? Jenkins 服务器是否在 8080 端口启动? because of this log message:由于此日志消息:

11:15:45.241+0000 [id=1] INFO oejserver.AbstractConnector#doStart: Started ServerConnector@4f0f2942{HTTP/1.1, (http/1.1)}{0.0.0.0:8080} 2021-04-03 11:15:45.241+0000 [id=1] INFO oejserver.AbstractConnector#doStart: 已启动 ServerConnector@4f0f2942{HTTP/1.1, (http/1.1)}{0.0.0.0:8080} 2021-04-03

I tried changing jenkins-deployment-yaml to point at port 8080 instead of 50000 , resulting in the updated jenkins-deployment-yaml :我尝试将jenkins-deployment-yaml更改为指向端口8080而不是50000 ,从而导致更新的jenkins-deployment-yaml

apiVersion: v1
kind: Service
metadata:
  name: my-jenkins-pod
spec:
  ports:
    - protocol: "TCP"
      port: 8080

But the same error is returned when I attempt to access http://161.61.222.16:5001/但是当我尝试访问http://161.61.222.16:5001/时返回相同的错误

Are my port mappings incorrect?我的端口映射不正确吗? Is this a correct method of adding an existing docker container that is available on the docker hub to a Kubernetes cluster?这是将 docker 集线器上可用的现有 docker 容器添加到 Kubernetes 集群的正确方法吗?

Update:更新:

The result of command kubectl describe services my-jenkins-pod-public is:命令kubectl describe services my-jenkins-pod-public的结果是:

Name:                     my-jenkins-pod-public
Namespace:                default
Labels:                   <none>
Annotations:              kubernetes.digitalocean.com/load-balancer-id: d46ae9ae-6e8a-4fd8-aa58-43c08310059a
Selector:                 app=my-jenkins-pod
Type:                     LoadBalancer
IP Families:              <none>
IP:                       10.245.152.228
IPs:                      10.245.152.228
LoadBalancer Ingress:     161.61.222.16
Port:                     <unset>  5001/TCP
TargetPort:               5001/TCP
NodePort:                 <unset>  30878/TCP
Endpoints:                10.214.12.12:5001
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Trying to access http://161.61.222.16:30878/ via browser returns:尝试通过浏览器访问http://161.61.222.16:30878/返回:

This site can't be reached159.65.211.46 refused to connect.无法访问此站点 159.65.211.46 拒绝连接。 Try:尝试:

Checking the connection Checking the proxy and the firewall ERR_CONNECTION_REFUSED检查连接 检查代理和防火墙 ERR_CONNECTION_REFUSED

Trying to access http://161.61.222.16:5001/ via browser returns:尝试通过浏览器访问http://161.61.222.16:5001/返回:

This page isn't working 161.61.222.16 didn't send any data.此页面不工作 161.61.222.16 没有发送任何数据。 ERR_EMPTY_RESPONSE ERR_EMPTY_RESPONSE

Seems the port 5001 is exposed/accessible but is not sending any data.似乎端口5001已公开/可访问,但未发送任何数据。

I also tried accessing 10.214.12.12 on ports 5001 & 30878 but both requests time out.我还尝试在端口500130878上访问10.214.12.12 ,但两个请求都超时。

You need to use http://161.61.222.16:30878/ from outside of the host which is running containers on.您需要从运行容器的主机外部使用http://161.61.222.16:30878/ Port 5001 is just accessible inside the cluster with internal IP (9.5.52.28 is in your case).端口 5001 只能在集群内部使用内部 IP 访问(在您的情况下为 9.5.52.28)。 Whenever you expose your deployment, automatically (also you can define manually) one of the NodePort (by default between 30000 - 32767)assign to the service for external request.每当您公开您的部署时,会自动(也可以手动定义)其中一个 NodePort(默认情况下在 30000 - 32767 之间)分配给服务以进行外部请求。

For service details, you need to run the below command.有关服务详细信息,您需要运行以下命令。 The command output will give you NodePort and another details.命令 output 将为您提供 NodePort 和其他详细信息。

kubectl describe services my-service

Please check related kubernetes documentation请查看相关 kubernetes 文档

Also you have configured service with port 5001 but Jenkins working with 8080 as far as I see in logs.此外,据我在日志中看到的,您还使用端口 5001 配置了服务,但 Jenkins 使用 8080。 Try to change target port of service to 8080 from 5001尝试将目标服务端口从 5001 更改为 8080

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM