文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-12-02 15:43

关注

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

应用镜像

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

Pipeline

借助GitHub Actions:

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

 

部署文件解析

下载release部署yaml;

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

镜像映射文件

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

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

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

下载镜像脚本

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

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

编写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 

 

来源:DevOps云学堂内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯