kubesphere流水线使用kaniko在容器中构建镜像
kubesphere流水线使用kaniko在容器中构建镜像
Kaniko - 更安全可靠的方式在Kubernetes内构建容器镜像
kaniko是一个在容器或Kubernetes内从Dockerfile构建容器镜像的工具。
kaniko不依赖Docker守护进程,完全在用户空间执行Docker文件中的每个命令。这使得在不能轻易或安全地运行Docker守护进程的环境中构建容器镜像,例如标准的Kubernetes集群。
kaniko是要作为一个镜像来运行:gcr.io/kaniko-project/executor
。我们不建议在其他镜像中运行kaniko二进制文件,因为可能无法工作。
Kaniko是如何工作的?
kaniko executor(执行器)
镜像负责从 Dockerfile 构建镜像并将其推送到registry
。在执行器镜像中,我们提取了基础镜像的文件系统(Dockerfile 中的 FROM 镜像)。 然后我们执行 Dockerfile 中的命令,在每个命令之后在用户空间中对文件系统进行快照。 在每个命令之后,我们将一层更改的文件附加到基础图像(如果有的话)并更新镜像元数据。
已知问题(Known Issues)
- kaniko不支持构建Windows容器
- 不支持在官方kaniko镜像以外的任何Docker镜像中运行kaniko(即YMMV)。
- 这包括将官方镜像中的kaniko可执行文件复制到另一个镜像中。
- kaniko不支持v1版registry API(Registry v1 API 已弃用)。
教程
本教程适用于想要开始使用 kaniko 的初学者,旨在建立一个快速入门的测试例子。
前提(Prerequisities)
- kubernetes(k8s)
- dockerhub账户,用于push创建的public镜像
准备Dockerfile
SSH进入k8s集群,并创建一个本地目录,该目录将被挂载到kaniko容器作为构建环境。在那里创建一个简单的dockerfile。
$ mkdir kaniko && cd kaniko $ echo 'FROM ubuntu' >> dockerfile $ echo 'ENTRYPOINT ["/bin/bash", "-c", "echo hello"]' >> dockerfile $ cat dockerfile FROM ubuntu ENTRYPOINT ["/bin/bash", "-c", "echo hello"] $ pwd /home/<user-name>/kaniko # 将此路径复制到 volume.yaml 文件中
注意:
volume.yaml
中的hostPath
需要替换为你创建的本地目录。
volume.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: dockerfile labels: type: local spec: capacity: storage: 10Gi accessModes: - ReadWriteOnce storageClassName: local-storage hostPath: path: <local-directory> # 替换你的本地目录
volume-claim.yaml
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: dockerfile-claim spec: accessModes: - ReadWriteOnce resources: requests: storage: 8Gi storageClassName: local-storage © 2022 GitHub, Inc. Terms Privacy
pod.yaml
apiVersion: v1 kind: Pod metadata: name: kaniko spec: containers: - name: kaniko image: kubebiz/kaniko:executor-v1.9.1 args: ["--dockerfile=/workspace/dockerfile", "--context=dir://workspace", "--destination=<user-name>/<repo>"] # 你的dockerhub账户 volumeMounts: - name: kaniko-secret mountPath: /kaniko/.docker - name: dockerfile-storage mountPath: /workspace restartPolicy: Never volumes: - name: kaniko-secret secret: secretName: regcred items: - key: .dockerconfigjson path: config.json - name: dockerfile-storage persistentVolumeClaim: claimName: dockerfile-claim
创建一个包含你的授权token的Secret
Kubernetes集群使用docker-registry类型的Secret来验证docker registry,用于推送镜像。
此 Secret,将其命名为 regcred:
kubectl create secret docker-registry regcred --docker-server=<your-registry-server> --docker-username=<your-name> --docker-password=<your-pword> --docker-email=<your-email>
<your-registry-server>
是你的私有 Docker Registry FQDN。<your-name>
是你的 Docker 用户名。<your-pword>
是你的 Docker 密码。<your-email>
是你的 Docker 电子邮件。
这个 secret 将在 pod.yaml
配置中使用。
在 Kubernetes 中创建资源
# 创建持久卷 $ kubectl create -f volume.yaml persistentvolume/dockerfile created # 创建持久卷声明 $ kubectl create -f volume-claim.yaml persistentvolumeclaim/dockerfile-claim created # 检查卷是否正确安装 $ kubectl get pv dockerfile NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE dockerfile 10Gi RWO Retain Bound default/dockerfile-claim local-storage 1m # 创建pod $ kubectl create -f pod.yaml pod/kaniko created $ kubectl get pods NAME READY STATUS RESTARTS AGE kaniko 0/1 ContainerCreating 0 7s # 检查构建是否完成并显示构建日志 $ kubectl get pods NAME READY STATUS RESTARTS AGE kaniko 0/1 Completed 0 34s $ kubectl logs kaniko ➜ kubectl logs kaniko INFO[0000] Resolved base name ubuntu to ubuntu INFO[0000] Resolved base name ubuntu to ubuntu INFO[0000] Downloading base image ubuntu INFO[0000] Error while retrieving image from cache: getting file info: stat /cache/sha256:1bbdea4846231d91cce6c7ff3907d26fca444fd6b7e3c282b90c7fe4251f9f86: no such file or directory INFO[0000] Downloading base image ubuntu INFO[0001] Built cross stage deps: map[] INFO[0001] Downloading base image ubuntu INFO[0001] Error while retrieving image from cache: getting file info: stat /cache/sha256:1bbdea4846231d91cce6c7ff3907d26fca444fd6b7e3c282b90c7fe4251f9f86: no such file or directory INFO[0001] Downloading base image ubuntu INFO[0001] Skipping unpacking as no commands require it. INFO[0001] Taking snapshot of full filesystem... INFO[0001] ENTRYPOINT ["/bin/bash", "-c", "echo hello"]
注意:
pod.yaml
中的destination
需要替换为你自己的。
拉取镜像并测试
如果符合预期,kaniko 将成功构建镜像并推送到 dockerhub。 拉取镜像到本地运行测试:
$ sudo docker run -it <user-name>/<repo-name> Unable to find image 'debuggy/helloworld:latest' locally latest: Pulling from debuggy/helloworld 5667fdb72017: Pull complete d83811f270d5: Pull complete ee671aafb583: Pull complete 7fc152dfb3a6: Pull complete Digest: sha256:2707d17754ea99ce0cf15d84a7282ae746a44ff90928c2064755ee3b35c1057b Status: Downloaded newer image for debuggy/helloworld:latest hello
以上内容引用站点:Kaniko中文教程 https://www.orchome.com/kaniko/index
在kubesphere流水线中以job的方式运行kaniko构建镜像
cat <<EOF>$jobname-kaniko.yml apiVersion: batch/v1 kind: Job metadata: name: $jobname-kaniko namespace: kubesphere-devops-worker spec: backoffLimit: 5 ttlSecondsAfterFinished: 10 # job任务执行完成后自动删除时间,单位秒 activeDeadlineSeconds: 300 template: spec: containers: - name: jobname-kaniko image: registry.cn-beijing.aliyuncs.com/pgy-k8s/kaniko:v1.9.2 args: ["--dockerfile=/workspace/$jobname/dockerfile", # dockerfile文件位置 "--context=dir://$jobname", #构建上下文,(注意:不能全路径,只能是dockerfile所在目录的名称) "--destination=myregster.com/$env/$jobname:$tag"] #构建镜像以及上传 volumeMounts: # 挂载dockerfile以及构建需要的文件到容器中 - name: kaniko-secret mountPath: /kaniko/.docker - name: dockerfile-storage mountPath: /workspace restartPolicy: Never volumes: - name: kaniko-secret # 挂载镜像仓库密钥配置 secret: secretName: regcred items: - key: .dockerconfigjson path: config.json - name: dockerfile-storage persistentVolumeClaim: claimName: jenkins-kaniko EOF
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通