【云原生 | Kubernetes 系列】--Gitops持续交付 Tekton Pipeline使用进阶(pvc和Results)
【云原生 | Kubernetes 系列】--Gitops持续交付 Tekton Pipeline使用进阶(pvc和Results)
Tekton Pipeline使用进阶
1. 基于Maven项目构建
主要实现source to package的过程
包含2个Task: fetch-from-source和build
使用pvc实现2个Task中的数据共享
git-url用来指定git仓库的地址
# cat 05-pipeline-source-to-package.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: source-2-package
spec:
params:
- name: git-url
type: string
workspaces:
- name: codebase
tasks:
- name: fetch-from-source
params:
- name: url
value: $(params.git-url)
taskSpec:
workspaces:
- name: source
params:
- name: url
steps:
- name: git-clone
image: alpine/git:v2.36.1
script: git clone -v $(params.url) $(workspaces.source.path)/source
workspaces:
- name: source
workspace: codebase
- name: build-package
runAfter:
- fetch-from-source
taskSpec:
steps:
- name: build
image: maven:3.8-openjdk-11-slim
workingDir: $(workspaces.source.path)/source
script: |
mvn clean install
workspaces:
- name: source
workspaces:
- name: source
workspace: codebase
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: source-2-package-run-001
spec:
pipelineRef:
name: source-2-package
params:
- name: git-url
value: http://192.168.31.199/tekton/app01.git
workspaces:
- name: codebase
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs-csi
构建pipelinerun
# kubectl apply -f 05-pipeline-source-to-package.yaml
pipeline.tekton.dev/source-2-package created
pipelinerun.tekton.dev/source-2-package-run-001 created
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# tkn pipeline ls |grep source-2-package
source-2-package 37 seconds ago source-2-package-run-001 37 seconds ago --- Running
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# tkn pipelinerun ls |grep source-2-package-run-001
source-2-package-run-001 53 seconds ago --- Running
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# tkn pipelinerun logs source-2-package-run-001
Pipeline still running ...
略...
[build-package : build] [INFO] ------------------------------------------------------------------------
[build-package : build] [INFO] BUILD SUCCESS
[build-package : build] [INFO] ------------------------------------------------------------------------
[build-package : build] [INFO] Total time: 07:11 min
[build-package : build] [INFO] Finished at: 2022-10-27T05:59:55Z
[build-package : build] [INFO] ------------------------------------------------------------------------
基于workspace赋值,无法实现pipelinerun之间的共享
2. 基于Pipeline的存储卷共享
在Task和Step上使用volume
在Task上定义volumes,volumes上有1到多个pvc存储卷
先在Task定义volume,再到step中引用
在Task中定义相当于在Pod定义,在Step引用相当于在容器中引用
使用的volumes需要事先自定义创建.
Workspace: 生命周期同PipelineRun
在同一个PipelineRun内部各TaskRun之间提供的存储空间;
不支持跨PipelineRun共享
Volumes: (在Task上定义,在Step中引用)生命周期独立于Pipeline
生命周期独立于PipelineRun,定义本身是静态的,属于TaskRun
基于同一个Pipeline的多个PipelineRun和TaskRun将共享使用volume
结合上面这个示例,显然在maven这一步给他定义一个volumes,这样在不同的PipelineRun之间就能实现数据共享
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: maven-cache
spec:
storageClassName: nfs-csi
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: source-2-package
spec:
params:
- name: git-url
type: string
workspaces:
- name: codebase
tasks:
- name: fetch-from-source
params:
- name: url
value: $(params.git-url)
taskSpec:
workspaces:
- name: source
params:
- name: url
steps:
- name: git-clone
image: alpine/git:v2.36.1
script: git clone -v $(params.url) $(workspaces.source.path)/source
workspaces:
- name: source
workspace: codebase
- name: build-package
runAfter:
- fetch-from-source
taskSpec:
steps:
- name: build
image: maven:3.8-openjdk-11-slim
workingDir: $(workspaces.source.path)/source
volumeMounts:
- name: m2
mountPath: /root/.m2
script: mvn clean install
workspaces:
- name: source
volumes:
- name: m2
persistentVolumeClaim:
claimName: maven-cache
workspaces:
- name: source
workspace: codebase
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: source-2-package-run-002
spec:
pipelineRef:
name: source-2-package
params:
- name: git-url
#value: https://gitee.com/mageedu/spring-boot-helloWorld.git
value: http://code.gitlab.svc.cluster.local/root/spring-boot-helloWorld.git
workspaces:
- name: codebase
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs-csi
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# cat 06-pipeline-source-to-package.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: maven-cache
spec:
storageClassName: nfs-csi
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: source-2-package
spec:
params:
- name: git-url
type: string
workspaces:
- name: codebase
tasks:
- name: fetch-from-source
params:
- name: url
value: $(params.git-url)
taskSpec:
workspaces:
- name: source
params:
- name: url
steps:
- name: git-clone
image: alpine/git:v2.36.1
script: git clone -v $(params.url) $(workspaces.source.path)/source
workspaces:
- name: source
workspace: codebase
- name: build-package
runAfter:
- fetch-from-source
taskSpec:
steps:
- name: build
image: maven:3.8-openjdk-11-slim
workingDir: $(workspaces.source.path)/source
volumeMounts:
- name: m2
mountPath: /root/.m2
script: mvn clean install
workspaces:
- name: source
volumes:
- name: m2
persistentVolumeClaim:
claimName: maven-cache
workspaces:
- name: source
workspace: codebase
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: source-2-package-run-002
spec:
pipelineRef:
name: source-2-package
params:
- name: git-url
value: https://gitee.com/mageedu/spring-boot-helloWorld.git
#value: http://code.gitlab.svc.cluster.local/root/spring-boot-helloWorld.git
workspaces:
- name: codebase
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs-csi
部署
# kubectl apply -f 06-pipeline-source-to-package.yaml
persistentvolumeclaim/maven-cache created
pipeline.tekton.dev/source-2-package configured
pipelinerun.tekton.dev/source-2-package-run-002 created
此时创建pvc maven-cache 用于TaskRun及PipelineRun之间实现贡献
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
maven-cache Bound pvc-fc28e317-4c36-485f-8db0-493521af5984 5Gi RWX nfs-csi 18s
[build-package : build] [INFO] Installing /workspace/source/source/pom.xml to /root/.m2/repository/com/neo/spring-boot-helloworld/0.9.6-SNAPSHOT/spring-boot-helloworld-0.9.6-SNAPSHOT.pom
[build-package : build] [INFO] ------------------------------------------------------------------------
[build-package : build] [INFO] BUILD SUCCESS
[build-package : build] [INFO] ------------------------------------------------------------------------
[build-package : build] [INFO] Total time: 08:04 min
[build-package : build] [INFO] Finished at: 2022-10-27T07:21:24Z
[build-package : build] [INFO] ------------------------------------------------------------------------
经过漫长的等待后,整个过程花费了8分钟.
再次跑一个pipelinerun
第二次执行时间明显少于第一次,可以看到第一次build过程中那些下载在第二次build过程中没有再次下载,说明2次build使用的是同一个存储卷.即实现了跨pipelinerun的文件共享
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ spring-boot-helloworld ---
[INFO] Building jar: /workspace/source/source/target/spring-boot-helloworld-0.9.6-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:repackage (repackage) @ spring-boot-helloworld ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ spring-boot-helloworld ---
[INFO] Installing /workspace/source/source/target/spring-boot-helloworld-0.9.6-SNAPSHOT.jar to /root/.m2/repository/com/neo/spring-boot-helloworld/0.9.6-SNAPSHOT/spring-boot-helloworld-0.9.6-SNAPSHOT.jar
[INFO] Installing /workspace/source/source/pom.xml to /root/.m2/repository/com/neo/spring-boot-helloworld/0.9.6-SNAPSHOT/spring-boot-helloworld-0.9.6-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.269 s
[INFO] Finished at: 2022-10-31T06:31:08Z
[INFO] ------------------------------------------------------------------------
3. Results使用
Results是用来在Pipeline的Task之间使用同一个Workspace完成数据共享.若需要对简单的字符串数据进行传递,可以使用Results API完成
Results可以用于Task及其Step进行结果的保存,并可以在同一个Pipeline中后续Task中调用结果.
在Task中使用Results
以列表形式定义在spec.results字段中
Task将会为每个results条目自动创建一个文件以进行保存,这些文件放置在/tekton/results目录中
每个results条目的相关值(value)需要在step中进行生成并保存,切task不会对相关数据进行任何多余的操作
在step代码中引用results条目的便捷格式为"$(results..path)",这样就可以避免硬编码
$(params.<param_name>): 引用参数值
( r e s u l t s . < r e s u l t n a m e > . p a t h ) : 保存 r e s u l t 的文件路径 , 通常是指 / t e k t o n / r e s u l t / < r e s u l t n a m e > 若想获得文件内容 s c r i p t : ∣ C O N T E N T = (results.<result_name>.path):保存result的文件路径,通常是指/tekton/result/<result_name> 若想获得文件内容 script: | CONTENT= (results.<resultname>.path):保存result的文件路径,通常是指/tekton/result/<resultname>若想获得文件内容script:∣CONTENT=(cat /tekton/result/<result_name>)
在Pipeline中引用Results
tasks..results.
tasks..results[‘’]或tasks..results[“”]
4. 测试Results调用
# cat 07-results-demo.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: generate-buildid
spec:
params:
- name: version
description: The version of the application
type: string
default: "Ehelp"
results:
- name: datetime
description: The current date and time
- name: buildId
description: The build ID
steps:
- name: generate-datetime
image: ikubernetes/admin-box:v1.2
script: |
#!/usr/bin/env bash
datetime=`date +%Y%m%d-%H%M%S` # 将日期写入datetime变量
echo -n ${datetime} | tee $(results.datetime.path) # 打印datetime变量
# 并将datetime的值写入/tekton/results 即$(results.datetime.path)
- name: generate-buildid
image: ikubernetes/admin-box:v1.2
script: |
#!/usr/bin/env bash
buildDatetime=`cat $(results.datetime.path)` # 将/tekton/results/datetime文件内容写入buildDatetime
buildId=$(params.version)-${buildDatetime}
echo -n ${buildId} | tee $(results.buildId.path)
部署task
# kubectl apply -f 07-results-demo.yaml
task.tekton.dev/generate-buildid created
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# tkn task ls
NAME DESCRIPTION AGE
generate-buildid 50 seconds ago
hello 1 week ago
hello-params 6 days ago
logger 6 days ago
multiple 6 days ago
script 6 days ago
source-lister 5 days ago
workspace-demo 5 days ago
运行taskrun
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# tkn task start generate-buildid
? Value for param `version` of type `string`? (Default is `Ehelp`) Ehelp
TaskRun started: generate-buildid-run-zm572
In order to track the TaskRun progress run:
tkn taskrun logs generate-buildid-run-zm572 -f -n default
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# kubectl get pods
NAME READY STATUS RESTARTS AGE
generate-buildid-run-zm572-pod 0/2 Completed 0 30s
运行结果
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# tkn taskrun describe generate-buildid-run-zm572
Name: generate-buildid-run-zm572
Namespace: default
Task Ref: generate-buildid
Service Account: default
Timeout: 1h0m0s
Labels:
app.kubernetes.io/managed-by=tekton-pipelines
tekton.dev/task=generate-buildid
🌡️ Status
STARTED DURATION STATUS
1 minute ago 20s Succeeded
⚓ Params
NAME VALUE
∙ version Ehelp
📝 Results
NAME VALUE
∙ datetime 20221031-054717
∙ buildId Ehelp-20221031-054717
🦶 Steps
NAME STATUS
∙ generate-datetime Completed
∙ generate-buildid Completed
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# tkn taskrun logs generate-buildid-run-zm572
[generate-datetime] 20221031-054717
[generate-buildid] Ehelp-20221031-054717
$(results.datetime.path)即是/tekton/results/datetime
root@k8s-master-01:/apps/tekton-and-argocd-in-practise/03-tekton-advanced# tkn taskrun logs generate-buildid-run-sgx8b
[generate-datetime] 20221031-062401ls /tekton/
[generate-datetime] total 24
[generate-datetime] drwxrwxrwx 2 root root 4096 Oct 31 06:23 bin
[generate-datetime] drwxrwxrwt 2 root root 40 Oct 31 06:23 creds
[generate-datetime] drwxrwxrwt 3 root root 100 Oct 31 06:24 downward
[generate-datetime] drwxrwxrwx 2 root root 4096 Oct 31 06:23 home
[generate-datetime] drwxrwxrwx 2 root root 4096 Oct 31 06:24 results
[generate-datetime] drwxr-xr-x 4 root root 4096 Oct 31 06:24 run
[generate-datetime] drwxrwxrwx 2 root root 4096 Oct 31 06:23 scripts
[generate-datetime] drwxrwxrwx 2 root root 4096 Oct 31 06:23 steps
[generate-datetime] -rw-rw-rw- 1 root root 0 Oct 31 06:23 termination
[generate-datetime] ls /tekton/results
[generate-datetime] total 4
[generate-datetime] -rw-r--r-- 1 root root 15 Oct 31 06:24 datetime
[generate-buildid] Ehelp-20221031-062401
更多推荐
所有评论(0)