Zum Inhalt springen
Trainingslager IT-Trainingslager

Tag 5: Umgebungen und Repo-Aufbau

Ziel: notes-api in eine gemeinsame Basis und Overlays pro Umgebung zerlegen, ohne den laufenden Betrieb zu stören, eine Umgebung dev dazunehmen und eine Version von dev nach prod befördern. · Dauer: ca. 75 min · Voraussetzung: Tag 4

Briefing

  • Eine Umgebung ist hier ein eigener Namespace mit eigener Datenbank: notes ist prod, notes-dev ist dev. In der Praxis sind es oft eigene Cluster. Das Prinzip bleibt gleich.
  • Base und Overlays sind Kustomize-Werkzeuge: Die Base enthält, was überall gleich ist. Ein Overlay nimmt die Base und ändert nur, was abweicht, etwa Namespace und Port.
  • Flux empfiehlt einen festen Aufbau für das Fleet-Repo:
 flux-fleet/
 ├── apps/                    was Teams betreiben
 │   ├── notes/
 │   │   ├── base/            gleich für alle Umgebungen
 │   │   └── overlays/
 │   │       ├── dev/         nur die Unterschiede
 │   │       └── prod/
 │   └── notes-db/            gleiches Muster
 ├── infrastructure/          was die Plattform für alle betreibt (Registry, später kro)
 └── clusters/
     └── lab/                 pro Cluster: welche Ordner hier laufen, mit welchen Werten
  • clusters/<name> enthält nur Flux-Objekte (Quellen, Kustomizations, Variablen). Die eigentlichen Manifeste liegen in apps/ und infrastructure/. Ein neuer Cluster ist damit ein neuer Ordner unter clusters/, der auf dieselben Overlays zeigt.

Drill 1: Umbauen ohne Neustart

Die Dateien wandern in base/. Für prod gibt es ein Overlay, das die Base nur übernimmt:

cd ~/flux-lab/fleet
for a in notes notes-db; do
  mkdir -p apps/$a/base apps/$a/overlays/prod
  git mv apps/$a/*.yaml apps/$a/base/
  printf 'apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\nresources:\n  - ../../base\n' \
    > apps/$a/overlays/prod/kustomization.yaml
done
sed -i 's#path: ./apps/notes$#path: ./apps/notes/overlays/prod#' clusters/lab/notes.yaml
sed -i 's#path: ./apps/notes-db$#path: ./apps/notes-db/overlays/prod#' clusters/lab/notes-db.yaml
grep path: clusters/lab/notes*.yaml

Erwartet:

clusters/lab/notes-db.yaml:  path: ./apps/notes-db/overlays/prod
clusters/lab/notes.yaml:  path: ./apps/notes/overlays/prod

Nach Tag 4 bist du vorsichtig. Bevor du pushst, prüfst du, dass sich im Cluster nichts ändert:

flux diff kustomization notes --path ./apps/notes/overlays/prod \
  --kustomization-file clusters/lab/notes.yaml; echo "Exit-Code $?"
flux diff kustomization notes-db --path ./apps/notes-db/overlays/prod \
  --kustomization-file clusters/lab/notes-db.yaml; echo "Exit-Code $?"

Erwartet:

Exit-Code 0
Exit-Code 0

Kein Unterschied: Dieselben Kustomizations wenden dieselben Objekte an, nur aus anderen Ordnern. Kein Objekt wechselt den Besitzer, also räumt prune nichts ab.

git add -A
git commit -m "notes: base und overlays"
git push
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes-db
flux reconcile kustomization notes
kubectl -n notes get pods

Erwartet (gekürzt): Die Pods von notes und notes-db sind älter als der Commit.

Fortschritt speichern? Kostenlos registrieren

Drill 2: Die Umgebung dev

Das Overlay für dev setzt einen anderen Namespace. Kustomize ändert damit auch den Namen des Namespace-Objekts aus der Base, aus notes wird notes-dev:

mkdir -p apps/notes-db/overlays/dev apps/notes/overlays/dev
cat > apps/notes-db/overlays/dev/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: notes-dev
resources:
  - ../../base
EOF

notes-api in dev braucht außerdem einen anderen NodePort. Den ändert ein Patch:

cat > apps/notes/overlays/dev/kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: notes-dev
resources:
  - ../../base
patches:
  - target:
      kind: Service
      name: notes
    patch: |
      - op: replace
        path: /spec/ports/0/nodePort
        value: 30081
EOF
kubectl kustomize apps/notes/overlays/dev | grep -E 'namespace:|nodePort'

Erwartet:

  namespace: notes-dev
  namespace: notes-dev
  - nodePort: 30081
  namespace: notes-dev

kubectl kustomize baut ein Overlay lokal, ohne Flux und ohne Variablen. Die beiden Kustomizations für dev kommen in eine Datei. dev bekommt eine neuere Version von notes-api als prod:

flux create kustomization notes-db-dev --source=GitRepository/flux-system \
  --path=./apps/notes-db/overlays/dev --prune=true --interval=10m --wait=true \
  --export > clusters/lab/notes-dev.yaml
flux create kustomization notes-dev --source=GitRepository/flux-system \
  --path=./apps/notes/overlays/dev --prune=true --interval=5m --wait=true \
  --timeout=2m --depends-on=notes-db-dev --export >> clusters/lab/notes-dev.yaml
cat >> clusters/lab/notes-dev.yaml <<'EOF'
  postBuild:
    substitute:
      NOTES_VERSION: 0.7.1
    substituteFrom:
      - kind: ConfigMap
        name: cluster-vars
EOF

NOTES_VERSION steht jetzt zweimal da: 0.5.3 in der ConfigMap, 0.7.1 unter substitute. Die Werte unter substitute haben Vorrang.

git add -A
git commit -m "notes: Umgebung dev"
git push
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes-db-dev
flux reconcile kustomization notes-dev
flux get kustomizations

Erwartet (gekürzt):

NAME            REVISION                SUSPENDED   READY   MESSAGE
flux-system     main@sha1:048e0670      False       True    Applied revision: main@sha1:048e0670
notes           main@sha1:048e0670      False       True    Applied revision: main@sha1:048e0670
notes-db        main@sha1:048e0670      False       True    Applied revision: main@sha1:048e0670
notes-db-dev    main@sha1:048e0670      False       True    Applied revision: main@sha1:048e0670
notes-dev       main@sha1:048e0670      False       True    Applied revision: main@sha1:048e0670
podinfo         6.17.0@sha256:3a5e82b1  False       True    Applied revision: 6.17.0@sha256:3a5e82b1
registry        main@sha1:048e0670      False       True    Applied revision: main@sha1:048e0670
curl -s localhost:30081/version; echo
curl -s localhost:30080/version; echo
kubectl -n notes-dev get deploy

Erwartet:

0.7.1
0.5.3
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
notes      1/1     1            1           10s
notes-db   1/1     1            1           34s

dev läuft mit einer Replica: NOTES_REPLICAS ist dort nicht gesetzt, also greift der Standardwert aus ${NOTES_REPLICAS:=1}.

Fortschritt speichern? Kostenlos registrieren

Drill 3: Befördern

0.7.1 hat sich in dev bewährt. Prod bekommt die Version über die ConfigMap des Clusters. Der Vorrang in dev ist damit überflüssig, er fliegt raus:

sed -i 's/NOTES_VERSION: 0.5.3/NOTES_VERSION: 0.7.1/' clusters/lab/cluster-vars.yaml
sed -i '/substitute:$/,/NOTES_VERSION: 0.7.1/d' clusters/lab/notes-dev.yaml
tail -5 clusters/lab/notes-dev.yaml

Erwartet:

  postBuild:
    substituteFrom:
      - kind: ConfigMap
        name: cluster-vars
git commit -am "notes 0.7.1 nach prod"
git push
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes
curl -s localhost:30080/version; echo
curl -s localhost:30080/notes

Erwartet (gekürzt):

✔ applied revision main@sha1:9d1fa13881ed44265c91e36f1f6fa8b399e97d1d
0.7.1
[{"id":"ec17fa30-3215-4493-841d-8e7dbb16dae7","text":"Hallo Flux"}]
  • Das Upgrade hat die Daten mitgenommen. Der Init-Container migrate passt das Schema an, bevor die neue Version startet.
  • Wie bei jedem Commit gleichen alle Kustomizations ab, die aus flux-system lesen. Das zweite reconcile sorgt nur dafür, dass notes die schon geänderte ConfigMap liest und nicht zufällig noch die alte.

Fortschritt speichern? Kostenlos registrieren

Kontrollpunkt

flux get kustomizations

Erwartet: sieben Kustomizations, alle READY True.

curl -s localhost:30081/version; echo

Erwartet: 0.7.1.

diff -r ~/flux-lab/fleet/apps ~/flux-lab/code/loesung/tag05/apps

Erwartet: keine Ausgabe.

Challenge

Das QA-Team möchte eine dritte Umgebung test: eigener Namespace notes-test, eigene Datenbank, notes-api auf Port 30082. Bau sie, prüf sie und bau sie danach wieder ab. Bleibt nach dem Abbau etwas übrig?

Hinweis: Die Overlays für dev sind eine gute Vorlage. Und erinnere dich an die Annotation am Namespace aus Tag 4.

Lösung

for a in notes notes-db; do
  cp -r apps/$a/overlays/dev apps/$a/overlays/test
  sed -i 's/notes-dev/notes-test/; s/30081/30082/' apps/$a/overlays/test/kustomization.yaml
done
sed 's/-dev/-test/g; s#overlays/dev#overlays/test#' clusters/lab/notes-dev.yaml   > clusters/lab/notes-test.yaml
git add -A
git commit -m "notes: Umgebung test"
git push
flux reconcile kustomization flux-system --with-source
flux reconcile kustomization notes-db-test
flux reconcile kustomization notes-test
curl -s localhost:30082/version; echo

Erwartet (gekürzt):

...
✔ applied revision main@sha1:1b213d7067ca133c438ccc74d84a11caffb28743
0.7.1

Abbauen heißt: die Dateien aus Git löschen.

git rm -q -r apps/notes/overlays/test apps/notes-db/overlays/test \
  clusters/lab/notes-test.yaml
git commit -m "notes: Umgebung test weg"
git push
flux reconcile kustomization flux-system --with-source
kubectl get ns notes-test
kubectl -n notes-test get all,pvc,secret

Erwartet:

NAME         STATUS   AGE
notes-test   Active   68s
No resources found in notes-test namespace.
  • flux-system hat die beiden Kustomizations gelöscht. Weil sie prune: true haben, räumen sie beim Löschen alles ab, was sie angelegt haben: Deployments, Services, Secrets, auch das Volume.
  • Nur der Namespace bleibt, die Annotation prune: disabled schützt ihn. Ein leerer Namespace stört nicht, aber du räumst ihn bewusst von Hand weg:
kubectl delete ns notes-test

Fortschritt speichern? Kostenlos registrieren

Enterprise-Notiz

  • Monorepo oder ein Repo pro Umgebung? Ein Repo für alles ist am einfachsten und zeigt jede Beförderung als Diff. Viele Firmen trennen trotzdem: Teams pflegen apps/ in eigenen Repos, das Plattform-Team besitzt clusters/ und infrastructure/. Flux verbindet das mit einer GitRepository pro Team-Repo (Tag 12).
  • Befördern per Pull Request: Eine Beförderung ist ein Commit wie jeder andere. Im Betrieb geht er als Pull Request durch ein Review, und prod liest vielleicht einen geschützten Branch oder ein freigegebenes OCI-Artefakt (Tag 3).
  • Variablen von außen: Kommt eine ConfigMap für substituteFrom nicht aus demselben Repo, sondern etwa von einem Cloud-Werkzeug, bekommt sie das Label reconcile.fluxcd.io/watch: Enabled. Dann gleichen die Kustomizations sofort ab, wenn sie sich ändert.
  • Cluster statt Namespaces: dev und prod im selben Cluster teilen sich Knoten, Netz und Admins. Für echte Trennung bekommt jede Umgebung einen eigenen Cluster mit eigenem Ordner clusters/dev und clusters/prod.