Tekton简介,安装和构建最简单ci/cd

简介

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,所以我这里使用以下命令进行安装:

bash 复制代码
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

bash 复制代码
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

bash 复制代码
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: fake-ci-cd-run
spec:
  taskRef:
    name: fake-ci-cd

安装到k8

bash 复制代码
kubectl apply -f fake-ci-cd.yaml
kubectl apply -f fake-ci-cd-run.yaml

查看输出

我们可以看到我们的cr顺利创建,task pod也顺利创建并执行任务了。

bash 复制代码
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

bash 复制代码
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

bash 复制代码
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: project-ci-cd-pipeline-run
spec:
  pipelineRef:
    name: project-ci-cd-pipeline

安装到k8

bash 复制代码
kubctl apply -f project-ci-cd-pipeline.yaml
kubctl apply -f project-ci-cd-pipeline-run.yaml

查看输出

bash 复制代码
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进行安装。

bash 复制代码
brew install tektoncd-cli
brew cleanup tektoncd-cli

使用tkn查看taskrun和pipelinerun的logs

bash 复制代码
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进行测试,就像本文这样。

相关推荐
橙子家9 小时前
关于 K8s 的一些基础概念整理-补充【k8s系列之五】
k8s
nangonghen10 小时前
在华为云通过operator部署Doris v2.1集群
kubernetes·华为云·doris·operator
于顾而言2 天前
【Enjoy Kubernetes】1. 基础入门
开发语言·云原生·k8s
roshy2 天前
POD 存储、PV、PVC
docker·k8s·pod
qq_338032923 天前
k8s总结
云原生·容器·kubernetes·k8s
KubeSphere 云原生3 天前
云原生周刊:Kubernetes v1.32 正式发布
云计算·k8s·容器平台·kubesphere
TiDB_PingCAP3 天前
知乎 PB 级别 TiDB 数据库集群管控实践
k8s·tidb·tidb operator
KubeSphere 云原生4 天前
拒绝 Helm? 如何在 K8s 上部署 KRaft 模式 Kafka 集群?
云计算·k8s·容器平台·kubesphere
年薪丰厚4 天前
如何查看K8S集群中service和pod定义的网段范围
docker·云原生·容器·kubernetes·k8s
麻辣翅尖8 天前
【解决】k8s使用flannel网络插件的问题整理
kubernetes·k8s·报错