Lab: Tekton on OpenShift
Last updated
Last updated
Tekton is an open source project to configure and run CI/CD pipelines within a OpenShift/Kubernetes cluster.
In this tutorial you'll learn
what are the basic concepts used by Tekton pipelines
how to create a pipeline to build and deploy a container
how to run the pipeline, check its status and troubleshoot problems
Also, check out this very good tutorial by Red Hat.
From the OpenShift UI, make sure you are in the Administrator View in the upper left. Select Operators->OperatorHub on the lefthand menu. Search for Pipelines and select the OpenShift Pipelines Operator (the non-community version)
Click Install on the next screen. Select Preview channel. Then click Subscribe
Create a route for the OpenShift registry if you have not done so already.
1 hour
Tekton provides a set of extensions to Kubernetes, in the form of Custom Resources, for defining pipelines. The following diagram shows the resources used in this tutorial. The arrows depict references from one resource to another resource.
The resources are used as follows.
A PipelineRun defines an execution of a pipeline. It references the Pipeline to run.
A Pipeline defines the set of Tasks that compose a pipeline.
A Task defines a set of build steps such as compiling code, running tests, and building and deploying images.
We will go into more detail about each resource during the walkthrough of the example.
Let's create a simple pipeline that
builds a Docker image from source files and pushes it to your private container registry
deploys the image to your Kubernetes cluster
You should clone this project to your workstation since you will need to edit some of the yaml files before applying them to your cluster. Check out the beta-update
branch after cloning.
We will work from the bottom-up, i.e. first we will define the Task resources needed to build and deploy the image, then we'll define the Pipeline resource that references the tasks, and finally we'll create the PipelineRun resource needed to run the pipeline.
The first thing that the pipeline needs is a task to clone the Git repository that the pipeline is building. This is such a common function that you don't need to write this task yourself. Tekton provides a library of reusable tasks called the Tekton catalog. It provides a git-clone
task which is described here.
The task is reproduced below so that we can talk about it.
A task can have one or more steps. Each step defines an image to run to perform the function of the step. This task has one step that uses a Tekton-provided container to clone a Git repo.
A task can have parameters. Parameters help to make a task reusable. This task accepts many parameters such as:
the URL of the Git repository to clone
the revision to check out
Parameters can have default values provided by the task or the values can be provided by the Pipeline
and PipelineRun
resources that we'll see later. Steps can reference parameter values by using the syntax $(params.name)
where name
is the name of the parameter. For example the step uses $(params.url)
to reference the url
parameter value.
The task requires a workspace where the clone is stored. From the point of view of the task, a workspace provides a file system path where it can read or write data. Steps can reference the path using the syntax $(workspaces.name.path)
where name
is the name of the workspace. We'll see later how the workspace becomes associated with a storage volume.
Apply the file to your cluster to create the task.
The next function that the pipeline needs is a task that builds a docker image and pushes it to a container registry. The catalog provides a kaniko
task which does this using Google's kaniko
tool. The task is described here.
The task is reproduced below.
You can see that this task needs a workspace as well. This workspace has the source to build. The pipeline will provide the same workspace that it used for the git-clone
task.
The kaniko
task also uses a feature called results. A result is a value produced by a task which can then be used as a parameter value to other tasks. This task declares a result named IMAGE-DIGEST
which it sets to the digest of the built image. A task sets a result by writing it to a file named /tekton/results/name
where name
is the name of the result, in this case IMAGE-DIGEST
. We will see later how the pipeline uses this result.
You may be wondering about how the task authenticates to the image repository for permission to push the image. This will be covered later on in the tutorial.
Apply the file to your cluster to create the task.
The final function that the pipeline needs is a task that deploys a docker image to a Kubernetes cluster. Below is a Tekton task that does this. You can find this yaml file at tekton/tasks/deploy-using-kubectl.yaml.
This task has two steps.
The first step runs sed
in an Alpine Linux container to update the yaml file used for deployment with the image that was built by the kaniko
task. The step requires the yaml file to have two character strings, __IMAGE__
and __DIGEST__
, which are substituted with parameter values.
The second step runs kubectl
using Lachlan Evenson's popular k8s-kubectl
container image to apply the yaml file to the same cluster where the pipeline is running.
As was the case in the git-clone and kaniko tasks, this task makes use of parameters in order to make the task as reusable as possible. It also needs the workspace to get the deployment yaml file.
You may be wondering about how the task authenticates to the cluster for permission to apply the resource(s) in the yaml file. This will be covered later on in the tutorial.
Apply the file to your cluster to create the task.
Below is a Tekton pipeline that runs the tasks we defined above. You can find this yaml file at tekton/pipeline/build-and-deploy-pipeline.yaml.
A Pipeline resource contains a list of tasks to run. Each pipeline task is assigned a name
within the pipeline; here they are clone-repo
, source-to-image
, and deploy-using-kubectl
.
The pipeline configures each task via the task's parameters. You can choose whether to expose a task parameter as a pipeline parameter, set the value directly, or let the value default inside the task (if it's an optional parameter). For example this pipeline exposes the CONTEXT
parameter from the kaniko task (under a different name, pathToContext
) but does not expose the DOCKERFILE
parameter and allows it to default inside the task.
This pipeline also shows how to take the result of one task and pass it to another task. We saw earlier that the kaniko
task produces a result named IMAGE-DIGEST
that holds the digest of the built image. The pipeline passes that value to the deploy-using-kubectl
task by using the syntax $(tasks.source-to-image.results.IMAGE-DIGEST)
where source-to-image
is the name used in the pipeline to run the kaniko
task.
By default Tekton assumes that pipeline tasks can be executed concurrently. In this pipeline each pipeline task depends on the previous one so they must be executed sequentially. One way that dependencies between pipeline tasks can be expressed is by using the runAfter
key. It specifies that the task must run after the given list of tasks has completed. In this example, the pipeline specifies that the source-to-image
pipeline task must run after the clone-repo
pipeline task.
The deploy-using-kubectl
pipeline task must run after the source-to-image
pipeline task but it doesn't need to specify the runAfter
key. This is because it references a task result from the source-to-image
pipeline task and Tekton is smart enough to figure out that this means it must run after that task.
Apply the file to your cluster to create the pipeline.
Before running the pipeline, we need to set up a service account so that it can access protected resources. The service account ties together a couple of secrets containing credentials for authentication along with RBAC-related resources for permission to create and modify certain Kubernetes resources.
The 3rd command will create a secret that contains credentials for accessing the internal OpenShift image registry
Next, run the get routes
command to view the registry endpoint. Copy it and insert it into the command after that replacing <Registry route>
. Then, replace with the email address you used for the IBM Cloud account.
This secret will be used to both push and pull images from your registry.
Now you can create the service account using the following yaml. You can find this yaml file at tekton/pipeline-account.yaml.
This yaml creates the following Kubernetes resources:
A ServiceAccount named pipeline-account
. The service account references the ibm-registry-secret
secret so that the pipeline can authenticate to your private container registry when it pushes and pulls a container image.
A Secret named kube-api-secret
which contains an API credential (generated by Kubernetes) for accessing the Kubernetes API. This allows the pipeline to use kubectl
to talk to your cluster.
A Role named pipeline-role
and a RoleBinding named pipeline-role-binding
which provide the resource-based access control permissions needed for this pipeline to create and modify Kubernetes resources.
Apply the file to your cluster to create the service account and related resources.
We've defined reusable Pipeline and Task resources for building and deploying an image. It is now time to look at how one runs the pipeline.
Below is a Tekton PipelineRun resource that runs the pipeline we defined above. You can find this yaml file at tekton/run/picalc-pipeline-run.yaml.
Although this file is small there is a lot going on here. Let's break it down from top to bottom:
The PipelineRun does not have a fixed name. It uses generateName
to generate a name each time it is created. This is because a particular PipelineRun resource executes the pipeline only once. If you want to run the pipeline again, you cannot modify an existing PipelineRun resource to request it to re-run -- you must create a new PipelineRun resource. While you could use name
to assign a unique name to your PipelineRun each time you create one, it is much easier to use generateName
.
The Pipeline resource is identified under the pipelineRef
key.
Parameters exposed by the pipeline are set to specific values such as the Git repository to clone, the image to build, and the yaml file to deploy. This example builds a go program that calculates an approximation of pi. The source includes a Dockerfile which runs tests, compiles the code, and builds an image for execution.
You must edit the picalc-pipeline-run.yaml
file to substitute the values of <REGISTRY>
with the information for your private container registry.
To find the value for <REGISTRY>
, enter the command oc get routes -n openshift-image-registry
.
The service account named pipeline-account
which we created earlier is specified to provide the credentials needed for the pipeline to run successfully.
The workspace used by the pipeline to clone the Git repository is mapped to a persistent volume claim which is a request for a storage volume.
Before you run the pipeline for the first time, you must create the persistent volume claim for the workspace.
The persistent volume claim requests Kubernetes to obtain a storage volume. Since each PipelineRun references the same claim and thus the same volume, this means the PipelineRun can only be run serially to avoid conflicting use of the volume. There is funtionality coming in Tekton to allow each PipelineRun to create its own persistent volume claim and thus use its own volume.
Check that the persistent volume claim is bound before continuing.
All the pieces are in place to run the pipeline.
Note that we're using kubectl create
here instead of kubectl apply
. As mentioned previously a given PipelineRun resource can run a pipeline only once so you need to create a new one each time you want to run the pipeline. kubectl
will respond with the generated name of the PipelineRun resource.
Let's view the status of our pipeline run
The pipeline will be successful when SUCCEEDED
is True
.
Check the status of the Kubernetes deployment. It should be ready.
You can curl the application using its NodePort service. First display the nodes and choose one of the node's external IP addresses. Then display the service to get its NodePort.
Let's take a look at what a PipelineRun failure would look like. Edit the PipelineRun yaml and change the gitUrl parameter to a non-existent Git repository to force a failure. Then create a new PipelineRun and describe it after letting it run for a minute or two.
The output tells us that the clone-repo
pipeline task failed. The Message
also tells us how to get the logs from the pod which was used to run the task:
If you run that kubectl logs
command you will see that there is a failure trying to fetch the non-existing Git repository.
An even easier way to get the logs from a PipelineRun is to use the tkn
CLI.
If you omit the -t
flag then the command will get the logs for all pipeline tasks that executed.
You can also get the logs for the last PipelineRun for a particular Pipeline using this command:
You should delete a PipelineRun when you no longer have a need to reference its logs. Deleting the PipelineRun deletes the pods that were used to run the pipeline tasks.
OpenShift provides a nice UI for the pipelines and the applications deployed. From the OpenShift console, click developer in the upper left drop-down to get to the developer view. Then click Topology to view your running app.
Click Pipelines to explore the pipline your created and explore the PipelineRuns
Tekton provides simple, easy-to-learn features for constructing CI/CD pipelines that run on Kubernetes. This tutorial covered the basics to get you started building your own pipelines. There are more features available and many more planned for upcoming releases.