DEV Community

Cover image for Tekton 101
mkdev.me for mkdev

Posted on • Updated on • Originally published at mkdev.me

Tekton 101

In this article, we are going to have an initial technical view of how to install Tekton and set up your first pipeline, diving deep into every detail.

Remember that this is not our first article about Tekton. If you want to learn how it works internally, there is an article by Kirill Shirinkin called "What is Tekton", so I recommend you first read this article and then come back here.

Let's start with Tekton, our powerful Kubernetes-native tool for building CI/CD systems.

First, we are going to install Tekton on our Kubernetes cluster.

kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml
Enter fullscreen mode Exit fullscreen mode

By running this command, we're applying a set of resources that Tekton needs to operate. This lays the foundation that will allow us to create and run pipelines in the future.

To ensure everything is in order, we'll check the pods in the 'tekton-pipelines' namespace. If Tekton was installed correctly, we should see several running pods.

kubectl get pods --namespace tekton-pipelines
Enter fullscreen mode Exit fullscreen mode

Perfect! The pods you see are Tekton's internal components, working together to enable us to define and run pipelines.

A pipeline in Tekton consists of 'Tasks', which define specific steps of work. Think of a 'Task' as an individual function, and a pipeline as a series of those functions running in a specific order or parallel, as defined.

Let's define our first 'Task'. This will be a simple 'Hello World'.

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: hello
spec:
  steps:
    - name: say-hello
      image: ubuntu
      command:
        - echo
      args: ["Hello World"]
Enter fullscreen mode Exit fullscreen mode

Here, we're defining a 'Task' that simply prints 'Hello World' using an Ubuntu image. Each 'Task' in Tekton is defined as a series of steps using container images to execute commands.

Once defined, we apply the 'Task' to our cluster.

kubectl apply -f hello-task.yaml
Enter fullscreen mode Exit fullscreen mode

And now, using tkn, Tekton's command line tool, we'll start it. In the next videos, we will see how the Event Listener works, but today we are focusing on the tkn command.

tkn task start --showlog hello
Enter fullscreen mode Exit fullscreen mode

Great! You should see a 'Hello World' in the output.

Now, we'll build a pipeline that uses this 'Task'. A pipeline is essentially a sequence of 'Tasks' that run in order or in parallel, as defined.

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: hello-pipeline
spec:
  tasks:
    - name: greet
      taskRef:
        name: hello
Enter fullscreen mode Exit fullscreen mode

This pipeline is simple and only includes one 'Task'. However, later on, we will add more 'Tasks' and set their order of execution, dependencies, and parallelism.

Let's apply this pipeline and then start it.

kubectl apply -f hello-pipeline.yaml
tkn pipeline start hello-pipeline --showlog
Enter fullscreen mode Exit fullscreen mode

But let's move forward. To pass parameters from the pipeline to the tasks, we'll start with this task:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: generate-random-number
spec:
  params:
    - name: limit
      description: The upper limit for random number generation.
      default: "100"
      type: string
  steps:
    - name: generate-it
      image: alpine:latest
      command:
        - /bin/ash
      args: ['-c', 'echo "Random Number: $(($RANDOM % $(params.limit)))"']
Enter fullscreen mode Exit fullscreen mode

As you can see, there is a step that generates a random number using a parameter that, in this case, has 100 as the default value. Now, let's see what the pipeline looks like.

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: generate-multiple-numbers
spec:
  tasks:
    - name: first-random-number
      taskRef:
        name: generate-random-number
      params:
        - name: limit
          value: "50"
    - name: second-random-number
      taskRef:
        name: generate-random-number
      params:
        - name: limit
          value: "200"
    - name: third-random-number
      taskRef:
        name: generate-random-number
      params:
        - name: limit
          value: "1000"
Enter fullscreen mode Exit fullscreen mode

As you can see, in the pipeline, we call the task three times with different parameters. So, if we now execute it using the tkn CLI:

➜ tekton tkn pipeline start generate-multiple-numbers --showlog
PipelineRun started: generate-multiple-numbers-run-9rrmp
Waiting for logs to be available...
[second-random-number : generate-it] Random Number: 58
[first-random-number : generate-it] Random Number: 2




[third-random-number : generate-it] Random Number: 172


Enter fullscreen mode Exit fullscreen mode

And that's it! Now you have a solid foundation to explore more of what Tekton can offer. Until next time!


Here' the same article in video form for your convenience:

.

Top comments (0)