Tekton实战案例--S2I

news/2024/7/4 7:48:07 标签: tekton, 云原生

案例环境说明

  • 示例项目:

    代码仓库: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 workspace
      params:
      - name: url
        type: string
        description: git url to clone
        default: ""
      - name: branch
        type: string
        description: git branch to checkout
        default: "main"
      workspaces:
      - name: source
        description: The code repo will clone in the workspace
      steps:
      - name: git-clone
        image: alpine/git:v2.36.1
        script: 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: source
        description: The code repo in the workspaces
      steps:
      - name: build
        image: maven:3.8-openjdk-11-slim
        workingDir: $(workspaces.source.path)/source
        volumeMounts:
        - name: m2
          mountPath: /root/.m2
        script: mvn clean install
      # 定义volume提供maven cache,但是前提得创建出来maven-cache的pvc
      volumes:
      - name: m2
        persistentVolumeClaim:
          claimName: maven-cache
    
  3. 03-generate-build-id.yaml

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:
      name: generate-build-id
    spec:
      params:
        - name: version
          description: The version of the application
          type: string
      results:
        - name: datetime
          description: The current date and time
        - name: buildId
          description: The build ID
      steps:
        - name: generate-datetime
          image: ikubernetes/admin-box:v1.2
          script: |
            #!/usr/bin/env bash
            datetime=`date +%Y%m%d-%H%M%S`
            echo -n ${datetime} | tee $(results.datetime.path)
        - name: generate-buildid
          image: ikubernetes/admin-box:v1.2
          script: |
            #!/usr/bin/env bash
            buildDatetime=`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: source
          description: The git repo
      params:
        - name: deploy-config-file
          description: The path to the yaml file to deploy within the git source
        - name: image-url
          description: Image name including repository
        - name: image-tag
          description: Image tag
      steps:
        - name: update-yaml
          image: alpine:3.16
          command: ["sed"]
          args:
            - "-i"
            - "-e"
            - "s@__IMAGE__@$(params.image-url):$(params.image-tag)@g"
            - "$(workspaces.source.path)/source/deploy/$(params.deploy-config-file)"
        - name: run-kubectl
          image: lachlanevenson/k8s-kubectl
          command: ["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: pathToContext
          description: The path to the build context, used by Kaniko - within the workspace
          default: .
        - name: image-url
          description: Url of image repository
        - name: deploy-config-file
          description: The path to the yaml file to deploy within the git source
          default: all-in-one.yaml
        - name: version
          description: The version of the application
          type: string
          default: "v0.10" 
      workspaces:
        - name: codebase
        - name: docker-config
      tasks:
        - name: git-clone
          taskRef:
            name: git-clone
          params:
            - name: url
              value: "$(params.git-url)"
          workspaces:
            - name: source
              workspace: codebase
        - name: build-to-package
          taskRef:
            name: build-to-package
          workspaces:
            - name: source
              workspace: codebase
          runAfter:
            - git-clone
        - name: generate-build-id
          taskRef:
            name: generate-build-id
          params:
            - name: version
              value: "$(params.version)"
          runAfter:
            - git-clone
        - name: image-build-and-push
          taskRef:
            name: image-build-and-push
          params:
            - name: image-url
              value: "$(params.image-url)"
            - name: image-tag
              value: "$(tasks.generate-build-id.results.buildId)"
          workspaces:
            - name: source
              workspace: codebase
            - name: dockerconfig
              workspace: docker-config
          runAfter:
            - generate-build-id
            - build-to-package
        - name: deploy-to-cluster
          taskRef:
            name: deploy-using-kubectl
          workspaces:
            - name: source
              workspace: codebase
          params:
            - name: deploy-config-file
              value: $(params.deploy-config-file)
            - name: image-url
              value: $(params.image-url)
            - name: image-tag
              value: "$(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.io
      kind: ClusterRole
      name: cluster-admin
    subjects:
    - kind: ServiceAccount
      name: helloworld-admin
      namespace: default
    
    
  9. 08-pipelinerun-s2i.yaml

    apiVersion: tekton.dev/v1beta1
    kind: PipelineRun
    metadata:
      name: s2i-buildid-run-00002
    spec:
      serviceAccountName: default
      taskRunSpecs:
        - pipelineTaskName: deploy-to-cluster
          taskServiceAccountName: helloworld-admin
      pipelineRef:
        name: source-to-image
      params:
        - name: git-url
          value: https://gitee.com/mageedu/spring-boot-helloWorld.git
        - name: image-url
          value: icloud2native/spring-boot-helloworld
        - name: version
          value: v0.1.2
      workspaces:
        - name: codebase
          volumeClaimTemplate:
            spec:
              accessModes:
                - ReadWriteOnce
              resources:
                requests:
                  storage: 1Gi
              storageClassName: nfs-csi
        - name: docker-config
          secret:
            secretName: docker-config
    
    

    运行:

    kubectl apply -f .
    

    结果:

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

http://www.niftyadmin.cn/n/92688.html

相关文章

OSCP-课外1(http万能密码、hydra密码暴力破解http、代码审计、Win缓存区溢出)

目录 难度 主机发现&端口扫描 信息收集 万能密码 hydra密码暴力破解

[Flink]部署模式(看pdf上的放上面)

运行一个wordcountval dataStream: DataStream[String] environment.socketTextStream("hadoop1", 7777) //流式数据不能进行groupBy,流式数据要来一条处理一次.0表示第一个元素,1表示第二个元素 //keyBy(0)根据第一个元素进行分组 val out: DataStream[(String, In…

Vue+Less/Scss实现主题切换功能

前言&#xff1a;目前&#xff0c;在众多的后台管理系统中&#xff0c;换肤功能已是一个很常见的功能。用户可以根据自己的喜好&#xff0c;设置页面的主题&#xff0c;从而实现个性化定制。目前&#xff0c;我所了解到的换肤方式&#xff0c;也是我目前所掌握的两种换肤方式&a…

联想昭阳E5-ITL电脑开机后绿屏怎么U盘重装系统?

联想昭阳E5-ITL电脑开机后绿屏怎么U盘重装系统&#xff1f;有用户电脑正常开机之后&#xff0c;出现了屏幕变成绿屏&#xff0c;无法进行操作的情况。这个问题是系统出现了问题&#xff0c;那么如何去进行问题的解决呢&#xff1f;接下来我们一起来分享看看如何使用U盘重装电脑…

MySQL千万级数据查询的优化技巧及思路

随着数据量的不断增长&#xff0c;MySQL千万级数据查询的优化问题也日益引人注目。在这篇文章中&#xff0c;我们将深入探讨MySQL千万级数据查询优化的方法和技巧&#xff0c;以帮助开发者更好地优化MySQL性能。 一、数据库设计 数据库设计是优化查询性能的关键&#xff0c;以…

promise静态方法及相关练习

promise的静态方法相对简单&#xff0c;这篇文章做个总结&#xff0c;以便漏补缺总结如下&#xff1a;1. Promise.all/Promise.anyPromise.allSettled/Promise.race都是接受数组&#xff0c;数组里面是promise2.. Promise.all 接收的promise数组只要有一个失败那么整个就是失败…

ctf pwn基础-2

今天学了一个保护的绕过&#xff0c;这里讲一讲&#xff0c;这个好像是使用的是格式化字符串漏洞。 目录 基础 实例讲解 基础 首先我们要知道什么是canary保护&#xff0c;就是在入栈EBP以后加一个Canary 我可能讲的不是很好&#xff0c;大家可以看看这些 文章 用通俗一点将就…

pycharm入门快捷操作(部分)

altenter&#xff1a;提示意图动作shift两次或者crtlshifta&#xff1a;查找框&#xff08;查找动作、类、项目等&#xff09;crtlw&#xff1a;一次一个字符、两次整个字符串&#xff08;if条件下选择整个判断体&#xff09;、三次整个句子、四次整个引用ctrlshiftw&#xff1…