ops-demo/docs/04-tekton-pipeline.md

272 lines
5.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Oefening 04 — Tekton Pipeline
**Tijd**: ~45 minuten
**Doel**: Een pipeline bouwen die automatisch de image-tag in Git aanpast en ArgoCD de update laat uitrollen — de volledige GitOps CI/CD-loop.
---
## Wat je leert
- Tekton-concepten: Task, Pipeline, PipelineRun, Workspace
- Hoe een pipeline via een Git-commit een GitOps-deployment triggert (geen container registry nodig)
- De volledige loop: pipeline push → ArgoCD detecteert → rolling update → nieuwe versie in browser
---
## De loop
```
Jij triggert een PipelineRun
Task 1: clone repo
Task 2: valideer manifests (kubectl dry-run)
Task 3: pas image-tag aan → deployment.yaml: 6.6.2 → 6.7.0
Task 4: git commit + push
ArgoCD detecteert de commit
ArgoCD synchroniseert de podinfo Deployment
Rolling update → podinfo v6.7.0 in je browser
```
---
## Vereisten
Oefeningen 0103 afgerond. podinfo is bereikbaar via **http://podinfo.192.168.56.200.nip.io** en toont versie **6.6.2**.
Je hebt nodig:
- Een GitHub Personal Access Token (PAT) met **repo**-scope (lezen + schrijven)
---
## Stappen
### 1. Tekton installeren via ArgoCD
**`manifests/ci/tekton/kustomization.yaml`**
```yaml
resources:
- https://storage.googleapis.com/tekton-releases/pipeline/previous/v0.65.1/release.yaml
```
**`apps/ci/tekton.yaml`**
```yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: tekton
namespace: argocd
annotations:
argocd.argoproj.io/sync-wave: "5"
spec:
project: workshop
source:
repoURL: JOUW_FORK_URL
targetRevision: HEAD
path: manifests/ci/tekton
kustomize: {}
destination:
server: https://kubernetes.default.svc
namespace: tekton-pipelines
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
- ServerSideApply=true
```
```bash
git add apps/ci/tekton.yaml manifests/ci/tekton/
git commit -m "feat: installeer Tekton via ArgoCD"
git push
```
Wacht tot Tekton draait (~35 minuten):
```bash
kubectl get pods -n tekton-pipelines
# tekton-pipelines-controller-xxx 1/1 Running
# tekton-pipelines-webhook-xxx 1/1 Running
```
---
### 2. Pipeline-resources aanmaken
**`manifests/ci/pipeline/serviceaccount.yaml`**
```yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: pipeline-runner
namespace: tekton-pipelines
```
**`manifests/ci/pipeline/pipeline.yaml`** — zie de solution branch voor de volledige inhoud, of kopieer uit `reference-solution`:
```bash
git show origin/solution/04-tekton-pipeline:manifests/ci/pipeline/pipeline.yaml
```
**`manifests/ci/pipeline/pipelinerun.yaml`**
```yaml
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: bump-podinfo-to-670
namespace: tekton-pipelines
spec:
pipelineRef:
name: gitops-image-bump
taskRunTemplate:
serviceAccountName: pipeline-runner
params:
- name: repo-url
value: JOUW_FORK_URL
- name: new-tag
value: "6.7.0"
workspaces:
- name: source
volumeClaimTemplate:
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 1Gi
- name: git-credentials
secret:
secretName: git-credentials
```
**`apps/ci/pipeline.yaml`**
```yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: workshop-pipeline
namespace: argocd
annotations:
argocd.argoproj.io/sync-wave: "6"
spec:
project: workshop
source:
repoURL: JOUW_FORK_URL
targetRevision: HEAD
path: manifests/ci/pipeline
destination:
server: https://kubernetes.default.svc
namespace: tekton-pipelines
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
```
```bash
git add apps/ci/pipeline.yaml manifests/ci/pipeline/
git commit -m "feat: voeg pipeline-resources toe"
git push
```
---
### 3. Git-credentials instellen
De pipeline moet kunnen pushen naar jouw fork. Maak een GitHub PAT aan met `repo`-scope en voer dan uit:
```bash
./scripts/vm/set-git-credentials.sh <jouw-github-gebruikersnaam> <jouw-pat>
```
Dit maakt een Kubernetes Secret aan in de cluster — **het PAT komt niet in Git**.
---
### 4. Pipeline triggeren
```bash
kubectl apply -f manifests/ci/pipeline/pipelinerun.yaml
```
Volg de voortgang:
```bash
kubectl get pipelinerun -n tekton-pipelines -w
```
Of per pod:
```bash
kubectl get pods -n tekton-pipelines -w
```
De PipelineRun duurt ~23 minuten.
---
### 5. Controleer de commit
```bash
git fetch origin
git log origin/main --oneline -3
# Je ziet: chore(pipeline): bump podinfo to 6.7.0
```
---
### 6. ArgoCD laten synchroniseren
Klik **Refresh** op de **podinfo** application in ArgoCD, of wacht op het automatische poll-interval.
```bash
kubectl rollout status deployment/podinfo -n podinfo
```
---
### 7. Controleer in de browser
Open **http://podinfo.192.168.56.200.nip.io** — je ziet nu versie **6.7.0**.
```bash
curl http://podinfo.192.168.56.200.nip.io | jq .version
# "6.7.0"
```
---
## Pipeline opnieuw uitvoeren
De naam van een PipelineRun moet uniek zijn:
```bash
kubectl delete pipelinerun bump-podinfo-to-670 -n tekton-pipelines
kubectl apply -f manifests/ci/pipeline/pipelinerun.yaml
```
---
## Probleemoplossing
| Symptoom | Oplossing |
|----------|-----------|
| PipelineRun blijft "Running" | `kubectl describe pipelinerun -n tekton-pipelines bump-podinfo-to-670` |
| Secret `git-credentials` niet gevonden | Voer `./scripts/vm/set-git-credentials.sh` uit |
| Push mislukt: 403 Forbidden | PAT heeft onvoldoende rechten — `repo`-scope vereist |
| ArgoCD synchroniseert niet | Klik **Refresh** in de UI |
---
## Volgende stap
In Oefening 05 kijk je terug op wat je gebouwd hebt en experimenteer je met drift detection.