前言
最近都是在做云原生相关的业务,所以对云原生CI/CD工具Tekton有一定的了解,所以和大家分享一下。
介绍
简单介绍一下tekton中的几个概念
PipelineResource
定义资源,例如输入输出参数等。
Task/ClusterTask
定义任务,也就是我们要执行的某一个或多个步骤构成的任务,任务模版。
PipelineRun/TaskRun
执行任务,也就是调用任务模版,并且传入PipelineResource资源,执行结束后,状态变为completed。
演示
这里展示一个测试用例,示意图:
具体步骤如下:
步骤1
从我自己的git仓库,拉去代码,打包,并且上传到自己的私有仓库。
步骤2
从私有仓库拉取镜像,完成部署
用例展示:
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: helloworld-task
spec:
params:
- name: pathToDockerFile
type: string
description: The path to the dockerfile to build
default: $(resources.inputs.docker-source.path)/Dockerfile
- name: pathToContext
type: string
description: The path to the context to build
default: $(resources.inputs.docker-source.path)
- name: image
type: string
description: |
The build context used by Kaniko
(https://github.com/GoogleContainerTools/kaniko#kaniko-build-contexts)
default: $(resources.outputs.builtImage.url)
resources:
inputs:
- name: docker-source
type: git
outputs:
- name: builtImage
type: image
workspaces:
- name: source
steps:
- name: build-and-push
image: gcr.io/kaniko-project/executor:v0.16.0
env:
- name: "DOCKER_CONFIG"
value: "/tekton/home/.docker/"
command:
- /kaniko/executor
args:
- --dockerfile=$(params.pathToDockerFile)
- --destination=$(params.image)
- --context=$(params.pathToContext)
- --insecure-registry=10.96.0.11:5000
- --verbosity=debug
- --cache=false
- name: pull-and-deploy
image: lachlanevenson/k8s-kubectl
command: ["kubectl"]
args:
- "apply"
- "-f"
- "$(resources.inputs.docker-source.path)/deploy"
---
apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
name: helloworld-git
spec:
type: git
params:
- name: revision
value: master
- name: url
value: https://github.com/wencaiwulue/helloworld.git
---
apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
name: helloworld-image
spec:
type: image
params:
- name: url
value: 10.96.0.11:5000/second/third:latest
---
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
name: helloworld-task-run
spec:
serviceAccountName: tutorial-service
taskRef:
name: helloworld-task
params:
- name: pathToDockerFile
value: $(resources.inputs.docker-source.path)/Dockerfile
- name: pathToContext
value: $(resources.inputs.docker-source.path)
- name: image
value: $(resources.outputs.builtImage.url)
workspaces:
- emptyDir: {}
name: source
resources:
inputs:
- name: docker-source
resourceRef:
name: helloworld-git
outputs:
- name: builtImage
resourceRef:
name: helloworld-image
---
说明
这个基于上一篇博客讲的,构建一个自己的私有仓库,所以这里的私有镜像仓库,需要更改为自己的地址。
验证
现在暂时贴不了结果了,因为自己的测试机,docker被我弄坏了,所以跑不起来,不过在公司的电脑是是可以正常运行的。这个周末再来更新一下吧。