云原生 | 下一代CI/CD工具,Tekton牛刀小试自动化流水线

ca4b6f6126e81690f2f8693a9706a48e.jpeg

ed539887fb8607236114e0938c048762.gif

关注回复【学习交流群】加入【安全开发运维】答疑交流群

请朋友们【多多点击文中的广告】,支持作者更新更多文章

原文连接: 

云原生 | 下一代CI/CD工具,Tekton牛刀小试自动化流水线本文主要讲解了Tekton中Task、Taskrun、Pipeline、Pipelinerun以及Triggers触发器等对象的简单示例样式,帮助各位看友可以快速入门学习及上手Tekton这款云原生的CI/CD工具,让各位运维人减少工作时间https://mp.weixin.qq.com/s/TMYdGw4A4G-3zDYzmFWrrw


目录:

13cd3e52a84e7be2bf09580c0ef71088.png

本文为作者原创文章,为尊重作者劳动成果禁止非授权转载,若需转载请在【全栈工程师修炼指南】公众号留言,或者发送邮件到 [master@weiyigeek.top] 中我将及时回复。


0x02 简单使用

1.tekton 最小单位 Task 任务创建运行

描述: Task在API中表示为一种Task对象,它定义了一系列按顺序运行的步骤,以执行Task所需的逻辑。每个Task都作为一个pod在Kubernetes集群上运行,每个步骤都在自己的容器中运行。

Step 1.创建一个最简单的Task资源清单, 执行输出一段字符串。

# create a Task
tee Hello-World-Task.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: hello
spec:
  steps:
    - name: echo
      image: alpine:3.18
      script: |
        #!/bin/sh
        echo "Hello World, Tekton!" 
EOF


# create a TaskRun
tee Hello-World-TaskRun.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: hello-task-run
spec:
  taskRef:
    name: hello
EOF

Step 2.创建运行Task 并查看运行的 TaskRun 任务。

# 部署 Task & TaskRun
kubectl apply --filename Hello-World-Task.yaml
  # task.tekton.dev/hello createdkubectl apply --filename Hello-World-TaskRun.yaml 
  # taskrun.tekton.dev/hello-task-run created


# 查看部署的 TaskRun 任务与Pod
kubectl get taskrun hello-task-run
  # NAME             SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
  # hello-task-run   True        Succeeded   118s        12s
kubectl get pod hello-task-run-pod
  # NAME                 READY   STATUS      RESTARTS   AGE
  # hello-task-run-pod   0/1     Completed   0          2m28s

Step 3.查看Task任务运行的结果

kubectl logs --selector=tekton.dev/taskRun=hello-task-run
  # Hello World, Tekton!

2.使用pipeline流水线调用带有参数的Task任务

描述: 此小节作者创建并运行第一个 Tekton 管道,可以预定义参数。

Step 1.创建一个名为user的 Task 任务,根据参数输出一段字符串,需要注意的是在没有设置默认值时必须传入指定的参数值。

tee User-Task.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: user
spec:
  params:
  - name: username
    type: string
  - name: url
    type: string
  steps:
    - name: hi
      image: ubuntu
      script: |
        #!/bin/bash
        echo "Hi, $(params.username)!"
        echo "Blog: $(params.url)"
EOF

Step 2.创建一个名为hello-user的 Pipeline 以及运行 PipelineRun 流水线资源清单。

# Tekton-Pipeline
tee Tekton-Pipeline.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: hello-user
spec:
  params:
  - name: username
    type: string
  - name: url
    type: string
  tasks:
    - name: hello
      taskRef:
        name: hello
    - name: user
      runAfter:
        - hello
      taskRef:
        name: user
      params:
      - name: username
        value: $(params.username)
      - name: url
        value: $(params.url)
EOF


# Tekton-PipelineRun : 传入预定义的参数值并调用执行 pipeline。
tee Tekton-PipelineRun.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: hello-user-run
spec:
  pipelineRef:
    name: hello-user
  params:
  - name: username
    value: "Tekton"
  - name: url
    value: "https://blog.weiyigeek.top"
EOF

Step 3.依次部署TaskPipeline以及PipelineRun

kubectl apply -f User-Task.yaml
  # task.tekton.dev/user created


kubectl apply -f Tekton-Pipeline.yaml 
  # pipeline.tekton.dev/hello-user created


kubectl apply -f Tekton-PipelineRun.yaml
  # pipelinerun.tekton.dev/hello-user-run created

Step 4.查看部署运行结果。

kubectl get pipelines hello-user
  # NAME         AGE
  # hello-user   81s


kubectl get pipelineruns hello-user-run
  # NAME             SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
  # hello-user-run   True        Succeeded   37s         11s
  
kubectl get pod -l tekton.dev/pipelineRun=hello-user-run     
  # NAME                      READY    STATUS      RESTARTS   AGE
  # hello-user-run-hello-pod   0/1     Completed      0          59s
  # hello-user-run-user-pod    0/1     Completed       0          55s


kubectl logs -l tekton.dev/pipelineRun=hello-user-run
  # Hello World, Tekton!
  # Hi, Tekton!
  # Blog: https://blog.weiyigeek.top

Step 5.我们也可以使用前面安装的tkn命令tkn pipelinerun logs hello-user-run -f -n default,来查看创建的pipelinerun执行结果。

5c4456fb1cfdd21670e3bb715e5ad6c0.png

Step 6.除此之外,我们也可以使用前面安装的Tekton Dashboard的UI界面查看运行情况。

2958c2267cb8e1d3eaa57f53ccc3c3d2.png


3.使用Triggers触发器来调用流水线从而带有参数的Task任务

描述: 此处小节将快速演示triggers触发器创建及其使用,在前面编写的 Task 与 Pipelines 下进行。

Step 1.首先创建一个 TriggerTemplate 以及 TriggerBinding 对象.

# Create a TriggerTemplate
tee Tekton-TriggerTemplate.yaml <<'EOF'
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
  name: hello-user-template
spec:
  params:
  - name: username
    default: "WeiyiGeek"
  - name: url
    default: "https://www.weiyigeek.top"
  resourcetemplates:
  - apiVersion: tekton.dev/v1beta1
    kind: PipelineRun
    metadata:
      generateName: hello-user-run-
    spec:
      pipelineRef:
        name: hello-user
      params:
      - name: username
        value: $(tt.params.username)
      - name: url
        value: $(tt.params.url)
EOF


# Create a TriggerBinding 
tee Tekton-TriggerBinding.yaml <<'EOF'
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:
  name: hello-user-binding
spec: 
  params:
  - name: username
    value: $(body.username)
  - name: url
    value: $(body.url)
EOF

Step 2.然后再创建 EventListener RABC 以及 EventListener (事件监听器)

# Create an EventListener RABC
tee Tekton-Trigger-Rabc.yaml <<'EOF'
apiVersion: v1
kind: ServiceAccount
metadata:
  name: tekton-robot
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: triggers-example-eventlistener-binding
subjects:
- kind: ServiceAccount
  name: tekton-robot
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: tekton-triggers-eventlistener-roles
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: triggers-example-eventlistener-clusterbinding
subjects:
- kind: ServiceAccount
  name: tekton-robot
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: tekton-triggers-eventlistener-clusterroles
EOF


#  Create an EventListener
tee Tekton-Trigger-EventListener.yaml <<'EOF'
apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
  name: hello-user-listener
spec:
  serviceAccountName: tekton-robot
  triggers:
    - name: hello-trigger 
      bindings:
      - ref: hello-user-binding
      template:
        ref: hello-user-template
EOF

Step 3.将上述资源清单配置到Kubernetes集群中

kubectl apply -f Tekton-TriggerTemplate.yaml
  # triggertemplate.triggers.tekton.dev/hello-user-template created


kubectl apply -f Tekton-TriggerBinding.yaml
  # triggerbinding.triggers.tekton.dev/hello-user-binding created


kubectl apply -f Tekton-Trigger-Rabc.yaml
  # serviceaccount/tekton-robot created
  # rolebinding.rbac.authorization.k8s.io/triggers-example-eventlistener-binding created
  # clusterrolebinding.rbac.authorization.k8s.io/triggers-example-eventlistener-clusterbinding created
  
kubectl apply -f Tekton-Trigger-EventListener.yaml
  # eventlistener.triggers.tekton.dev/hello-user-listener created

Step 4.验证触发器部署情况

# 查看创建的 triggertemplates,triggerbindings,eventliste 等资源
kubectl get triggertemplates,triggerbindings,eventliste


# 事件触发地址
eventlistener.triggers.tekton.dev/hello-user-listener  
http://el-hello-user-listener.default.svc.cluster.test:8080

57bb7bf8c777803de3103c795d6dabcf.png

Step 5.管理与验证触发器是否能触发Pipeline流水线的执行, 此处我直接使用SVC服务名进行演示请求,在实际场景中你可能需要使用ingress进行暴露。

# 查看 K8S 集群的DNS
$ kubectl get svc -n kube-system
  NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
  kube-dns         ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   405d


# 设置集群DNS的地址到主机resolv.conf文件中
vim /etc/resolv.conf


# 验证解析
ping el-hello-user-listener.default.svc.cluster.test


# 使用 curl 模拟请求触发,注意传递的参数哟。
curl -v \
   -H 'content-Type: application/json' \
   -d '{"username": "Tekton","url": "https://www.weiyigeek.top"}' \
   http://el-hello-user-listener.default.svc.cluster.test:8080

执行结果: {"eventListener":"hello-user-listener","namespace":"default","eventListenerUID":"6a6f31ab-47c7-4ef0-9d78-46450fbbc334","eventID":"7accd3ec-21b6-4d38-a164-145a84a3347e"}

5e4f7342f3477f57eb6e78ed36617f74.png


偷偷的告诉你哟?极客全栈修炼】微信小程序已开放

可直接在微信里面直接浏览博主文章哟,后续将上线更多有趣的小工具。


Step 6.最后验证是否触发绑定的流水线的执行,此时检查PipelineRun日志该名称是自动生成的,每次运行都添加一个后缀,在本例中为hello-user-run-s29pd

kubectl get pipelineruns -l triggers.tekton.dev/eventlistener=hello-user-listener
  # NAME                   SUCCEEDED   REASON      STARTTIME   COMPLETIONTIME
  # hello-user-run         True        Succeeded   142m        142m
  # hello-user-run-s29pd   True        Succeeded   4m44s       118s


# 再使用tkn命令中指定PiepelineRun名称查看对应日志
tkn pipelinerun logs hello-user-run-s29pd -f
  [hello : echo] Hello World, Tekton!
  
  [user : hi] Hi, Tekton!
  [user : hi] Blog: https://www.weiyigeek.top

4.使用dashboard来创建运行 TaskRuns 与 PipelineRuns 触发任务执行

描述: 此处简单使用 tekton-dashboard 界面来创建运行 TaskRuns 与 PipelineRuns 示例,值得注意的是tekton-dashboard 不支持Task任务以及Pipeline对象创建,所以你需要提前在K8S中执行创建。

Step 1.首先,我们需要在集群中使用kubectl命令来创建Tekton的最小单元Task资源清单, 此处模拟拉取作者的主页HTML源码。

tee Git-Task.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: git
spec:
  params:
  - name: project-name
    type: string
  - name: project-url
    type: string
  steps:
    - name: git
      image: bitnami/git:2.41.0
      script: |
        #!/usr/bin/env bash
        echo -e "Hello, Tekton Dashboard Test!\nAuthor:WeiyiGeek"
        echo "Project Name:$(params.project-name)"
        echo "Project Url:$(params.project-url)"
        pwd && git version && cd /tmp
        git clone $(params.project-url)
        cd ./$(params.project-name) && pwd && ls
EOF


# 部署该Task
kubectl apply -f Git-Task.yaml
  # task.tekton.dev/git created

Step 2.访问tekton-dashboard 界面创建 TaskRuns,点击 Tekton 资源 -> TaskRuns -> 创建名为 git-task -> 选择参数配置 或者YAML方式配置 (此处我将Yaml配置贴上)

apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: git-task
  namespace: default
  labels:
    action: git
spec:
  taskRef:
    name: git
    kind: Task
  params:
    - name: project-name
      value: weiyigeek
    - name: project-url
      value: https://gitee.com/WeiyiGeek/weiyigeek.git
  serviceAccountName: default

a7afde35bc8a781dacfe8d4a50bfdceb.png

Step 3.点击创建的git-task查看此Task运行结果

bbeff7492f3b997437cf79a5a9f140d2.png

Step 4.前面完成TaskRun对象的创建执行,此处将演示PipelineRuns对象创建和触发执行,同样的你需要在K8S集群先执行部署。

tee Git-Pipeline.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: git
spec:
  params:
  - name: project-name
    type: string
  - name: project-url
    type: string
  tasks:
    - name: git
      taskRef:
        name: git
      params:
      - name: project-name
        value: $(params.project-name)
      - name: project-url
        value: $(params.project-url)
EOF


kubectl apply -f Git-Pipeline.yaml 
  # pipeline.tekton.dev/git created

Step 5.点击,如上所创建 pipelineRuns -> 选择参数或者YAML方式配置, 此处我传递了存放我的主页的代码仓库信息。

apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: git-pipeline-run
  namespace: default
  labels:
    action: git
spec:
  pipelineRef:
    name: git
  params:
    - name: project-name
      value: weiyigeek
    - name: project-url
      value: https://gitee.com/WeiyiGeek/weiyigeek.git
  timeouts:
    pipeline: git
    tasks: git

6fcab3b4b30f5826acffa4db47f49ae8.png

亲,文章就要看完了,不关注一下【全栈工程师修炼指南】吗?

Step 6.然后点击创建的 git-pipeline-run 流水线任务查看其运行的结果。

acb4be2b2ddf9fca66bc0ae50fe4db54.png

温馨提示:更多的发现、搜索和共享可重复使用的任务和管道,可以参考官方提供的类似于Hub的地址 https://hub.tekton.dev/ , 作为新手应该多多学习。

d26bd5a95dc89afbba9ca1727175cc2a.png

至此,在Tekton-Dashboar中创建执行 TaskRuns 以及 PipelineRuns 对象演示完毕。

本文至此完毕,更多技术文章,尽情等待下篇好文!

原文地址: https://blog.weiyigeek.top/2023/7-22-768.html

【 如果此篇文章对你有帮助,请你将它分享给更多的人! 】

b19038a78dd063909a52f5a2e374b778.gif

c4194009c55b855c8d67ccfa43c92d32.png 学习书籍推荐 往期发布文章 2e81ddc2fea44c0a29a30d1225bfe869.png

回复【0014】获取【Nginx学习之路汇总】

回复【0015】获取【Jenkins学习之路汇总】

回复【10005】获取【adb工具刷抖音赚米】

回复【0011】获取【k8S二进制安装部署教程】

回复【10006】获取【CentOS8安全加固脚本】

回复【10001】获取【WinServer安全加固脚本】

回复【0008】获取【Ubuntu22.04安装与加固脚本】

回复【10002】获取【KylinOS银河麒麟安全加固脚本】

 热文推荐  

按(扫描)二维码 关注 【全栈工程师修炼指南】(^U^)ノ~YO

欢迎添加作者微信【weiyigeeker】,一起入坑吧!

关注回复【学习交流群】即可加入【安全开发运维沟通交流群

      各位亲爱的读者,现在公众号更改了推送规则,如果您需要第一时间看到我们推送的好内容。

一定要记得给公众号星标,经常点赞、在看、转发、分享和留下您的评论 !

点击【"阅读原文"】获取更多有趣的知识                                            若有帮助请点个【在看 赞 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值