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 天前
Linux云计算 |【第五阶段】CLOUD-DAY8
linux·运维·docker·云计算·k8s·学习方法
少陽君3 天前
k8s Sidecar代理
运维·云原生·容器·kubernetes·k8s
小云小白3 天前
springboot 传统应用程序,适配云原生改造
云原生·系统架构·k8s·springboot
小安运维日记4 天前
Linux云计算 |【第五阶段】CLOUD-DAY7
linux·运维·docker·云计算·k8s·podman
小安运维日记4 天前
Linux云计算 |【第五阶段】CLOUD-DAY9
linux·运维·docker·云计算·k8s·改行学it
Stride Max Zz5 天前
k8s技术全景:架构、应用与优化
云原生·k8s
为什么这亚子5 天前
六、Go语言快速入门之数组和切片
开发语言·后端·python·云原生·golang·kubernetes·k8s
小安运维日记6 天前
Linux云计算 |【第五阶段】CLOUD-DAY10
linux·运维·云计算·k8s·grafana·prometheus
为什么这亚子6 天前
三、k8s快速入门之Kubectl 命令基础操作
云原生·容器·kubernetes·k8s
小安运维日记6 天前
Linux云计算 |【第五阶段】CLOUD-DAY6
linux·运维·服务器·docker·云计算·k8s