阿里云Kubernetes服务上使用Tekton完成应用发布初体验

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: Tekton 是一个功能强大且灵活的 Kubernetes 原生开源框架,用于创建持续集成和交付(CI/CD)系统。通过抽象底层实现细节,用户可以跨多云平台和本地系统进行构建、测试和部署。

Tekton 是一个功能强大且灵活的 Kubernetes 原生开源框架,用于创建持续集成和交付(CI/CD)系统。通过抽象底层实现细节,用户可以跨多云平台和本地系统进行构建、测试和部署。

本文是基于阿里云Kubernetes服务部署Tekton Pipeline,并使用它完成源码拉取、应用打包、镜像推送和应用部署的实践过程。

image

Tekton Pipeline中有5类对象,核心理念是通过定义yaml定义构建过程.构建任务的状态存放在status字段中。

其中5类对象分别是:PipelineResouce、Task、TaskRun、Pipeline、PipelineRun。

Task是单个任务的构建过程,需要通过定义TaskRun任务去运行Task。

Pipeline包含多个Task,并在此基础上定义input和output,input和output以PipelineResource作为交付。

PipelineResource是可用于input和output的对象集合。

同样地,需要定义PipelineRun才会运行Pipeline。

1. 在阿里云Kubernetes集群中部署Tekton Pipeline

kubectl apply --filename https://storage.googleapis.com/tekton-releases/latest/release.yaml

查看Tekton Pipelines组件是否运行正常:

$ kubectl -n tekton-pipelines get po
NAME                                                     READY   STATUS      RESTARTS   AGE
tekton-pipelines-controller-6bcd7ff5d6-vzmrh             1/1     Running     0          25h
tekton-pipelines-webhook-6856cf9c47-l6nj6                1/1     Running     0          25h

2. 创建Git Resource, Registry Resource

编辑 git-pipeline-resource.yaml :

apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
  name: git-pipeline-resource
spec:
  type: git
  params:
    - name: revision
      value: tekton
    - name: url
      value: https://code.aliyun.com/haoshuwei/jenkins-demo.git

git repo的分支名称为 tekton

编辑 registry-pipeline-resource.yaml :

apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
  name: registry-pipeline-resource
spec:
  type: image
  params:
    - name: url
      value: registry.cn-hangzhou.aliyuncs.com/haoshuwei/tekton-demo

容器镜像仓库地址为 registry.cn-hangzhou.aliyuncs.com/haoshuwei/tekton-demo, 标签为 latest

创建pipeline resource:

$ kubectl -n tekton-pipelines create -f git-pipeline-resource.yaml
$ kubectl -n tekton-pipelines create -f registry-pipeline-resource.yaml

查看已创建的pipeline resource资源:

$ kubectl -n tekton-pipelines get PipelineResource
NAME                         AGE
git-pipeline-resource        2h
registry-pipeline-resource   2h

3. 创建Git Repo/Docker Registry Authentication

拉取私有git源码项目需要配置使用Git Repo Authentication;拉取和推送docker镜像需要配置Docker Registry Authentication。在Tekton Pipeline中,Git Repo/Docker Registry Authentication会被定义成ServiceAccount来使用。

编辑 secret tekton-basic-user-pass-git.yaml :

apiVersion: v1
kind: Secret
metadata:
  name: tekton-basic-user-pass-git
  annotations:
    tekton.dev/git-0: https://code.aliyun.com
type: kubernetes.io/basic-auth
stringData:
  username: <cleartext non-encoded>
  password: <cleartext non-encoded>

编辑 secret tekton-basic-user-pass-registry.yaml :

apiVersion: v1
kind: Secret
metadata:
  name: tekton-basic-user-pass-registry
  annotations:
    tekton.dev/docker-0: https://registry.cn-hangzhou.aliyuncs.com
type: kubernetes.io/basic-auth
stringData:
  username: <cleartext non-encoded>
  password: <cleartext non-encoded>

编辑 serviceaccount tekton-git-and-registry.yaml :

apiVersion: v1
kind: ServiceAccount
metadata:
  name: tekton-git-and-registry
secrets:
  - name: tekton-basic-user-pass-git
  - name: tekton-basic-user-pass-registry

创建serviceaccount:

$ kubectl -n tekton-pipelines create -f tekton-basic-user-pass-git.yaml
$ kubectl -n tekton-pipelines create -f tekton-basic-user-pass-registry.yaml
$ kubectl -n tekton-pipelines create -f tekton-git-and-registry.yaml

查看secret以及sa:

$ kubectl -n tekton-pipelines get secret
NAME                                      TYPE                                  DATA   AGE
default-token-pwncj                       kubernetes.io/service-account-token   3      25h
tekton-basic-user-pass-git                kubernetes.io/basic-auth              2      151m
tekton-basic-user-pass-registry           kubernetes.io/basic-auth              2      151m
tekton-git-and-registry-token-tr95m       kubernetes.io/service-account-token   3      151m
tekton-pipelines-controller-token-lc2fv   kubernetes.io/service-account-token   3      25h  
webhook-certs                             Opaque                                3      25h
$  kubectl -n tekton-pipelines get sa
NAME                          SECRETS   AGE
default                       1         25h
tekton-git-and-registry       3         152m
tekton-pipelines-controller   1         25h

4. 配置serviceaccount tekton-git-and-registry获取命名空间tekton-pipelines的管理权限用于部署应用

创建ClusterRoleBinding tekton-cluster-admin :

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: tekton-cluster-admin
subjects:
  - kind: ServiceAccount
    name: tekton-git-and-registry
    namespace: tekton-pipelines
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

5. 创建一个Task

创建task build-app.yaml :

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: build-app
spec:
  inputs:
    resources:
      - name: java-demo
        type: git
    params:
      - name: pathToDockerFile
        description: The path to the dockerfile to build
        default: /workspace/java-demo/Dockerfile
      - name: pathToContext
        description: The build context used by Kaniko
        default: /workspace/java-dem
      - name: pathToYaml
        description: The path to teh manifest to apply
  outputs:
    resources:
      - name: builtImage
        type: image
  steps:
    - name: build-mvn-package
      image: registry.cn-beijing.aliyuncs.com/acs-sample/jenkins-slave-maven:3.3.9-jdk-8-alpine
      workingDir: /workspace/java-demo
      command:
        - mvn
      args:
        - package
        - -B
        - -DskipTests
    - name: build-docker-image
      image: registry.cn-beijing.aliyuncs.com/acs-sample/jenkins-slave-kaniko:0.6.0
      command:
        - kaniko
      args:
        - --dockerfile=${inputs.params.pathToDockerFile}
        - --destination=${outputs.resources.builtImage.url}
        - --context=${inputs.params.pathToContext}
    - name: deploy-app
      image: registry.cn-beijing.aliyuncs.com/acs-sample/jenkins-slave-kubectl:1.11.5
      command:
        - kubectl
      args:
        - apply
        - -f
        - ${inputs.params.pathToYaml}

6. 创建TaskRun运行任务

创建taskrun build-app-task-run.yaml :

apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: build-app-task-run
spec:
  serviceAccount: tekton-git-and-registry
  taskRef:
    name: build-app
  trigger:
    type: manual
  inputs:
    resources:
      - name: java-demo
        resourceRef:
          name: git-pipeline-resource
    params:
      - name: pathToDockerFile
        value: Dockerfile
      - name: pathToContext
        value: /workspace/java-demo
      - name: pathToYaml
        value: /workspace/java-demo/deployment.yaml
  outputs:
    resources:
      - name: builtImage
        resourceRef:
          name: registry-pipeline-resource

7. 查看构建状态以及日志

查看taskrun状态:

$ kubectl -n tekton-pipelines get taskrun
NAME                 SUCCEEDED   REASON    STARTTIME   COMPLETIONTIME
build-app-task-run   Unknown     Pending   4s

查看构建日志:

$ kubectl -n tekton-pipelines get po
NAME                                           READY   STATUS    RESTARTS   AGE
build-app-task-run-pod-b8f890                  3/5     Running   0          75s
tekton-pipelines-controller-6bcd7ff5d6-vzmrh   1/1     Running   0          25h
tekton-pipelines-webhook-6856cf9c47-l6nj6      1/1     Running   0          25h
$ kubectl -n tekton-pipelines logs -f build-app-task-run-pod-b8f890
Error from server (BadRequest): a container name must be specified for pod build-app-task-run-pod-b8f890, choose one of:   [build-step-git-source-git-pipeline-resource-77l5v build-step-build-mvn-package build-step-build-docker-image build-step-deploy-app nop] or one of the init containers: [build-step-credential-initializer-8dsnm build-step-place-tools]

mvn build的日志:

$ kubectl -n tekton-pipelines logs -f build-app-task-run-pod-b8f890 -c build-step-build-mvn-package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building jenkins-demo-web 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom (8 KB at 7.3 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/23/maven-plugins-23.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/23/maven-plugins-23.pom (9 KB at 26.7 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/22/maven-parent-22.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/22/maven-parent-22.pom (30 KB at 61.3 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/11/apache-11.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/11/apache-11.pom (15 KB at 45.3 KB/sec)
....

docker build的日志:

$ kubectl -n tekton-pipelines logs -f build-app-task-run-pod-b8f890 -c build-step-build-docker-image
INFO[0000] Downloading base image tomcat
2019/05/06 11:58:46 No matching credentials were found, falling back on anonymous
INFO[0003] Taking snapshot of full filesystem...
INFO[0003] Skipping paths under /builder/home, as it is a whitelisted directory
INFO[0003] Skipping paths under /builder/tools, as it is a whitelisted directory
INFO[0003] Skipping paths under /dev, as it is a whitelisted directory
INFO[0003] Skipping paths under /kaniko, as it is a whitelisted directory
INFO[0003] Skipping paths under /proc, as it is a whitelisted directory
INFO[0003] Skipping paths under /run/secrets/kubernetes.io/serviceaccount, as it is a whitelisted directory
INFO[0003] Skipping paths under /sys, as it is a whitelisted directory
INFO[0003] Skipping paths under /var/run, as it is a whitelisted directory
INFO[0003] Skipping paths under /workspace, as it is a whitelisted directory
INFO[0003] Using files from context: [/workspace/java-demo/target/demo.war]
INFO[0003] ADD target/demo.war /usr/local/tomcat/webapps/demo.war
INFO[0003] Taking snapshot of files...
...

app-deploy的日志:

$ kubectl -n tekton-pipelines logs -f build-app-task-run-pod-637855 -c build-step-deploy-app
deployment.extensions/jenkins-java-demo created
service/jenkins-java-demo created

taskrun的完成状态为True则构建部署过程完成:

$ kubectl -n tekton-pipelines get taskrun
NAME                 SUCCEEDED   REASON   STARTTIME   COMPLETIONTIME
build-app-task-run   True                 4m          2m

8. 小结

Tekton Pipeline中任务模板可以拿来复用,而不需要重复定义,另外通过CRD重新定义CI/CD是一大亮点,初学者可能会觉得有些绕。

持续实验持续更新中。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
1月前
|
供应链 安全 Cloud Native
阿里云飞天企业版获【可信云·容器平台安全能力】先进级认证
阿里云飞天企业版容器系列产品获中国信息通信研究院【可信云·容器平台安全能力】先进级认证,这是飞天企业版容器产品获得《等保四级PaaS平台》和《 云原生安全配置基线规范V2.0》之后,本年度再一次获得行业权威认可,证明飞天企业版的容器解决方案具备符合行业标准的最高等级容器安全能力。
阿里云飞天企业版获【可信云·容器平台安全能力】先进级认证
|
1月前
|
缓存 Kubernetes Docker
GitLab Runner 全面解析:Kubernetes 环境下的应用
GitLab Runner 是 GitLab CI/CD 的核心组件,负责执行由 `.gitlab-ci.yml` 定义的任务。它支持多种执行方式(如 Shell、Docker、Kubernetes),可在不同环境中运行作业。本文详细介绍了 GitLab Runner 的基本概念、功能特点及使用方法,重点探讨了流水线缓存(以 Python 项目为例)和构建镜像的应用,特别是在 Kubernetes 环境中的配置与优化。通过合理配置缓存和镜像构建,能够显著提升 CI/CD 流水线的效率和可靠性,助力开发团队实现持续集成与交付的目标。
|
5天前
|
Cloud Native Serverless 数据中心
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
ACK One注册集群已正式支持ACS(容器计算服务)算力,为企业的容器化工作负载提供更多选择和更强大的计算能力。
|
27天前
|
存储 监控 对象存储
ACK 容器监控存储全面更新:让您的应用运行更稳定、更透明
针对本地存储和 PVC 这两种容器存储使用方式,我们对 ACK 的容器存储监控功能进行了全新升级。此次更新完善了对集群中不同存储类型的监控能力,不仅对之前已有的监控大盘进行了优化,还针对不同的云存储类型,上线了全新的监控大盘,确保用户能够更好地理解和管理容器业务应用的存储资源。
113 21
|
1月前
|
存储 监控 对象存储
ACK容器监控存储全面更新:让您的应用运行更稳定、更透明
介绍升级之后的ACK容器监控体系,包括各大盘界面展示和概要介绍。
|
2月前
|
Prometheus Kubernetes 监控
OpenAI故障复盘 - 阿里云容器服务与可观测产品如何保障大规模K8s集群稳定性
聚焦近日OpenAI的大规模K8s集群故障,介绍阿里云容器服务与可观测团队在大规模K8s场景下我们的建设与沉淀。以及分享对类似故障问题的应对方案:包括在K8s和Prometheus的高可用架构设计方面、事前事后的稳定性保障体系方面。
|
2月前
|
Kubernetes 算法 调度
阿里云 ACK FinOps成本优化最佳实践
本文源自2024云栖大会梁成昊演讲,讨论了成本优化策略的选择与实施。文章首先介绍了成本优化的基本思路,包括优化购买方式、调整资源配置等基础策略,以及使用弹性、资源混部等高级策略。接着,文章详细探讨了集群优化和应用优化的具体方法,如使用抢占式实例降低成本、通过资源画像识别并优化资源配置,以及利用智能应用弹性策略提高资源利用效率。
|
2月前
|
运维 Kubernetes Serverless
阿里云Argo X K8s玩转工作流引擎,实现大规模并行计算
本文基于2024云栖大会田双坤的演讲,介绍了Kubernetes作为云原生操作系统的角色及其在各类任务中的应用,重点探讨了Argo Workflows在Kubernetes上编排并行任务的能力。面对自建Argo Workflows的挑战,如稳定性、成本和安全性等问题,阿里巴巴云推出了全托管的Serverless Argo工作流,提供全托管、免运维、可观测和易集成的特点,显著提升了任务编排的效率和稳定性。适用于数据处理、科学计算、自动驾驶仿真等多个领域。
|
1月前
|
缓存 容灾 网络协议
ACK One多集群网关:实现高效容灾方案
ACK One多集群网关可以帮助您快速构建同城跨AZ多活容灾系统、混合云同城跨AZ多活容灾系统,以及异地容灾系统。
|
2月前
|
Kubernetes Ubuntu 网络安全
ubuntu使用kubeadm搭建k8s集群
通过以上步骤,您可以在 Ubuntu 系统上使用 kubeadm 成功搭建一个 Kubernetes 集群。本文详细介绍了从环境准备、安装 Kubernetes 组件、初始化集群到管理和使用集群的完整过程,希望对您有所帮助。在实际应用中,您可以根据具体需求调整配置,进一步优化集群性能和安全性。
148 12

相关产品

  • 容器服务Kubernetes版