Skip to content

Commit 51041db

Browse files
committed
Add hook examples
Signed-off-by: Luke Roy <luke.roy@ibm.com>
1 parent 32b4f42 commit 51041db

3 files changed

Lines changed: 135 additions & 0 deletions

File tree

serverless-fleets/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,43 @@ An IBM Cloud Logs instance is being setup and enabled by default during the auto
558558
559559
![](./images/prototype_logs.png)
560560
561+
### How to customize fleet workers
562+
563+
> **Note:** This is an experimental feature to unlock specific use cases and might change or will be deprecated.
564+
565+
Fleet workers can be customized using startup hooks to prepare the environment before tasks are executed. This allows you to install additional software, pull container images, or configure services that your tasks will use.
566+
567+
#### Example 1: Running Ollama on Fleet Workers
568+
569+
See `run_hook_ollama` for a complete example that demonstrates:
570+
- Running Ollama (local LLM runtime) on fleet workers
571+
- Automatic GPU detection and configuration
572+
- Preloading AI models during worker startup
573+
- Using `__CE_INTERNAL_HOOK_AFTER_STARTUP` to execute setup scripts
574+
575+
Key environment variables used:
576+
- `__CE_INTERNAL_HOOK_AFTER_STARTUP`: Script to run after worker startup
577+
- `__CE_INTERNAL_HOOK_AFTER_STARTUP_RETRY_LIMIT=3`: Retry attempts if hook fails
578+
- `__CE_INTERNAL_HOOK_AFTER_STARTUP_MAX_EXECUTION_TIME=30m`: Maximum hook execution time
579+
580+
#### Example 2: Running Podman-in-Podman
581+
582+
See `run_hook_podman_in_podman` for a complete example that demonstrates:
583+
- Running Podman inside fleet workers for nested containerization
584+
- Preloading container images during startup
585+
- Using privileged containers and host path mounts
586+
587+
Additional environment variables used:
588+
- `__CE_INTERNAL_PRIVILEGED_CONTAINER=true`: Enable privileged mode (required for nested containers)
589+
- `__CE_INTERNAL_HOSTPATH_MOUNTS=/var/lib/containers:/var/lib/containers`: Mount host paths
590+
591+
**Available Hook Environment Variables:**
592+
- `__CE_INTERNAL_HOOK_AFTER_STARTUP`: The script to execute after worker startup
593+
- `__CE_INTERNAL_HOOK_AFTER_STARTUP_RETRY_LIMIT`: Number of retry attempts if the hook fails
594+
- `__CE_INTERNAL_HOOK_AFTER_STARTUP_MAX_EXECUTION_TIME`: Maximum time allowed for hook execution
595+
- `__CE_INTERNAL_PRIVILEGED_CONTAINER`: Enable privileged mode
596+
- `__CE_INTERNAL_HOSTPATH_MOUNTS`: Mount host paths into the container
597+
561598
### Cleanup the Environment
562599
563600
To clean up all IBM Cloud resources, that have been created as part of the provided script, run:

serverless-fleets/run_hook_ollama

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
uuid=$(uuidgen | tr '[:upper:]' '[:lower:]' | awk -F- '{print $1}')
6+
7+
PREHOOK=$(cat <<'EOF'
8+
#!/bin/bash
9+
10+
if nvidia-smi >/dev/null 2>&1; then
11+
echo "NVIDIA GPU detected"
12+
podman run -d --device nvidia.com/gpu=all -v ollama:/root/.ollama -p 11434:11434 --name ollama docker.io/ollama/ollama
13+
else
14+
echo "No NVIDIA GPU detected"
15+
podman run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama docker.io/ollama/ollama:latest
16+
fi
17+
18+
# pull the model into ollama
19+
podman exec -it ollama ollama pull granite4:350m
20+
EOF
21+
)
22+
23+
echo code-engine fleet create --name "fleet-${uuid}-1"
24+
echo " "--tasks-state-store fleet-task-store
25+
echo " "--subnetpool-name fleet-subnetpool
26+
echo " "--image registry.access.redhat.com/ubi10/ubi-minimal
27+
echo " "--max-scale 1
28+
echo " "--command="curl"
29+
echo " "--arg "http://host.containers.internal:8080"
30+
echo " "--tasks 1
31+
echo " "--env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}"
32+
echo " "__CE_INTERNAL_HOOK_AFTER_STARTUP_RETRY_LIMIT=3
33+
echo " "__CE_INTERNAL_HOOK_AFTER_STARTUP_MAX_EXECUTION_TIME=10m
34+
echo " "--cpu 2
35+
echo " "--memory 4G
36+
37+
ibmcloud code-engine fleet create --name "fleet-${uuid}-1" \
38+
--tasks-state-store fleet-task-store \
39+
--subnetpool-name fleet-subnetpool \
40+
--image registry.access.redhat.com/ubi10/ubi-minimal \
41+
--max-scale 1 \
42+
--command="curl" \
43+
--arg "http://host.containers.internal:11434/api/tags" \
44+
--tasks 1 \
45+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}" \
46+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP_RETRY_LIMIT=3 \
47+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP_MAX_EXECUTION_TIME=30m \
48+
--cpu 2 \
49+
--memory 4G
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
ibmcloud login --sso -a cloud.ibm.com -r eu-de -c 1260a3541bb44fefb9c58d5cc92aaddb -g luke-new-ce-fleet-sandbox--rg
6+
ibmcloud ce project select -n luke-new-ce-fleet-sandbox--ce-project
7+
8+
uuid=$(uuidgen | tr '[:upper:]' '[:lower:]' | awk -F- '{print $1}')
9+
10+
PREHOOK=$(cat <<'EOF'
11+
#!/bin/bash
12+
podman pull docker.io/library/hello-world:latest
13+
EOF
14+
)
15+
16+
17+
echo ibmcloud code-engine fleet create --name "fleet-${uuid}-1"
18+
echo " "--tasks-state-store fleet-task-store
19+
echo " "--subnetpool-name fleet-subnetpool
20+
echo " "--image quay.io/podman/stable:latest
21+
echo " "--max-scale 1
22+
echo " "--command="podman"
23+
echo " "--arg "run"
24+
echo " "--arg "hello-world"
25+
echo " "--tasks 1
26+
echo " "--env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}"
27+
echo " "__CE_INTERNAL_HOOK_AFTER_STARTUP_RETRY_LIMIT=3
28+
echo " "__CE_INTERNAL_HOOK_AFTER_STARTUP_MAX_EXECUTION_TIME=10m
29+
echo " "--env __CE_INTERNAL_PRIVILEGED_CONTAINER=true
30+
echo " "--env __CE_INTERNAL_HOSTPATH_MOUNTS=/var/lib/containers:/var/lib/containers
31+
echo " "--cpu 2
32+
echo " "--memory 4G
33+
34+
ibmcloud code-engine fleet create --name "fleet-${uuid}-1" \
35+
--tasks-state-store fleet-task-store \
36+
--subnetpool-name fleet-subnetpool \
37+
--image quay.io/podman/stable:latest \
38+
--max-scale 1 \
39+
--command="podman" \
40+
--arg "run" \
41+
--arg "hello-world" \
42+
--tasks 1 \
43+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}" \
44+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP_RETRY_LIMIT=3 \
45+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP_MAX_EXECUTION_TIME=10m \
46+
--env __CE_INTERNAL_PRIVILEGED_CONTAINER=true \
47+
--env __CE_INTERNAL_HOSTPATH_MOUNTS=/var/lib/containers:/var/lib/containers \
48+
--cpu 2 \
49+
--memory 4G

0 commit comments

Comments
 (0)