編輯搜圖
前面已經(jīng)完成了Tekton的安裝和理論知識的介紹,如果你認真的看完了文章,相信你會有所收獲。
這篇文章主要帶你來真正實踐一下,完成自己的第一條流水線。
我們流水線的整體流程如下。
編輯搜圖
整個流程是不是很簡單?是的,這是最基本的流程,其實只需要把最基本的搞通,其他的都是在基礎之上進行擴展。
這里使用Go簡單寫了一個小代碼用于測試,地址是:https://gitee.com/coolops/devops-hello-world.git。
如果使用Jenkins來實現(xiàn)上面的功能,就只需要編寫一個Jenkinsfile,然后在里面寫4個stage就好。現(xiàn)在用Tekton,就需要將上面的步驟定義為4個Task,然后通過Pipeline將它們串起來,下面會先定義Task,再將Task組合成Pipeline。
拉取代碼
代碼是交付的基石,是后續(xù)的所有動作做鋪墊的,我們需要創(chuàng)建一個拉取代碼的Task。
不過這個Task,我們可以不用自己寫,直接用Tekton Hub上別人寫好的,地址是:https://hub.tekton.dev/tekton/task/git-clone。這個Task支持的功能比較全,參數(shù)也比較多,具體有哪些參數(shù)可以到上面的地址進行查看和學習。
其安裝方式有兩種:kubectl和tkn客戶端。
(1)使用kubectl進行安裝。
kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/git-clone/0.5/git-clone.yaml1.
(2)使用tkn客戶端進行安裝。
tkn hub install task git-clone1.
我這里使用的第二種安裝方式,安裝后可以看到具體的Task。
# tkn hub install task git-cloneTask git-clone(0.5) installed in default namespace# kubectl get task | grep git-clonegit-clone 54s1.2.3.4.
這個Task到底能不能滿足我們的需求呢?我們可以創(chuàng)建一個TaskRun來進行測試,如下(由于還沒有配置拉取代碼倉庫的用戶名和密碼,這里先用一個公開的倉庫進行測試)。
apiVersion: tekton.dev/v1beta1kind: TaskRunmetadata: name: test-git-clone namespace: defaultspec: 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-clone1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.
運行過后,可以看到代碼正常拉取。
編輯搜圖
單元測試
單元測試比較簡單,基本就是執(zhí)行g(shù)o test ./... 命令就行,比如。
> go test ./...ok devops-hello-world 0.313sok devops-hello-world/pkg (cached)1.2.3.
所以這個Task,只需要一個Go環(huán)境,能執(zhí)行Go命令即可,如下:
apiVersion: tekton.dev/v1beta1kind: Task metadata: name: unit-testspec: 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.
構(gòu)建鏡像/推送
為什么這里沒有單獨把應用構(gòu)建組成一個Task呢?主要是我們在這里采用了多階段構(gòu)建,我們可以將應用構(gòu)建-鏡像打包寫在一個Dockerfile中,所以這里只需要寫一個Task。
apiVersion: tekton.dev/v1beta1kind: Task metadata: name: build-push-imagespec: 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進行構(gòu)建鏡像,用這種方式不用掛載docker.sock文件,但是我們需要將docker config保存在/kaniko/.docker目錄下。我們可以通過如下命令來創(chuàng)建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.json1.
如果在運行上面命令的時候沒有jq命令,就需要你安裝一下。
yum install jq -y1.
部署應用
這里采用的deployment的方式部署應用,所以只需要使用kubectl進行部署即可。
不過在使用kubectl的時候需要/root/.kube/config文件,所以這里依然將config文件通過secret掛載到容器中。
創(chuàng)建一個secret,如下:
kubectl create secret generic kubernetes-config --from-file=/root/.kube/config1.
然后創(chuàng)建Task,如下:
apiVersion: tekton.dev/v1alpha1kind: Taskmetadata: name: deploy-to-k8sspec: 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
上面我們已經(jīng)把每一步整理成了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-image1.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.
運行測試
運行測試就是創(chuàng)建PipelineRun,不過在創(chuàng)建之前,我們先創(chuàng)建需要的認證信息。
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: default1.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.
然后我們就可以創(chuàng)建PipelineRun了,如下:
apiVersion: tekton.dev/v1beta1kind: PipelineRunmetadata: name: devops-hello-world-pipeline-runspec: 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運行成功。
編輯搜圖
應用也啟動成功。
# kubectl get po | grep httphttpserver-696479dd5d-qplnx 2/2 Running 0 2m18s1.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" # 定義鏡像參數(shù) - 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-image1.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,構(gòu)建出來的鏡像Tag就是commit ID,如下:
代碼倉庫最后一次提交的commit ID。
編輯搜圖
總結(jié)
整個流水線看起來很簡單,在調(diào)試的時候還是費一定的周折,主要是定義的參數(shù)傳過去傳過來,有時候就忘記指定了,就要不斷的調(diào)試。