tasks.md 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
---
title: "Getting started with Tasks"
likTitle: "Tasks"
weight: 1
description: >
  Set up and run your first Tekton task
---

This tutorial shows you how to 

1. Create a Kubernetes cluster with [minikube](https://minikube.sigs.k8s.io/).
1. Install Tekton pipelines.
1. Create a task.
1. Use `TaskRun` to instantiate and run your task.

## Prerequisites

1.  [Install minikube](https://minikube.sigs.k8s.io/docs/start/). You only have
    to complete the step 1, "Installation".
1.  [Install kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)

## Create your Kubernetes cluster

Create a cluster

```bash
minikube start
```

The process takes a few seconds, you see an output symilar to the following,
depending on the [minikube driver](https://minikube.sigs.k8s.io/docs/drivers/)
that you are using:

<pre>
😄  minikube v1.25.1
✨  Using the docker driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔄  Restarting existing docker container for "minikube" ...
🐳  Preparing Kubernetes v1.23.1 on Docker 20.10.12 ...
    ▪ kubelet.housekeeping-interval=5m
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
</pre>

You can check that the cluster was successfully created with `kubectl`:

```bash
kubectl cluster-info
```

The output confirms that Kubernetes is running:

<pre>
Kubernetes control plane is running at https://127.0.0.1:39509
CoreDNS is running at
https://127.0.0.1:39509/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
</pre>

## Install Tekton Pipelines

1. To install the latest version of Tekton Pipelines, use `kubectl`:

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

1. Monitor the installation:

   ```bash
   kubectl get pods --namespace tekton-pipelines --watch
   ```

When all components show `Running` under the `STATUS` column the installation
is complete.

Hit *Ctrl + C* to stop monitoring.

## Create and run a basic task

A **task**, represented in the API as an object of kind `Task`, defines a
series of **steps** that run sequentially to perform logic that the task
requires. Every task runs as a pod on your Kubernetes cluster, with each step
running in its own container.

1.  To create a task, open your favorite editor and create a file named
    `hello-world.yaml` with the following content:

    ```yaml
    apiVersion: tekton.dev/v1beta1
    kind: Task
    metadata:
      name: hello
    spec:
      steps:
        - name: echo
          image: alpine
          script: |
            #!/bin/bash
            echo "Hello World"
    ```

    


1.  Apply the changes your cluster:

    ```bash
    kubectl apply --filename hello-world.yaml
    ```

      The output confirms that the task was completed successfully.

      ```bash
      task.tekton.dev/hello created
      ```

1.  To run this task, you must instantiate it using `TaskRun`. Create another
    file named `hello-world-run.yaml` with the following content:

    ```yaml
    apiVersion: tekton.dev/v1beta1
    kind: TaskRun
    metadata:
      name: hello-task-run
    spec:
      taskRef:
        name: hello
    ```

1.  Apply the changes to your cluster to launch the task:

    ```bash
    kubectl apply --filename hello-world-run.yaml
    ``` 

1.  Verify that everything worked correctly:

    ```bash
    kubectl get taskrun hello-task-run
    ```

    The output of this command shows the status of the task

     <pre>
     NAME                               SUCCEEDED    REASON       STARTTIME   COMPLETIONTIME
     hello-task-run          True         Succeeded    22h         22h
     </pre>

    The value `True` under `SUCCEEDED` confirms that TaskRun completed with no errors.


1.  Take a look at the logs:

    ```
    kubectl logs --selector=tekton.dev/taskRun=hello-task-run
    ```

    The output displays the message:

    ```
    Hello World
    ```

## Cleanup

If you want to continue and create a pipeline, do not delete your cluster and
proceed to the [next tutorial](/docs/getting-started/pipelines/).

To delete the cluster that you created for this quickstart run:

```bash
minikube delete
```

The output confirms that your cluster was deleted:

<pre>
🔥  Deleting "minikube" in docker ...
🔥  Deleting container "minikube" ...
🔥  Removing /home/user/.minikube/machines/minikube ...
💀  Removed all traces of the "minikube" cluster.
</pre>