DEV Community

AMIT CHATURVEDI
AMIT CHATURVEDI

Posted on

Understanding Tekton: A Comprehensive Guide to Cloud-Native CI/CD

Image description
Introduction:
Tekton is an open-source project that facilitates the creation and operation of cloud-native continuous integration and continuous delivery (CI/CD) pipelines. In this blog post, we'll explore the fundamentals of Tekton, its architecture, and its role in the modern DevOps landscape.

What is Tekton?
Tekton is a Kubernetes-native CI/CD framework that provides a set of custom resources and controllers to define and run CI/CD pipelines as containers in Kubernetes clusters. It is designed to be flexible, extensible, and scalable, making it an excellent choice for building cloud-native applications.

Key Components of Tekton:
Task:
A Task is the smallest unit of work in Tekton. It represents a single, well-defined step in the CI/CD pipeline. Tasks can be reused across different pipelines.

Pipeline:
A Pipeline defines a series of interconnected tasks to be executed as part of the CI/CD process. Pipelines enable the orchestration of tasks and define the workflow.

PipelineRun:
A PipelineRun is an instantiation of a Pipeline. It represents a single execution of a CI/CD process, tying together the defined tasks and pipelines.

Trigger:
Tekton Triggers allow the automation of pipeline execution based on events. Events can be external (e.g., a new code push) or internal (e.g., a time-based trigger).

Tekton Architecture:
Tekton is built on a set of custom Kubernetes resources, allowing it to seamlessly integrate with Kubernetes-native features. The architecture includes:

Controller:
The Tekton Controller manages the custom resources and orchestrates the execution of tasks and pipelines. It monitors changes in resources and ensures the desired state is maintained.

CRDs (Custom Resource Definitions):
Tekton introduces custom resources such as Task, Pipeline, and Trigger to represent CI/CD concepts. These CRDs define the structure of CI/CD pipelines.

Tekton CLI:
The Tekton CLI (tkn) provides a command-line interface to interact with and manage Tekton resources. It simplifies pipeline management and execution.

Advantages of Tekton:
Portability:
Tekton allows you to define CI/CD pipelines as code, making them portable across different Kubernetes clusters. This ensures consistent pipeline execution in various environments.

Extensibility:
Tekton is highly extensible, allowing users to create custom tasks and share them across projects. The extensibility ensures that Tekton can adapt to diverse CI/CD requirements.

Kubernetes Integration:
Tekton leverages native Kubernetes constructs, enabling seamless integration with other Kubernetes tools and services. This integration simplifies the management of CI/CD pipelines within Kubernetes environments.

Reusability:
The modular nature of Tekton allows for the reuse of tasks and pipelines. This promotes code sharing, reduces duplication, and enhances maintainability.

Getting Started with Tekton:
Installation:
Install Tekton in your Kubernetes cluster using the provided installation manifests or Helm charts. Tekton can run on any Kubernetes distribution.

Defining Tasks and Pipelines:
Use the Tekton CLI or YAML manifests to define tasks and pipelines. Tasks can be simple, like running tests, or complex, involving multiple steps.

Executing Pipelines:
Trigger the execution of pipelines manually or automate them using Tekton Triggers. Monitor the execution using the Tekton CLI or the Kubernetes dashboard.

Extending Tekton:
Explore and contribute to the Tekton ecosystem by creating custom tasks or integrating existing tools into your pipelines.

Install Tekton Pipelines

Prerequisites
A Kubernetes cluster running version 1.25 or later.
Kubectl.

Grant cluster-admin privileges to the current user.
(Optional) Install a Metrics Server if you need support for high availability use cases.

Latest official release:

root@master:~# kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml

root@master:~# kubectl get pods --namespace tekton-pipelines
NAME                                           READY   STATUS    RESTARTS   AGE
tekton-events-controller-8b5bb559c-fnnvs       1/1     Running   0          6m1s
tekton-pipelines-controller-65fb8b9d46-qbmnm   1/1     Running   0          6m2s
tekton-pipelines-webhook-66ff4b6644-pvhbc      1/1     Running   0          5m54s

Enter fullscreen mode Exit fullscreen mode

Install and set up Tekton Triggers
Prerequisites
Kubernetes cluster version 1.18 or later.
Kubectl.
Tekton Pipelines.
Grant cluster-admin privileges to the user that installed Tekton Pipelines.

Image description
Configuring a Task
Define a Task for Building the Node.js Application:

#cat build-task.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: build-task
spec:
  steps:
    - name: npm-install
      image: node:14
      workingDir: /workspace/source
      script: |
        npm install

    - name: build-app
      image: node:14
      workingDir: /workspace/source
      script: |
        npm run build
Enter fullscreen mode Exit fullscreen mode

This Task consists of two steps:

npm-install: Installs the Node.js dependencies.
build-app: Builds the Node.js application

Define a Task for Deploying the Containerized Application:

#cat deploy-task.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: deploy-task
spec:
  steps:
    - name: deploy-app
      image: gcr.io/cloud-builders/kubectl
      script: |
        kubectl apply -f /workspace/source/deployment.yaml

# kubectl get task
NAME                AGE
build-task          16m
deploy-task         16m
Enter fullscreen mode Exit fullscreen mode

This Task includes a single step (deploy-app) that uses the kubectl tool to apply the Kubernetes deployment manifest for your application.

Define a Pipeline that Uses the Tasks:

#cat example-pipeline.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: example-pipeline
spec:
  tasks:
    - name: build
      taskRef:
        name: build-task
    - name: deploy
      taskRef:
        name: deploy-task

# kubectl get pipeline | grep -i example
example-pipeline   15m

Enter fullscreen mode Exit fullscreen mode

This Pipeline consists of two tasks: build and deploy. Each task references the previously defined tasks (build-task and deploy-task).

Apply the Tekton Resources:
Apply the Tekton resources to your Kubernetes cluster:

kubectl apply -f build-task.yaml
kubectl apply -f deploy-task.yaml
kubectl apply -f example-pipeline.yaml
Enter fullscreen mode Exit fullscreen mode

Run the Pipeline:

# cat pipeline-run.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: example-pipeline-run
spec:
  pipelineRef:
    name: example-pipeline
Enter fullscreen mode Exit fullscreen mode

Apply the PipelineRun to start the execution of the pipeline

kubectl apply -f pipeline-run.yaml
Enter fullscreen mode Exit fullscreen mode

Monitor PipelineRun Status

# kubectl get PipelineRun --watch
NAME                   SUCCEEDED   REASON    STARTTIME   COMPLETIONTIME
example-pipeline-run   Unknown     Running   40s

Enter fullscreen mode Exit fullscreen mode

Top comments (0)