Zum Inhalt springen
Trainingslager IT-Trainingslager

Anhang B: Abschlussprüfung

Ziel: Vier Übungen ohne Anleitung, die alles aus dem Buch verbinden. · Dauer: ca. 60 min · Voraussetzung: Tag 0–15

Spielregeln

  • Du arbeitest in VM argo im Fleet-Repo ~/argo-lab/argocd-fleet. Alles, was den Cluster ändert, geht über Git. Ausnahmen sind nur Benutzer in argocd-cm und Passwörter.
  • Jede Übung hat Prüfbefehle. Erst wenn sie zeigen, was dort steht, ist die Übung gelöst.
  • Die Lösungen liegen auch in ~/argo-lab/code/loesung/pruefung/.
cd ~/argo-lab/argocd-fleet
git pull

Übung 1: Eine Umgebung staging

notes bekommt eine Umgebung staging im Namespace notes-staging auf NodePort 30088. Sie läuft mit notes-api 0.7.0, unabhängig davon, was base sagt. Angelegt wird keine Application, nur ein Ordner.

Prüfe:

curl -s localhost:30088/version; echo
argocd app get notes-staging | grep -E "^Project|Sync Status|Health Status"

Erwartet (gekürzt):

0.7.0

Project:            notes
Sync Status:        Synced to main (25dc551)
Health Status:      Healthy

Lösung

mkdir -p apps/notes/overlays/staging
cat > apps/notes/overlays/staging/env.yaml <<'EOF'
name: notes-staging
namespace: notes-staging
EOF
cat > apps/notes/overlays/staging/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../../base
images:
  - name: ghcr.io/thomaszachmann/notes-api
    newTag: 0.7.0
patches:
  - path: nodeport.yaml
EOF
cat > apps/notes/overlays/staging/nodeport.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
  name: notes
spec:
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30088
EOF
git add -A
git commit -m "Umgebung staging"
git push
kubectl -n argocd annotate appset notes \
  argocd.argoproj.io/application-set-refresh=true --overwrite
sleep 15
argocd app wait notes-staging --health --operation --timeout 300 | grep "^Phase"

Erwartet (gekürzt):

…
Phase:              Succeeded

Das ApplicationSet aus Tag 8 erzeugt die Application, das Projekt aus Tag 9 erlaubt notes-staging, und images aus Tag 6 setzt die Version.

Fortschritt speichern? Kostenlos registrieren

Übung 2: Ein Benutzer, der nur liest

Lege einen lokalen Benutzer bob mit dem Passwort lab-bob-pw-123 an. bob sieht alle Applications im Projekt notes, darf aber nichts synchronisieren. Die Regel steht in Git.

Prüfe:

argocd login 172.16.0.182:30443 --username bob --password lab-bob-pw-123 \
  --insecure --name bob
argocd account can-i get applications "notes/*"
argocd account can-i sync applications "notes/*"
argocd app list -o name
argocd context 172.16.0.182:30443

Erwartet:

'bob:login' logged in successfully
Context 'bob' updated
yes
no
argocd/notes
argocd/notes-dev
argocd/notes-rollout
argocd/notes-staging
Switched to context '172.16.0.182:30443'

Lösung

kubectl -n argocd patch cm argocd-cm --type merge \
  -p '{"data":{"accounts.bob":"login"}}'
argocd account update-password --account bob \
  --current-password lab-argo-pw-123 --new-password lab-bob-pw-123
cat >> platform/argocd/argocd-rbac-cm.yaml <<'EOF'
    p, role:notes-leser, applications, get, notes/*, allow
    g, bob, role:notes-leser
EOF
tail -6 platform/argocd/argocd-rbac-cm.yaml
git commit -am "RBAC: bob liest notes"
git push
argocd app get argocd-extras --refresh
sleep 15

Erwartet (gekürzt):

configmap/argocd-cm patched
Password updated
  policy.csv: |
    p, role:notes-entwickler, applications, get, notes/*, allow
    p, role:notes-entwickler, applications, sync, notes/notes-dev, allow
    g, alice, role:notes-entwickler
    p, role:notes-leser, applications, get, notes/*, allow
    g, bob, role:notes-leser
…

Die neuen Zeilen brauchen dieselbe Einrückung wie die alten, sonst gehören sie nicht mehr zu policy.csv.

Fortschritt speichern? Kostenlos registrieren

Übung 3: Ein Addon für alle Cluster

Auf jedem Cluster mit einem Label stufe soll im Namespace hallo eine ConfigMap hallo mit dem Eintrag gruss: Hallo aus dem Fleet-Repo liegen. Ändere dafür kein ApplicationSet und keine Application.

Prüfe:

argocd app list -o name | grep hallo
kubectl --kubeconfig ~/.kube/prod.yaml -n hallo get cm hallo \
  -o jsonpath='{.data.gruss}'; echo
kubectl -n hallo get cm hallo -o jsonpath='{.data.gruss}'; echo

Erwartet:

argocd/hallo-in-cluster
argocd/hallo-prod
Hallo aus dem Fleet-Repo
Hallo aus dem Fleet-Repo

Lösung

mkdir -p addons/hallo
cat > addons/hallo/configmap.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: hallo
data:
  gruss: Hallo aus dem Fleet-Repo
EOF
git add -A
git commit -m "Addon hallo"
git push
kubectl -n argocd annotate appset addons \
  argocd.argoproj.io/application-set-refresh=true --overwrite
sleep 30

Der Matrix-Generator aus Tag 11 kombiniert jeden Ordner unter addons mit jedem Cluster mit Label stufe. Ein neuer Ordner reicht.

Fortschritt speichern? Kostenlos registrieren

Übung 4: staging folgt der Reihe 0.7

Der Image Updater soll auch notes-staging überwachen, mit derselben Bedingung wie für notes und notes-dev. Danach steht in Git eine neue Version für staging, ohne dass du sie eingetragen hast.

Prüfe (nach etwa drei Minuten):

git pull
git log -1 --format="%an: %s"
grep newTag apps/notes/overlays/staging/kustomization.yaml
curl -s localhost:30088/version; echo
kubectl -n argocd get imageupdater notes

Erwartet (gekürzt):

…
argocd-image-updater: build: automatic update of notes-staging
    newTag: 0.7.1
0.7.1

NAME    APPS   IMAGES   LAST CHECKED   READY
notes   3      3        56s            True

Lösung

applicationRefs ist der letzte Block in applications/notes-updater.yaml. Du hängst einen dritten Eintrag an:

cat >> applications/notes-updater.yaml <<'EOF'
    - namePattern: notes-staging
      images:
        - alias: api
          imageName: ghcr.io/thomaszachmann/notes-api:~0.7
EOF
git commit -am "ImageUpdater auch für notes-staging"
git push
argocd app get root --refresh
sleep 150

Der Image Updater ersetzt in der kustomization.yaml von staging 0.7.0 durch 0.7.1, genau wie kustomize edit set image. Der feste Tag aus Übung 1 war also nur der Startpunkt.

Fortschritt speichern? Kostenlos registrieren

Aufräumen

staging, das Addon und bob kommen wieder weg. Die beiden Dateien, an die du etwas angehängt hast, schreibst du ganz neu:

cd ~/argo-lab/argocd-fleet
git pull
git rm -r apps/notes/overlays/staging addons/hallo
cat > applications/notes-updater.yaml <<'EOF'
apiVersion: argocd-image-updater.argoproj.io/v1alpha1
kind: ImageUpdater
metadata:
  name: notes
  namespace: argocd
spec:
  writeBackConfig:
    method: git
    gitConfig:
      branch: main
      writeBackTarget: kustomization
  applicationRefs:
    - namePattern: notes
      images:
        - alias: api
          imageName: ghcr.io/thomaszachmann/notes-api:~0.7
    - namePattern: notes-dev
      images:
        - alias: api
          imageName: ghcr.io/thomaszachmann/notes-api:~0.7
EOF
cat > platform/argocd/argocd-rbac-cm.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-rbac-cm
  namespace: argocd
  labels:
    app.kubernetes.io/name: argocd-rbac-cm
    app.kubernetes.io/part-of: argocd
data:
  policy.csv: |
    p, role:notes-entwickler, applications, get, notes/*, allow
    p, role:notes-entwickler, applications, sync, notes/notes-dev, allow
    g, alice, role:notes-entwickler
EOF
git commit -am "Prüfung aufgeräumt"
git push
kubectl -n argocd patch cm argocd-cm --type json \
  -p '[{"op":"remove","path":"/data/accounts.bob"}]'
argocd app get root --refresh
argocd app get argocd-extras --refresh
kubectl -n argocd annotate appset notes \
  argocd.argoproj.io/application-set-refresh=true --overwrite
kubectl -n argocd annotate appset addons \
  argocd.argoproj.io/application-set-refresh=true --overwrite
sleep 30
kubectl delete ns notes-staging hallo
kubectl --kubeconfig ~/.kube/prod.yaml delete ns hallo
argocd app list | awk 'NR>1 {print $5, $6}' | sort | uniq -c

Erwartet (gekürzt):

…
namespace "notes-staging" deleted
namespace "hallo" deleted
namespace "hallo" deleted
     11 Synced Healthy
  • notes-staging bleibt nach dem Entfernen des Ordners stehen, wegen preserveResourcesOnDeletion. Deshalb löschst du den Namespace selbst.
  • Die Applications von hallo hatten den Finalizer, die ConfigMaps sind schon weg. Nur die Namespaces bleiben, wie in Tag 7.

Ausprobiert: argocd context --delete bob hat nicht nur den Kontext von bob gelöscht, sondern die ganze lokale Konfiguration der CLI, auch die Anmeldungen als admin und alice. Alle drei Kontexte teilen sich denselben Server. Lass den Kontext bob deshalb einfach stehen. Ohne accounts.bob kann er nichts mehr.