目录
一、kaniko是什么
二、kaniko工作原理
三、kanijo工作在Containerd上
基于serverless的考虑,我们选择了kaniko作为镜像打包工具,它是google提供了一种不需要特权就可以构建的docker镜像构建工具。
kaniko 是一种在容器或 Kubernetes 集群内从 Dockerfile 构建容器镜像的工具。kaniko 不依赖于 Docker 守护进程,而是完全在用户空间中执行 Dockerfile 中的每个命令。这使得在无法轻松或安全地运行 Docker 守护程序的环境中构建容器镜像成为可能,例如标准的 Kubernetes 集群。
kaniko作为一个容器镜像运行,它接受三个参数:一个 Dockerfile ,一个构建上下文以及将镜像推送到的注册表。它在执行程序镜像中提取基本镜像的文件系统。然后,在Dockerfile中执行任何命令,快照用户空间中的文件系统。Kaniko在每个命令后都会将一层已更改的文件附加到基本镜像。最后,执行程序将新镜像推送到指定的注册表。由于Kaniko在执行程序镜像的用户空间中完全执行了这些操作,因此它完全避免了在用户计算机上需要任何特权访问。
我们的环境中只安装了containerd.io 容器运行时没有 Docker 或者 Kubernetes 环境时,我们也可以采用kaniko进行镜像构建与发布,具体操作流程步骤如下:
环境说明
操作系统版本:
root@testmachine:/# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.5 LTS
Release: 20.04
Codename: focal
root@testmachine:/#
containerd版本:
root@testmachine:/# containerd -v
containerd github.com/containerd/containerd v1.7.0 1fbd70374134b891f97ce19c70b6e50c7b9f4e0d
root@testmachine:/#
ctr版本:
root@testmachine:/# ctr -v
ctr github.com/containerd/containerd v1.7.0
root@testmachine:/#
可以提前拉取 kaniko-executor:latest 镜像以加快构建速度,此处将镜像拉到默认的名称空间下
官方镜像特殊原因国内无法访问,可以直接访问大神阿里云的镜像仓库拉取
拉取镜像
ctr -n default images pull registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
查看本地镜像
ctr image list | grep "kaniko-executor"
root@testmachine:/# ctr image list | grep "kaniko-executor"
registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest application/vnd.docker.distribution.manifest.v2+json sha256:5aacba9599e8e112279e6e316f1164e584e9b59e5f159b275c6b482e0913c13e 24.6 MiB linux/amd64 -
root@testmachine:/#
需要推送的阿里云容器镜像仓库的账号以及密码,可以按照下述的流程进行生成config.json文件。
在linux环境运行下面命令得到账号密码是base64编码字符串
echo -n "username:password" | base64
注意下述为格式为 你的容器镜像仓库账号:你的容器镜像密码
oot@testmachine:/# echo -n "username:password" | base64
dXNlcm5hbWU6cGFzc3dvcmQ=
root@testmachine:/#
这里就是BASE64 编码:dXNlcm5hbWU6cGFzc3dvcmQ=
生成认证所需的凭据
cat > config.json <
config.json文件内容:
cat config.json
{
"auths": {
"registry.cn-hangzhou.aliyuncs.com": {
"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="
}
}
}
tee dockerfile <<'EOF'
FROM docker.io/library/busybox:1.35.0
LABEL MAINTAINER=Andy BUILDTOOLS=kaniko BUILDENV=containerd.io;
ENTRYPOINT ["/bin/sh", "-c", "echo hello,busybox"]
EOF
dockerfile文件内容
FROM docker.io/library/busybox:1.35.0
LABEL MAINTAINER=Andy BUILDTOOLS=kaniko BUILDENV=containerd.io;
ENTRYPOINT ["/bin/sh", "-c", "echo hello,busybox"]
[root@testmachine /]# tree 5 /data
/data
└── kaniko
├── config
│ └── config.json
└── demo1
└── dockerfile
[root@testmachine /]#
执行containerd.io提供的ctr客户端工具直接创建容器,例如如下命令:构建busybox镜像并推送
ctr -n default run --rm --net-host --env DOCKERHUB=docker.io \
--mount type=bind,src=/data/kaniko/config,dst=/kaniko/.docker,options=rbind:ro \
--mount type=bind,src=/data/kaniko/demo1,dst=/workspace,options=rbind:rw \
registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest kaniko-executor \
/kaniko/executor --dockerfile=/workspace/dockerfile --context=dir://workspace \
--destination=registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999
运行结果
[root@testmachine /]#
[root@testmachine /]# ctr -n default run --rm --net-host --env DOCKERHUB=docker.io \
> --mount type=bind,src=/data/kaniko/config,dst=/kaniko/.docker,options=rbind:ro \
> --mount type=bind,src=/data/kaniko/demo1,dst=/workspace,options=rbind:rw \
> registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest kaniko-executor \
> /kaniko/executor --dockerfile=/workspace/dockerfile --context=dir://workspace \
> --destination=registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999
INFO[0001] Retrieving image manifest docker.io/library/busybox:1.35.0
INFO[0001] Retrieving image docker.io/library/busybox:1.35.0 from registry index.docker.io
INFO[0013] Built cross stage deps: map[]
INFO[0013] Retrieving image manifest docker.io/library/busybox:1.35.0
INFO[0013] Returning cached image manifest
INFO[0013] Executing 0 build triggers
INFO[0013] Building stage 'docker.io/library/busybox:1.35.0' [idx: '0', base-idx: '-1']
INFO[0013] Skipping unpacking as no commands require it.
INFO[0013] LABEL MAINTAINER=Andy BUILDTOOLS=kaniko BUILDENV=containerd.io;
INFO[0013] Applying label MAINTAINER=Andy
INFO[0013] Applying label BUILDTOOLS=kaniko
INFO[0013] Applying label BUILDENV=containerd.io;
INFO[0013] ENTRYPOINT ["/bin/sh", "-c", "echo hello,busybox"]
INFO[0013] Pushing image to registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999
INFO[0015] Pushed registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox@sha256:557cc2302c0ae328d99ca9f7ed8c96928034a5f94a7432f95dbc41a9740a123f
[root@testmachine /]#
上述参数定义:
-n 指定名称空间
--rm 在退出容器时删除容器
--net-host 使用主机网络
--env 指定容器内部shell变量
--mount 指定挂载到容器内部的本地文件,src是指定宿主机上文件目录路径,而dst是指定容器内部目录
--dockerfile 指定Dockerfile
--context 定义位置获取编排位置,即上下文
--destination 远端镜像仓库
--insecure=true 仓库为私有http仓库
--skip-tls-verify=true 跳过tls验证
在阿里云容器镜像站上查看
https://cr.console.aliyun.com/repository/cn-hangzhou
1.从仓库拉取刚刚构建好镜像
ctr -n default images pull --user 仓库账号:仓库密码 registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999
运行结果
[root@localhost /]# ctr -n default images pull --user 仓库账号:仓库密码 registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999
registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999: resolved |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:557cc2302c0ae328d99ca9f7ed8c96928034a5f94a7432f95dbc41a9740a123f: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:ff69b8070e65ef902da17038ea9821d3bfaf74cd2afcf0ceb1b2df365930cecb: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:db2e1e3b46c0af1ae524f68073dccd02b5b10a0388a7b3a3f1617ee996376c34: done |++++++++++++++++++++++++++++++++++++++|
elapsed: 18.7s total: 2.1 Mi (115.6 KiB/s)
unpacking linux/amd64 sha256:557cc2302c0ae328d99ca9f7ed8c96928034a5f94a7432f95dbc41a9740a123f...
done: 128.981892ms
[root@localhost /]#
2.查看本地镜像
ctr image list | grep "busybox"
运行结果
[root@testmachine/]#
[root@testmachine/]# ctr image list | grep "busybox"
registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999 application/vnd.docker.distribution.manifest.v2+json sha256:557cc2302c0ae328d99ca9f7ed8c96928034a5f94a7432f95dbc41a9740a123f 2.1 MiB linux/amd64 -
[root@testmachine/]#
3.运行容器,查看输出结果 (--rm运行后删除容器)
ctr -n default run --rm registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999 busybox
运行结果
[root@testmachine /]#
[root@testmachine /]# ctr -n default run --rm registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999 busybox
hello,busybox
[root@testmachine /]#
到此结束,在containerd.io 环境中,进行镜像构建并发布到镜像仓库中实战成功!
kaniko官方github页面 https://github.com/GoogleContainerTools/kaniko