Zum Inhalt springen
IT-Trainingslagerhands-on · kostenlos
GPU-Infrastruktur auf Kubernetes · Kapitel 7 von 19 Tag 5: GPUs teilen mit Time-Slicing

Tag 5: GPUs teilen mit Time-Slicing

Ziel: Eine GPU mehreren Pods gleichzeitig geben, die Grenzen des Teilens kennen und es so konfigurieren, dass niemand aus Versehen mehr bekommt, als er glaubt · Dauer: ca. 60 min · Voraussetzung: Tag 4

Briefing

  • Viele GPU-Workloads nutzen ihre Karte kaum: ein Jupyter-Notebook, ein kleines Modell zum Testen, eine Demo. Eine ganze A100 dafür ist Verschwendung.
  • Time-Slicing: Der Device Plugin meldet jede GPU mehrfach (Replicas). Vier Pods bekommen dieselbe GPU und wechseln sich bei der Rechenzeit ab.
  • Was Time-Slicing nicht kann: Speicher trennen, Leistung garantieren, Fehler isolieren. Belegt ein Pod 39 der 40 GB, scheitern die anderen. Stürzt einer ab, kann er die anderen mitreißen. Für harte Trennung gibt es MIG (Tag 6).
  • Mokka simuliert das Zuteilen, nicht das Rechnen. Was du heute siehst, ist das echte Verhalten des Device Plugins und des Schedulers. Wie sich vier Pods auf einer echten GPU die Rechenzeit teilen, zeigt Mokka nicht.
  1 physische GPU  ──Device Plugin, replicas: 4──▶  4 × nvidia.com/gpu im allocatable
                                                     Pod A, B, C, D sehen dieselbe UUID

Drill 1: Die Konfiguration

Der Device Plugin liest seine Einstellungen aus einer Datei:

mkdir -p ~/gpu/tag05 && cd ~/gpu/tag05
kubectl -n kube-system exec ds/nvidia-device-plugin-mock -- nvidia-device-plugin --help \
  | grep -i config-file | sed 's/  */ /g'

Erwartet:

 --config-file value the path to a config file as an alternative to command line options or environment variables [$CONFIG_FILE]
cat > time-slicing.yaml <<EOF
version: v1
flags:
  migStrategy: none
sharing:
  timeSlicing:
    resources:
    - name: nvidia.com/gpu
      replicas: 4
EOF
kubectl -n kube-system create configmap gpu-sharing --from-file=config.yaml=time-slicing.yaml

Erwartet:

configmap/gpu-sharing created

Fortschritt speichern? Kostenlos registrieren

Drill 2: Den Device Plugin umstellen

Die ConfigMap als Datei einhängen und dem Plugin den Pfad geben:

kubectl -n kube-system patch ds nvidia-device-plugin-mock --type=json -p='[
 {"op":"add","path":"/spec/template/spec/volumes/-",
  "value":{"name":"sharing","configMap":{"name":"gpu-sharing"}}},
 {"op":"add","path":"/spec/template/spec/containers/0/volumeMounts/-",
  "value":{"name":"sharing","mountPath":"/config"}},
 {"op":"add","path":"/spec/template/spec/containers/0/args/-",
  "value":"--config-file=/config/config.yaml"}]'
kubectl -n kube-system rollout status ds/nvidia-device-plugin-mock --timeout=120s
sleep 10
kubectl get node gpu-worker2 -o custom-columns='CAP:.status.capacity.nvidia\.com/gpu,ALLOC:.status.allocatable.nvidia\.com/gpu'

Erwartet (gekürzt):

daemonset.apps/nvidia-device-plugin-mock patched
daemon set "nvidia-device-plugin-mock" successfully rolled out
CAP   ALLOC
32    32

Aus 8 GPUs werden 32. Im Log steht, was der Plugin verstanden hat:

kubectl -n kube-system logs ds/nvidia-device-plugin-mock | grep -A8 '"sharing"'

Erwartet:

  "sharing": {
    "timeSlicing": {
      "resources": [
        {
          "name": "nvidia.com/gpu",
          "devices": "all",
          "replicas": 4
        }
      ]

Fortschritt speichern? Kostenlos registrieren

Drill 3: Zehn Notebooks auf acht GPUs

kubectl create namespace tag05
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: notebook
  namespace: tag05
spec:
  replicas: 10
  selector:
    matchLabels:
      app: notebook
  template:
    metadata:
      labels:
        app: notebook
    spec:
      tolerations:
      - key: nvidia.com/gpu
        operator: Exists
        effect: NoSchedule
      containers:
      - name: c
        image: ubuntu:24.04
        command: [sleep, infinity]
        resources:
          limits:
            nvidia.com/gpu: 1
EOF
kubectl -n tag05 rollout status deploy/notebook --timeout=180s | tail -1
for p in $(kubectl -n tag05 get pods -l app=notebook -o name); do
  kubectl -n tag05 exec $p -- printenv NVIDIA_VISIBLE_DEVICES
done | sort | uniq -c
kubectl describe node gpu-worker2 | sed -n '/Allocated resources/,/Events/p' | grep nvidia

Erwartet:

deployment "notebook" successfully rolled out
      1 GPU-12345678-1234-1234-1234-123456780000
      1 GPU-12345678-1234-1234-1234-123456780001
      1 GPU-12345678-1234-1234-1234-123456780002
      2 GPU-12345678-1234-1234-1234-123456780003
      1 GPU-12345678-1234-1234-1234-123456780004
      1 GPU-12345678-1234-1234-1234-123456780005
      1 GPU-12345678-1234-1234-1234-123456780006
      2 GPU-12345678-1234-1234-1234-123456780007
  nvidia.com/gpu     10          10

Zehn Pods auf acht GPUs, zwei GPUs teilen sich je zwei Pods. Der Plugin verteilt gleichmäßig, bevor er stapelt. Für Kubernetes sind es zehn ganz normale nvidia.com/gpu-Zuteilungen. Wer welche GPU teilt, sieht nur, wer die UUIDs vergleicht.

Fortschritt speichern? Kostenlos registrieren

Drill 4: Die Labels lügen

kubectl get node gpu-worker2 -o jsonpath='{.metadata.labels.nvidia\.com/gpu\.replicas} {.metadata.labels.nvidia\.com/gpu\.sharing-strategy} {.metadata.labels.nvidia\.com/gpu\.product}{"\n"}'

Erwartet:

1 none NVIDIA-A100-SXM4-40GB

Der Knoten teilt seine GPUs, aber die Labels von GFD sagen „nicht geteilt“. Ein Team, das per nodeSelector auf NVIDIA-A100-SXM4-40GB plant, bekommt ein Viertel einer A100 und weiß es nicht. GFD braucht dieselbe Konfiguration wie der Device Plugin:

cp ~/gpu/tag03/gfd-werte.yaml gfd-werte.yaml
cat >> gfd-werte.yaml <<EOF
config:
  map:
    default: |-
$(sed 's/^/      /' time-slicing.yaml)
EOF
helm upgrade gfd nvdp/nvidia-device-plugin --version 0.18.2 -n gpu-feature-discovery \
  -f gfd-werte.yaml --wait --timeout 180s | grep STATUS
sleep 75
kubectl get node gpu-worker2 -o jsonpath='{.metadata.labels.nvidia\.com/gpu\.replicas} {.metadata.labels.nvidia\.com/gpu\.sharing-strategy} {.metadata.labels.nvidia\.com/gpu\.product}{"\n"}'

Erwartet:

STATUS: deployed
4 time-slicing NVIDIA-A100-SXM4-40GB-SHARED

Jetzt ehrlich: vier Replicas, Time-Slicing, und das Produkt trägt -SHARED. Wer ausdrücklich eine ganze A100 will, plant auf das Produkt ohne Endung und findet hier keinen Platz mehr.

Fortschritt speichern? Kostenlos registrieren

Drill 5: Zwei geteilte GPUs sind keine zwei GPUs

Ein Pod fordert zwei GPUs an:

kubectl -n tag05 scale deploy/notebook --replicas=30
kubectl -n tag05 rollout status deploy/notebook --timeout=180s | tail -1
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: will-zwei
  namespace: tag05
spec:
  tolerations:
  - key: nvidia.com/gpu
    operator: Exists
    effect: NoSchedule
  containers:
  - name: c
    image: ubuntu:24.04
    command: [sleep, infinity]
    resources:
      limits:
        nvidia.com/gpu: 2
EOF
kubectl -n tag05 wait --for=condition=Ready pod/will-zwei --timeout=60s
kubectl -n tag05 exec will-zwei -- printenv NVIDIA_VISIBLE_DEVICES

Erwartet:

pod/will-zwei created
pod/will-zwei condition met
GPU-12345678-1234-1234-1234-123456780006,GPU-12345678-1234-1234-1234-123456780003

Im Labor hatte der Pod Glück: zwei verschiedene GPUs. Garantiert ist das nicht. Zwei Replicas können auf derselben physischen GPU liegen, dann glaubt das Programm, zwei GPUs zu haben, und teilt sich eine mit drei anderen. Das verhindert eine Einstellung:

kubectl -n tag05 delete pod will-zwei
kubectl -n tag05 scale deploy/notebook --replicas=10
cat > time-slicing.yaml <<EOF
version: v1
flags:
  migStrategy: none
sharing:
  timeSlicing:
    failRequestsGreaterThanOne: true
    resources:
    - name: nvidia.com/gpu
      replicas: 4
EOF
kubectl -n kube-system create configmap gpu-sharing --from-file=config.yaml=time-slicing.yaml \
  --dry-run=client -o yaml | kubectl replace -f -
kubectl -n kube-system rollout restart ds/nvidia-device-plugin-mock
kubectl -n kube-system rollout status ds/nvidia-device-plugin-mock --timeout=120s | tail -1
sleep 8

Erwartet (gekürzt):

configmap/gpu-sharing replaced
daemon set "nvidia-device-plugin-mock" successfully rolled out
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: will-zwei
  namespace: tag05
spec:
  tolerations:
  - key: nvidia.com/gpu
    operator: Exists
    effect: NoSchedule
  containers:
  - name: c
    image: ubuntu:24.04
    command: [sleep, infinity]
    resources:
      limits:
        nvidia.com/gpu: 2
EOF
sleep 8
kubectl -n tag05 get pod will-zwei
kubectl -n tag05 get pod will-zwei -o jsonpath='{.status.reason}: {.status.message}{"\n"}'

Erwartet:

NAME        READY   STATUS                     RESTARTS   AGE
will-zwei   0/1     UnexpectedAdmissionError   0          9s
UnexpectedAdmissionError: Pod was rejected: Allocate failed due to rpc error: code =
Unknown desc = invalid allocation request for "nvidia.com/gpu": invalid request: maximum
request size for shared resources is 1; found 2, which is unexpected

Der Scheduler hat den Pod eingeplant, der Kubelet weist ihn beim Zuteilen ab. Der Pod bleibt in diesem Zustand stehen, er startet nicht neu. Ein Controller (Deployment, Job) würde einen neuen Pod anlegen, der ebenfalls scheitert. Die Meldung ist eindeutig, aber sie erscheint spät. Deshalb gehört die Regel zusätzlich in eine Admission-Richtlinie.

kubectl -n tag05 delete pod will-zwei

Fortschritt speichern? Kostenlos registrieren

Übung: Speed-Drills

  1. Zeige, wie viele GPU-Replicas der Knoten meldet und wie viele vergeben sind. Zeitziel: 1 min
  2. Zeige die Sharing-Konfiguration, die der Device Plugin geladen hat. Zeitziel: 1 min
  3. Zeige, welche Pods sich eine bestimmte GPU teilen. Zeitziel: 3 min

Lösung

kubectl describe node gpu-worker2 | grep -E '^  nvidia.com/gpu'
kubectl -n kube-system get configmap gpu-sharing -o jsonpath='{.data.config\.yaml}'
for p in $(kubectl -n tag05 get pods -l app=notebook -o name); do
  echo "$(kubectl -n tag05 exec $p -- printenv NVIDIA_VISIBLE_DEVICES) $p"
done | sort

Fortschritt speichern? Kostenlos registrieren

Kontrollpunkt

kubectl get node gpu-worker2 -o jsonpath='{.status.allocatable.nvidia\.com/gpu}{"\n"}'

Erwartet: 32

kubectl get node gpu-worker2 -o jsonpath='{.metadata.labels.nvidia\.com/gpu\.sharing-strategy}{"\n"}'

Erwartet: time-slicing

Challenge

Teams sollen bewusst entscheiden, ob sie eine geteilte GPU wollen. Gib den geteilten GPUs einen eigenen Ressourcennamen. Danach schaltest du das Teilen ganz ab, damit Tag 6 mit acht ganzen GPUs beginnt.

Hinweis: Die Option heißt renameByDefault. Und beim Abschalten: Nimm GFD die Konfiguration nicht einfach weg.

Lösung

kubectl -n tag05 delete deploy notebook
cat > time-slicing.yaml <<EOF
version: v1
flags:
  migStrategy: none
sharing:
  timeSlicing:
    renameByDefault: true
    failRequestsGreaterThanOne: true
    resources:
    - name: nvidia.com/gpu
      replicas: 4
EOF
kubectl -n kube-system create configmap gpu-sharing --from-file=config.yaml=time-slicing.yaml \
  --dry-run=client -o yaml | kubectl replace -f -
kubectl -n kube-system rollout restart ds/nvidia-device-plugin-mock
kubectl -n kube-system rollout status ds/nvidia-device-plugin-mock --timeout=120s | tail -1
sleep 15
kubectl get node gpu-worker2 -o json | jq -c '{capacity: (.status.capacity
  | with_entries(select(.key|startswith("nvidia")))), allocatable: (.status.allocatable
  | with_entries(select(.key|startswith("nvidia"))))}'

Erwartet (gekürzt):

{"capacity":{"nvidia.com/gpu":"32","nvidia.com/gpu.shared":"32"},"allocatable":{"nvidia.com/gpu":"0","nvidia.com/gpu.shared":"32"}}

32 Mal nvidia.com/gpu.shared. Der alte Name bleibt in der Kapazität als Rest stehen, mit allocatable 0. Wer jetzt nvidia.com/gpu anfordert, wartet. Wer teilen will, sagt es:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: geteilt
  namespace: tag05
spec:
  tolerations:
  - key: nvidia.com/gpu
    operator: Exists
    effect: NoSchedule
  containers:
  - name: c
    image: ubuntu:24.04
    command: [sleep, infinity]
    resources:
      limits:
        nvidia.com/gpu.shared: 1
EOF
kubectl -n tag05 wait --for=condition=Ready pod/geteilt --timeout=60s
kubectl -n tag05 exec geteilt -- nvidia-smi -L

Erwartet:

pod/geteilt created
pod/geteilt condition met
GPU 0: NVIDIA A100-SXM4-40GB (UUID: GPU-12345678-1234-1234-1234-123456780007)

Abschalten. Der Device Plugin bekommt eine Konfiguration ohne Sharing:

kubectl delete namespace tag05
printf 'version: v1\nflags:\n  migStrategy: none\n' > ohne-sharing.yaml
kubectl -n kube-system create configmap gpu-sharing --from-file=config.yaml=ohne-sharing.yaml \
  --dry-run=client -o yaml | kubectl replace -f -
kubectl -n kube-system rollout restart ds/nvidia-device-plugin-mock
kubectl -n kube-system rollout status ds/nvidia-device-plugin-mock --timeout=120s | tail -1

Für GFD liegt der naheliegende Weg nahe: die alten Werte von Tag 3 ohne config einspielen. Im Labor ging das schief:

helm upgrade gfd nvdp/nvidia-device-plugin --version 0.18.2 -n gpu-feature-discovery \
  -f ~/gpu/tag03/gfd-werte.yaml --wait --timeout 180s
helm -n gpu-feature-discovery list
kubectl -n gpu-feature-discovery get events --sort-by=.lastTimestamp | tail -1 | cut -c1-120

Erwartet (gekürzt):

NAME    NAMESPACE               REVISION    UPDATED STATUS  CHART                       APP VERSION
gfd     gpu-feature-discovery   3           ...     failed  nvidia-device-plugin-0.18.2 0.18.2
36s         Warning   FailedCreate       daemonset/gfd-nvidia-device-plugin-gpu-feature-discovery   Error creating: pods "gfd-nvidia-device-plugin-gpu-feature-discovery-" is forbidden: error looking up service account gpu-feature-discovery/gfd-nvidia-device-plugin-service-account: serviceaccount "gfd-nvidia-device-plugin-service-account" not found

Mit config legt das Chart einen ServiceAccount für einen Konfigurations-Sidecar an. Ohne config löscht es ihn wieder, aber das DaemonSet verweist noch darauf, und GFD startet nicht mehr. Die Labels veralten still. Der saubere Weg: GFD behält eine Konfiguration, nur eben eine ohne Sharing.

cp ~/gpu/tag03/gfd-werte.yaml gfd-ohne-sharing.yaml
printf 'config:\n  map:\n    default: |-\n%s\n' "$(sed 's/^/      /' ohne-sharing.yaml)" \
  >> gfd-ohne-sharing.yaml
helm upgrade gfd nvdp/nvidia-device-plugin --version 0.18.2 -n gpu-feature-discovery \
  -f gfd-ohne-sharing.yaml --wait --timeout 180s | grep STATUS
sleep 70
kubectl get node gpu-worker2 -o jsonpath='{.status.allocatable.nvidia\.com/gpu} {.metadata.labels.nvidia\.com/gpu\.replicas} {.metadata.labels.nvidia\.com/gpu\.sharing-strategy} {.metadata.labels.nvidia\.com/gpu\.product}{"\n"}'

Erwartet:

STATUS: deployed
8 1 none NVIDIA-A100-SXM4-40GB

Fortschritt speichern? Kostenlos registrieren

Aufräumen

Erledigt in der Challenge: acht ganze GPUs, kein Sharing, GFD mit Konfiguration ohne Sharing. Die ConfigMap gpu-sharing bleibt, Tag 6 braucht sie für MIG.

Interview-Notiz

  • Was ist Time-Slicing auf GPUs? Der Device Plugin meldet jede GPU mehrfach, mehrere Pods bekommen dieselbe GPU und teilen sich die Rechenzeit. Keine Trennung von Speicher und Fehlern, keine Leistungsgarantie.
  • Wann Time-Slicing, wann MIG? Time-Slicing für Entwicklung, Notebooks und kleine Tests, auf jeder GPU. MIG, wenn Teams sich nicht stören dürfen, und nur auf GPUs, die es können (A100, H100 und neuer).
  • Warum failRequestsGreaterThanOne? Weil zwei geteilte Replicas auf derselben GPU liegen können. Ohne die Option glaubt ein Programm, zwei GPUs zu haben.

Enterprise-Notiz

  • Geteilte GPUs bekommen einen eigenen Namen (renameByDefault) oder einen eigenen Knotenpool. Dann ist in jeder Abrechnung und jedem Quota klar, wer eine ganze und wer eine geteilte GPU hat.
  • Der GPU Operator verteilt die Sharing-Konfiguration an Device Plugin und GFD gemeinsam, per Label sogar pro Knoten unterschiedlich. Wer beide selbst betreibt, muss sie synchron halten, sonst lügen die Labels wie in Drill 4.
  • NVIDIA MPS ist eine dritte Variante mit begrenzter Speicheraufteilung. Sie braucht einen MPS-Daemon auf dem Knoten und ist mit Mokka nicht nachzustellen.

Inhalt

Mehr