kaniko 基础

发布时间 2023-12-02 20:08:41作者: 小吉猫

kaniko 概述

kaniko是一个从Dockerfile构建容器镜像的工具,可以在容器或Kubernetes集群中使用。
kaniko不依赖于Docker守护进程,并且完全在用户空间中执行Dockerfile中的每个命令。
Kaniko是作为image运行: gcr.io/kaniko-project/executor

kaniko Build Contexts

运行 kaniko 时,使用--context前缀标志来指定构建上下文的位置.

kaniko Build Contexts 存储位置

Source Prefix Example
Local Directory dir://[path to a directory in the kaniko container] dir:///workspace
Local Tar Gz tar://[path to a .tar.gz in the kaniko container] tar:///path/to/context.tar.gz
Standard Input tar://[stdin] tar://stdin
GCS Bucket gs://[bucket name]/[path to .tar.gz] gs://kaniko-bucket/path/to/context.tar.gz
S3 Bucket s3://[bucket name]/[path to .tar.gz] s3://kaniko-bucket/path/to/context.tar.gz
Azure Blob Storage https://[account].[azureblobhostsuffix]/[container]/[path to .tar.gz] https://myaccount.blob.core.windows.net/container/path/to/context.tar.gz
Git Repository git://[repository url][#reference][#commit-id] git://github.com/acme/myproject.git#refs/heads/mybranch#<desired-commit-id>

kaniko Caching

Caching Layers

kaniko可以在远程存储库中缓存由RUN(--cache-run-layers)和COPY(--cache-copy-layers)命令创建的层。在执行命令之前,kaniko会检查该层的缓存。如果存在,kaniko将提取缓存层,而不是执行命令。如果没有,kaniko将执行该命令,然后将新创建的层推送到缓存中。
kaniko不能在缓存丢失后从缓存中读取层:一旦在缓存中没有找到一个层,所有后续层都在本地构建,而不需要咨询缓存。
用户可以通过设置--cache=true标志来启用缓存。可以通过--cache-repo标志提供用于存储缓存层的远程存储库。如果未提供此标志,则将从提供的--destination推断出缓存的repo。

Caching Base Images

Kaniko可以在本地目录中缓存图像,该目录可以卷挂载到Kaniko pod中。要做到这一点,必须首先填充缓存,因为它是只读的。
docker run -v $(pwd):/workspace gcr.io/kaniko-project/warmer:latest --cache=true --cache-dir=/workspace/cache --image=<image to cache> --image=<another image to cache>
docker run -v $(pwd):/workspace gcr.io/kaniko-project/warmer:latest --cache=true --cache-dir=/workspace/cache --dockerfile=<path to dockerfile>
docker run -v $(pwd):/workspace gcr.io/kaniko-project/warmer:latest --cache=true --cache-dir=/workspace/cache --dockerfile=<path to dockerfile> --build-arg version=1.19

kaniko 常用参数

--build-arg: 构建时传参
--cache:     启用缓存,可用值:true
--cache-dir: base image 本地缓存目录。默认/cache。需要和--cache=true一起使用。
--cache-repo:设置缓存层的远程仓库。默认在--destination值下的/cache目录。
--cache-copy-layers: 缓存复制层。
--cache-run-layers:  缓存运行层。默认为true。
--cache-ttl duration:缓存超时时间(h),默认两周。
--compressed-caching:对缓存层进行tar压缩,默认为true。这将增加构建的运行时间,但会减少内存使用量。在资源不足时启用。
--context-sub-path:  上下文子目录。
--custom-platform:   指定构建平台。可选值:linux/arm、linux/arm/v5 ...。
--dockerfile:        Dockerfile路径。默认Dockerfile。
--label:             为image设置标签。
--log-format:        设置日志格式。默认为color。
-log-timestamp:      将时间戳添加到日志。默认为false。
--no-push:           只构建image不进行推送。
--push-retry:        推送image到仓库的重试次数。
--registry-mirror:   设置image仓库地址。
--image-download-retry: 下载image重试次数。默认为0。

kaniko 使用私有git repo

使用Personal Access Tokens访问git repo。
git://TOKEN@github.com/acme/myproject.git#refs/heads/mybranch

kaniko push image到docker hub

生成docker hub账号信息

config.json

{
  "auths": {
    "https://index.docker.io/v1/": {
      "auth": "xxxxxxxxxxxxxxx"  # echo -n USER:PASSWORD | base64
    }
  }
}

push image 到 docker hub

docker run -ti --rm -v `pwd`:/workspace -v `pwd`/config.json:/kaniko/.docker/config.json:ro gcr.io/kaniko-project/executor:latest --dockerfile=Dockerfile --destination=yourimagename

kaniko 使用示例

在kubernetes中运行

apiVersion: v1
kind: Pod
metadata:
  name: kaniko
spec:
  containers:
    - name: kaniko
      image: gcr.io/kaniko-project/executor:latest
      args:
        - "--dockerfile=<path to Dockerfile within the build context>"
        - "--context=gs://<GCS bucket>/<path to .tar.gz>"
        - "--destination=<gcr.io/$PROJECT/$IMAGE:$TAG>"
      volumeMounts:
        - name: kaniko-secret
          mountPath: /secret
      env:
        - name: GOOGLE_APPLICATION_CREDENTIALS
          value: /secret/kaniko-secret.json
  restartPolicy: Never
  volumes:
    - name: kaniko-secret
      secret:
        secretName: kaniko-secret

以docker方式运行

docker run \
    -v "$HOME"/.config/gcloud:/root/.config/gcloud \
    -v /path/to/context:/workspace \
    gcr.io/kaniko-project/executor:latest \
    --dockerfile /workspace/Dockerfile \
    --destination "gcr.io/$PROJECT_ID/$IMAGE_NAME:$TAG" \
    --context dir:///workspace/

参考文档

https://github.com/GoogleContainerTools/kaniko