Zum Inhalt springen
Trainingslager IT-Trainingslager

Tag 0: Das Trainingsgelände

Ziel: Zwei Lab-VMs, k3s, die argocd-CLI und Helm, ein Fleet-Repo mit Deploy-Key und notes-api einmal von Hand im Cluster, damit du siehst, welches Problem Argo CD löst. · Dauer: ca. 45 min · Voraussetzung: ein Konto auf github.com

Briefing

  • GitOps heißt: Der gewünschte Zustand des Clusters steht in einem Git-Repo. Ein Programm im Cluster vergleicht das Repo laufend mit dem Cluster und gleicht ab. Wer etwas ändern will, ändert Git, nicht den Cluster.
  • Argo CD ist so ein Programm. Es läuft im Namespace argocd, hat eine Weboberfläche, eine CLI und eigene Kubernetes-Objekte wie Application.
  • Das Repo heißt in diesem Buch Fleet-Repo (argocd-fleet). Es liegt privat in deinem GitHub-Konto. Argo CD liest es über einen Deploy-Key, einen SSH-Schlüssel, der nur für dieses eine Repo gilt.
 Laptop ──ssh──► VM argo                       VM prod (ab Tag 11)
                  └─ k3s                          └─ k3s
                      ├─ argocd: Argo CD (ab Tag 1) ───► verwaltet prod
                      └─ notes:  notes-api (heute von Hand, ab Tag 2 aus Git)
                         ▲
 github.com/<du>/argocd-fleet ── Argo CD holt per SSH ──┘
 curl ─► http://<argo-IP>:30080  notes-api

Drill 1: Die Lab-VMs

Du brauchst zwei Linux-VMs mit Ubuntu 24.04 im selben Netz:

VM vCPU RAM Platte ab
argo 4 8 GB 40 GB Tag 0
prod 2 4 GB 30 GB Tag 11

Auf dem Laptop geht das zum Beispiel mit Multipass von multipass.run:

multipass launch 24.04 --name argo --cpus 4 --memory 8G --disk 40G
multipass launch 24.04 --name prod --cpus 2 --memory 4G --disk 30G
multipass info argo | grep IPv4
  • Jede andere Linux-VM mit sudo und Internetzugang tut es auch. Die VMs müssen github.com erreichen, umgekehrt muss GitHub sie nicht erreichen.
  • prod brauchst du erst an Tag 11. Bis dahin darf sie aus sein.

Ab hier läuft alles in der VM argo. Im Buch hat sie die Adresse 172.16.0.182, prod hat 172.16.0.123. Setze deine Adressen ein.

Fortschritt speichern? Kostenlos registrieren

Drill 2: k3s

curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.37.2+k3s1 \
  sh -s - --disable traefik --disable servicelb

Erwartet (gekürzt):

[INFO]  Using v1.37.2+k3s1 as release
…
[INFO]  systemd: Enabling k3s unit
[INFO]  systemd: Starting k3s
  • get.k3s.io ist das offizielle Installationsskript. Es lädt k3s, legt den Dienst an und verlinkt kubectl.
  • --disable traefik --disable servicelb: Einen Ingress-Controller und einen Load-Balancer brauchst du nicht, alles läuft über NodePorts.

Die Zugangsdaten gehören root. Du machst dir eine Kopie:

mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown "$(id -u):$(id -g)" ~/.kube/config
chmod 600 ~/.kube/config
echo 'export KUBECONFIG=~/.kube/config' >> ~/.bashrc
export KUBECONFIG=~/.kube/config
kubectl get nodes

Erwartet:

NAME   STATUS   ROLES           AGE   VERSION
argo   Ready    control-plane   21s   v1.37.2+k3s1

KUBECONFIG ist bei k3s Pflicht. Ohne die Variable liest dessen kubectl immer /etc/rancher/k3s/k3s.yaml und scheitert mit permission denied. Die argocd-CLI liest dieselbe Variable.

Fortschritt speichern? Kostenlos registrieren

Drill 3: argocd-CLI und Helm

Beide Programme kommen als fertige Binärdatei. Die argocd-CLI ist eine einzelne Datei auf GitHub:

mkdir -p ~/.local/bin ~/argo-lab
cd ~/argo-lab
curl -fsSL -o argocd \
  https://github.com/argoproj/argo-cd/releases/download/v3.5.7/argocd-linux-amd64
install -m 755 argocd ~/.local/bin/argocd
rm argocd

Helm brauchst du an Tag 1 für die Installation und an Tag 5 für eigene Charts:

curl -fsSLO https://get.helm.sh/helm-v4.3.2-linux-amd64.tar.gz
tar xzf helm-v4.3.2-linux-amd64.tar.gz
mv linux-amd64/helm ~/.local/bin/
rm -r linux-amd64 helm-v4.3.2-linux-amd64.tar.gz

Ubuntu nimmt ~/.local/bin beim Anmelden in den PATH auf, sobald der Ordner existiert. Melde dich einmal ab und wieder an:

exit
ssh [email protected]
argocd version --client --short

Erwartet:

argocd: v3.5.7+6d73296
helm version --short

Erwartet:

v4.3.2+g8346aaa

Fortschritt speichern? Kostenlos registrieren

Drill 4: Fleet-Repo und Deploy-Key

Lege im Browser ein privates Repo an: github.com → New repository → Name argocd-fleet, Private, ohne README. Mit der GitHub-CLI auf dem Laptop geht es auch mit gh repo create argocd-fleet --private.

In der VM erzeugst du einen eigenen Schlüssel nur für dieses Repo:

ssh-keygen -q -t ed25519 -N "" -C argocd-fleet -f ~/.ssh/argocd-fleet
cat ~/.ssh/argocd-fleet.pub

Erwartet (dein Schlüssel weicht ab):

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFaWVjfNDS1nnR5RjlbUFtrY8/U1V30V864SgtlgfuyJ argocd-fleet

Den öffentlichen Teil trägst du im Repo ein: Settings → Deploy keys → Add deploy key, Titel argo-lab, Schlüssel einfügen, Allow write access anhaken. Schreiben darf der Schlüssel, weil du von der VM aus pushst und Argo CD an Tag 12 selbst Commits macht.

git soll für github.com genau diesen Schlüssel nehmen:

cat > ~/.ssh/config <<'EOF'
Host github.com
  IdentityFile ~/.ssh/argocd-fleet
EOF
chmod 600 ~/.ssh/config
ssh-keyscan -t ed25519 github.com >> ~/.ssh/known_hosts
ssh -T [email protected]

Erwartet (gekürzt):

# github.com:22 SSH-2.0-11529790
Hi <du>/argocd-fleet! You've successfully authenticated, but GitHub does not provide shell access.

GitHub nennt den Repo-Namen statt deines Kontos. Daran erkennst du, dass ein Deploy-Key greift und kein persönlicher Schlüssel. ssh-keyscan holt den Host-Key von GitHub. Vergleiche ihn bei Bedarf mit den Fingerprints unter docs.github.com → „GitHub’s SSH key fingerprints“.

Jetzt klonst du das leere Repo und stellst git ein:

cd ~/argo-lab
git clone [email protected]:<du>/argocd-fleet.git
git config --global user.name "Dein Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main

Erwartet:

Cloning into 'argocd-fleet'...
warning: You appear to have cloned an empty repository.

Fortschritt speichern? Kostenlos registrieren

Drill 5: Der Begleitcode

git clone -q https://github.com/thomaszachmann/argocd-zum-anfassen.git ~/argo-lab/code
ls ~/argo-lab/code

Erwartet:

LICENSE  README.md  loesung  wettkampf
  • loesung/tagNN enthält den Stand deines Fleet-Repos am Ende jedes Tags. Du schreibst alles selbst und vergleichst am Ende mit diff.
  • wettkampf/ brauchst du erst an Tag 15.

Ein Image musst du nicht bauen. notes-api liegt fertig in der GitHub Container Registry: ghcr.io/thomaszachmann/notes-api, unter anderem in den Versionen 0.5.1 bis 0.7.1.

Fortschritt speichern? Kostenlos registrieren

Drill 6: notes-api von Hand

Bevor Argo CD ins Spiel kommt, bringst du notes-api so in den Cluster, wie es ohne GitOps üblich ist: Manifest schreiben, kubectl apply. Ohne Datenbank speichert notes-api die Notizen im Speicher, das reicht für heute. Postgres kommt an Tag 4.

mkdir -p ~/argo-lab/hand
cd ~/argo-lab/hand
cat > notes.yaml <<'EOF'
apiVersion: v1
kind: Namespace
metadata:
  name: notes
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: notes
  namespace: notes
spec:
  replicas: 1
  selector:
    matchLabels:
      app: notes
  template:
    metadata:
      labels:
        app: notes
    spec:
      containers:
        - name: api
          image: ghcr.io/thomaszachmann/notes-api:0.5.1
          ports:
            - containerPort: 8080
          env:
            - name: NOTES_API_KEY
              value: lab-api-key-123
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: notes
  namespace: notes
spec:
  type: NodePort
  selector:
    app: notes
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30080
EOF
kubectl apply -f notes.yaml
kubectl -n notes rollout status deploy/notes

Erwartet:

namespace/notes created
deployment.apps/notes created
service/notes created
Waiting for deployment "notes" rollout to finish: 0 of 1 updated replicas are available...
deployment "notes" successfully rolled out
curl -s localhost:30080/version

Erwartet:

0.5.1

Antwortet curl nicht, richtet k3s den NodePort noch ein. Ein paar Sekunden warten und noch einmal.

Fortschritt speichern? Kostenlos registrieren

Drill 7: Die Abweichung

Ein Kollege hat es eilig und zieht notes-api direkt im Cluster auf eine neue Version:

kubectl -n notes set image deploy/notes api=ghcr.io/thomaszachmann/notes-api:0.7.1
kubectl -n notes rollout status deploy/notes

Erwartet (gekürzt):

deployment.apps/notes image updated
…
deployment "notes" successfully rolled out
curl -s localhost:30080/version

Erwartet:

0.7.1

Deine Datei sagt weiter 0.5.1. Cluster und Datei sind auseinandergelaufen, und niemand merkt es. Sichtbar wird es nur, wenn jemand nachsieht:

kubectl diff -f notes.yaml

Erwartet (gekürzt):

-        image: ghcr.io/thomaszachmann/notes-api:0.7.1
+        image: ghcr.io/thomaszachmann/notes-api:0.5.1

Diese Abweichung heißt Drift. Ohne GitOps hängt es an Disziplin, ob sie jemand bemerkt und zurückdreht. Ab Tag 3 erledigt Argo CD das von selbst.

Fortschritt speichern? Kostenlos registrieren

Kontrollpunkt

kubectl get nodes

Erwartet: ein Node argo mit STATUS Ready.

argocd version --client --short

Erwartet: argocd: v3.5.7+6d73296.

git -C ~/argo-lab/argocd-fleet remote -v

Erwartet: zweimal [email protected]:<du>/argocd-fleet.git, einmal mit (fetch), einmal mit (push).

Challenge

Dreh die Abweichung von Hand zurück und prüfe, dass kubectl diff danach schweigt. Räume danach den Namespace notes ab, denn ab Tag 2 legt ihn Argo CD an.

Hinweis: kubectl apply setzt den Stand der Datei durch. kubectl diff endet ohne Ausgabe, wenn nichts abweicht.

Lösung

kubectl apply -f notes.yaml
kubectl -n notes rollout status deploy/notes
kubectl diff -f notes.yaml

Erwartet (gekürzt):

namespace/notes unchanged
deployment.apps/notes configured
service/notes unchanged
…
deployment "notes" successfully rolled out

kubectl diff gibt danach nichts mehr aus. Aufräumen:

kubectl delete -f notes.yaml

Erwartet:

namespace "notes" deleted
deployment.apps "notes" deleted from notes namespace
service "notes" deleted from notes namespace

Fortschritt speichern? Kostenlos registrieren

Enterprise-Notiz

  • In Firmen-Clustern installiert ein Plattform-Team Argo CD. Teams bekommen Projekte und Rechte, nicht cluster-admin (Tag 9).
  • Deploy-Keys gelten für genau ein Repo. Für viele Repos nimmt man eine GitHub App oder ein Maschinenkonto mit Lese-Token, nie den Schlüssel einer Person.
  • kubectl direkt am Produktivcluster ist mit GitOps die Ausnahme, etwa im Notfall. Viele Teams geben Menschen dort nur Leserechte.