雲原生CICD:Tekton之Task&TaskRun概念篇

Tekton之Task&TaskRun概念篇

1 Tasks

1.1 task定義和簡單使用

Tasks就是任務,任務是你希望在連續集成流程中運行的順序步驟的集合。1個task在k8s集羣中是以pod的形式運行,task中的每個step都是由各自的容器去完成step。舉個例子:

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: echo-hello-world
spec:
  steps:
    - name: echo
      image: ubuntu
      command:
        - echo
      args:
        - "hello world"

上面這個task中的steps後面就是各個step。這邊只有一個step,該step的內容就是在ubuntu:latest容器中運行 echo "hello world"這個命令,你可以在在一個task中定義多個step,每個step還可以執行腳本,後面我會加一個執行腳本的例子。你光創建task資源,任務不會執行,需要使用taskRun纔可以運行task。下面簡單定義一個taskRun:

apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: echo-hello-world-task-run
spec:
  taskRef:
    name: echo-hello-world

這兩個yaml創建後,就可以跑任務了,查看運行的任務情況,使用tkn命令前需要先安裝tekton CLI地址, 這邊簡單介紹下linux下安裝tekton cli:

# Get the tar.xz
curl -LO https://github.com/tektoncd/cli/releases/download/v0.7.1/tkn_0.7.1_Linux_x86_64.tar.gz
# Extract tkn to your PATH (e.g. /usr/local/bin)
sudo tar xvzf tkn_0.7.1_Linux_x86_64.tar.gz -C /usr/local/bin/ 

然後運行tkn命令查看taskrun任務:
tkn taskrun describe echo-hello-world-task-run
可以看下如下輸出:

Name:        echo-hello-world-task-run
Namespace:   default
Task Ref:    echo-hello-world

Status
STARTED         DURATION    STATUS
4 minutes ago   9 seconds   Succeeded

Input Resources
No resources

Output Resources
No resources

Params
No params

Steps
NAME
echo

當然對於想進一步獲取task中每個step日誌信息的用戶可以去這個地址上看下 log。我這邊參見下面的章節有專門講述日誌的。

task的step可用於執行腳本,如下所示:

apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  generateName: step-script-
spec:
  taskSpec:#直接定義task。而不引用task。我不建議這樣使用,這樣的話task無法複用。task和taskrun藕合在一塊了
    params:
    - name: PARAM
      default: param-value

    steps:
    - name: noshebang
      image: ubuntu
      script: echo "no shebang"
    - name: bash
      image: ubuntu
      env:#環境變量
      - name: FOO
        value: foooooooo
      script: |
        #!/usr/bin/env bash
        set -euxo pipefail
        echo "Hello from Bash!"
        echo FOO is ${FOO}
        echo substring is ${FOO:2:4}
        for i in {1..10}; do
          echo line $i
        done
    - name: place-file
      image: ubuntu
      script: |
        #!/usr/bin/env bash
        echo "echo Hello from script file" > /workspace/hello
        chmod +x /workspace/hello
    - name: run-file
      image: ubuntu
      script: |
        #!/usr/bin/env bash
        /workspace/hello
    - name: contains-eof
      image: ubuntu
      script: |
        #!/usr/bin/env bash
        cat > file << EOF
        this file has some contents
        EOF
        cat file
    - name: node
      image: node
      script: |
        #!/usr/bin/env node
        console.log("Hello from Node!")
    - name: python
      image: python
      script: |
        #!/usr/bin/env python3
        print("Hello from Python!")
    - name: perl
      image: perl
      script: |
        #!/usr/bin/perl
        print "Hello from Perl!"
    # Test that param values are replaced.
    - name: params-applied
      image: python
      script: |
        #!/usr/bin/env python3
        v = '$(params.PARAM)'
        if v != 'param-value':
          print('Param values not applied')
          print('Got: ', v)
          exit(1)
    # Test that args are allowed and passed to the script as expected.
    - name: args-allowed
      image: ubuntu
      args: ['hello', 'world']
      script: |
        #!/usr/bin/env bash
        [[ $# == 2 ]]
        [[ $1 == "hello" ]]
        [[ $2 == "world" ]]

step中可執行腳本,可操作性大,可以在容器中執行你要的任務。

1.2 task的輸入和輸出

在常用的場景中,task需要多個步驟(step)來處理輸入和輸出資源,例如task需要從Github上拉代碼倉庫或者構建docker鏡像。PipelineResources就是被用來定義任務輸入和輸出的工件,有幾種系統定義的資源類型可供使用,以下是兩個常用資源的示例。
這是一個定義git的pipelineResource

apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
  name: skaffold-git
spec:
  type: git
  params:
    - name: revision
      value: master #分支名
    - name: url
      value: https://github.com/fishingfly/golang-test.git #github倉庫地址

這是定義docker image的pipelineResource:

apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
  name: skaffold-image-leeroy-web
spec:
  type: image
  params:
    - name: url
      value: gcr.io/<use your project>/leeroy-web #這邊是谷歌雲的地址,最好改成你阿里雲倉庫地址或者也可以是你是私有倉庫比地址(私有倉庫的話需要額外配置,後面會有例子)

1.3 task、pipelineResource和taskRun使用場景

這三者連用就可以實現的場景就是製作鏡像並推送到倉庫。代碼源來自Github,可以是公有倉庫代碼也可以是私有倉庫代碼,拉下來之後根據代碼是java還是golang進行構建編譯,最後根據倉庫中的Dockerfile進行鏡像製作,製作完成可以推送到公有云容器鏡像倉庫或者你的私有倉庫。這類場景是我用tekton的初衷。之後會分享出來,盡情期待。

2 TaskRun

2.1 定義

使用TaskRun資源對象創建並運行羣集上的進程以完成操作。task只是定義了一個任務模版,taskRun才真正代表了一次實際的運行,啓動taskrun纔可以運行task,當然你也可以自己手動創建一個taskRun,taskRun創建出來之後,就會自動觸發task描述的構建任務。taskRun只有當task的所有Step都執行完成纔會運行完。

2.2 使用

在taskrun中引用task的方式:

spec:
  taskRef:
    name: read-task

在taskrun中直接定義task的內嵌:

spec:
  taskSpec:
    resources:
      inputs:
        - name: workspace
          type: git
    steps:
      - name: build-and-push
        image: gcr.io/kaniko-project/executor:v0.17.1
        # specifying DOCKER_CONFIG is required to allow kaniko to detect docker credential
        env:
          - name: "DOCKER_CONFIG"
            value: "/tekton/home/.docker/"
        command:
          - /kaniko/executor
        args:
          - --destination=gcr.io/my-project/gohelloworld

taskrun中設置參數:

spec:
  params:
    - name: flags
      value: -someflag

提供resource:
task需要inputresource和outputresouce,分別代表task的輸入資源和輸出資源,可以由已有的pipelineresource提供:

spec:
  resources:
    inputs:
      - name: workspace
        resourceRef:
          name: java-git-resource
    outputs:
      - name: image
        resourceRef:
          name: my-app-image

當然你直接可以在taskrun中定義這些resource:

spec:
  resources:
    inputs:
      - name: workspace
        resourceSpec:
          type: git
          params:
            - name: url
              value: https://github.com/pivotal-nader-ziada/gohelloworld

配置超時時間:
default-timeout-minutes未設置的i情況下,默認60分鐘,也就是說如果taskrun在60分鐘內沒有運行完成,就會自動kill taskrun,以免掛着佔用資源,這個參數可以在config/config.yaml中修改,這個之後會有案例。
下面是task和taskrun聯合使用的例子:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: mytask
  namespace: default
spec:
  steps:
    - name: writesomething
      image: ubuntu
      command: ["bash", "-c"]
      args: ["echo 'foo' > /my-cache/bar"]
      volumeMounts:
        - name: my-cache
          mountPath: /my-cache
---
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: mytaskrun
  namespace: default
spec:
  taskRef:
    name: mytask
  podTemplate:
    schedulerName: volcano
    securityContext: #安全策略
      runAsNonRoot: true #使用非root的用戶去運行
    volumes:
    - name: my-cache
      persistentVolumeClaim:
        claimName: my-volume-claim

總結

本篇只介紹了task和taskrun的基本概念,屬於教程類,後面我會根據實際需要寫一個完整的例子,便於大家理解和實際使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章