Knative - Tekton Pipeline入门案例 【十六】
基于Tekton创建并运行Pipeline,通常遵循的步骤
基于Tekton创建并运行Pipeline,通常遵循的步骤:
1、定义Tasks,在各Task中定义Step完成具体的操作步骤;
每个Step都会运行为一个独立的容器,通常我们要根据该Step的目标来选择合用的容器Image;
2、定义Pipeline,引用Task,并以特定的次序组织这些Task的的运行顺序;
3、定义PipelineRun,手动触发Pipeline运行;
创建完成之后,就相当于自动地将Pipeline中引用的Task依次运行为TaskRun;
Pipeline的触发条件:
手动:手动创建一个PipelineRun来完成;
3、定义Trigger,自动触发Pipeline执行;
创建PipelineRun Template,并订阅某类事件;
事件产生时,会自动提取事件中的属性,并赋值给PipelineRun Template,完成PipelineRun实例化
部署实验
Task
【01】Task简单运行
[root@xksmaster1 02-tekton-basics]# cat 01-task-hello.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello
spec:
steps:
- name: say-hello
image: alpine:3.15
command: ['/bin/sh']
args: ['-c', 'echo Hello World']
[root@xksmaster1 02-tekton-basics]# cat 01-taskrun-hello.yaml
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
name: hello-run-00001
spec:
taskRef:
kind: Task
name: hello
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 01-task-hello.yaml
task.tekton.dev/hello configured
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 01-taskrun-hello.yaml
taskrun.tekton.dev/hello-run-00001 created
[root@xksmaster1 02-tekton-basics]# kubectl get pods | grep 0001
hello-run-00001-pod 0/2 Completed 1 58s
#查看日志情况
[root@xksmaster1 02-tekton-basics]# tkn taskrun logs -f hello-run-00001
[say-hello] Hello World
[istio-proxy] 2023/06/26 06:05:06 Exiting...
【02】Task-执行应用 默认参数和指定参数
#Task 默认参数为 MageEdu.Com
[root@xksmaster1 02-tekton-basics]# cat 02-task-with-params.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello-params
spec:
params:
- name: target
type: string
description: Name of somebody or something to greet
default: MageEdu.Com
steps:
- name: say-hello
image: alpine:3.15
command:
- /bin/sh
args: ['-c', 'echo Hello $(params.target)']
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 02-task-with-params.yaml
task.tekton.dev/hello-params created
[root@xianchaomaster1 02-tekton-basics]# tkn task list
NAME DESCRIPTION AGE
hello-params 32 seconds ago
#启动task
[root@xksmaster1 02-tekton-basics]# tkn task start hello-params --showlog
? Value for param `target` of type `string`? (Default is `MageEdu.Com`) MageEdu.Com
TaskRun started: hello-params-run-fg9hd
Waiting for logs to be available...
[say-hello] Hello MageEdu.Com
[root@xksmaster1 02-tekton-basics]# tkn task start hello-params --showlog -p target=BIRKHOFFXIA
TaskRun started: hello-params-run-l8ppr
Waiting for logs to be available...
[say-hello] Hello BIRKHOFFXIA
【03】Task-多Step-执行步骤 First和Second步骤
[root@xksmaster1 02-tekton-basics]# cat 03-task-multi-steps.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: multiple
spec:
steps:
- name: first
image: alpine:3.15
command:
- /bin/sh
args: ['-c', 'echo First Step']
- name: second
image: alpine:3.15
command:
- /bin/sh
args: ['-c', 'echo Second Step']
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 03-task-multi-steps.yaml
task.tekton.dev/multiple created
[root@xksmaster1 02-tekton-basics]# kubectl get task | grep multiple
multiple 22s
[root@xksmaster1 02-tekton-basics]# tkn task start multiple --showlog
TaskRun started: multiple-run-wv2hv
Waiting for logs to be available...
[first] First Step
[second] Second Step
【04】Task-模拟进行多行复杂任务-执行脚本
[root@xksmaster1 02-tekton-basics]# cat 04-task-step-with-script.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: script
spec:
steps:
- name: step-with-script
image: alpine:3.16
script: |
#!/bin/sh
echo "Step with Script..."
echo "Installing necessary tooling"
apk add curl
curl -s www.magedu.com && echo "Success" || echo "Fail"
echo "All done!"
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 04-task-step-with-script.yaml
task.tekton.dev/script created
[root@xianchaomaster1 02-tekton-basics]# tkn task list
NAME DESCRIPTION AGE
script 12 seconds ago
#环境原因 运行失败了 不影响
[root@ca-k8s-master01 02-tekton-basics]# tkn task start script --showlog
TaskRun started: script-run-rmdd2
Waiting for logs to be available...
[step-with-script] Step with Script...
[step-with-script] Installing necessary tooling
[step-with-script] fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
[step-with-script] fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
[step-with-script] (1/5) Installing ca-certificates (20230506-r0)
[step-with-script] (2/5) Installing brotli-libs (1.0.9-r5)
[step-with-script] (3/5) Installing nghttp2-libs (1.46.0-r2)
[step-with-script] (4/5) Installing libcurl (8.5.0-r0)
[step-with-script] (5/5) Installing curl (8.5.0-r0)
[step-with-script] Executing busybox-1.34.1-r7.trigger
[step-with-script] Executing ca-certificates-20230506-r0.trigger
[step-with-script] OK: 8 MiB in 19 packages
[step-with-script] <html>
[step-with-script] <head><title>403 Forbidden</title></head>
[step-with-script] <body>
[step-with-script] <center><h1>403 Forbidden</h1></center>
[step-with-script] <hr><center>nginx</center>
[step-with-script] </body>
[step-with-script] </html>
[step-with-script] Success
[step-with-script] All done!
【05】Task-多行结合参数
[root@xianchaomaster1 02-tekton-basics]# cat 05-task-script-and-parameters.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: logger
spec:
params:
- name: text
type: string
description: something to log
default: "-"
steps:
- name: log
image: alpine:3.15
script: |
apk add -q tzdata
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
DATETIME=$(date "+%F %T")
echo [$DATETIME] - $(params.text)
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 05-task-script-and-parameters.yaml
task.tekton.dev/logger configured
[root@xianchaomaster1 02-tekton-basics]# tkn task list
NAME DESCRIPTION AGE
logger 40 seconds ago
#默认参数为 -
root@xksmaster1 02-tekton-basics]# tkn task start logger --showlog
? Value for param `text` of type `string`? (Default is `-`) -
TaskRun started: logger-run-6qhpk
Waiting for logs to be available...
[log] [2023-07-10 10:56:38] - -
#指定参数为:0319
[root@xianchaomaster1 02-tekton-basics]# tkn task start logger --showlog
? Value for param `text` of type `string`? (Default is `-`) 0319
TaskRun started: logger-run-4hgxk
Waiting for logs to be available...
[log] [2023-07-10 10:56:38] - 0319
Pipeline
【06】Pipeline-顺序执行 taskRef、runafter
[root@xksmaster1 02-tekton-basics]# cat 06-pipeline-demo.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: pipeline-demo
spec:
tasks:
- name: first-task
taskRef:
name: hello
- name: second-task
taskRef:
name: multiple
runAfter:
- first-task
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 06-pipeline-demo.yaml
pipeline.tekton.dev/pipeline-demo created
[root@xksmaster1 02-tekton-basics]# kubectl get pipelines
NAME AGE
hello-goodbye-xks 3h25m
pipeline-demo 38s
[root@xksmaster1 02-tekton-basics]# tkn pipeline list
NAME AGE LAST RUN STARTED DURATION STATUS
hello-goodbye-xks 3 hours ago hello-goodbye-run-ubuntu-xks 3 hours ago 46s Succeeded
pipeline-demo 47 seconds ago --- --- --- ---
[root@xksmaster1 02-tekton-basics]# tkn pipeline start pipeline-demo --showlog
PipelineRun started: pipeline-demo-run-4p97w
Waiting for logs to be available...
[first-task : say-hello] Hello World
[second-task : first] First Step
[second-task : second] Second Step
【07】Pipelinerun-可以指定serviceaccount 、 timeout、
[root@xksmaster1 02-tekton-basics]# cat 07-pipelinerun-demo.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: pipeline-demo-run-xxxxx
namespace: default
spec:
pipelineRef:
name: pipeline-demo
serviceAccountName: default
timeout: 1h0m0s
[root@xianchaomaster1 02-tekton-basics]# kubectl apply -f 07-pipelinerun-demo.yaml
pipelinerun.tekton.dev/pipeline-demo-run-xxxxx created
[root@xianchaomaster1 02-tekton-basics]# tkn pipelinerun list
NAME STARTED DURATION STATUS
pipeline-demo-run-xxxxx 3 minutes ago 8s Succeeded
【08】Pipeline-指定参数 对应关系
[root@xksmaster1 02-tekton-basics]# cat 08-pipeline-with-params.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: pipeline-with-params
spec:
params:
- name: text
type: string
- name: username
type: string
tasks:
- name: task-one
taskRef:
name: hello-params
params:
- name: target
value: $(params.username)
- name: task-two
taskRef:
name: logger
params:
- name: text
value: $(params.text)
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 08-pipeline-with-params.yaml
pipeline.tekton.dev/pipeline-with-params created
[root@xianchaomaster1 02-tekton-basics]# tkn pipeline list
NAME AGE LAST RUN STARTED DURATION STATUS
pipeline-with-params 13 seconds ago --- --- --- ---
【09】PipelineRun-赋值
[root@xksmaster1 02-tekton-basics]# cat 09-pipelinerun-with-params.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: pipeline-with-params-run-xxxxx
namespace: default
spec:
params:
- name: username
value: "MageEdu CloudNative Course"
- name: text
value: "Something has happened ~~."
pipelineRef:
name: pipeline-with-params
serviceAccountName: default
timeout: 10m0s
[root@xksmaster1 02-tekton-basics]# kubectl apply -f 09-pipelinerun-with-params.yaml
pipelinerun.tekton.dev/pipeline-with-params-run-xxxxx created
#查看pipeline 列表
[root@xksmaster1 02-tekton-basics]# tkn pipelinerun list
NAME STARTED DURATION STATUS
pipeline-with-params-run-xxxxx 1 minute ago 16s Succeeded
#查看日志是否执行成功
[root@xianchaomaster1 02-tekton-basics]# tkn pipelinerun logs pipeline-with-params-run-xxxxx -f
[task-one : say-hello] Hello MageEdu CloudNative Course
[task-two : log] [2023-07-10 12:13:18] - Something has happened ~~.
【10】Pipeline-定义各Task的次序

[root@xksmaster1 02-tekton-basics]# cat 10-pipeline-with-ordered-task.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: pipeline-task-ordering
spec:
tasks:
- name: task-a
taskRef:
name: logger
params:
- name: text
value: "task-A executed"
- name: task-b
taskRef:
name: logger
params:
- name: text
value: "Executed after task-A"
runAfter: ["task-a"]
- name: task-c
taskRef:
name: logger
params:
- name: text
value: "Executed after task-A"
runAfter: ["task-a"]
- name: task-d
taskRef:
name: logger
params:
- name: text
value: "Executed after task-B and task-C"
runAfter: ["task-b", "task-c"]
[root@xianchaomaster1 02-tekton-basics]# kubectl apply -f 10-pipeline-with-ordered-task.yaml
pipeline.tekton.dev/pipeline-task-ordering created
[root@xksmaster1 02-tekton-basics]# tkn pipeline list
NAME AGE LAST RUN STARTED DURATION STATUS
pipeline-task-ordering 8 seconds ago --- --- --- ---
#启动pipeline
[root@xianchaomaster1 02-tekton-basics]# tkn pipeline start pipeline-task-ordering --showlog
PipelineRun started: pipeline-task-ordering-run-fb8jx
Waiting for logs to be available...
[task-a : log] [2023-07-10 12:07:35] - task-A executed
[task-b : log] [2023-07-10 12:07:54] - Executed after task-A
[task-c : log] [2023-07-10 12:08:10] - Executed after task-A
[task-d : log] [2023-07-10 12:09:41] - Executed after task-B and task-C
#下图界面显示
#1.启动task-a

#2.task-a启动完成,同时启动task-b、task-c


#3.task-b、task-c完成启动 task-c


#4.全部完成


浙公网安备 33010602011771号