乔克视界 乔克视界
首页
  • 运维
  • 开发
  • 监控
  • 安全
  • 随笔
  • Docker
  • Golang
  • Python
  • AIOps
  • DevOps
  • Kubernetes
  • 心情杂货
  • 读书笔记
  • 面试
  • 实用技巧
  • 博客搭建
友链
关于
收藏
  • 分类
  • 标签
  • 归档

乔克

云原生爱好者
首页
  • 运维
  • 开发
  • 监控
  • 安全
  • 随笔
  • Docker
  • Golang
  • Python
  • AIOps
  • DevOps
  • Kubernetes
  • 心情杂货
  • 读书笔记
  • 面试
  • 实用技巧
  • 博客搭建
友链
关于
收藏
  • 分类
  • 标签
  • 归档
  • Docker

  • Golang

  • AIOps

  • Python

  • DevOps

    • Tekton

      • Tekton系列之安装篇
      • Tekton系列之理论篇
      • Tekton系列之实践篇-我的第一条Pipeline
        • 拉取代码
        • 单元测试
        • 构建镜像/推送
        • 部署应用
        • 整合成 Pipeline
        • 运行测试
        • 总结
      • Tekton系列之实践篇-把Jenkinsfile变成Tekton Pipeline
      • Tekton系列之实践篇-使用Jenkins来管理Tekton
      • Tekton系列之实践篇-使用Tekton Trigger使Tekton使用更简单
      • Tekton系列之实践篇-Tekton和Argocd的梦幻联动
    • ArgoWorkflow

    • Gitlab

    • Ansible

  • Kubernetes

  • 专栏
  • DevOps
  • Tekton
乔克
2025-07-19
目录

Tekton系列之实践篇-我的第一条Pipeline

前面已经完成了 Tekton 的安装和理论知识的介绍,如果你认真的看完了文章,相信你会有所收获。

这篇文章主要带你来真正实践一下,完成自己的第一条流水线。

我们流水线的整体流程如下。

eb64dd63113e9b06498ec3993929b804 MD5

整个流程是不是很简单?是的,这是最基本的流程,其实只需要把最基本的搞通,其他的都是在基础之上进行扩展。

这里使用 Go 简单写了一个小代码用于测试,地址是:https://gitee.com/coolops/devops-hello-world.git (opens new window)。

如果使用 Jenkins 来实现上面的功能,就只需要编写一个 Jenkinsfile,然后在里面写 4 个 stage 就好。现在用 Tekton,就需要将上面的步骤定义为 4 个 Task,然后通过 Pipeline 将它们串起来,下面会先定义 Task,在将 Task 组合成 Pipeline。

# 拉取代码

代码是交付的基石,是后续的所有动作做铺垫的,我们需要创建一个拉取代码的 Task。

不过这个 Task,我们可以不用自己写,直接用 Tekton Hub 上别人写好的,地址是:https://hub.tekton.dev/tekton/task/git-clone (opens new window)。这个 Task 支持的功能比较全,参数也比较多,具体有哪些参数可以到上面的地址进行查看和学习。

其安装方式有两种:kubectl 和 tkn 客户端。

(1)使用 kubectl 进行安装

kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/git-clone/0.5/git-clone.yaml
1

(2)使用 tkn 客户端进行安装

tkn hub install task git-clone
1

我这里使用的第二种安装方式,安装后可以看到具体的 Task。

# tkn hub install task git-clone
Task git-clone(0.5) installed in default namespace
# kubectl get task | grep git-clone
git-clone        54s
1
2
3
4

这个 Task 到底能不能满足我们的需求呢?我们可以创建一个 TaskRun 来进行测试,如下(由于还没有配置拉取代码仓库的用户名和密码,这里先用一个公开的仓库进行测试)。

apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: test-git-clone
  namespace: default
spec:
  workspaces:
    - name: output
      emptyDir: {}
  params:
    - name: url
      value: "https://gitee.com/coolops/tekton-install.git"
    - name: revision
      value: "master"
    - name: gitInitImage
      value: "registry.cn-hangzhou.aliyuncs.com/coolops/tekton-git-init:v0.29"
  taskRef:
    name: git-clone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

运行过后,可以看到代码正常拉取。

3d424fe2f665710f34a419125a4f9630 MD5

# 单元测试

单元测试比较简单,基本就是执行go test ./...命令就行,比如。

> go test ./...
ok      devops-hello-world      0.313s
ok      devops-hello-world/pkg  (cached)
1
2
3

所以这个 Task,只需要一个 Go 环境,能执行 Go 命令即可,如下:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: unit-test
spec:
  workspaces:
    - name: source
  steps:
    - name: unit-test
      workingDir: $(workspaces.source.path)
      image: golang:1.17.5
      env:
        - name: GOPROXY
          value: https://goproxy.cn
      command: ["go"]
      args:
        - "test"
        - "./..."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 构建镜像/推送

为什么这里没有单独把应用构建组成一个 Task 呢?主要是我们在这里采用了多阶段构建,我们可以将应用构建-镜像打包写在一个 Dockerfile 中,所以这里只需要写一个 Task。

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: build-push-image
spec:
  params:
    - name: pathToDockerfile
      description: The path to the dockerfile to build (relative to the context)
      default: Dockerfile
    - name: imageUrl
      description: Url of image repository
    - name: imageTag
      description: Tag to apply to the built image
      default: latest
  workspaces:
    - name: source
    - name: dockerconfig
      mountPath: /kaniko/.docker # config.json 的挂载目录
  steps:
    - name: build-and-push
      image: registry.cn-hangzhou.aliyuncs.com/coolops/kaniko-executor:v1.5.0
      workingDir: $(workspaces.source.path)
      command:
        - /kaniko/executor
      args:
        - --dockerfile=$(params.pathToDockerfile)
        - --destination=$(params.imageUrl):$(params.imageTag)
        - --context=$(workspaces.source.path)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

我们这里采用 kaniko 进行构建镜像,用这种方式不用挂载 docker.sock 文件,但是我们需要将 docker config 保存在/kaniko/.docker 目录下。我们可以通过如下命令来创建 secret。

kubectl create secret docker-registry dockerhub --docker-server=https://index.docker.io/v1/ --docker-username=[USERNAME] --docker-password=[PASSWORD] --dry-run=client -o json | jq -r '.data.".dockerconfigjson"' | base64 -d > /tmp/config.json && kubectl create secret generic docker-config --from-file=/tmp/config.json && rm -f /tmp/config.json
1

如果在运行上面命令的时候没有 jq 命令,就需要你安装一下。

yum install jq -y
1

# 部署应用

这里采用的 deployment 的方式部署应用,所以只需要使用 kubectl 进行部署即可。

不过在使用 kubectl 的时候需要/root/.kube/config 文件,所以这里依然将 config 文件通过 secret 挂载到容器中。

创建一个 secret,如下:

kubectl create secret generic kubernetes-config --from-file=/root/.kube/config
1

然后创建 Task,如下:

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: deploy-to-k8s
spec:
  workspaces:
    - name: source
    - name: kubernetesconfig
      mountPath: /root/.kube
  params:
    - name: pathToYamlFile
      description: The path to the yaml file to deploy within the git source
      default: deployment.yaml
    - name: IMAGE
    - name: TAG
  steps:
    - name: run-kubectl
      image: registry.cn-hangzhou.aliyuncs.com/coolops/kubectl:1.19.16
      workingDir: $(workspaces.source.path)
      script: |
        sed -i s#IMAGE#$(params.IMAGE)#g $(params.pathToYamlFile)
        sed -i s#TAG#$(params.TAG)#g $(params.pathToYamlFile)
        kubectl apply -f $(params.pathToYamlFile)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 整合成 Pipeline

上面我们已经把每一步整理成了 Task,下面就应该进行 Pipeline 的组合了,然后再声明需要的变量就可以,如下:

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: devops-hello-world-pipeline
spec:
  workspaces: # 声明 workspaces
    - name: go-repo-pvc
    - name: docker-config
    - name: kubernetes-config
  params:
    # 定义代码仓库
    - name: git_url
    - name: revision
      type: string
      default: "master"
    - name: gitInitImage
      type: string
      default: "registry.cn-hangzhou.aliyuncs.com/coolops/tekton-git-init:v0.29"
    # 定义镜像参数
    - name: pathToDockerfile
      description: The path to the build context, used by Kaniko - within the workspace
      default: .
    - name: imageUrl
      description: Url of image repository
    - name: imageTag
      description: Tag to apply to the built image
      default: latest
  tasks: # 添加task到流水线中
    - name: clone
      taskRef:
        name: git-clone
      workspaces:
        - name: output
          workspace: go-repo-pvc
      params:
        - name: url
          value: $(params.git_url)
        - name: revision
          value: $(params.revision)
        - name: gitInitImage
          value: $(params.gitInitImage)
    - name: unit-test
      workspaces: # 传递 workspaces
        - name: source
          workspace: go-repo-pvc
      taskRef:
        name: unit-test
      runAfter:
        - clone
    - name: build-push-image
      params:
        - name: pathToDockerfile
          value: $(params.pathToDockerfile)
        - name: imageUrl
          value: $(params.imageUrl)
        - name: imageTag
          value: $(params.imageTag)
      taskRef:
        name: build-push-image
      runAfter:
        - unit-test
      workspaces: # 传递 workspaces
        - name: source
          workspace: go-repo-pvc
        - name: dockerconfig
          workspace: docker-config
    - name: deploy-to-k8s
      taskRef:
        name: deploy-to-k8s
      params:
        - name: pathToYamlFile
          value: deployment.yaml
        - name: IMAGE
          value: $(params.imageUrl)
        - name: TAG
          value: $(params.imageTag)
      workspaces:
        - name: source
          workspace: go-repo-pvc
        - name: kubernetesconfig
          workspace: kubernetes-config
      runAfter:
        - build-push-image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

# 运行测试

运行测试就是创建 PipelineRun,不过在创建之前,我们先创建需要的认证信息。

apiVersion: v1
kind: Secret
metadata:
  name: gitlab-auth
  annotations:
    tekton.dev/git-0: https://gitee.com/ # 这里使用的gitee仓库
type: kubernetes.io/basic-auth
stringData:
  username: xxxx
  password: xxxx
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: tekton-build-sa
secrets:
  - name: gitlab-auth
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: tekton-clusterrole-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: edit
subjects:
  - kind: ServiceAccount
    name: tekton-build-sa
    namespace: default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

然后我们就可以创建 PipelineRun 了,如下:

apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: devops-hello-world-pipeline-run
spec:
  pipelineRef:
    name: devops-hello-world-pipeline
  params:
    - name: revision
      value: master
    - name: git_url
      value: https://gitee.com/coolops/devops-hello-world.git
    - name: imageUrl
      value: registry.cn-hangzhou.aliyuncs.com/coolops/devops-hello-world
    - name: imageTag
      value: latest
    - name: pathToDockerfile
      value: Dockerfile
  workspaces:
    - name: go-repo-pvc
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          storageClassName: openebs-hostpath
          resources:
            requests:
              storage: 1Gi
    - name: docker-config
      secret:
        secretName: docker-config
    - name: kubernetes-config
      secret:
        secretName: kubernetes-config
  serviceAccountName: tekton-build-sa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

然后我们在 Tekton 的 Dashboard 上看到 Pipeline 运行成功。

661a66d2c67737d04190ac91f6d2fe0c MD5

应用也启动成功。

# kubectl get po | grep http
httpserver-696479dd5d-qplnx                                       2/2     Running     0          2m18s
1
2

访问页面,返回正常。

# curl 10.102.140.2:8080
{ "data": 300, "say": "Hello World" }
1
2

但是上面我们是固定的镜像 TAG,在实际工作中,很多固定,所以我们对其改造一下。在 Tekton Hub 上的 git-clone Task 会输出 commit results,我们可以使用 commit ID 作为镜像 Tag,改造后的的 Pipeline 如下:

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: devops-hello-world-pipeline
spec:
  workspaces: # 声明 workspaces
    - name: go-repo-pvc
    - name: docker-config
    - name: kubernetes-config
  params:
    # 定义代码仓库
    - name: git_url
    - name: revision
      type: string
      default: "master"
    - name: gitInitImage
      type: string
      default: "registry.cn-hangzhou.aliyuncs.com/coolops/tekton-git-init:v0.29"
    # 定义镜像参数
    - name: pathToDockerfile
      description: The path to the build context, used by Kaniko - within the workspace
      default: .
    - name: imageUrl
      description: Url of image repository
    - name: imageTag
      description: Tag to apply to the built image
      default: latest
  tasks: # 添加task到流水线中
    - name: clone
      taskRef:
        name: git-clone
      workspaces:
        - name: output
          workspace: go-repo-pvc
      params:
        - name: url
          value: $(params.git_url)
        - name: revision
          value: $(params.revision)
        - name: gitInitImage
          value: $(params.gitInitImage)
    - name: unit-test
      workspaces: # 传递 workspaces
        - name: source
          workspace: go-repo-pvc
      taskRef:
        name: unit-test
      runAfter:
        - clone
    - name: build-push-image
      params:
        - name: pathToDockerfile
          value: $(params.pathToDockerfile)
        - name: imageUrl
          value: $(params.imageUrl)
        - name: imageTag
          value: $(tasks.clone.results.commit)
      taskRef:
        name: build-push-image
      runAfter:
        - unit-test
      workspaces: # 传递 workspaces
        - name: source
          workspace: go-repo-pvc
        - name: dockerconfig
          workspace: docker-config
    - name: deploy-to-k8s
      taskRef:
        name: deploy-to-k8s
      params:
        - name: pathToYamlFile
          value: deployment.yaml
        - name: IMAGE
          value: $(params.imageUrl)
        - name: TAG
          value: $(tasks.clone.results.commit)
      workspaces:
        - name: source
          workspace: go-repo-pvc
        - name: kubernetesconfig
          workspace: kubernetes-config
      runAfter:
        - build-push-image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

我们在后面的 Task 中引用前面 Task 的 输出,使用$(tasks.clone.results.commit)即可。

重新跑 Pipeline,构建出来的镜像 Tag 就是 commit ID,如下:

d3d2dc5196613ff86fa29c2daee7a8d2 MD5

代码仓库最后一次提交的 commit ID。

fe0a399e9638a198752027aed9b706c4 MD5

# 总结

整个流水线看起来很简单,在调试的时候还是费一定的周折,主要是定义的参数传过去传过来,有时候就忘记指定了,就要不断的调试。

文章中所有的代码和 YAML 清单我都放在 Gitee(https://gitee.com/coolops/devops-hello-world (opens new window))上了,有需要的可以自己去拿。

上次更新: 2025/07/19, 11:33:23
Tekton系列之理论篇
Tekton系列之实践篇-把Jenkinsfile变成Tekton Pipeline

← Tekton系列之理论篇 Tekton系列之实践篇-把Jenkinsfile变成Tekton Pipeline→

最近更新
01
读《额尔古纳河右岸》有感
07-19
02
读《命运》有感
07-19
03
读《月亮与六便士》有感
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2025 乔克 | MIT License | 渝ICP备20002153号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式