首页 > 解决方案 > Docker 配置中的 Docker

问题描述

我让 Jenkins 在 K8s 中运行,现在我正在尝试运行:docker build 作为 Jenkins 构建中的步骤之一。由于 Jenkins 在 Docker 中运行,我从这篇文章中找到了在 Docker 中使用 Docker 的解决方案:https ://medium.com/hootsuite-engineering/building-docker-images-inside-kubernetes-42c6af855f25

但是,我修改了部署yaml文件后,还是不行。

有 2 个容器正在运行:Jenkins(Jenkins 镜像)和 dind(docker 镜像中的 docker)。我可以在 dind 容器中运行 docker 命令,但无法在 Jenkins 或 pod 中运行 docker 命令。

这是yaml文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "9"
    field.cattle.io/publicEndpoints: '[{"addresses":["10.0.0.111"],"port":80,"protocol":"HTTP","serviceName":"jenkins-with-did:jenkins-with-did","ingressName":"jenkins-with-did:jenkins-with-did","hostname":"jenkins.dtl.miproad.ad","allNodes":true}]'
  creationTimestamp: "2020-04-30T06:38:40Z"
  generation: 11
  labels:
    app.kubernetes.io/component: jenkins-master
    app.kubernetes.io/instance: jenkins-with-did
    app.kubernetes.io/managed-by: Tiller
    app.kubernetes.io/name: jenkins
    helm.sh/chart: jenkins-1.18.0
    io.cattle.field/appId: jenkins-with-did
  name: jenkins-with-did
  namespace: jenkins-with-did
  resourceVersion: "29233038"
  selfLink: /apis/apps/v1/namespaces/jenkins-with-did/deployments/jenkins-with-did
  uid: 6439c48d-c4ce-418c-8553-d06fee13c7d1
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/component: jenkins-master
      app.kubernetes.io/instance: jenkins-with-did
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        cattle.io/timestamp: "2020-04-30T18:15:50Z"
        checksum/config: fda7089fede91f066c406bbba5e2a1d59f71183eebe9bca3fe7de19d13504058
        field.cattle.io/ports: '[[{"containerPort":8080,"dnsName":"jenkins-with-did","hostPort":0,"kind":"ClusterIP","name":"http","protocol":"TCP","sourcePort":0},{"containerPort":50000,"dnsName":"jenkins-with-did","hostPort":0,"kind":"ClusterIP","name":"slavelistener","protocol":"TCP","sourcePort":0}]]'
      creationTimestamp: null
      labels:
        app.kubernetes.io/component: jenkins-master
        app.kubernetes.io/instance: jenkins-with-did
        app.kubernetes.io/managed-by: Tiller
        app.kubernetes.io/name: jenkins
        helm.sh/chart: jenkins-1.18.0
    spec:
      containers:
      - args:
        - --argumentsRealm.passwd.$(ADMIN_USER)=$(ADMIN_PASSWORD)
        - --argumentsRealm.roles.$(ADMIN_USER)=admin
        - --httpPort=8080
        env:
        - name: JAVA_OPTS
        - name: JENKINS_OPTS
        - name: JENKINS_SLAVE_AGENT_PORT
          value: "50000"
        - name: POD_NAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.name
        - name: ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              key: jenkins-admin-password
              name: jenkins-with-did
              optional: false
        - name: ADMIN_USER
          valueFrom:
            secretKeyRef:
              key: jenkins-admin-user
              name: jenkins-with-did
              optional: false
        image: jenkins/jenkins:lts
        imagePullPolicy: Always
        livenessProbe:
          failureThreshold: 5
          httpGet:
            path: /login
            port: http
            scheme: HTTP
          initialDelaySeconds: 90
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
          env:
            - name: DOCKER_HOST
              value: tcp://localhost:2375
        name: jenkins
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
        - containerPort: 50000
          name: slavelistener
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /login
            port: http
            scheme: HTTP
          initialDelaySeconds: 60
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          limits:
            cpu: "2"
            memory: 4Gi
          requests:
            cpu: 50m
            memory: 256Mi
        securityContext:
          capabilities: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /tmp
          name: tmp
        - mountPath: /var/jenkins_home
          name: jenkins-home
        - mountPath: /var/jenkins_config
          name: jenkins-config
          readOnly: true
        - mountPath: /usr/share/jenkins/ref/secrets/
          name: secrets-dir
        - mountPath: /usr/share/jenkins/ref/plugins/
          name: plugin-dir
      - image: docker:18.05-dind
        imagePullPolicy: IfNotPresent
        name: dind
        resources: {}
        securityContext:
          privileged: true
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /var/lib/docker
          name: dind-storage
      dnsPolicy: ClusterFirst
      initContainers:
      - command:
        - sh
        - /var/jenkins_config/apply_config.sh
        env:
        - name: ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              key: jenkins-admin-password
              name: jenkins-with-did
              optional: false
        - name: ADMIN_USER
          valueFrom:
            secretKeyRef:
              key: jenkins-admin-user
              name: jenkins-with-did
              optional: false
        image: jenkins/jenkins:lts
        imagePullPolicy: Always
        name: copy-default-config
        resources:
          limits:
            cpu: "2"
            memory: 4Gi
          requests:
            cpu: 50m
            memory: 256Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /var/lib/docker
          name: dind-storage
        - mountPath: /tmp
          name: tmp
        - mountPath: /var/jenkins_home
          name: jenkins-home
        - mountPath: /var/jenkins_config
          name: jenkins-config
        - mountPath: /usr/share/jenkins/ref/secrets/
          name: secrets-dir
        - mountPath: /var/jenkins_plugins
          name: plugin-dir
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        runAsUser: 0
      serviceAccount: jenkins-with-did
      serviceAccountName: jenkins-with-did
      terminationGracePeriodSeconds: 30
      volumes:
      - emptyDir: {}
        name: dind-storage
      - emptyDir: {}
        name: plugins
      - emptyDir: {}
        name: tmp
      - configMap:
          defaultMode: 420
          name: jenkins-with-did
        name: jenkins-config
      - emptyDir: {}
        name: secrets-dir
      - emptyDir: {}
        name: plugin-dir
      - name: jenkins-home
        persistentVolumeClaim:
          claimName: jenkins-with-did
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2020-04-30T18:20:47Z"
    lastUpdateTime: "2020-04-30T18:20:47Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-04-30T06:38:40Z"
    lastUpdateTime: "2020-04-30T18:20:47Z"
    message: ReplicaSet "jenkins-with-did-5db85986b6" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 11
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

非常感谢您!

标签: dockerjenkinskubernetesyaml

解决方案


你的想法是一种有效的方法。

常规的 jenkins 镜像不提供 docker cli - 因此使用 docker 不能开箱即用。您可以构建自己的提供 docker 命令的 jenkins 映像,也可以使用包含 docker cli 的预构建 jenkins 映像,例如:https ://hub.docker.com/r/trion/jenkins-docker-client


推荐阅读