Tekton入门

Tekton入门

什么是流水线

流水线是把一个重复的过程分解为若干个子过程,使每个子过程与其他子过程并行进行的技术,也叫 Pipeline。以“构建镜像更新应用”为例:
在这里插入图片描述当一次代码变更之后,jenkins都需要拉取最新的代码,进行代码编译,构建镜像,推送镜像到镜像仓库,最后更新k8s资源对象。而且每次代码变更,此过程都是不变的。所以使用流水线工具可以极大的提升这一过程的效率,只需要进行简单的配置便可以轻松的完成重复性的工作,这样的过程也被称之为 CI。

什么是Tekton

Tekton 是一个基于 Kubernetes 的云原生 CI/CD 开源框架,属于 CD 基金会的项目之一。Tekton 通过定义 CRD 的方式,让用户以灵活的自定义流水线以满足自身 CI/CD 需求。

Tekton使用人员

  • 为组织中的开发人员构建 CI/CD 系统的平台工程师
  • 使用这些 CI/CD 系统完成工作的开发人员

Tekton优点

  • 可定制Tekton 实体是完全可定制的,具有高度的灵活性。平台工程师可以定义高度详细的构建块目录,供开发人员在各种场景中使用。
  • 可重复使用的Tekton 实体是完全可移植的,因此一旦定义,组织内的任何人都可以使用给定的管道并重用其构建块。这使开发人员无需“重新发明轮子”即可快速构建复杂的管道。
  • 可扩展Tekton Catalog 是社区驱动的 Tekton 构建块存储库。您可以使用 Tekton Catalog 中的预制组件快速创建新管道并扩展现有管道。
  • 标准化Tekton 在您的 Kubernetes 集群上作为扩展安装和运行,并使用完善的 Kubernetes 资源模型。Tekton 工作负载在 Kubernetes 容器内执行。

Tekton组成

Tekton 由以下组件组成:

  • **Tekton PipelinesTekton**的基础。它定义了一组 Kubernetes自定义资源 ,用作构建块,您可以从中组装 CI/CD 管道。
  • **Tekton Triggers**允许您根据事件实例化管道。例如,您可以在每次将 PR 合并到 GitHub 存储库时触发管道的实例化和执行。您还可以构建启动特定 Tekton 触发器的用户界面。
  • **Tekton CLI**提供了一个名为 的命令行界面tkn,它构建在 Kubernetes CLI 之上,允许您与 Tekton 进行交互。
  • **Tekton Dashboard**是 Tekton Pipelines 的基于 Web 的图形界面,可显示有关管道执行的信息。它目前正在进行中。
  • **Tekton Catalog**是一个由社区贡献的高质量 Tekton 构建块的存储库 -TasksPipelines,等等 - 可以在您自己的管道中使用。
  • **Tekton Hub**是一个基于 Web 的图形界面,用于访问 Tekton Catalog。
  • **Tekton Operator**是一种 Kubernetes Operator 模式 ,允许您在 Kubernetes 集群上安装、更新和删除 Tekton 项目。

Tekton Pipelines基本概念

在这里插入图片描述

  • Tekton Pipelines最基本的四个概念:Task、TaskRun、Pipeline、PipelineRun。

    • Task: Task 为构建任务,是 Tekton 中不可分割的最小单位,正如同 Pod 在 Kubernetes 中的概念一样。在 Task 中,可以有多个 Step,每个 Step 由一个 Container 按照顺序来执行。
    • Pipeline: Pipeline 由一个或多个 Task 组成。在 Pipeline 中,用户可以定义这些 Task 的执行顺序以及依赖关系来组成 DAG(有向无环图)。
    • PipelineRun: PipelineRun 是 Pipeline 的实际执行产物,当用户定义好 Pipeline 后,可以通过创建 PipelineRun 的方式来执行流水线,并生成一条流水线记录。
    • TaskRun: PipelineRun 被创建出来后,会对应 Pipeline 里面的 Task 创建各自的 TaskRun。一个 TaskRun 控制一个 Pod,Task 中的 Step 对应 Pod 中的 Container。当然,TaskRun 也可以单独被创建。
  • PipelineResource概念
    在这里插入图片描述PipelineResource 代表着一系列的资源,主要承担作为 Task 的输入或者输出的作用。它有以下几种类型:

    • git:代表一个 git 仓库,包含了需要被构建的源代码。将 git 资源作为 Task 的 Input,会自动 clone 此 git 仓库。
    • pullRequest:表示来自配置的 url(通常是一个 git 仓库)的 pull request 事件。将 pull request 资源作为 Task 的 Input,将自动下载 pull request 相关元数据的文件,如 base/head commit、comments 以及 labels。
    • image:代表镜像仓库中的镜像,通常作为 Task 的 Output,用于生成镜像。
    • cluster:表示一个除了当前集群外的 Kubernetes 集群。可以使用 Cluster 资源在不同的集群上部署应用。
    • storage:表示 blob 存储,它包含一个对象或目录。将 Storage 资源作为 Task 的 Input 将自动下载存储内容,并允许 Task 执行操作。目前仅支持 GCS。
    • cloud event:会在 TaskRun 执行完成后发送事件信息(包含整个 TaskRun) 到指定的 URI 地址,在与第三方通信的时候十分有用。

Tekton Triggers基本概念

在这里插入图片描述
Tekton Triggers是一个基于事件的触发器,它可以检测到事件的发生,并能提取事件中的信息,TaskRunsPipelineRuns 可以根据该信息进行实例化和执行。

Tekton Triggers最基本的四个概念:TriggerTemplate、TriggerBinding、EventListener、Interceptor。

TriggerTemplate:指定要在检测到事件时实例化和执行的资源,如TaskRunsPipelineRuns

TriggerBinding:指定要从事件信息中提取的字段,并将字段信息传递给TriggerTemplate,这样字段就可以在TaskRunsPipelineRuns中使用。

EventListener:在Kubernetes集群启动一个事件监听服务、并暴露服务,将TriggerTemplateTriggerBinding绑定。

Interceptor:事件拦截器,用于过滤事件,运行在TriggerBinding之前。

构建流水线

下边以接收gitlab pr merged事件自动从gitlab拉取代码、构建镜像、推送镜像到harbor仓库、更新k8s集群资源为例,演示tekton构建流水线。

定义PipelineResource

  • git-input表示Task的输入,type=git表示使用git仓库类型,url表示gitlab的地址,revision表示分支。

    apiVersion: tekton.dev/v1alpha1
    kind: PipelineResource
    metadata:
      name: git-input
      namespace: test-tekton
    spec:
      type: git
      params:
        - name: url
          value: https://xxxx.git
        - name: revision
          value: dev
    
  • image-output表示Task的输出,type=image表示使用镜像仓库类型,url表示仓库地址。

    apiVersion: tekton.dev/v1alpha1
    kind: PipelineResource
    metadata:
      name: image-output
      namespace: test-tekton
    spec:
      type: image
      params:
        - name: url
          value: xxxx/xxxx
    

配置认证信息

由于使用了私有gitlab和镜像仓库,所以需要配置仓库的认证信息。

  • git-basic-user-pass表示gitlab的认证信息,annotationstekton.dev/git-tekton.dev/git-开头的key表示git类型的secret,value则是gitlab的地址,type表示认证类型,支持kubernetes.io/basic-authkubernetes.io/ssh-auth

    apiVersion: v1
    kind: Secret
    metadata:
      name: git-basic-user-pass
      namespace: test-tekton
      annotations:
        tekton.dev/git-0: https://xxxx.git
    type: kubernetes.io/basic-auth
    stringData:
      username: xxxx
      password: xxxx
    
  • repo-basic-user-pass表示harbor的认证信息,annotationstekton.dev/docker-开头的key表示docker仓库类型的secret,value则是harbor的地址,type表示认证类型,支持kubernetes.io/basic-authkubernetes.io/dockercfgkubernetes.io/dockerconfigjson

    apiVersion: v1
    kind: Secret
    metadata:
      name: repo-basic-user-pass
      namespace: test-tekton
      annotations:
        tekton.dev/docker-0: xxxx
    type: kubernetes.io/basic-auth
    stringData:
      username: xxxx
      password: xxxx
    
  • build-bot表示PipelineRun、TaskRun使用的ServiceAccount,build-bot绑定git-basic-user-pass、repo-basic-user-pass,这样PipelineRun、TaskRun就能使用相应认证信息。

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: build-bot
      namespace: test-tekton
    secrets:
      - name: git-basic-user-pass
      - name: repo-basic-user-pass
    

    由于部署应用时使用kubectl、triggers,所以build-bot需要集群权限,这里给了一个cluster-admin的权限。

    kubectl create clusterrolebinding build-bot-cluster-admin --clusterrole=cluster-admin --serviceaccount=test-tekton:build-bot
    

定义Task

  • build-push表示构建、推送镜像任务,params下表示表示定义变量并设置了默认值,$()表示引用某个变量,workspaces类似于k8s中volume,用于存储、共享数据,支持pvc、configmap、secret,本例由于需要在task之间共享部署的yaml,所以在pipelineRun中指定使用pvc作为存储实现。

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:
      name: build-push
      namespace: test-tekton
    spec:
      workspaces:
        - name: ws
      resources:
        inputs:
        - name: docker-source
          type: git
        outputs:
        - name: build-image
          type: image
      params:
        - name: pathToDockerfile
          type: string
          default: $(resources.inputs.docker-source.path)/Dockerfile
        - name: pathToContext
          type: string
          default: $(resources.inputs.docker-source.path)
        - name: commitID
          type: string
          default: latest
      steps:
      - name: build-push
        image: registry.cn-hangzhou.aliyuncs.com/tanber_rp/gcr.io.kaniko-project.executor:v0.16.0
        env:
        - name: "DOCKER_CONFIG"
          value: "tekton/home/.docker/"
        command:
          - /kaniko/executor
        args:
          - --dockerfile=$(params.pathToDockerfile)
          - --destination=$(resources.outputs.build-image.url):$(params.commitID)
          - --context=$(params.pathToContext)
      - name: store-image-url
        image: busybox
        script: |  #这里代码目录下deploy为部署文件,可根据实际情况修改
          cp -r $(resources.inputs.docker-source.path)/deploy/ $(workspaces.ws.path)/
        	url='$(resources.outputs.build-image.url):$(params.commitID)'
          url=${url//\//\\/}
          sed -i "s/image: tanber\/saml_idp:v1/image: ${url}/" $(workspaces.ws.path)/deploy/deploy.yaml
    
    
  • deploy表示部署应用。

    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:
      name: deploy
      namespace: test-tekton
    spec:
      workspaces:
        - name: ws
      params:
        - name: pathToDeploy
          type: string
          default: $(workspaces.ws.path)/deploy/
      steps:
      - name: deploy
        image: docker.io/bitnami/kubectl:1.22.1
        securityContext:
          runAsUser: 0
        securityContext:
          runAsUser: 0
        command:
          - kubectl
        args:
          - apply
          - -f
          - $(params.pathToDeploy)
    

定义Pipeline

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: pipeline
  namespace: test-tekton
spec:
  workspaces:
    - name: ws
  resources:
  - name: docker-source
    type: git
  - name: build-image
    type: image
  params:
    - name: mrState
      type: string
    - name: targetBranch
      type: string
    - name: commitID
      type: string
    - name: pathToDockerfile
      type: string
      default: $(resources.inputs.docker-source.path)/Dockerfile
    - name: pathToContext
      type: string
      default: $(resources.inputs.docker-source.path)
    - name: pathToDeploy
      type: string
      default: $(workspaces.ws.path)/deploy
  tasks:
    - name: build-push
      when:
      - input: "$(params.mrState)"
        operator: in
        values: ["merged"]
      - input: "$(params.targetBranch)"
        operator: in
        values: ["dev"]
      taskRef:
        name: build-push
      workspaces:
        - name: ws
          workspace: ws
      params: 
       - name: commitID
         value: $(params.commitID)
       - name: pathToDockerfile
         value: $(params.pathToDockerfile)
       - name: pathToContext
         value: $(params.pathToContext)
      resources:
        inputs:
          - name: docker-source
            resource: docker-source
        outputs:
          - name: build-image
            resource: build-image
    - name: deploy
      when:
      - input: "$(params.mrState)"
        operator: in
        values: ["merged"]
      - input: "$(params.targetBranch)"
        operator: in
        values: ["dev"]
      runAfter:
      - build-push
      taskRef:
        name: deploy
      workspaces:
        - name: ws
          workspace: ws
      params:
       - name: pathToDeploy
         value: $(params.pathToDeploy)

定义TriggerTemplate

apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
  name: triggertemplate
  namespace: test-tekton
spec:
  params:
    - name: mrState
    - name: targetBranch
    - name: commitID
  resourcetemplates:
    - apiVersion: tekton.dev/v1beta1
      kind: PipelineRun
      metadata:
        generateName: pipelinerun-
        namespace: test-tekton
      spec:
        serviceAccountName: build-bot
        pipelineRef:
          name: pipeline
        workspaces:
          - name: ws
            volumeClaimTemplate:
              spec:
                accessModes:
                  - ReadWriteOnce
                resources:
                  requests:
                    storage: 1Gi
        params: 
          - name: pathToDockerfile
            value: $(resources.inputs.docker-source.path)/Dockerfile
          - name: pathToContext
            value: $(resources.inputs.docker-source.path)
          - name: pathToDeploy
            value: $(workspaces.ws.path)/deploy
          - name: mrState
            value: $(tt.params.mrState)
          - name: targetBranch
            value: $(tt.params.targetBranch)
          - name: commitID
            value: $(tt.params.commitID)
        resources:
          - name: docker-source
            resourceRef:
              name: git-input
          - name: build-image
            resourceRef:
              name: image-output

定义TriggerBinding

apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:
  name: triggerbinding
  namespace: test-tekton
spec:
  params:
    - name: mrState
      value: $(body.object_attributes.state)
    - name: targetBranch
      value: $(body.object_attributes.target_branch)
    - name: commitID
      value: $(body.object_attributes.merge_commit_sha)

定义EventListener

apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
  name: eventlistener
  namespace: test-tekton
spec:
  serviceAccountName: build-bot
  triggers:
    - bindings:
        - ref: triggerbinding
      template:
        ref: triggertemplate

gitlab设置

在Settings–>Webhooks下添加一个webhook,其中URL为创建Eventlistener时自动为你创建的svc,此svc默认是clusterIP,可自行创建一个NodePort类型svc。
在这里插入图片描述

测试

在gitlab上提一个pr到dev分支,并合并就会触发流水线,查看生成的流水线记录。

$ kubectl -n test-tekton get pipelineruns.tekton.dev 
NAME                SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
pipelinerun-mxlbw   True        Completed   8d          8d
pipelinerun-ngj6h   True        Succeeded   8d          7d23h

$ kubectl -n test-tekton get pod
NAME                                           READY   STATUS      RESTARTS   AGE
el-eventlistener-67464796fd-5k62t              1/1     Running     0          8d
pipelinerun-ngj6h-build-push-qjbcz-pod-vltrq   0/5     Completed   0          7d23h
pipelinerun-ngj6h-deploy-rxmd9-pod-hnzps       0/1     Completed   0          7d23h
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值