当前位置: 首页 > news >正文

Tekton实战案例--S2I

案例环境说明

  • 示例项目:

    代码仓库:https://gitee.com/mageedu/spring-boot-helloWorld.git

    构建工具maven

  • pipeline各Task

    • git-clone:克隆项目的源代码

    • build-to-package: 代码测试,构建和打包

    • generate-build-id:生成build id

    • image-build-and-push:镜像构建和推送

    • deploy-to-cluster:将新版本的镜像部署到kubernetes集群

  • Workspace

    • 基于PVC,跨task数据共享

在这里插入图片描述

2.2.5.2 pipeline完成Image构建,推送和部署

  1. 01-git-clone的Task

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:name: git-clone
    spec:description: Clone code to the workspaceparams:- name: urltype: stringdescription: git url to clonedefault: ""- name: branchtype: stringdescription: git branch to checkoutdefault: "main"workspaces:- name: sourcedescription: The code repo will clone in the workspacesteps:- name: git-cloneimage: alpine/git:v2.36.1script: git clone -b $(params.branch) -v $(params.url) $(workspaces.source.path)/source
  2. 02–build-to-package.yaml

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:name: build-to-package
    spec:workspaces:- name: sourcedescription: The code repo in the workspacessteps:- name: buildimage: maven:3.8-openjdk-11-slimworkingDir: $(workspaces.source.path)/sourcevolumeMounts:- name: m2mountPath: /root/.m2script: mvn clean install# 定义volume提供maven cache,但是前提得创建出来maven-cache的pvcvolumes:- name: m2persistentVolumeClaim:claimName: maven-cache
    
  3. 03-generate-build-id.yaml

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:name: generate-build-id
    spec:params:- name: versiondescription: The version of the applicationtype: stringresults:- name: datetimedescription: The current date and time- name: buildIddescription: The build IDsteps:- name: generate-datetimeimage: ikubernetes/admin-box:v1.2script: |#!/usr/bin/env bashdatetime=`date +%Y%m%d-%H%M%S`echo -n ${datetime} | tee $(results.datetime.path)- name: generate-buildidimage: ikubernetes/admin-box:v1.2script: |#!/usr/bin/env bashbuildDatetime=`cat $(results.datetime.path)`buildId=$(params.version)-${buildDatetime}echo -n ${buildId} | tee $(results.buildId.path)
  4. 04-build-image-push.yaml

    要想能推送镜像到镜像仓库,必须创建一个secret对象,挂在到kaniko的/kaniko/.docker目录下,具体创建secret的方法有两种:

    1、先在一台机器上login镜像仓库,这里以dockerhub为例,将会把认证文件保存在~/.docker/config.json:
    在这里插入图片描述

  5. 基于config,json创建sectet,这里的secret的类型选择generic

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

    2、先基于user/password创建一个base64:

    echo -n USER:PASSWORD | base64
    

    创建一个config.json,然后将创建出来的base64替换到下面xxxxxxxxxxxxxxx

    {"auths": {"https://index.docker.io/v1/": {"auth": "xxxxxxxxxxxxxxx"}}
    }
    

    最后创建一个secret

    kubectl create secret generic docker-config --from-file=<path to .docker/config.json>
    
  6. 05-deploy-task.yaml

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:name: deploy-using-kubectl
    spec:workspaces:- name: sourcedescription: The git repoparams:- name: deploy-config-filedescription: The path to the yaml file to deploy within the git source- name: image-urldescription: Image name including repository- name: image-tagdescription: Image tagsteps:- name: update-yamlimage: alpine:3.16command: ["sed"]args:- "-i"- "-e"- "s@__IMAGE__@$(params.image-url):$(params.image-tag)@g"- "$(workspaces.source.path)/source/deploy/$(params.deploy-config-file)"- name: run-kubectlimage: lachlanevenson/k8s-kubectlcommand: ["kubectl"]args:- "apply"- "-f"- "$(workspaces.source.path)/source/deploy/$(params.deploy-config-file)"
    
  7. 06-pipelinerun-s2i.yaml

    apiVersion: tekton.dev/v1beta1
    kind: Pipeline
    metadata:name: source-to-image
    spec:params:- name: git-url- name: pathToContextdescription: The path to the build context, used by Kaniko - within the workspacedefault: .- name: image-urldescription: Url of image repository- name: deploy-config-filedescription: The path to the yaml file to deploy within the git sourcedefault: all-in-one.yaml- name: versiondescription: The version of the applicationtype: stringdefault: "v0.10" workspaces:- name: codebase- name: docker-configtasks:- name: git-clonetaskRef:name: git-cloneparams:- name: urlvalue: "$(params.git-url)"workspaces:- name: sourceworkspace: codebase- name: build-to-packagetaskRef:name: build-to-packageworkspaces:- name: sourceworkspace: codebaserunAfter:- git-clone- name: generate-build-idtaskRef:name: generate-build-idparams:- name: versionvalue: "$(params.version)"runAfter:- git-clone- name: image-build-and-pushtaskRef:name: image-build-and-pushparams:- name: image-urlvalue: "$(params.image-url)"- name: image-tagvalue: "$(tasks.generate-build-id.results.buildId)"workspaces:- name: sourceworkspace: codebase- name: dockerconfigworkspace: docker-configrunAfter:- generate-build-id- build-to-package- name: deploy-to-clustertaskRef:name: deploy-using-kubectlworkspaces:- name: sourceworkspace: codebaseparams:- name: deploy-config-filevalue: $(params.deploy-config-file)- name: image-urlvalue: $(params.image-url)- name: image-tagvalue: "$(tasks.generate-build-id.results.buildId)"runAfter:- image-build-and-push
  8. 07-rbac.yaml

    因为06task的容器要执行kubectl,所以,给这个pod要指定一个serviceaccount,这样才能操作集群的资源

    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:name: helloworld-admin
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:name: helloworld-admin
    roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-admin
    subjects:
    - kind: ServiceAccountname: helloworld-adminnamespace: default
  9. 08-pipelinerun-s2i.yaml

    apiVersion: tekton.dev/v1beta1
    kind: PipelineRun
    metadata:name: s2i-buildid-run-00002
    spec:serviceAccountName: defaulttaskRunSpecs:- pipelineTaskName: deploy-to-clustertaskServiceAccountName: helloworld-adminpipelineRef:name: source-to-imageparams:- name: git-urlvalue: https://gitee.com/mageedu/spring-boot-helloWorld.git- name: image-urlvalue: icloud2native/spring-boot-helloworld- name: versionvalue: v0.1.2workspaces:- name: codebasevolumeClaimTemplate:spec:accessModes:- ReadWriteOnceresources:requests:storage: 1GistorageClassName: nfs-csi- name: docker-configsecret:secretName: docker-config

    运行:

    kubectl apply -f .
    

    结果:

    1. 整个pipeline执行成功
      在这里插入图片描述
      2、image推送到dockerhub
      在这里插入图片描述
      3、查看部署
      在这里插入图片描述
      更多关于tekton文章,后续更新。。。
http://www.lryc.cn/news/17523.html

相关文章:

  • 四、使用类实现功能
  • Java多线程不安全的例子
  • vivo X Flip会是高端手机市场的又一折叠屏爆款吗?
  • MySQL中MVCC如何解决不可重复读以及幻读?
  • 设计模式第八讲:观察者模式和中介者模式详解
  • 关于 mac 本地配置域名能 ping 通,但是浏览器不能访问的问题(而其他电脑操作可访问)
  • 【代码随想录二刷】Day23-二叉树-C++
  • Linux GPIO 开发指南
  • 记一次后端生成Zip文件通过浏览器下载后文件损坏,无法打开,不可预知的末端错误,下载后文件比源文件增大
  • python中savgol_filter的详细解释
  • C语言--指针进阶1
  • ssh的使用
  • Apache Hadoop生态-目录汇总-持续更新
  • 「JVM 编译后话」编译器优化技术
  • 【python学习笔记】:输出与输入
  • 汽车电子社区交流宣传
  • String、StringBuilder 和 StringBuffer 详解
  • windows服务器上传文件解决方案
  • Android Studio翻译插件推介(Translation)
  • DNS,DNS污染劫持,DNS加密
  • 【Python】如何度量优秀代码——静态分析工具
  • Open3D 点云高程归一化(基于2维地面点,Python版本)
  • 动态系统的建模与分析
  • QCC51XX---HCI log
  • Redis四 原理篇
  • 从0开始写Vue项目-Vue实现数据渲染和数据的增删改查
  • AI技术的发展,人工智能对我们的生活有那些影响?
  • Unity中的Mathf数学运算讲解(值得收藏)
  • ABBYY FineReader16最新PDF图片文字识别软件
  • Leetcode14. 最长公共前缀