免费企业网站系统源码,中铁建设集团有限公司下属公司,wordpress 时区,网站建设制作设计优化目录
一、kaniko是什么
二、kaniko工作原理
三、kanijo工作在Containerd上 基于serverless的考虑#xff0c;我们选择了kaniko作为镜像打包工具#xff0c;它是google提供了一种不需要特权就可以构建的docker镜像构建工具。 一、kaniko是什么
kaniko 是一种在容器或 Kube…
目录
一、kaniko是什么
二、kaniko工作原理
三、kanijo工作在Containerd上 基于serverless的考虑我们选择了kaniko作为镜像打包工具它是google提供了一种不需要特权就可以构建的docker镜像构建工具。 一、kaniko是什么
kaniko 是一种在容器或 Kubernetes 集群内从 Dockerfile 构建容器镜像的工具。kaniko 不依赖于 Docker 守护进程而是完全在用户空间中执行 Dockerfile 中的每个命令。这使得在无法轻松或安全地运行 Docker 守护程序的环境中构建容器镜像成为可能例如标准的 Kubernetes 集群。 二、kaniko工作原理
kaniko作为一个容器镜像运行它接受三个参数一个 Dockerfile 一个构建上下文以及将镜像推送到的注册表。它在执行程序镜像中提取基本镜像的文件系统。然后在Dockerfile中执行任何命令快照用户空间中的文件系统。Kaniko在每个命令后都会将一层已更改的文件附加到基本镜像。最后执行程序将新镜像推送到指定的注册表。由于Kaniko在执行程序镜像的用户空间中完全执行了这些操作因此它完全避免了在用户计算机上需要任何特权访问。 读取并解析指定的Dockerfile提取基础镜像的文件系统Dockerfile 中的 FROM 镜像在独立的Dockerfile中分别运行每个命令每次运行后都会对用户空间文件系统的做快照每次运行时将快照层附加到基础层并更新镜像元数据最后推送镜像 三、kanijo工作在Containerd上
我们的环境中只安装了containerd.io 容器运行时没有 Docker 或者 Kubernetes 环境时我们也可以采用kaniko进行镜像构建与发布具体操作流程步骤如下
环境说明
操作系统版本
roottestmachine:/# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.5 LTS
Release: 20.04
Codename: focal
roottestmachine:/# containerd版本
roottestmachine:/# containerd -v
containerd github.com/containerd/containerd v1.7.0 1fbd70374134b891f97ce19c70b6e50c7b9f4e0d
roottestmachine:/# ctr版本
roottestmachine:/# ctr -v
ctr github.com/containerd/containerd v1.7.0
roottestmachine:/#
1.提前拉取kaniko-executor镜像
可以提前拉取 kaniko-executor:latest 镜像以加快构建速度此处将镜像拉到默认的名称空间下
官方镜像特殊原因国内无法访问可以直接访问大神阿里云的镜像仓库拉取
拉取镜像
ctr -n default images pull registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest 查看本地镜像
ctr image list | grep kaniko-executor
roottestmachine:/# ctr image list | grep kaniko-executor
registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest application/vnd.docker.distribution.manifest.v2json sha256:5aacba9599e8e112279e6e316f1164e584e9b59e5f159b275c6b482e0913c13e 24.6 MiB linux/amd64 -
roottestmachine:/#
2.准备镜像仓库认证所需的凭据
需要推送的阿里云容器镜像仓库的账号以及密码可以按照下述的流程进行生成config.json文件。
在linux环境运行下面命令得到账号密码是base64编码字符串
echo -n username:password | base64 注意下述为格式为 你的容器镜像仓库账号:你的容器镜像密码
oottestmachine:/# echo -n username:password | base64
dXNlcm5hbWU6cGFzc3dvcmQ
roottestmachine:/#
这里就是BASE64 编码dXNlcm5hbWU6cGFzc3dvcmQ
生成认证所需的凭据
cat config.json EOF
{auths: {https://index.docker.io/v1/: {auth: BASE64编码}}
}
EOF
config.json文件内容 cat config.json
{ auths: {registry.cn-hangzhou.aliyuncs.com: {auth: dXNlcm5hbWU6cGFzc3dvcmQ}}}
3.准备dockerfile文件
tee dockerfile EOF
FROM docker.io/library/busybox:1.35.0
LABEL MAINTAINERAndy BUILDTOOLSkaniko BUILDENVcontainerd.io;
ENTRYPOINT [/bin/sh, -c, echo hello,busybox]
EOF
dockerfile文件内容
FROM docker.io/library/busybox:1.35.0
LABEL MAINTAINERAndy BUILDTOOLSkaniko BUILDENVcontainerd.io;
ENTRYPOINT [/bin/sh, -c, echo hello,busybox]
工作目录和所有涉及到文件
[roottestmachine /]# tree 5 /data/data
└── kaniko├── config│ └── config.json└── demo1└── dockerfile[roottestmachine /]#
4.ctr运行容器构建镜像
执行containerd.io提供的ctr客户端工具直接创建容器例如如下命令:构建busybox镜像并推送
ctr -n default run --rm --net-host --env DOCKERHUBdocker.io \
--mount typebind,src/data/kaniko/config,dst/kaniko/.docker,optionsrbind:ro \
--mount typebind,src/data/kaniko/demo1,dst/workspace,optionsrbind:rw \
registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest kaniko-executor \
/kaniko/executor --dockerfile/workspace/dockerfile --contextdir://workspace \
--destinationregistry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999
运行结果
[roottestmachine /]#
[roottestmachine /]# ctr -n default run --rm --net-host --env DOCKERHUBdocker.io \--mount typebind,src/data/kaniko/config,dst/kaniko/.docker,optionsrbind:ro \--mount typebind,src/data/kaniko/demo1,dst/workspace,optionsrbind:rw \registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest kaniko-executor \/kaniko/executor --dockerfile/workspace/dockerfile --contextdir://workspace \--destinationregistry.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 MAINTAINERAndy BUILDTOOLSkaniko BUILDENVcontainerd.io;
INFO[0013] Applying label MAINTAINERAndy
INFO[0013] Applying label BUILDTOOLSkaniko
INFO[0013] Applying label BUILDENVcontainerd.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/busyboxsha256:557cc2302c0ae328d99ca9f7ed8c96928034a5f94a7432f95dbc41a9740a123f
[roottestmachine /]# 上述参数定义
-n 指定名称空间 --rm 在退出容器时删除容器 --net-host 使用主机网络 --env 指定容器内部shell变量 --mount 指定挂载到容器内部的本地文件src是指定宿主机上文件目录路径而dst是指定容器内部目录
--dockerfile 指定Dockerfile
--context 定义位置获取编排位置即上下文
--destination 远端镜像仓库
--insecuretrue 仓库为私有http仓库
--skip-tls-verifytrue 跳过tls验证
在阿里云容器镜像站上查看
https://cr.console.aliyun.com/repository/cn-hangzhou 5.测试
1.从仓库拉取刚刚构建好镜像
ctr -n default images pull --user 仓库账号:仓库密码 registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999
运行结果
[rootlocalhost /]# 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
[rootlocalhost /]# 2.查看本地镜像
ctr image list | grep busybox
运行结果
[roottestmachine/]#
[roottestmachine/]# ctr image list | grep busybox
registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999 application/vnd.docker.distribution.manifest.v2json sha256:557cc2302c0ae328d99ca9f7ed8c96928034a5f94a7432f95dbc41a9740a123f 2.1 MiB linux/amd64 -
[roottestmachine/]# 3.运行容器查看输出结果 (--rm运行后删除容器)
ctr -n default run --rm registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999 busybox
运行结果
[roottestmachine /]#
[roottestmachine /]# ctr -n default run --rm registry.cn-hangzhou.aliyuncs.com/gmaaa123/busybox:1.999 busybox
hello,busybox
[roottestmachine /]#
到此结束在containerd.io 环境中进行镜像构建并发布到镜像仓库中实战成功!
kaniko官方github页面 https://github.com/GoogleContainerTools/kaniko