1.新建部署job fangliProvider部署测试环境

简单测试参数化构建

流水线脚本:

podTemplate(name: 'jenkins-slave', cloud: 'kubernetes',
  namespace: 'jenkins', label: 'jenkins-slave',volumes: [
    hostPathVolume(mountPath: '/usr/bin/kubectl', hostPath: '/usr/bin/kubectl'),
    hostPathVolume(mountPath: '/var/jenkins_home/', hostPath: '/var/jenkins_home/')
]) 
  {
  node('jenkins-slave') {


    stage('apply update') {
      container('jnlp') {
          sh '''
          kubectl apply -f $deployfile -n testfl
 
          '''
      }
    }

部署成功截图

日志:

Console Output

 
Started by user fangli Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] podTemplate [Pipeline] { [Pipeline] node Created Pod: kubernetes jenkins/jenkins-slave-m274k-g0chs [Normal][jenkins/jenkins-slave-m274k-g0chs][Scheduled] Successfully assigned jenkins/jenkins-slave-m274k-g0chs to svn Still waiting to schedule task ‘jenkins-slave-m274k-g0chs’ is offline [Normal][jenkins/jenkins-slave-m274k-g0chs][Pulled] Container image "jenkins/inbound-agent:4.3-4" already present on machine [Normal][jenkins/jenkins-slave-m274k-g0chs][Created] Created container jnlp [Normal][jenkins/jenkins-slave-m274k-g0chs][Started] Started container jnlp Created Pod: kubernetes jenkins/jenkins-slave-m274k-s7rgw [Normal][jenkins/jenkins-slave-m274k-s7rgw][Scheduled] Successfully assigned jenkins/jenkins-slave-m274k-s7rgw to server-18 [Normal][jenkins/jenkins-slave-m274k-s7rgw][Pulling] Pulling image "jenkins/inbound-agent:4.3-4" 
Created Pod: kubernetes jenkins/jenkins-slave-m274k-f73q0
[Normal][jenkins/jenkins-slave-m274k-f73q0][Scheduled] Successfully assigned jenkins/jenkins-slave-m274k-f73q0 to svn
[Normal][jenkins/jenkins-slave-m274k-f73q0][Pulled] Container image "jenkins/inbound-agent:4.3-4" already present on machine
[Normal][jenkins/jenkins-slave-m274k-f73q0][Created] Created container jnlp
[Normal][jenkins/jenkins-slave-m274k-f73q0][Started] Started container jnlp
Agent jenkins-slave-m274k-f73q0 is provisioned from template jenkins-slave-m274k
---
apiVersion: "v1"
kind: "Pod"
metadata:
  annotations:
    buildUrl: "http://192.168.10.40:32634/job/fanglitestProvider%E9%83%A8%E7%BD%B2%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83/4/"
    runUrl: "job/fanglitestProvider%E9%83%A8%E7%BD%B2%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83/4/"
  labels:
    jenkins: "slave"
    jenkins/label-digest: "5059d2cd0054f9fe75d61f97723d98ab1a42d71a"
    jenkins/label: "jenkins-slave"
  name: "jenkins-slave-m274k-f73q0"
spec:
  containers:
  - env:
    - name: "JENKINS_SECRET"
      value: "********"
    - name: "JENKINS_TUNNEL"
      value: "10.1.243.253:50000"
    - name: "JENKINS_AGENT_NAME"
      value: "jenkins-slave-m274k-f73q0"
    - name: "JENKINS_NAME"
      value: "jenkins-slave-m274k-f73q0"
    - name: "JENKINS_AGENT_WORKDIR"
      value: "/home/jenkins/agent"
    - name: "JENKINS_URL"
      value: "http://192.168.10.40:32634/"
    image: "jenkins/inbound-agent:4.3-4"
    name: "jnlp"
    resources:
      limits: {}
      requests:
        memory: "256Mi"
        cpu: "100m"
    volumeMounts:
    - mountPath: "/usr/bin/kubectl"
      name: "volume-0"
      readOnly: false
    - mountPath: "/var/jenkins_home"
      name: "volume-1"
      readOnly: false
    - mountPath: "/home/jenkins/agent"
      name: "workspace-volume"
      readOnly: false
  nodeSelector:
    kubernetes.io/os: "linux"
  restartPolicy: "Never"
  volumes:
  - hostPath:
      path: "/usr/bin/kubectl"
    name: "volume-0"
  - hostPath:
      path: "/var/jenkins_home/"
    name: "volume-1"
  - emptyDir:
      medium: ""
    name: "workspace-volume"

Running on jenkins-slave-m274k-f73q0 in /home/jenkins/agent/workspace/fanglitestProvider部署测试环境
[Pipeline] {
[Pipeline] stage
[Pipeline] { (apply update)
[Pipeline] container
[Pipeline] {
[Pipeline] sh

2.错误的解决

a. 云环境cloud: 'kubernetes' 不存在

解决:配置云环境,没插件下载k8s对应插件

系统管理》节点管理》》configure clouds》》add a new Cloud

因为jenkins已经是k8s管理的容器,默认连接测试通常会成功。

 

b.  运行流水线pod模板 k8s会自动创建 jenkins从节点slave(也是一个pod)去跑这个job.

从节点运行不成功。5000端口不可达

解决:

   1. jenkins-service.xml  配置5000端口

apiVersion: v1
kind: Service
metadata:
  name: jenkins
spec:
  type: NodePort
  ports:
    - name: web
      port: 8080
      targetPort: web
      nodePort: 32634
    - name: agent
      port: 50000
      targetPort: agent
  selector:
    app: jenkins

2. 部署文件配置5000容器端口 name和jenkins-service.xml对应

3.最重要的一点,jenkins配置通道地址 IP+端口 前面不要有http之类

这个地址可在 系统管理》》系统信息    中找到

 

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐