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

相关推荐
菜地里的小菜鸟5 天前
failed to get imageFs info: non-existent label “docker-images
k8s·failedtogetimagefsin·k8s报错
菜地里的小菜鸟7 天前
kubectl debug
k8s·kubectldebug
腾飞开源9 天前
01_K8s干货笔记之认识K8s
运维·笔记·云原生·容器·kubernetes·k8s·容器化部署
小匠石钧知12 天前
02_在多个RockyLinux10虚拟机上安装k8s集群
云原生·容器·kubernetes·k8s
ShirleyWang01218 天前
让headlamp控制台能访问
linux·服务器·python·k8s·k3s
spider_xcxc18 天前
K8s 部署学习笔记
docker·容器·kubernetes·云计算·k8s
ShirleyWang01218 天前
Day02 K3s NGF(Nginx Gateway Fabric)单 Worker 环境网关更新与故障处置 SOP
linux·服务器·python·k8s·k3s
ShirleyWang01218 天前
DAY01 K3s 私有化部署排障复盘与 SOP
linux·服务器·k8s·k3s·企业部署
XUHUOJUN22 天前
AKS 不是安装在 Windows Server 上,而是运行在 Windows Server 之上的 Azure 平台能力
windows·架构·k8s·azure local·azure stack
Geek-Chow22 天前
Connecting kubectl to a Private EKS Cluster Over an Internal Domain
kubernetes·k8s·aws