云原生之旅 – 12)使用 Kaniko 在 Kubernetes上构建 Docker 容器镜像

前言

前一篇文章【云原生之旅 – 11)基于 Kubernetes 动态伸缩 Jenkins Build Agents】有讲到在 Kubernetes Pod (Jenkins build agent) 里面构建 docker 容器镜像,当时我们采取了一种简单快速的方式来 run docker in docker,也就是 mount /var/run/docker.sock 到主机的 docker engine,这需要docker run在特权 privileged 模式下,有很大的安全隐患。另外这种方式还有个很大的缺陷就是当一台机器上同时运行多个docker build agent时,会出现阻塞的情况,因为这一批agent用的都是宿主机上的同一个docker engine。

所以我们今天介绍一款专门用于Kubernetes 构建容器镜像的工具
Kaniko

,它是 Google 创建的一个开源工具. 并且不需要 privileged access to the host,解决了前一种方式的缺陷。

关键词:使用Kaniko构建容器镜像,Run docker in docker, 在 Kubernetes上构建 Docker 容器镜像,无需特权在Kubernetes中构建镜像,docker privileged,更安全的方式构建容器镜像

本文同步发表于知乎

https://zhuanlan.zhihu.com/p/584805862

原理

Kaniko会先提取基础镜像(Dockerfile FROM 之后的镜像)的文件系统,然后根据Dockerfile中所描述的,一条条执行命令,每一条命令执行完以后会在用户空间下面创建一个snapshot,并与存储与内存中的上一个状态进行比对,如果有变化,就将新的修改生成一个镜像层添加在基础镜像上,并且将相关的修改信息写入镜像元数据中。等所有命令执行完,kaniko会将最终镜像推送到指定的远端镜像仓库。

准备

我们需要在Kubernetes 里创建一个 docker-registry 类型的 secret,后面会挂载到 kaniko container,这样kaniko才有权限push image到Docker Hub registry

kubectl -n jenkins create secret docker-registry dockercred \
--docker-server=https://index.docker.io/v1/ \
--docker-username= \
--docker-password=

使用 Kaniko 构建镜像

Define kaniko container in Pod Template

并且挂载刚才建的 docker-registry类型的secret

kind: Pod
spec:
  containers:  # list of containers that you want present for your build, you can define a default container in the Jenkinsfile
    - name: kaniko
      image: gcr.io/kaniko-project/executor:v1.9.0-debug # include shell
      imagePullPolicy: IfNotPresent
      command:
        - /busybox/cat
      tty: true
      resources:
        limits:
          cpu: 500m
          memory: 1024Mi
      volumeMounts:
      - name: kaniko-secret
        mountPath: /kaniko/.docker
  volumes:
  - name: kaniko-secret
    secret:
        secretName: dockercred
        items:
        - key: .dockerconfigjson
          path: config.json

Jenkins pipeline

  • –context: This is the location of the Dockerfile.

  • –destination: You need to replace Docker Hub username wadexu007 with your Docker Hub username for kaniko to push the image to the Docker Hub registry.

pipeline {
  options {
    timeout(time: 10, unit: 'MINUTES')
    buildDiscarder(logRotator(numToKeepStr: '10', daysToKeepStr: '7'))
  }

  agent {
    kubernetes {
      idleMinutes 3  // how long the pod will live after no jobs have run on it
      yamlFile 'Jenkins/kaniko-demo/build-pod.yaml'  // path to the pod definition relative to the root of our project
    }
  }
  stages {
    stage('Kaniko Build and Push Docker Image') {
      steps {
        script {
          dir('Jenkins/kaniko-demo') {
            container('kaniko') {
              sh """
                /kaniko/executor --context pwd --destination wadexu007/demo-app:1.0.5
"""
            }
          }
        }
      }
    }

  }
}

完整例子参考我的 Github Repo

本文同步发表于知乎 https://zhuanlan.zhihu.com/p/584805862

测试

创建一个Pipeline job

Repository URL = https://github.com/wadexu007/learning_by_doing

Script Path = Jenkins/kaniko-demo/Jenkinsfile

Click Build now

云原生之旅 - 12)使用 Kaniko 在 Kubernetes上构建 Docker 容器镜像

感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以打赏和推荐,您的鼓励是我创作的动力

本文同步发表于知乎 https://zhuanlan.zhihu.com/p/584805862

Learning by Doing

Original: https://www.cnblogs.com/wade-xu/p/16883616.html
Author: WadeXu
Title: 云原生之旅 – 12)使用 Kaniko 在 Kubernetes上构建 Docker 容器镜像

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/798355/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球