1

我是 Dev Ops 的新手,正在尝试使用 Jenkins 构建我的代码并将其上传到托管在 IBM 云上的 kubernetes 集群上。但是当我在 Jenkins 脚本中运行 Docker 运行命令时,我不断收到此错误。安装了所有最新的插件和

+ docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.

这是我不知道是对还是错的 Jenkins 脚本。我搜索了几篇文章和问题。他们都没有给我一个积极的结果。在 GCP/Kubernetes 上的 Docker 中尝试了这个Jenkins Docker

podTemplate(
    cloud: "kubernetes",
    label:"mypod",
    containers:[
        containerTemplate(
            name:"nodejs",
            image:"node",
            ttyEnabled:true,
            command:'cat',
            alwaysPullImage: true,
            resourceRequestCpu: '200m',
            resourceRequestMemory: '100Mi',
        ),
        containerTemplate(
            name:"docker",
            image:"",
            ttyEnabled:true,
            command:'cat',
            alwaysPullImage: true,
            resourceRequestCpu: '200m',
            resourceRequestMemory: '100Mi',
        ),
        containerTemplate(
            name:"helm",
            image:"alpine/helm",
            ttyEnabled:true,
            command:'cat',
            alwaysPullImage: true,
            resourceRequestCpu: '200m',
            resourceRequestMemory: '100Mi',
        )
    ],
    volumes:[
        hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')
    ]
){
    node("mypod"){
        def commitId
        stage ("Fetch repo"){
            checkout scm
            commitId = sh(script: 'git rev-parse --short HEAD',returnStdout:true).trim()
        }
        stage ("Installing packages"){
            container("nodejs"){
                sh 'npm install'
            }
        }
        stage ("Build"){
            container("nodejs"){
                sh 'npm run build'
            }
        }
        def repository
        stage ("Docker"){
            container('docker'){
                docker.withRegistry("https://us.icr.io/api","ibm-cloud"){
                    sh "docker run hello-world"
                }
            }
        }
        stage ("Deploy"){
            container ("helm"){
                sh 'helm version'
            }
        }
    }
}

这是我的 Jenkins pod 的部署文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-uat
  labels:
    app: jenkins
    chart: jenkins-5.0.18
    release: jenkins-uat
    heritage: Helm
spec:
  selector:
    matchLabels:
      app: jenkins
      release: jenkins-uat
  template:
    metadata:
      labels:
        app: jenkins
        chart: jenkins-5.0.18
        release: jenkins-uat
        heritage: Helm
    spec:      
      securityContext:
        fsGroup: 1001
      containers:
        - name: jenkins
          image: docker.io/bitnami/jenkins:2.235.1-debian-10-r7
          imagePullPolicy: "IfNotPresent"
          securityContext:
            runAsUser: 1001
          env:
            - name: JENKINS_USERNAME
              value: "hlpjenkin"
            - name: JENKINS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: jenkins-uat
                  key: jenkins-password
            - name: JENKINS_HOME
              value: "/opt/bitnami/jenkins/jenkins_home"
            - name: DISABLE_JENKINS_INITIALIZATION
              value: "no"
          ports:
            - name: http
              containerPort: 8080
            - name: https
              containerPort: 8443
          livenessProbe:
            httpGet:
              path: /login
              port: http
            initialDelaySeconds: 180
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 6
          readinessProbe:
            httpGet:
              path: /login
              port: http
            initialDelaySeconds: 30
            periodSeconds: 5
            timeoutSeconds: 3
            successThreshold: 1
            failureThreshold: 3
          resources:
            limits: {}
            requests:
              cpu: 300m
              memory: 512Mi
          volumeMounts:
            - name: jenkins-data
              mountPath: /bitnami/jenkins
      volumes:
        - name: jenkins-data
          persistentVolumeClaim:
            claimName: jenkins-uat
4

3 回答 3

1

所以我在我的 k8s 集群中安装了 Jenkins 作为容器:) 并设法重现了同样的错误:

docker run --rm hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

如何解决它

为了解决这个问题,你肯定需要访问 K8s 节点上的 Docker。jpetazzo很好地解释了它是如何工作的。

从技术上讲,您不需要“Docker 中的 Docker”(即 Docker 中的“完整 Docker 设置”)。你只想能够从你的 CI 系统运行 Docker,而这个 CI 系统本身就在一个容器中。这样您的 CI 系统(如 Jenkins)就可以启动容器。

因此,当您启动 CI 容器(Jenkins 或其他)时,不要使用 Docker-in-Docker 来破解某些东西,而是通过访问/var/run/docker.sock主主机来启动它。

您可以在下面看到我的Yamls中对此负责的部分。
这允许我的 CI 容器访问 Docker 套接字,因此 CI 容器将能够启动容器。

除了启动“子”容器之外,它会启动“兄弟”容器,但这在我们的上下文中非常好。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
...
spec:
  template:
    spec:
      containers:
      - env:
        volumeMounts:
        - mountPath: /var/run/docker.sock
          name: docker-sock
      ...
      volumes:
      - hostPath:
          path: /var/run/docker.sock
          type: File
        name: docker-sock

因此,就我而言,我创建的管道会生成以下日志:

####pipeline

pipeline {
    agent any

    stages     {
        stage('second_stage'){
            steps{
                sh 'docker run --rm hello-world'
            }
        }
    }
}

####logs

+ docker run --rm hello-world

Hello from Docker!
于 2020-08-20T16:48:13.680 回答
1

我遇到了类似的问题,我通过使我的用户成为 docker 组的一部分并执行 docker 来解决这个问题。当您的用户无法找到 docker 时,就会发生这种情况。

安装 docker 后,您需要按照安装后的步骤进行操作。

  1. 创建码头工人组 sudo groupadd docker

  2. 将您的用户添加到 docker 组。 sudo usermod -aG docker $USER

  3. 重启 docker 服务 sudo service docker stopsudo service docker start

  4. 从当前用户退出/注销并重新登录以验证

于 2020-08-17T14:22:29.837 回答
1

所以我在你的 podtemplate 中看到了几个问题。

首先,对于 docker 容器,您没有指定任何图像。您应该在此容器中使用 docker 映像。创建您自己的容器并在其中安装 docker,或者您可以使用https://hub.docker.com/r/volaka/ibm-cloud-cli这个镜像。它包括 ibmcloud cli、kubectl、helm 和 docker,用于 IBM Cloud 上的 kubernetes 自动化。

第二件事是我认为它与 Jenkins Kubernetes 有关。在管道中创建 podTemplate 后,即使您编辑模板,有时在最新的 pod 中也看不到更改。我遇到了这种错误,所以我使用编辑过的 podTemplate 删除并重新创建了管道。我这样说是因为即使您在 podTemplate 中声明了卷绑定,我在创建的 pod 的 yaml 中也看不到它。因此,我建议您使用最终的 podTemplate 重新创建管道。

我创建了有关如何在 IBM Kubernetes Service 上安装、配置和自动化 Jenkins 管道的详细演练。随意检查它。https://volaka.gitbook.io/jenkins-on-k8s/

于 2020-09-21T09:48:34.317 回答