Kaniko容器化构建调研

简介

Github 地址:https://github.com/GoogleContainerTools/kaniko

Kaniko 是谷歌开源的一款构建容器镜像的工具。Kaniko 并不依赖于 Docker 守护进程,完全在用户空间根据 Dockerfile 的内容逐行执行命令来构建镜像,这就使得在一些无法获取 Docker 守护进程的环境下也能够构建镜像。

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

获取 Kaniko 镜像

# 通过代理拉取镜像
docker pull --platform linux/amd64 m.daocloud.io/gcr.io/kaniko-project/executor:latest

# 修改 tag
docker tag m.daocloud.io/gcr.io/kaniko-project/executor:latest your-registry.example.com/kaniko-executor:latest

# 推送镜像
docker push your-registry.example.com/kaniko-executor:latest

--platform linux/amd64:实现在 Mac 上 pull amd64 的镜像。pull 镜像时会默认匹配当前架构,因此若不设置该参数,则会自动匹配下载 arm64 镜像,在 amd64 上运行时会遇见报错:exec /kaniko/executor: exec format error

Kaniko Demo

demo 项目

在 GitLab 上新建一个 demo 项目,仅添加一个 Dockerfile 即可:

FROM ubuntu:20.04
LABEL maintainer="yourname@example.com"
RUN apt-get update && apt-get install -y nginx

验证点:

Docker 环境测试

生成推送镜像的凭证:

export AUTH=$(echo -n YOUR_USERNAME:YOUR_PASSWORD | base64)

cat > config.json <<-EOF
{
    "auths": {
        "https://your-registry.example.com": {
            "auth": "${AUTH}"
        }
    }
}
EOF

执行构建:

docker run \
  --interactive \
  -v `pwd`/config.json:/kaniko/.docker/config.json \
  m.daocloud.io/gcr.io/kaniko-project/executor:latest \
  --context git://gitlab.example.com/user/kaniko-build-test \
  --dockerfile Dockerfile \
  --destination=your-registry.example.com/ops/kaniko-demo:v2

参数说明:

K8s 集群环境测试

生成推送镜像凭证:

kubectl create secret docker-registry registry-secret \
  --docker-server=your-registry.example.com \
  --docker-username=<YOUR_USERNAME> \
  --docker-password=<YOUR_PASSWORD> \
  --docker-email=<YOUR_EMAIL> \
  -n <YOUR_NAMESPACE>

参数说明:

构建 Pod 的 yaml:

apiVersion: v1
kind: Pod
metadata:
  name: kaniko-builder
  namespace: default
spec:
  containers:
    - name: kaniko
      image: m.daocloud.io/gcr.io/kaniko-project/executor:latest
      args:
        - "--dockerfile=Dockerfile"
        - "--context=git://gitlab.example.com/user/kaniko-build-test"
        - "--destination=your-registry.example.com/ops/kaniko-demo:v3"
      volumeMounts:
        - name: kaniko-secret
          mountPath: "/kaniko/.docker"
  volumes:
    - name: kaniko-secret
      secret:
        secretName: registry-secret
        items:
          - key: .dockerconfigjson
            path: config.json
  restartPolicy: Never

执行构建:

kubectl apply -f kaniko-test.yaml

查看运行日志:

kubectl logs pod/kaniko-builder -n default

INFO[0020] Taking snapshot of full filesystem...
INFO[0022] Pushing image to your-registry.example.com/ops/kaniko-demo:v3
INFO[0032] Pushed your-registry.example.com/ops/kaniko-demo@sha256:baf97628a3981542e85db343f77c9e0dba5f666c868c2c6671290f7bfdc0d207

构建完成后 Pod 状态变为 Completed:

kubectl get pod -n default
NAME             READY   STATUS      RESTARTS   AGE
kaniko-builder   0/1     Completed   0          2m22s