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

相关推荐
行走的山峰1 天前
k8s证书过期处理
k8s
G皮T3 天前
【Kubernetes】K8s 的鉴权管理(二):基于属性 / 节点 / Webhook 的访问控制
kubernetes·k8s·鉴权·rbac·webhook·访问控制·abac
橙子家4 天前
k8s 中的 Service 简介【k8s 系列之二】
k8s
KubeSphere 云原生4 天前
云原生周刊:OpenTofu Registry 获得用户界面和 API|2024.9.9
云计算·k8s·容器平台·kubesphere
阿珂来了7 天前
怎么安装Grafana-Loki
linux·k8s·grafana
滴石编程9 天前
云原生技术:‌引领数字化转型的新浪潮
云原生·k8s·容器自动化
solinger10 天前
生成tekton dashboard
tekton
王一横不要停止努力吖10 天前
ELK学习笔记(二)——使用K8S部署Kibana8.15.0
笔记·学习·elk·k8s
solinger10 天前
tekton构建标准ci(clone repo, test, build & push img)
ci/cd·tekton