云原生CI/CD框架Tekton国内部署方式

时间:2021-11-24 08:18:12

云原生CI/CD框架Tekton国内部署方式

Tekton 是一款功能非常强大而灵活的 CI/CD 开源的云原生框架。致力于提供全功能、标准化的云原生 CI/CD 解决方案。【本文主要是通过流水线自动化的将tekton镜像同步到腾讯云仓库,并部署tekton】

应用镜像

阿里云镜像仓库居然有限制...这次转到腾讯云镜像仓库了;ccr.ccs.tencentyun.com/tektons/dashboard

云原生CI/CD框架Tekton国内部署方式

Pipeline

借助GitHub Actions:

  1. 同步镜像并生成镜像映射文件(json):
  2. 收集镜像映射文件为制品;
  1. This is a basic workflow to help you get started with Actions
  2. name: Get Tekton Images
  3. env:
  4. VERSION: v0.29.0
  5. on:
  6. push:
  7. paths:
  8. - '.github/workflows/tekton.yaml'
  9. - 'tekton/**'
  10. jobs:
  11. build:
  12. runs-on: ubuntu-18.04
  13. steps:
  14. - uses: actions/checkout@v2
  15. - name: build
  16. run: |
  17. curl https://storage.googleapis.com/tekton-releases/pipeline/previous/${{ env.VERSION }}/release.yaml -o release.yaml
  18. grep -v "#" release.yaml | grep -v "^$" > release1.yaml ; sed -i 's/\-\-\-/###/g' release1.yaml
  19. python3 tekton/get_tekton_images.py ${{ secrets.DOCKER_USER}} ${{ secrets.DOCKER_PASSWD}}
  20. - uses: actions/upload-artifact@v2
  21. with:
  22. name: ${{ env.VERSION }}-tekton-images
  23. path: tekton_images.json

云原生CI/CD框架Tekton国内部署方式

部署文件解析

1.下载release部署yaml;

2.解析Deployments对象中的images;

a.tekton-pipelines-controller

b.tekton-pipelines-webhook

c.tekton-dashboard(最新tag)

  1. gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/controller:v0.29.0@sha256:72f79471f06d096cc53e51385017c9f0f7edbc87379bf415f99d4bd11cf7bc2b
  2. gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/kubeconfigwriter:v0.29.0@sha256:6d058f2203b9ab66f538cb586c7dc3b7cc31ae958a4135dd99e51799f24b06c9
  3. gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:v0.29.0@sha256:c0b0ed1cd81090ce8eecf60b936e9345089d9dfdb6ebdd2fd7b4a0341ef4f2b9
  4. gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/entrypoint:v0.29.0@sha256:66958b78766741c25e31954f47bc9fd53eaa28263506b262bf2cc6df04f18561
  5. gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/nop:v0.29.0@sha256:6a037d5ba27d9c6be32a9038bfe676fb67d2e4145b4f53e9c61fb3e69f06e816
  6. gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/imagedigestexporter:v0.29.0@sha256:e38dd0d32253fce5aaf1e501c0bc71facc3720564b7e97055921cc5390d612e0
  7. gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/pullrequest-init:v0.29.0@sha256:d28202fb8b33a1d4c05f261ef8dcbcdcf3b469887d4dad256ce91f73c917420e
  8. gcr.io/google.com/cloudsdktool/cloud-sdk@sha256:27b2c22bf259d9bc1a291e99c63791ba0c27a04d2db0a43241ba0f1f20f4067f
  9. gcr.io/distroless/base@sha256:aa4fd987555ea10e1a4ec8765da8158b5ffdfef1e72da512c7ede509bc9966c4
  10. mcr.microsoft.com/powershell:nanoserver@sha256:b6d5ff841b78bdf2dfed7550000fd4f3437385b8fa686ec0f010be24777654d6
  11. gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/webhook:v0.29.0@sha256:46d5b90a7f4e9996351ad893a26bcbd27216676ad4d5316088ce351fb2c2c3dd

用Python编写一个数据解析脚本:

  1. import yaml
  2. import json
  3. import sys
  4. import os
  5. class Tekton :
  6. def __init__(self, file_name, registry_user, registry_passwd):
  7. self.yaml_file = file_name
  8. self.arg_imgs = ["gcr.io/tekton-releases/github.com/tektoncd/dashboard/cmd/dashboard@sha256:95f71a2568ced67ec370b5360f88bec3280601908cac9e62dfbb801114480437"]
  9. self.split_str = "###"
  10. self.deployments = ["tekton-pipelines-controller", "tekton-pipelines-webhook"]
  11. self.kind_type = "Deployment"
  12. self.target_registry = "ccr.ccs.tencentyun.com/tektons/"
  13. self.repos = [ "controller", "kubeconfigwriter", "git-init",
  14. "entrypoint","nop","imagedigestexporter",
  15. "pullrequest-init", "cloud-sdk", "base", "powershell", "webhook"]
  16. self.result = []
  17. self.registry_user = registry_user
  18. self.registry_passwd = registry_passwd
  19. def load_yaml(self, data):
  20. content = yaml.load(data)
  21. return content
  22. def load_json(self, data):
  23. content = json.loads(data)
  24. return content
  25. def get_images(self):
  26. f = open(self.yaml_file, 'r').read()
  27. for i in f.split("###")[:-1]:
  28. try:
  29. content = self.load_yaml(i.replace("###", ""))
  30. if content["kind"] == self.kind_type:
  31. deploy_name = content["metadata"]["name"]
  32. # 获取image
  33. if deploy_name in self.deployments:
  34. img = content["spec"]["template"]["spec"]["containers"][0]["image"]
  35. self.arg_imgs.append(img)
  36. # 获取参数中的images
  37. if deploy_name == "tekton-pipelines-controller":
  38. arg_img = content["spec"]["template"]["spec"]["containers"][0]["args"]
  39. for a in arg_img:
  40. if not a.startswith("-"):
  41. self.arg_imgs.append(a)
  42. except Exception as e:
  43. print(e)
  44. return self.arg_imgs
  45. def save_json_file(self, data, file_name):
  46. for i in self.arg_imgs:
  47. self.result.append({
  48. "s_image": i,
  49. "t_image": self.target_registry + i.split("/")[-1].split("@")[0]
  50. })
  51. newdata = json.dumps(self.result, indent=4)
  52. a=open(file_name, 'w')
  53. a.write(newdata)
  54. a.close()
  55. def sync_images(self):
  56. f = open("tekton_images.json", 'r').read()
  57. content = self.load_json(f)
  58. docker_login_cmd = "docker login -u {0} -p {1} {2}".format(
  59. self.registry_user,
  60. self.registry_passwd,
  61. self.target_registry.split("/")[0])
  62. os.system(docker_login_cmd)
  63. for item in content:
  64. print("[GetImages] {}".format(item))
  65. docker_pull_cmd = "docker pull {0}".format(item["s_image"])
  66. docker_tag_cmd = "docker tag {0} {1}".format(item["s_image"], item["t_image"])
  67. docker_push_cmd = "docker push {0}".format(item["t_image"])
  68. os.system(docker_pull_cmd + "&&" + docker_tag_cmd + "&&" + docker_push_cmd )
  69. print("[GetImagesDone] {}".format(item))
  70. if __name__ == '__main__':
  71. tekton = Tekton("release1.yaml", sys.argv[1], sys.argv[2])
  72. images = tekton.get_images()
  73. tekton.save_json_file(images, "tekton_images.json")
  74. tekton.sync_images()

镜像映射文件

s_image 原始镜像名称, t_image 目标镜像名称; 这里使用腾讯云的镜像仓库;

  1. [
  2. {
  3. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/controller:v0.29.0@sha256:72f79471f06d096cc53e51385017c9f0f7edbc87379bf415f99d4bd11cf7bc2b",
  4. "t_image": "ccr.ccs.tencentyun.com/tektons/controller:v0.29.0"
  5. },
  6. {
  7. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/kubeconfigwriter:v0.29.0@sha256:6d058f2203b9ab66f538cb586c7dc3b7cc31ae958a4135dd99e51799f24b06c9",
  8. "t_image": "ccr.ccs.tencentyun.com/tektons/kubeconfigwriter:v0.29.0"
  9. },
  10. {
  11. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:v0.29.0@sha256:c0b0ed1cd81090ce8eecf60b936e9345089d9dfdb6ebdd2fd7b4a0341ef4f2b9",
  12. "t_image": "ccr.ccs.tencentyun.com/tektons/git-init:v0.29.0"
  13. },
  14. {
  15. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/entrypoint:v0.29.0@sha256:66958b78766741c25e31954f47bc9fd53eaa28263506b262bf2cc6df04f18561",
  16. "t_image": "ccr.ccs.tencentyun.com/tektons/entrypoint:v0.29.0"
  17. },
  18. {
  19. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/nop:v0.29.0@sha256:6a037d5ba27d9c6be32a9038bfe676fb67d2e4145b4f53e9c61fb3e69f06e816",
  20. "t_image": "ccr.ccs.tencentyun.com/tektons/nop:v0.29.0"
  21. },
  22. {
  23. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/imagedigestexporter:v0.29.0@sha256:e38dd0d32253fce5aaf1e501c0bc71facc3720564b7e97055921cc5390d612e0",
  24. "t_image": "ccr.ccs.tencentyun.com/tektons/imagedigestexporter:v0.29.0"
  25. },
  26. {
  27. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/pullrequest-init:v0.29.0@sha256:d28202fb8b33a1d4c05f261ef8dcbcdcf3b469887d4dad256ce91f73c917420e",
  28. "t_image": "ccr.ccs.tencentyun.com/tektons/pullrequest-init:v0.29.0"
  29. },
  30. {
  31. "s_image": "gcr.io/google.com/cloudsdktool/cloud-sdk@sha256:27b2c22bf259d9bc1a291e99c63791ba0c27a04d2db0a43241ba0f1f20f4067f",
  32. "t_image": "ccr.ccs.tencentyun.com/tektons/cloud-sdk"
  33. },
  34. {
  35. "s_image": "gcr.io/distroless/base@sha256:aa4fd987555ea10e1a4ec8765da8158b5ffdfef1e72da512c7ede509bc9966c4",
  36. "t_image": "ccr.ccs.tencentyun.com/tektons/base"
  37. },
  38. {
  39. "s_image": "mcr.microsoft.com/powershell:nanoserver@sha256:b6d5ff841b78bdf2dfed7550000fd4f3437385b8fa686ec0f010be24777654d6",
  40. "t_image": "ccr.ccs.tencentyun.com/tektons/powershell:nanoserver"
  41. },
  42. {
  43. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/webhook:v0.29.0@sha256:46d5b90a7f4e9996351ad893a26bcbd27216676ad4d5316088ce351fb2c2c3dd",
  44. "t_image": "ccr.ccs.tencentyun.com/tektons/webhook:v0.29.0"
  45. },
  46. {
  47. "s_image": "gcr.io/tekton-releases/github.com/tektoncd/dashboard/cmd/dashboard@sha256:95f71a2568ced67ec370b5360f88bec3280601908cac9e62dfbb801114480437",
  48. "t_image": "ccr.ccs.tencentyun.com/tektons/dashboard"
  49. }
  50. ]

镜像映射文件可以在GitHubActions页面下载:

云原生CI/CD框架Tekton国内部署方式

下载镜像脚本

解析上面生成的镜像文件,docker pull下载对应的镜像到本地;

  1. import json
  2. import os
  3. class Tekton:
  4. def __init__(self):
  5. self.json_file = "tekton_images.json"
  6. self.target_registry = "ccr.ccs.tencentyun.com/tektons/"
  7. # self.registry_user = registry_user
  8. # self.registry_passwd = registry_passwd
  9. def load_json(self, data):
  10. content = json.loads(data)
  11. return content
  12. def down_images(self):
  13. f = open(self.json_file, 'r').read()
  14. content = self.load_json(f)
  15. # docker_login_cmd = "docker login -u {0} -p {1} {2}".format(
  16. # self.registry_user,
  17. # self.registry_passwd,
  18. # self.target_registry.split("/")[0])
  19. for item in content:
  20. print("[GetImages] {}".format(item["t_image"]))
  21. docker_pull_cmd = "docker pull {0}".format(item["t_image"])
  22. # docker_tag_cmd = "docker tag {0} {1}".format(item["t_image"], item["s_image"].split("@")[0])
  23. os.system(docker_pull_cmd + "&&" + docker_tag_cmd )
  24. print("[GetImagesDone] {}".format(item))
  25. if __name__ == '__main__':
  26. t = Tekton().down_images()

部署Tekton

替换部署文件中的镜像:

  1. 手动更新release.yaml中的镜像;然后kubectl apply release.yaml 部署(后续有时间再优化脚本,实现自动更新release.yaml)
  2. 手动更新tekton-dashboard-release.yaml中的镜像;然后部署;
  1. [root@master ~]# kubectl -n tekton-pipelines get pod
  2. NAME READY STATUS RESTARTS AGE
  3. tekton-dashboard-5c4b89d9-2z8g7 1/1 Running 0 21m
  4. tekton-pipelines-controller-b96f647bb-gff69 1/1 Running 0 13h
  5. tekton-pipelines-webhook-76bc9c97b9-cd2m4 1/1 Running 0 13h

编写一个Ingress来暴露tekton dashboard:

  1. apiVersion: extensions/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: tekton-service
  5. namespace: tekton-pipelines
  6. annotations:
  7. kubernetes.io/ingress.class: nginx
  8. nginx.ingress.kubernetes.io/proxy-body-size: 256m
  9. spec:
  10. rules:
  11. - host: tekton.idevops.site
  12. http:
  13. paths:
  14. - path: /
  15. backend:
  16. serviceName: tekton-dashboard
  17. servicePort: 9097

访问UI页面:

云原生CI/CD框架Tekton国内部署方式

编写Pipeline

  1. apiVersion: tekton.dev/v1beta1
  2. kind: Task
  3. metadata:
  4. name: tektoncd-task
  5. spec:
  6. resources:
  7. inputs:
  8. - name: repo
  9. type: git
  10. steps:
  11. - name: run-test
  12. image: maven:3-jdk-8
  13. workingDir: /workspace/repo
  14. command: ["mvn"]
  15. args: ["clean", "package"]
  16. ---
  17. apiVersion: tekton.dev/v1alpha1
  18. kind: PipelineResource
  19. metadata:
  20. name: tektoncd-resource
  21. spec:
  22. type: git
  23. params:
  24. - name: url
  25. value: http://192.168.1.200/devops/devops-maven-service.git
  26. - name: revision
  27. value: master
  28. ---
  29. apiVersion: tekton.dev/v1beta1
  30. kind: TaskRun
  31. metadata:
  32. name: cdpipeline
  33. spec:
  34. taskRef:
  35. name: tektoncd-task
  36. resources:
  37. inputs:
  38. - name: repo
  39. resourceRef:
  40. name: tektoncd-resource

原文链接:https://mp.weixin.qq.com/s/Rn44lRhaEC4YkUi2dD7TKg