Skip to content

Commit f4af7c7

Browse files
Merge pull request #359 from Luke-Roy-IBM/experimental-fleet-hooks
Add hook examples
2 parents 5bb3f94 + b828805 commit f4af7c7

3 files changed

Lines changed: 133 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. These hooks are configured through special environment variables that you set when creating the fleet. This customization capability allows you to install additional software, pull container images, or configure services that your tasks will use—all automatically before your workload begins processing.
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 the environment variable `__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:11434/api/tags"
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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
6+
uuid=$(uuidgen | tr '[:upper:]' '[:lower:]' | awk -F- '{print $1}')
7+
8+
PREHOOK=$(cat <<'EOF'
9+
#!/bin/bash
10+
podman pull docker.io/library/hello-world:latest
11+
EOF
12+
)
13+
14+
15+
echo ibmcloud code-engine fleet create --name "fleet-${uuid}-1"
16+
echo " "--tasks-state-store fleet-task-store
17+
echo " "--subnetpool-name fleet-subnetpool
18+
echo " "--image quay.io/podman/stable:latest
19+
echo " "--max-scale 1
20+
echo " "--command="podman"
21+
echo " "--arg "run"
22+
echo " "--arg "hello-world"
23+
echo " "--tasks 1
24+
echo " "--env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}"
25+
echo " "__CE_INTERNAL_HOOK_AFTER_STARTUP_RETRY_LIMIT=3
26+
echo " "__CE_INTERNAL_HOOK_AFTER_STARTUP_MAX_EXECUTION_TIME=10m
27+
echo " "--env __CE_INTERNAL_PRIVILEGED_CONTAINER=true
28+
echo " "--env __CE_INTERNAL_HOSTPATH_MOUNTS=/var/lib/containers:/var/lib/containers
29+
echo " "--cpu 2
30+
echo " "--memory 4G
31+
32+
ibmcloud code-engine fleet create --name "fleet-${uuid}-1" \
33+
--tasks-state-store fleet-task-store \
34+
--subnetpool-name fleet-subnetpool \
35+
--image quay.io/podman/stable:latest \
36+
--max-scale 1 \
37+
--command="podman" \
38+
--arg "run" \
39+
--arg "hello-world" \
40+
--tasks 1 \
41+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP="${PREHOOK}" \
42+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP_RETRY_LIMIT=3 \
43+
--env __CE_INTERNAL_HOOK_AFTER_STARTUP_MAX_EXECUTION_TIME=10m \
44+
--env __CE_INTERNAL_PRIVILEGED_CONTAINER=true \
45+
--env __CE_INTERNAL_HOSTPATH_MOUNTS=/var/lib/containers:/var/lib/containers \
46+
--cpu 2 \
47+
--memory 4G

0 commit comments

Comments
 (0)