简介
Tekton是一种基于k8的支持CI/CD的operator。
说到持续集成,我们比较熟悉的有jenkins,gitlab ci等,但只有Tekton是云原生的。
既然Tekton是一种operator,那就必须了解它的CRD,然后我们定义CR,让Tekton在k8上进行调谐。
Tekton CRD
Task: 一个构建任务,含多步骤:编译代码,构建对象,发布的repo等
Pipeline: n Tasks + PipelineResources + variables
TaskRun: 一个Task实例
PipelineRun:一个Pipeline实例
PipelineResource: Pipeline input(如github repo), Pipeline output(如docker hub repo)
安装
安装前请确保您有k8 cluster,我已经安装了minikube和启动了minikube,所以我这里使用以下命令进行安装:
mkdir tekton_learning
cd tekton_learning
curl -k https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml -o release.yaml
kubectl apply -f release.yaml
执行我们可以看到,一堆的rbac, crd, confgimap, deployment, service已经created到tekton-pipelines namespace下。
如果您的minikube没启动,您会遇到connection refuse的error。
定义最简单的task和taskrun
fack-ci-cd.yaml
apiVersion: tekton.dev/v1beta1 kind: Task metadata: name: fake-ci-cd spec: steps: - name: echo image: alpine script: | #!/bin/sh echo "fack ci/cd jobs"
fake-ci-cd-run.yaml
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
name: fake-ci-cd-run
spec:
taskRef:
name: fake-ci-cd
安装到k8
kubectl apply -f fake-ci-cd.yaml
kubectl apply -f fake-ci-cd-run.yaml
查看输出
我们可以看到我们的cr顺利创建,task pod也顺利创建并执行任务了。
carawang@tekton_learning %kubectl get task
NAME AGE
fake-ci-cd 10m
carawang@tekton_learning %kubectl get taskrun
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
fake-ci-cd-run True Succeeded 18m 17m
carawang@tekton_learning %kubectl get events
LAST SEEN TYPE REASON OBJECT MESSAGE
18m Normal Scheduled pod/fake-ci-cd-run-pod Successfully assigned default/fake-ci-cd-run-pod to minikube
18m Normal Pulling pod/fake-ci-cd-run-pod Pulling image "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/entrypoint:v0.63.0@sha256:d83f21f007858846568cb9eaef6c6db89822561bb94545c2f34f3e3f
...
17m Normal Started pod/fake-ci-cd-run-pod Started container prepare
container place-scripts
17m Normal Started pod/fake-ci-cd-run-pod Started container place-scripts
17m Normal Pulling pod/fake-ci-cd-run-pod Pulling image "alpine"
17m Normal Pulled pod/fake-ci-cd-run-pod Successfully pulled image "alpine" in 7.003s (7.003s including waiting). Image size: 7797760 bytes.
17m Normal Created pod/fake-ci-cd-run-pod Created container step-echo
17m Normal Started pod/fake-ci-cd-run-pod Started container step-echo
18m Normal Started taskrun/fake-ci-cd-run
18m Normal Pending taskrun/fake-ci-cd-run Pending
18m Normal Pending taskrun/fake-ci-cd-run pod status "PodReadyToStartContainers":"False"; message: ""
17m Normal Pending taskrun/fake-ci-cd-run pod status "Initialized":"False"; message: "containers with incomplete status: [place-scripts]"
17m Normal Pending taskrun/fake-ci-cd-run pod status "Ready":"False"; message: "containers with unready status: [step-echo]"
17m Normal Running taskrun/fake-ci-cd-run Not all Steps in the Task have finished executing
17m Normal Succeeded taskrun/fake-ci-cd-run All Steps have completed executing
...
定义最简单的pipeline和pipeline
我们在pipeline里引用我们的刚定义的task
project-ci-cd-pipeline.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: project-ci-cd-pipeline
spec:
tasks:
- name: fake-ci-cd
taskRef:
name: fake-ci-cd
project-ci-cd-pipeline-run.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: project-ci-cd-pipeline-run
spec:
pipelineRef:
name: project-ci-cd-pipeline
安装到k8
kubctl apply -f project-ci-cd-pipeline.yaml
kubctl apply -f project-ci-cd-pipeline-run.yaml
查看输出
carawang@tekton_learning %kubectl get pods
NAME READY STATUS RESTARTS AGE
project-ci-cd-pipeline-run-fake-ci-cd-pod 0/1 Completed 0 95s
carawang@tekton_learning %kubectl get pipelinerun
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
project-ci-cd-pipeline-run True Succeeded 2m6s 57s
安装Tekton CLI tkn
我的环境是macos,所以我使用brew进行安装。
brew install tektoncd-cli
brew cleanup tektoncd-cli
使用tkn查看taskrun和pipelinerun的logs
carawang@tekton_learning %tkn pipelinerun logs
[fake-ci-cd : echo] fack ci/cd jobs
carawang@tekton_learning %tkn taskrun logs
? Select taskrun: project-ci-cd-pipeline-run-fake-ci-cd started 10 minutes ago
[echo] fack ci/cd jobs
可以看到更具体的我们的task和pipeline的输出。
tkn help提供了全部的管理tekton pipeline的cmds。请查看并使用。
到底需不需要写taskrun
不必须,我们知道,一个pipeline可以包含多个task, 一个pipelinerun是一个pipeline的实例,而这个pipelinerun的实例自然是实例化多个task来的。当然,有时候为了快速测试你的task没问题,你也可以随时编写taskrun进行测试而不用写pipeline进行测试,就像本文这样。