关注「WeiyiGeek」公众号

设为「特别关注」每天带你玩转网络安全运维、应用开发、物联网IOT学习!

希望各位看友【关注、点赞、评论、收藏、投币】,助力每一个梦想。


本章目录


首发地址: https://mp.weixin.qq.com/s/wchtH6i0xKrIrqSuYKmWkg

原文地址: https://blog.weiyigeek.top/2022/9-1-684.html

0x00 前言简述

快速介绍

什么是Kaniko?

kaniko 是一个在容器或 Kubernetes 集群内从 Dockerfile 构建容器镜像的工具 ( Build Container Images In Kubernetes )。

温馨提示: kaniko不是谷歌官方发布支持的产品.

为啥用Kaniko?

由于 kaniko 不依赖于 Docker 守护进程,并且完全在用户空间中执行 Dockerfile 中的每个命令,这使得能够在轻松或安全地运行在无Docker守护程序的环境(如标准Kubernetes集群 V1.24.x)中构建容器映像。

在 Kubernetes V1.24.x 版本之后默认采用 containerd.io 作为缺省的cri,不在支持 docker-shim 意味着我们不需要安装 docker 环境

Kaniko 是如何工作的?

kaniko 执行器镜像负责从 Dockerfile 构建镜像并将其推送到注册表,其流程大致如下:

  • 首先在执行者图像中,我们提取基础镜像的文件系统(Dockerfile 中的 FROM 镜像)。
  • 其次我们执行 Dockerfile 中的命令,之后在用户空间中对文件系统进行快照每一个。
  • 然后在每个命令之后,我们将一层更改的文件附加到基础 图像(如果有)并更新图像元数据。

Kaniko 已知功能问题

  • kaniko 不支持构建 Windows 容器。
  • kaniko 不支持 v1 Registry API。(由于其不安全性当前基本都是使用V2协议, 例如 Harbor)
  • kaniko 不支持在除官方kaniko映像之外的任何Docker映像中运行二进制Kanico可执行文件(即YMMV)。

kaniko 构建上下文

描述: kaniko 的构建上下文与您发送 Docker 守护程序以进行映像构建的构建上下文非常相似;它代表一个包含 Dockerfile 的目录,kaniko 将使用它来构建您的图像。

例如, Dockerfile 中的 COPY 命令应该引用构建上下文中的文件, 所以您需要将构建上下文存储在 kaniko 可以访问的位置。

目前kaniko 支持以下存储解决方案:

  • GCS Bucket
  • S3 Bucket
  • Azure Blob Storage
  • Local Directory
  • Local Tar
  • Standard Input
  • Git Repository

运行 kaniko 时,使用 --context 带有适当前缀的标志指定构建上下文的位置, 如果您不指定前缀 kaniko 将假定一个本地目录, 该参数可用值:

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#

例如,要使用名为 kaniko-bucket 的 GCS 存储桶,您需要传入 --context=gs://kaniko-bucket/path/to/context.tar.gz

温馨提示:kaniko 允许的唯一标准输入是 .tar.gz 格式, 如果要创建压缩 tar,您可以运行 tar -C <path to build context> -zcvf context.tar.gz .命令。

  1. $ ls cache/
  2. Dockerfile
  3. # 压缩上下文目录
  4. $ tar -C cache/ -zcvf context.tar.gz .
  5. ./
  6. ./Dockerfile
  7. # 查看压缩文件
  8. $ tar -ztvf context.tar.gz
  9. drwxr-xr-x root/root 0 2022-09-08 23:03 ./
  10. -rw-r--r-- root/root 52 2022-09-08 23:04 ./Dockerfile

kaniko 缓存构建

  • Caching Layers :kaniko 可以在远程存储库中缓存由RUN(由flag--cache-RUN-layers配置)和COPY(由flag--cache-COPY-layeers配置)命令创建的层。

    在执行命令之前 kaniko 会检查层的缓存,如果存在 kaniko将拉取并提取缓存层,而不是执行命令。如果没有 kaniko将执行命令,然后将新创建的层推送到缓存。

    用户可以通过设置 --cache=true 标志选择缓存,并且可以通过--cache-repo 标志提供用于存储缓存层的远程存储库, 如果未提供此标志则将从提供的--destination推断缓存的repo。

    温馨提示: 在缓存未命中后,kaniko无法从缓存中找到读取层,所有后续层都将在本地构建,而无需咨询缓存。

  • Caching Base Images: kaniko 可以将图像缓存在本地目录中,该目录可以卷装载到KanikoPod中。为此必须首先填充缓存, 我们在 gcr.io/kaniko-project/warmer 提供了一个kaniko缓存预热映像:

    --image : 指定所需任意数量的图像, 填充缓存后 使用与上述相同的 --cache=true 标志选择缓存, 本地缓存的位置通过 --cache-dir 标志提供,默认为 /cache 与缓存预热器一样, 在实践中通常与 Kubernetes 集群和持久缓存卷一起使用。

    示例: docker run -v $(pwd):/workspace gcr.io/kaniko-project/warmer:latest --cache-dir=/workspace/cache --image=<image to cache> --image=<another image to cache>


0x01 部署使用

环境依赖

  • kaniko 图像镜像
  • 一个 Kubernetes 集群 或者 一个 Containerd 容器运行环境。
  • 一个 dockerhub 帐户 用于将构建的图像公开。

小试牛刀之在Kubernetes集群中构建并发布镜像

描述: 此处我们准备在一个K8S集群中使用kaniko提供的镜像,按照提供的Dockerfile指令进行镜像构建,并上传到 docker hub 仓库中,以下为操作流程、

操作流程

步骤 01.首先, 为了加快构建速度, 我们提前在集群中拉取 gcr.io/kaniko-project/executor 镜像到本地, 由于国内无法直接拉取此处我采用这篇【使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建】 文章中的方法进行拉取构建国外gcr.io仓库中的镜像。

  1. # 此处我已经创建了国内可以访问拉取的 executor 镜像, 不想在Aliyun容器镜像服务中进行创建拉取的朋友可以直接使用如下仓库地址。
  2. registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
  3. # 使用 ctr 或者 crictl 进行镜像拉取
  4. $ crictl pull registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
  5. $ crictl images | grep "kaniko-executor"
  6. registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor latest da9592dbe1de3 25.8MB

步骤 02.准备一个 Dockerfile 此处将 registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor 镜像打包上传到hub中作为演示。

  1. # 创建存放dockerfile目录以及持久化缓存目录
  2. mkdir -vp /storage/dev/soft/kaniko/{cache,demo}
  3. cd /storage/dev/soft/kaniko/demo
  4. tee dockerfile <<'EOF'
  5. FROM registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
  6. LABEL MAINTAINER=master@weiyigeeek.top BUILDTYPE=kaniko
  7. EOF

步骤 03.创建一个授权令牌的 Secret , 此处以公共的docker hub为例。

  1. # 语法:
  2. ~$ kubectl create secret docker-registry dockerhub --docker-server=<your-registry-server> --docker-username=<your-name> --docker-password=<your-pword> --docker-email=<your-email>
  3. # 参数值:
  4. # <your-registry-server> is your Private Docker Registry FQDN. (https://index.docker.io/v1/ for DockerHub)
  5. # <your-name> is your Docker username.
  6. # <your-pword> is your Docker password.
  7. # <your-email> is your Docker email.
  8. # 创建示例: 此 docker-registry 将在 pod.yaml 配置中使用
  9. ~$ kubectl create secret docker-registry dockerhub \
  10. --docker-server=https://index.docker.io/v1/ \
  11. --docker-username=weiyigeek \
  12. --docker-password=PASSWORD \
  13. --docker-email=master@weiyigeek.top
  14. # secret/dockerhub created
  15. # 查看创建的 secrets 情况
  16. ~$ kubectl get secrets dockerhub
  17. NAME TYPE DATA AGE
  18. dockerhub kubernetes.io/dockerconfigjson 1 16s
  19. ~$ kubectl get secrets dockerhub -o yaml
  20. apiVersion: v1
  21. data:
  22. .dockerconfigjson: eyJhdXRo*******VhsbE1qQXhPUT09In19fQ==
  23. kind: Secret
  24. metadata:
  25. name: dockerhub

步骤 04.创建一个在k8s集群中运行的Pod,其资源清单如下所示:

  1. tee kaniko.yaml <<'EOF'
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: kaniko
  6. spec:
  7. containers:
  8. - name: kaniko
  9. image: registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
  10. env:
  11. - name: DOCKERHUB
  12. value: "docker.io"
  13. - name: AUTHOR
  14. value: "weiyigeek"
  15. - name: IMAGE_NAME
  16. value: "kaniko-executor"
  17. - name: IMAGE_VERSION
  18. value: "v1.9.0"
  19. args: [ "--dockerfile=/workspace/dockerfile",
  20. "--context=dir://workspace",
  21. "--destination=docker.io/weiyigeek/kaniko-executor:v1.9.0",
  22. "--cache",
  23. "--cache-dir=/cache"]
  24. volumeMounts:
  25. - name: kaniko-secret
  26. mountPath: /kaniko/.docker
  27. - name: dockerfile-storage
  28. mountPath: /workspace
  29. - name: kaniko-cache
  30. mountPath: /cache
  31. restartPolicy: Never
  32. nodeSelector:
  33. kubernetes.io/hostname: "weiyigeek-226"
  34. volumes:
  35. - name: kaniko-secret
  36. secret:
  37. secretName: dockerhub
  38. items:
  39. - key: .dockerconfigjson
  40. path: config.json
  41. - name: dockerfile-storage
  42. hostPath:
  43. path: /storage/dev/soft/kaniko/demo
  44. type: DirectoryOrCreate
  45. - name: kaniko-cache
  46. hostPath:
  47. path: /storage/dev/soft/kaniko/cache
  48. type: DirectoryOrCreate
  49. EOF
  50. # args 参数说明
  51. --dockerfile=/workspace/dockerfile # 指定 dockerfile 路径
  52. --context=dir://workspace # 指定构建上下文
  53. --destination=docker.io/weiyigeek/kaniko-executor:v1.9.0 # 指定生成镜像的tag
  54. --cache # 使用缓存
  55. --cache-dir # 指定缓存目录

温馨提示: kaniko 中的二进制可执行文件 executor 支持的参数详解(https://github.com/GoogleContainerTools/kaniko/#additional-flags)

步骤 05.执行 kubectl apply 部署资源清单生成运行 pod , 此处通过 kubectl logs 日志命令可以发现kaniko执行镜像构建,以及上传镜像到docker hub之中

  1. kubectl apply -f kaniko.yaml
  2. # pod/kaniko created
  3. kubectl logs -f kaniko
  4. # INFO[0005] Retrieving image manifest registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
  5. # INFO[0005] Retrieving image registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest from registry registry.cn-hangzhou.aliyuncs.com
  6. # INFO[0006] Built cross stage deps: map[]
  7. # INFO[0006] Retrieving image manifest registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
  8. # INFO[0006] Returning cached image manifest
  9. # INFO[0006] Executing 0 build triggers
  10. # INFO[0006] Building stage 'registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest' [idx: '0', base-idx: '-1']
  11. # INFO[0006] Skipping unpacking as no commands require it.
  12. # INFO[0006] LABEL MAINTAINER=master@weiyigeeek.top BUILDTYPE=kaniko
  13. # INFO[0006] Applying label MAINTAINER=master@weiyigeeek.top
  14. # INFO[0006] Applying label BUILDTYPE=kaniko
  15. # INFO[0006] Pushing image to docker.io/weiyigeek/kaniko-executor:v1.9.0

步骤 06.在客户端中可以使用 docker 或者 ctr 、crictl 命令将上传到hub中的镜像进行拉取, 并且查看hub仓库中的 kaniko-executor:v1.9.0 镜像信息(https://hub.docker.com/r/weiyigeek/kaniko-executor)。

  1. docker pull weiyigeek/kaniko-executor:v1.9.0
  2. 9d4299bbd943: Already exists
  3. ..............
  4. a8dae3110e38: Already exists
  5. v1.9.0: Pulling from weiyigeek/kaniko-executor
  6. Digest: sha256:9b0ef02e7650d00d24bbca683e317bc103d6d842311ff13ec6daee60c37b1e62
  7. Status: Downloaded newer image for weiyigeek/kaniko-executor:v1.9.0
  8. docker.io/weiyigeek/kaniko-executor:v1.9.0

步骤 07.扩展补充,除了上述方式指定dockerfile文件和上下文外,我们还可以在运行 kaniko 时使用标准输入构建上下文,但需要添加 -i, --interactive参数, 一旦kaniko运行它将从STDIN获取数据,并将构建上下文创建为压缩tar,然后它将在启动映像构建之前解包构建上下文的压缩tar。

如何使用 .tar.gz 标准输入数据交互运行 kaniko 的完整示例,使用带有临时容器和完全无 docker 环境的 Kubernetes 命令行来进行镜像构建与发布:

  1. echo -e 'FROM alpine \nRUN echo "created from standard input"' > Dockerfile | tar -cf - Dockerfile | gzip -9 | kubectl run kaniko-executor \
  2. --rm --stdin=true \
  3. --image=registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest --restart=Never \
  4. --overrides='{
  5. "apiVersion": "v1",
  6. "spec": {
  7. "containers": [
  8. {
  9. "name": "kaniko-executor",
  10. "image": "registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest",
  11. "stdin": true,
  12. "stdinOnce": true,
  13. "args": [
  14. "--dockerfile=Dockerfile",
  15. "--context=tar://stdin",
  16. "--destination=docker.io/weiyigeek/alpine:v4.2"
  17. ],
  18. "volumeMounts": [
  19. {
  20. "name": "kaniko-secret",
  21. "mountPath": "/kaniko/.docker/"
  22. }
  23. ]
  24. }
  25. ],
  26. "nodeSelector": {
  27. "kubernetes.io/hostname": "weiyigeek-226"
  28. },
  29. "volumes": [
  30. {
  31. "name": "kaniko-secret",
  32. "secret": {
  33. "secretName": "dockerhub",
  34. "items": [{"key":".dockerconfigjson", "path": "config.json"}]
  35. }
  36. },
  37. {
  38. "name": "dockerfile-storage",
  39. "hostPath": {
  40. "path": "/storage/dev/soft/kaniko/demo",
  41. "type": "DirectoryOrCreate"
  42. }
  43. },
  44. {
  45. "name": "kaniko-cache",
  46. "hostPath": {
  47. "path": "/storage/dev/soft/kaniko/cache",
  48. "type": "DirectoryOrCreate"
  49. }
  50. }
  51. ]
  52. }
  53. }'

执行结果:

  1. INFO[0003] Retrieving image manifest alpine
  2. INFO[0003] Retrieving image alpine from registry index.docker.io
  3. INFO[0009] Built cross stage deps: map[]
  4. INFO[0009] Retrieving image manifest alpine
  5. INFO[0009] Returning cached image manifest
  6. INFO[0009] Executing 0 build triggers
  7. INFO[0009] Building stage 'alpine' [idx: '0', base-idx: '-1']
  8. INFO[0009] Unpacking rootfs as cmd RUN echo "created from standard input" requires it.
  9. INFO[0036] RUN echo "created from standard input"
  10. INFO[0036] Initializing snapshotter ...
  11. INFO[0036] Taking snapshot of full filesystem...
  12. INFO[0036] Cmd: /bin/sh
  13. INFO[0036] Args: [-c echo "created from standard input"]
  14. INFO[0036] Running: [/bin/sh -c echo "created from standard input"]
  15. created from standard input
  16. INFO[0036] Taking snapshot of full filesystem...
  17. INFO[0037] No files were changed, appending empty layer to config. No layer added to image.
  18. INFO[0037] Pushing image to docker.io/weiyigeek/alpine:v4.2
  19. INFO[0042] Pushed index.docker.io/weiyigeek/alpine@sha256:0ef53bcc0a6f261124e5f292fa17041d7e5f81f5542802b89c249351597167e4
  20. pod "kaniko-executor" deleted

至此在 K8s 集群中使用 kaniko 构建镜像简单演示结束。

小试牛刀之在Containerd.io中构建并发布镜像

描述:当我们的环境中只安装了containerd.io 容器运行时没有 Docker 或者 Kubernetes 环境时,我们也可以采用kaniko进行镜像构建与发布,具体操作流程步骤如下:

环境说明

  1. $ lsb_release -a
  2. Distributor ID: Ubuntu
  3. Description: Ubuntu 20.04.3 LTS
  4. Release: 20.04
  5. Codename: focal
  6. $ containerd -v
  7. containerd containerd.io 1.4.12 7b11cfaabd73bb80907dd23182b9347b4245eb5d
  8. $ ctr -v
  9. ctr containerd.io 1.4.12

温馨提示: 此处使用的是 Ubuntu 20.04 操作系统, 该系统已做安全加固和内核优化符合等保2.0要求【SecOpsDev/Ubuntu-InitializeSecurity.sh at master · WeiyiGeek/SecOpsDev 】, 如你的Linux未进行相应配置环境可能与读者有些许差异, 如需要进行(windows server、Ubuntu、CentOS)安全加固请参照如下加固脚本进行加固, 请大家疯狂的star 。

加固脚本地址:【 https://github.com/WeiyiGeek/SecOpsDev/blob/master/OS-操作系统/Linux/Ubuntu/Ubuntu-InitializeSecurity.sh

温馨提示:如果你使用的是最新 Ubuntu 22.04 操作系统,并需要对其安全加固和内核优化以满足等保2.0要求可参考如下加固脚本 【https://github.com/WeiyiGeek/SecOpsDev/tree/master/OperatingSystem/Security/Ubuntu】。

操作流程

步骤 01.此处假设你已经安装配置好containerd.io了,如果没有安装配置请参考此篇文章【 1.Containerd容器运行时初识与尝试 - https://blog.weiyigeek.top/2021/6-27-570.html 】,此处不再累述。

步骤 02.验证 containerd.io 服务状态以及提前拉取 kaniko-executor:latest 镜像以加快构建速度,此处将镜像拉到默认的名称空间下。

  1. $ systemctl status containerd.service
  2. containerd.service - containerd container runtime
  3. Loaded: loaded (/lib/systemd/system/containerd.service; enabled; vendor preset: enabled)
  4. Active: active (running) since Thu 2022-09-08 11:48:30 CST; 4h 49min ago
  5. Docs: https://containerd.io
  6. Process: 561811 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
  7. Main PID: 561812 (containerd)
  8. Tasks: 106
  9. Memory: 4.0G
  10. $ ctr -n default images pull registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest

步骤 03.准备登录 hub docker 的账号以及密码,你可以按照下述的流程进行生成config.json文件。

  1. mkdir /storage/dev/soft/kaniko/{config,demo1}
  2. cd /storage/dev/soft/kaniko/config
  3. # 生成认证所需的凭据
  4. # BASE64 编码,注意下述为格式为 你的hub账号:你的hub密码
  5. AUTH=$(echo -n "weiyigeek:password" | base64)
  6. # BASE64 解码
  7. echo ${AUTH} | base64 -d
  8. # 使用该方法可以解析变量 AUTH (值得注意)
  9. cat > config.json <<EOF
  10. {
  11. "auths": {
  12. "https://index.docker.io/v1/": {
  13. "auth": "${AUTH}"
  14. }
  15. }
  16. }
  17. EOF
  18. # 生成结果
  19. cat config.json
  20. {
  21. "auths": {
  22. "https://index.docker.io/v1/": {
  23. "auth": "d2VpeWlnZ************AxOQ=="
  24. }
  25. }
  26. }

步骤 04.准备dockerfile文件,此处将busybox:1.35.0镜像重新构建后上传到我的账户下的hub仓库中,该文件示例如下:

  1. cd /storage/dev/soft/kaniko/demo1
  2. tee dockerfile <<'EOF'
  3. FROM docker.io/library/busybox:1.35.0
  4. LABEL MAINTAINER=master@weiyigeeek.top BUILDTOOLS=kaniko BUILDENV=containerd.io;
  5. ENTRYPOINT ["/bin/sh", "-c", "echo hello,busybox"]
  6. EOF

步骤 05.当上述都准备完成后我们便可以执行containerd.io提供的ctr客户端工具直接创建容器,例如如下命令:

  1. ctr -n default run --rm --net-host --env DOCKERHUB=docker.io \
  2. --mount type=bind,src=/storage/dev/soft/kaniko/config,dst=/kaniko/.docker,options=rbind:ro \
  3. --mount type=bind,src=/storage/dev/soft/kaniko/demo1,dst=/workspace,options=rbind:rw \
  4. registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest kaniko-executor \
  5. /kaniko/executor --dockerfile=/workspace/dockerfile --context=dir://workspace --destination=docker.io/weiyigeek/busybox:1.35.0
  6. # 参数说明
  7. -n 指定名称空间
  8. --rm 在退出容器时删除容器
  9. --net-host 使用主机网络
  10. --env 指定容器内部shell变量
  11. --mount 指定挂载到容器内部的本地文件,src是指定宿主机上文件目录路径,而dst是指定容器内部目录。

执行结果:

  1. INFO[0002] Retrieving image manifest docker.io/library/busybox:1.35.0
  2. INFO[0002] Retrieving image docker.io/library/busybox:1.35.0 from registry index.docker.io
  3. INFO[0006] Built cross stage deps: map[]
  4. INFO[0006] Retrieving image manifest docker.io/library/busybox:1.35.0
  5. INFO[0006] Returning cached image manifest
  6. INFO[0006] Executing 0 build triggers
  7. INFO[0006] Building stage 'docker.io/library/busybox:1.35.0' [idx: '0', base-idx: '-1']
  8. INFO[0006] Skipping unpacking as no commands require it.
  9. INFO[0006] LABEL MAINTAINER=master@weiyigeeek.top BUILDTOOLS=kaniko BUILDENV=containerd.io;
  10. INFO[0006] Applying label MAINTAINER=master@weiyigeeek.top
  11. INFO[0006] Applying label BUILDTOOLS=kaniko
  12. INFO[0006] Applying label BUILDENV=containerd.io;
  13. INFO[0006] ENTRYPOINT ["/bin/sh", "-c", "echo hello,busybox"]
  14. INFO[0006] Pushing image to docker.io/weiyigeek/busybox:1.35.0
  15. INFO[0010] Pushed index.docker.io/weiyigeek/busybox@sha256:d6ed480cc7864b9e19b40f09263abfad4689a9244a5abeb2e3eaf14a439cc55f

步骤 06.查看上传到docker hub中 的 busybox:1.35.0 镜像信息以及拉取到本地进行运行测试验证。

  1. ctr -n default images pull docker.io/weiyigeek/busybox:1.35.0
  2. ctr -n default run --rm docker.io/weiyigeek/busybox:1.35.0 busybox
  3. hello,busybox

至此,在containerd.io 环境中,进行镜像构建并发布到hub中实践完毕!

小试牛刀之在Docker中使用kaniko构建并发布镜像

描述:前面说到kaniko的出现实际是为了在没有docker环境的情况之下,按照 Dockerfile 文件中的指令进行镜像构建,不过此处还是简单的介绍一下在docker环境中的使用。 (实际情况中不建议如此多此一举)

步骤 01.执行如下命令生成 docker hub 认证票据(存储路径为 ~/.docker/config.json)以及提前拉取 kaniko 项目中 executor:latest 镜像。

  1. docker login -u weiyigeek
  2. # Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
  3. # Login Succeeded
  4. docker pull registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest

步骤 02.创建 dockerfile 文件其中 FROM 指定 K8S 集群中常用的NFS动态持久卷镜像,我们将其上传到 hub 仓库中的WeiyiGeek账户下面。

  1. mkdir /storage/dev/soft/kaniko/demo2
  2. cd /storage/dev/soft/kaniko/demo2
  3. tee dockerfile <<'EOF'
  4. FROM registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest
  5. LABEL MAINTAINER=master@weiyigeeek.top BUILDTOOLS=kaniko BUILDENV=docker;
  6. EOF

步骤 03.使用如下示例命令进行 kaniko-executor 容器的创建运行,并进行镜像构建并上传到公共的docker hub 仓库中。

  1. docker rm -f kaniko-executor
  2. docker run --rm --name kaniko-executor \
  3. -v $HOME/.docker/:/kaniko/.docker \
  4. -v /storage/dev/soft/kaniko/demo2:/workspace \
  5. registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest \
  6. --dockerfile=/workspace/dockerfile --context=dir://workspace --destination=docker.io/weiyigeek/nfs-subdir-external-provisioner:latest

执行结果:

  1. INFO[0002] Retrieving image manifest registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest
  2. INFO[0002] Retrieving image registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest from registry registry.cn-hangzhou.aliyuncs.com
  3. INFO[0003] Built cross stage deps: map[]
  4. INFO[0003] Retrieving image manifest registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest
  5. INFO[0003] Returning cached image manifest
  6. INFO[0003] Executing 0 build triggers
  7. INFO[0003] Building stage 'registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest' [idx: '0', base-idx: '-1']
  8. INFO[0003] Skipping unpacking as no commands require it.
  9. INFO[0003] LABEL MAINTAINER=master@weiyigeeek.top BUILDTOOLS=kaniko BUILDENV=docker;
  10. INFO[0003] Applying label MAINTAINER=master@weiyigeeek.top
  11. INFO[0003] Applying label BUILDTOOLS=kaniko
  12. INFO[0003] Applying label BUILDENV=docker;
  13. INFO[0003] Pushing image to docker.io/weiyigeek/nfs-subdir-external-provisioner:latest
  14. INFO[0012] Pushed index.docker.io/weiyigeek/nfs-subdir-external-provisioner@sha256:4dc0d27b8fa4608c9e2d8a6f2368d2029df32b9b55f96f27a9218a620ea14828

步骤 04.查看上传到docker hub 仓库中的 nfs-subdir-external-provisioner:latest 信息 (https://hub.docker.com/r/weiyigeek/nfs-subdir-external-provisioner) 。

步骤 05.当然我们也可以在安装有docker环境中使用上下文使用标准输入,并采用docker进行创建kaniko-executor容器,从标准输入接收dockerfile文件并进行镜像构建与推送。

  1. mkdir /storage/dev/soft/kaniko/demo3
  2. echo -e 'FROM alpine \nRUN echo "created from standard input"' > Dockerfile | tar -cf - Dockerfile | gzip -9 | docker run \
  3. --interactive -v /storage/dev/soft/kaniko/demo3:/workspace -v $HOME/.docker/:/kaniko/.docker \
  4. registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest \
  5. --context tar://stdin \
  6. --destination=docker.io/weiyigeek/alpine:4.2

执行结果: 如果在交互运行期间没有数据管道传输,则需要按Ctrl+D自行发送EOF信号。

  1. INFO[0000] To simulate EOF and exit, press 'Ctrl+D'
  2. INFO[0002] Retrieving image manifest alpine
  3. INFO[0002] Retrieving image alpine from registry index.docker.io
  4. INFO[0005] Built cross stage deps: map[]
  5. INFO[0005] Retrieving image manifest alpine
  6. INFO[0005] Returning cached image manifest
  7. INFO[0005] Executing 0 build triggers
  8. INFO[0005] Building stage 'alpine' [idx: '0', base-idx: '-1']
  9. INFO[0005] Unpacking rootfs as cmd RUN echo "created from standard input" requires it.
  10. INFO[0008] RUN echo "created from standard input"
  11. INFO[0008] Initializing snapshotter ...
  12. INFO[0008] Taking snapshot of full filesystem...
  13. INFO[0008] Cmd: /bin/sh
  14. INFO[0008] Args: [-c echo "created from standard input"]
  15. INFO[0008] Running: [/bin/sh -c echo "created from standard input"]
  16. created from standard input
  17. INFO[0008] Taking snapshot of full filesystem...
  18. INFO[0008] No files were changed, appending empty layer to config. No layer added to image.
  19. INFO[0008] Pushing image to docker.io/weiyigeek/alpine:4.2
  20. INFO[0016] Pushed index.docker.io/weiyigeek/alpine@sha256:49360dc74ecf57ea94fbec9d7a3b5cf59dfba8aa5e60f8802cc6299e668a3e1e

至此,在 Docker 中使用 kaniko 进行镜像构建与发布实践完毕。


0x0n 参考来源

项目地址: https://github.com/GoogleContainerTools/kaniko

kaniko 图像镜像仓库: gcr.io/kaniko-project/executor

kaniko 国内镜像仓库源:weiyigeek/kaniko-executor:latest 或者 registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest

原文地址: https://blog.weiyigeek.top/2022/9-1-684.html

本文至此完毕,更多技术文章,尽情期待下一章节!


WeiyiGeek Blog 个人博客 - 为了能到远方,脚下的每一步都不能少 】

欢迎各位志同道合的朋友一起学习交流【点击加入交流群】,如文章有误请在下方留下您宝贵的经验知识!

作者主页: 【 https://weiyigeek.top

博客地址: 【 https://blog.weiyigeek.top 】

专栏书写不易,如果您觉得这个专栏还不错的,请给这篇专栏 【点个赞、投个币、收个藏、关个注,转个发,留个言】(人间六大情),这将对我的肯定,谢谢!。

  • echo "【点个赞】,动动你那粗壮的拇指或者芊芊玉手,亲!"

  • printf("%s", "【投个币】,万水千山总是情,投个硬币行不行,亲!")

  • fmt.Printf("【收个藏】,阅后即焚不吃灰,亲!")

  • console.info("【转个发】,让更多的志同道合的朋友一起学习交流,亲!")

  • System.out.println("【关个注】,后续浏览查看不迷路哟,亲!")

  • cout << "【留个言】,文章写得好不好、有没有错误,一定要留言哟,亲! " << endl;

更多网络安全、系统运维、应用开发、物联网实践、网络工程、全栈文章,尽在 https://blog.weiyigeek.top 之中,谢谢各位看又支持!

企业运维实践-丢弃手中的 docker build , 使用Kaniko直接在Kubernetes集群或Containerd环境中快速进行构建推送容器镜像的更多相关文章

  1. 企业运维 | MySQL关系型数据库在Docker与Kubernetes容器环境中快速搭建部署主从实践

    [点击 关注「 WeiyiGeek」公众号 ] 设为「️ 星标」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 ...

  2. 企业运维实践-还不会部署高可用的kubernetes集群?使用kubeadm方式安装高可用k8s集群v1.23.7

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 文章目录: 0x00 前言简述 ...

  3. 企业运维实践-Nginx使用geoip2模块并利用MaxMind的GeoIP2数据库实现处理不同国家或城市的访问最佳实践指南

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x00 前言 ...

  4. vivo大规模 Kubernetes 集群自动化运维实践

    作者:vivo 互联网服务器团队-Zhang Rong 一.背景 随着vivo业务迁移到K8s的增长,我们需要将K8s部署到多个数据中心.如何高效.可靠的在数据中心管理多个大规模的K8s集群是我们面临 ...

  5. 阿里巴巴大规模神龙裸金属 Kubernetes 集群运维实践

    作者 | 姚捷(喽哥)阿里云容器平台集群管理高级技术专家 本文节选自<不一样的 双11 技术:阿里巴巴经济体云原生实践>一书,点击即可完成下载. 导读:值得阿里巴巴技术人骄傲的是 2019 ...

  6. Linux实战型企业运维工程师试题测评

    Linux实战型企业运维工程师试题答案 作者:尹正杰      最近在网上看到了一套有意思的面试题,我们一起来看一下这些题怎么破吧,哈哈~我先放在这里,有时间了一起来看看.多学点东西终究是没有坏处的! ...

  7. 华为刘腾:华为终端云Cassandra运维实践分享

    点击此处观看完整活动视频 各位线上的嘉宾朋友大家好,我是来自华为消费者BG云服务部的刘腾,我今天给大家分享的主题是华为终端云Cassandra运维实践.和前面王峰老师提到的Cassandra在360中 ...

  8. 阿里智能运维实践|阿里巴巴DevOps实践指南

    编者按:本文源自阿里云云效团队出品的<阿里巴巴DevOps实践指南>,扫描上方二维码或前往:https://developer.aliyun.com/topic/devops,下载完整版电 ...

  9. 灵雀云:etcd 集群运维实践

    [编者的话]etcd 是 Kubernetes 集群的数据核心,最严重的情况是,当 etcd 出问题彻底无法恢复的时候,解决问题的办法可能只有重新搭建一个环境.因此围绕 etcd 相关的运维知识就比较 ...

随机推荐

  1. raid划分及创建

    RAID 的划分 RAID 0 - RAID 0是最早出现的,是数据分条技术.组建磁盘阵列中最简单的一种形式,可以提高整个磁盘的性能和吞吐量,利用率100%,缺点:一但磁盘损坏,raid0将失效,数据 ...

  2. 前端下载图片的N种方法

    前几天一个简单的下载图片的需求折腾了我后端大佬好几天,最终还是需要前端来搞,开始说不行的笔者最后又行了,所以趁着这个机会来总结一下下载图片到底有多少种方法. 先起个服务 使用expressjs起个简单 ...

  3. js--js实现基础排序算法

    前言 文本来总结常见的排序算法,通过 JvavScript  来实现 正文 1.冒泡排序 算法思想:比较相邻两个元素的大小,如果第一个比第二个大,就交换它们.从头遍历到尾部,当一轮遍历完后,数组最后一 ...

  4. Java:如何打印整个字符串数组?

    例: public static void main(String[] args) { String prodName = "雇员姓名,雇员唯一号"; String[] prodN ...

  5. C#(.net) 面试题

    1.ASP.NET的页面生存周期 .aspx/.ashx->IIS->Asp.net_isapi.dll->HttpRuntime.ProcessRequest() ->Htt ...

  6. 升级了Springboot版本后项目启动不了了

    问题背景 项目上使用的springboot版本是2.1.1.RELEASE,现在因为要接入elasticsearch7.x版本,参考官方文档要求,需要将springboot版本升级到2.5.14. 本 ...

  7. 4-9 基于Spring JDBC的事务管理(续)

    10. 基于Spring JDBC的事务管理(续) 当需要方法是事务性的,可以使用@Transactional注解,此注解可以添加在: 接口 会使得此接口的实现类的所有实现方法都是事务性的 接口中的抽 ...

  8. Java 技术栈中间件优雅停机方案设计与实现全景图

    欢迎关注公众号:bin的技术小屋,阅读公众号原文 本系列 Netty 源码解析文章基于 4.1.56.Final 版本 本文概要 在上篇文章 我为 Netty 贡献源码 | 且看 Netty 如何应对 ...

  9. 素数算法(Prime Num Algorithm)

    素数算法(Prime Num Algorithm) 数学是科学的皇后,而素数可以说是数学的最为核心的概念之一.围绕素数产生了很多伟大的故事,最为著名莫过于哥德巴赫猜想.素数定理和黎曼猜想(有趣的是,自 ...

  10. 关于分组查询的一道sql题

    背景:想做一道sql的测试题,题目为: 按照角色分组算出每个角色按有办公室和没办公室的统计人数(列出角色,数量,有无办公室,注意一个角色如果部分有办公室,部分没有需分开统计) 如下,构造测试环境与对应 ...