# services/ai-llm/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ollama
  namespace: ai
spec:
  replicas: 1
  revisionHistoryLimit: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
  selector:
    matchLabels:
      app: ollama
  template:
    metadata:
      labels:
        app: ollama
      annotations:
        ai.bstein.dev/model: qwen2.5-coder:1.5b-instruct-q4_0,qwen2.5:14b-instruct-q4_0
        ai.bstein.dev/gpu: GPU pool (titan-20/21)
        ai.bstein.dev/restartedAt: "2026-01-26T12:00:00Z"
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: In
                    values:
                      - titan-20
                      - titan-21
      runtimeClassName: nvidia
      volumes:
        - name: models
          # Accelerator nodes are not Longhorn attach targets; keep the model cache pod-local.
          emptyDir: {}
      initContainers:
        - name: warm-model
          image: ollama/ollama@sha256:2c9595c555fd70a28363489ac03bd5bf9e7c5bdf2890373c3a830ffd7252ce6d
          env:
            - name: OLLAMA_HOST
              value: 0.0.0.0
            - name: NVIDIA_VISIBLE_DEVICES
              value: all
            - name: NVIDIA_DRIVER_CAPABILITIES
              value: compute,utility
            - name: OLLAMA_MODELS
              value: /root/.ollama
            - name: OLLAMA_MODEL
              value: qwen2.5:14b-instruct-q4_0
            - name: OLLAMA_FAST_MODEL
              value: qwen2.5-coder:1.5b-instruct-q4_0
          command:
            - /bin/sh
            - -c
            - |
              set -e
              ollama serve >/tmp/ollama.log 2>&1 &
              sleep 6
              for model in "${OLLAMA_FAST_MODEL}" "${OLLAMA_MODEL}"; do
                ollama pull "${model}"
              done
              pkill ollama || true
          volumeMounts:
            - name: models
              mountPath: /root/.ollama
          resources:
            requests:
              cpu: 500m
              memory: 2Gi
              nvidia.com/gpu.shared: 1
            limits:
              cpu: "2"
              memory: 4Gi
              nvidia.com/gpu.shared: 1
      containers:
        - name: ollama
          image: ollama/ollama@sha256:2c9595c555fd70a28363489ac03bd5bf9e7c5bdf2890373c3a830ffd7252ce6d
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 11434
          env:
            - name: OLLAMA_HOST
              value: 0.0.0.0
            - name: OLLAMA_FAST_MODEL
              value: qwen2.5-coder:1.5b-instruct-q4_0
            - name: OLLAMA_KEEP_ALIVE
              value: 6h
            - name: OLLAMA_MODELS
              value: /root/.ollama
            - name: NVIDIA_VISIBLE_DEVICES
              value: all
            - name: NVIDIA_DRIVER_CAPABILITIES
              value: compute,utility
          command:
            - /bin/sh
            - -c
            - |
              set -e
              ollama serve &
              pid="$!"
              trap 'kill -TERM "$pid"; wait "$pid"' TERM INT
              sleep 6
              timeout 180s ollama run "${OLLAMA_FAST_MODEL}" "reply with just pong" >/tmp/ollama-fast-warm.log 2>&1
              touch /tmp/ollama-fast-ready
              wait "$pid"
          volumeMounts:
            - name: models
              mountPath: /root/.ollama
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - test -f /tmp/ollama-fast-ready && ollama list >/dev/null 2>&1
            initialDelaySeconds: 15
            periodSeconds: 10
            timeoutSeconds: 5
          resources:
            requests:
              cpu: "4"
              memory: 10Gi
              nvidia.com/gpu.shared: 1
            limits:
              cpu: "8"
              memory: 14Gi
              nvidia.com/gpu.shared: 1
