Tekton 构建容器镜像

2023-12-19 00:01
文章标签 镜像 构建 容器 tekton

本文主要是介绍Tekton 构建容器镜像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Tekton 构建容器镜像

介绍如何使用 Tektonhub 官方 kaniko task 构建docker镜像,并推送到远程dockerhub镜像仓库。

在这里插入图片描述

kaniko task yaml文件下载地址:https://hub.tekton.dev/tekton/task/kaniko

查看kaniko task yaml内容:
在这里插入图片描述

点击Install,选择一种方式创建 task
在这里插入图片描述

这里使用kubectl命令创建官方kaniko task

kubectl apply -f \
https://raw.githubusercontent.com/tektoncd/catalog/main/task/kaniko/0.6/kaniko.yaml

在执行镜像构建前Dockerfile存放在git仓库中,需要将代码克隆到本地,因此也需要安装git-clone task,安装方式类似。

kubectl apply -f \
https://raw.githubusercontent.com/tektoncd/catalog/main/task/git-clone/0.9/git-clone.yaml

查看创建的task

$ kubectl get task
NAME             AGE
git-clone        25h
kaniko           13h

Task创建后,可以通过taskRunpipelineRun进行调用。

配置dockerhub认证

镜像构建完成后自动推送到dockerhub,需要为dockerhub配置认证信息。

安装jq工具

apt install -y jq

生成config.json,替换docker-usernamedocker-password为您的值。

kubectl create secret docker-registry dockerhub \
--docker-server=https://index.docker.io/v1/ \
--docker-username=<your-docker-username> \
--docker-password=<your-docker-password> \
--dry-run=client -o json | jq -r '.data.".dockerconfigjson"' | base64 -d > /tmp/config.json

基于config.json创建secret

kubectl create secret generic docker-config --from-file=/tmp/config.json

创建serviceaccount,绑定到secret

$ cat serviceaccount.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:name: build-bot
secrets:- name: docker-config

应用yaml文件

kubectl apply -f serviceaccount.yaml 

创建pipeline和pipelinerun

官方示例pipeline:https://github.com/tektoncd/catalog/blob/main/task/kaniko/0.6/tests/run.yaml

该pipeline 首先运行git clone task,从https://github.com/kelseyhightower/nocode.git 克隆代码,然后运行kaniko task 基于根目录的Dockerfile文件构建镜像,并推送到dockerhub。

$ cat kaniko-run.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:name: kaniko-test-pipeline
spec:workspaces:- name: shared-workspace- name: docker-configparams:- name: repo-urltype: stringdescription: The git repository URL to clone from.- name: branch-nametype: stringdescription: The git branch to clone.- name: gitInitImagetype: stringdescription: The gitInitImage params.- name: httpProxytype: stringdescription: The httpProxy params.- name: httpsProxytype: string- name: dockerfiletype: stringdescription: reference of the image to build- name: builder-imagetype: stringdescription: reference of the image to build- name: imagetype: stringdescription: reference of the image to buildtasks:- name: fetch-repositorytaskRef:name: git-cloneworkspaces:- name: outputworkspace: shared-workspaceparams:- name: urlvalue: $(params.repo-url)- name: revisionvalue: $(params.branch-name)- name: gitInitImagevalue: $(params.gitInitImage)- name: httpProxyvalue: $(params.httpProxy)- name: httpsProxyvalue: $(params.httpsProxy)- name: kanikotaskRef:name: kanikorunAfter:- fetch-repositoryworkspaces:- name: sourceworkspace: shared-workspace- name: dockerconfigworkspace: docker-configparams:- name: DOCKERFILEvalue: $(params.dockerfile)- name: IMAGEvalue: $(params.image)- name: BUILDER_IMAGEvalue: $(params.builder-image)- name: verify-digestrunAfter:- kanikoparams:- name: digestvalue: $(tasks.kaniko.results.IMAGE_DIGEST)taskSpec:params:- name: digeststeps:- name: bashimage: ubuntuscript: |echo $(params.digest)case .$(params.digest) in".sha"*) exit 0 ;;*)       echo "Digest value is not correct" && exit 1 ;;esac- name: verify-urlrunAfter:- kanikoparams:- name: urlvalue: $(tasks.kaniko.results.IMAGE_URL)taskSpec:params:- name: urlsteps:- name: bashimage: ubuntuscript: |echo $(params.url)case .$(params.url) in*"/kaniko-nocode") exit 0 ;;*)       echo "URL value is not correct" && exit 1 ;;esac
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:generateName: kaniko-test-pipeline-run-
spec:serviceAccountName: build-botpipelineRef:name: kaniko-test-pipelineparams:- name: repo-urlvalue: https://github.com/kelseyhightower/nocode.git- name: branch-namevalue: master- name: gitInitImage#value: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:latestvalue: dyrnq/tektoncd-pipeline-cmd-git-init:latest- name: httpProxyvalue: http://192.168.72.1:7890/- name: httpsProxyvalue: http://192.168.72.1:7890/- name: dockerfilevalue: ./Dockerfile- name: imagevalue: docker.io/willdockerhub/kaniko-nocode- name: builder-image# value: gcr.io/kaniko-project/executor:v1.5.1@sha256:c6166717f7fe0b7da44908c986137ecfeab21f31ec3992f6e128fff8a94be8a5value: docker.io/bitnami/kaniko:latestworkspaces:- name: shared-workspacevolumeClaimTemplate:spec:accessModes:- ReadWriteOnceresources:requests:storage: 1Gi- name: docker-configsecret:secretName: docker-config

参数说明:

  • gitInitImage:执行git clone任务的镜像,官方镜像无法访问,推荐在docekrhub中查找替代镜像
  • builder-image:执行kaniko 构建任务的镜像,官方镜像无法访问,推荐在docekrhub中查找替代镜像
  • serviceAccountName:指定serviceAccountName用于认证
  • shared-workspace:用于在不同任务之间共享数据,PipelineRun中定义了volumeClaimTemplate类型的workspaces,能够动态申请所需的持久卷,使用kubectl get storageclass命令,确认k8s集群有默认可用的storageclass资源可用,本示例输出为openebs-hostpath (default)
  • docker-config workspace:用于dockerhub认证的secret卷,将secret中的config.json挂载到/kaniko/.docker

应用yaml文件

kubectl create -f kaniko-run.yaml

查看pipelinerun执行结果
在这里插入图片描述

查看镜像构建结果

在这里插入图片描述

这篇关于Tekton 构建容器镜像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/510242

相关文章

Python容器转换与共有函数举例详解

《Python容器转换与共有函数举例详解》Python容器是Python编程语言中非常基础且重要的概念,它们提供了数据的存储和组织方式,下面:本文主要介绍Python容器转换与共有函数的相关资料,... 目录python容器转换与共有函数详解一、容器类型概览二、容器类型转换1. 基本容器转换2. 高级转换示

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

python项目打包成docker容器镜像的两种方法实现

《python项目打包成docker容器镜像的两种方法实现》本文介绍两种将Python项目打包为Docker镜像的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录简单版:(一次成功,后续下载对应的软件依赖)第一步:肯定是构建dockerfile,如下:第二步

docker 重命名镜像的实现方法

《docker重命名镜像的实现方法》在Docker中无法直接重命名镜像,但可通过添加新标签、删除旧镜像后重新拉取/构建,或在DockerCompose中修改配置文件实现名称变更,感兴趣的可以了解一下... 目录使用标签(Tagging)删除旧的php镜像并重新拉取或构建使用docker Compose在Do

Java JUC并发集合详解之线程安全容器完全攻略

《JavaJUC并发集合详解之线程安全容器完全攻略》Java通过java.util.concurrent(JUC)包提供了一整套线程安全的并发容器,它们不仅是简单的同步包装,更是基于精妙并发算法构建... 目录一、为什么需要JUC并发集合?二、核心并发集合分类与详解三、选型指南:如何选择合适的并发容器?在多

使用Node.js和PostgreSQL构建数据库应用

《使用Node.js和PostgreSQL构建数据库应用》PostgreSQL是一个功能强大的开源关系型数据库,而Node.js是构建高效网络应用的理想平台,结合这两个技术,我们可以创建出色的数据驱动... 目录初始化项目与安装依赖建立数据库连接执行CRUD操作查询数据插入数据更新数据删除数据完整示例与最佳

python语言中的常用容器(集合)示例详解

《python语言中的常用容器(集合)示例详解》Python集合是一种无序且不重复的数据容器,它可以存储任意类型的对象,包括数字、字符串、元组等,下面:本文主要介绍python语言中常用容器(集合... 目录1.核心内置容器1. 列表2. 元组3. 集合4. 冻结集合5. 字典2.collections模块

Spring Boot中获取IOC容器的多种方式

《SpringBoot中获取IOC容器的多种方式》本文主要介绍了SpringBoot中获取IOC容器的多种方式,包括直接注入、实现ApplicationContextAware接口、通过Spring... 目录1. 直接注入ApplicationContext2. 实现ApplicationContextA

linux配置podman阿里云容器镜像加速器详解

《linux配置podman阿里云容器镜像加速器详解》本文指导如何配置Podman使用阿里云容器镜像加速器:登录阿里云获取专属加速地址,修改Podman配置文件并移除https://前缀,最后拉取镜像... 目录1.下载podman2.获取阿里云个人容器镜像加速器地址3.更改podman配置文件4.使用po

k8s容器放开锁内存限制问题

《k8s容器放开锁内存限制问题》nccl-test容器运行mpirun时因NCCL_BUFFSIZE过大导致OOM,需通过修改docker服务配置文件,将LimitMEMLOCK设为infinity并... 目录问题问题确认放开容器max locked memory限制总结参考:https://Access