Skip to content

Commit 493fd80

Browse files
committed
merge pod template content, update page filenames
Signed-off-by: Niels Bantilan <[email protected]>
1 parent 070591c commit 493fd80

File tree

6 files changed

+231
-334
lines changed

6 files changed

+231
-334
lines changed

content/user-guide/task-configuration/pod-templates.md

Lines changed: 227 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,251 @@ variants: +flyte +serverless +byoc +selfmanaged
66

77
# Pod templates
88

9-
The `pod_template` parameter in `TaskEnvironment` (and in the @env.task decorator, if you are overriding) allows you to customize the Kubernetes pod specification that will be used to run your tasks.
10-
This provides fine-grained control over the underlying Kubernetes resources, enabling you to configure advanced pod settings like image pull secrets, environment variables, labels, annotations, and other pod-level configurations.
9+
Flyte is built on Kubernetes and leverages its powerful container orchestration capabilities. A Kubernetes [pod](https://kubernetes.io/docs/concepts/workloads/pods/) is a group of one or more containers that share storage and network resources. While Flyte automatically runs your task code in a container, pod templates let you customize the entire pod specification for advanced use cases.
1110

12-
## Overview
11+
The `pod_template` parameter in `TaskEnvironment` allows you to:
1312

14-
Pod templates in Flyte allow you to:
13+
- **Add sidecar containers**: Run metrics exporters, service proxies, or specialized services alongside your task
14+
- **Mount volumes**: Attach persistent storage or cloud storage like GCS or S3
15+
- **Configure metadata**: Set custom labels and annotations for monitoring, routing, or cluster policies
16+
- **Manage resources**: Configure resource requests, limits, and affinities
17+
- **Inject configuration**: Add secrets, environment variables, or config maps
18+
- **Access private registries**: Specify image pull secrets
1519

16-
- **Configure pod metadata**: Set custom labels and annotations for your pods.
17-
- **Specify image pull secrets**: Access private container registries.
18-
- **Set environment variables**: Configure container-level environment variables.
19-
- **Customize pod specifications**: Define advanced Kubernetes pod settings.
20-
- **Control container configurations**: Specify primary container settings.
20+
## How it works
2121

22-
The `pod_template` parameter accepts either a string reference or a `PodTemplate` object that defines the complete pod specification.
22+
When you define a pod template:
23+
24+
1. **Primary container**: Flyte automatically injects your task code into the container specified by `primary_container_name` (default: `"primary"`)
25+
2. **Automatic monitoring**: Flyte watches the primary container and exits the entire pod when it completes
26+
3. **Image handling**: The image for your task environment is built automatically by Flyte; images for sidecar containers must be provided by you
27+
4. **Local execution**: When running locally, only the task code executes—additional containers are not started
28+
29+
## Requirements
30+
31+
To use pod templates, install the Kubernetes Python client:
32+
33+
```bash
34+
pip install kubernetes
35+
```
36+
37+
Or add it to your image dependencies:
38+
39+
```python
40+
image = flyte.Image.from_debian_base().with_pip_packages("kubernetes")
41+
```
2342

2443
## Basic usage
2544

26-
Here's a complete example showing how to use pod templates with a `TaskEnvironment`:
45+
Here's a complete example showing how to configure labels, annotations, environment variables, and image pull secrets:
2746

2847
{{< code file="/external/unionai-examples/v2/user-guide/task-configuration/pod-templates/pod_template.py" fragment="pod-template" lang="python" >}}
2948

30-
## PodTemplate components
49+
## PodTemplate parameters
50+
51+
The `PodTemplate` class provides the following parameters:
52+
53+
| Parameter | Type | Description |
54+
|-----------|------|-------------|
55+
| `primary_container_name` | `str` | Name of the container where task code runs (default: `"primary"`). Must match a container in `pod_spec`. |
56+
| `pod_spec` | `V1PodSpec` | Kubernetes pod specification for configuring containers, volumes, security contexts, and more. |
57+
| `labels` | `dict[str, str]` | Pod labels for organization and selection by Kubernetes selectors. |
58+
| `annotations` | `dict[str, str]` | Pod annotations for metadata and integrations (doesn't affect scheduling). |
59+
60+
## Volume mounts
61+
62+
Pod templates are commonly used to mount volumes for persistent storage or cloud storage access:
63+
64+
```python
65+
from kubernetes.client import (
66+
V1Container,
67+
V1PodSpec,
68+
V1Volume,
69+
V1VolumeMount,
70+
V1CSIVolumeSource,
71+
)
72+
import flyte
73+
74+
pod_template = flyte.PodTemplate(
75+
primary_container_name="primary",
76+
pod_spec=V1PodSpec(
77+
containers=[
78+
V1Container(
79+
name="primary",
80+
volume_mounts=[
81+
V1VolumeMount(
82+
name="data-volume",
83+
mount_path="/mnt/data",
84+
read_only=False,
85+
)
86+
],
87+
)
88+
],
89+
volumes=[
90+
V1Volume(
91+
name="data-volume",
92+
csi=V1CSIVolumeSource(
93+
driver="your-csi-driver",
94+
volume_attributes={"key": "value"},
95+
),
96+
)
97+
],
98+
),
99+
)
100+
101+
env = flyte.TaskEnvironment(
102+
name="volume-example",
103+
pod_template=pod_template,
104+
image=flyte.Image.from_debian_base(),
105+
)
106+
107+
@env.task
108+
async def process_data() -> str:
109+
# Access mounted volume
110+
with open("/mnt/data/input.txt", "r") as f:
111+
data = f.read()
112+
return f"Processed {len(data)} bytes"
113+
```
114+
115+
### GCS/S3 volume mounts
116+
117+
Mount cloud storage directly into your pod for efficient data access:
118+
119+
```python
120+
from kubernetes.client import V1Container, V1PodSpec, V1Volume, V1VolumeMount, V1CSIVolumeSource
121+
import flyte
122+
123+
# GCS example with CSI driver
124+
pod_template = flyte.PodTemplate(
125+
primary_container_name="primary",
126+
annotations={
127+
"gke-gcsfuse/volumes": "true",
128+
"gke-gcsfuse/cpu-limit": "2",
129+
"gke-gcsfuse/memory-limit": "1Gi",
130+
},
131+
pod_spec=V1PodSpec(
132+
containers=[
133+
V1Container(
134+
name="primary",
135+
volume_mounts=[V1VolumeMount(name="gcs", mount_path="/mnt/gcs")],
136+
)
137+
],
138+
volumes=[
139+
V1Volume(
140+
name="gcs",
141+
csi=V1CSIVolumeSource(
142+
driver="gcsfuse.csi.storage.gke.io",
143+
volume_attributes={"bucketName": "my-bucket"},
144+
),
145+
)
146+
],
147+
),
148+
)
149+
```
150+
151+
## Sidecar containers
152+
153+
Add sidecar containers to run alongside your task. Common use cases include:
154+
155+
- **Metrics exporters**: Prometheus, Datadog agents
156+
- **Service proxies**: Istio, Linkerd sidecars
157+
- **Data services**: Databases, caches, or specialized services like Nvidia NIMs
158+
159+
```python
160+
from kubernetes.client import V1Container, V1PodSpec
161+
import flyte
162+
163+
pod_template = flyte.PodTemplate(
164+
primary_container_name="primary",
165+
pod_spec=V1PodSpec(
166+
containers=[
167+
# Primary container (where your task code runs)
168+
V1Container(name="primary"),
169+
170+
# Sidecar container
171+
V1Container(
172+
name="metrics-sidecar",
173+
image="prom/pushgateway:latest",
174+
ports=[{"containerPort": 9091}],
175+
),
176+
],
177+
),
178+
)
179+
180+
env = flyte.TaskEnvironment(
181+
name="sidecar-example",
182+
pod_template=pod_template,
183+
image=flyte.Image.from_debian_base().with_pip_packages("requests"),
184+
)
185+
186+
@env.task
187+
async def task_with_metrics() -> str:
188+
import requests
189+
190+
# Send metrics to sidecar
191+
requests.post("http://localhost:9091/metrics", data="my_metric 42")
192+
193+
# Your task logic
194+
return "Task completed with metrics"
195+
```
196+
197+
## Image pull secrets
31198

32-
The `PodTemplate` class provides the following parameters for customizing your pod configuration:
199+
Configure private registry access:
33200

34201
```python
202+
from kubernetes.client import V1Container, V1PodSpec, V1LocalObjectReference
203+
import flyte
204+
35205
pod_template = flyte.PodTemplate(
36-
primary_container_name: str = "primary",
37-
pod_spec: Optional[V1PodSpec] = None,
38-
labels: Optional[Dict[str, str]] = None,
39-
annotations: Optional[Dict[str, str]] = None
206+
primary_container_name="primary",
207+
pod_spec=V1PodSpec(
208+
containers=[V1Container(name="primary")],
209+
image_pull_secrets=[V1LocalObjectReference(name="my-registry-secret")],
210+
),
40211
)
41212
```
42213

43-
### Parameters
214+
## Cluster-specific configuration
215+
216+
Pod templates are often used to configure Kubernetes-specific settings required by your cluster, even when not using multiple containers:
217+
218+
```python
219+
import flyte
220+
221+
pod_template = flyte.PodTemplate(
222+
primary_container_name="primary",
223+
annotations={
224+
"iam.amazonaws.com/role": "my-task-role", # AWS IAM role
225+
"cluster-autoscaler.kubernetes.io/safe-to-evict": "false",
226+
},
227+
labels={
228+
"cost-center": "ml-team",
229+
"project": "recommendations",
230+
},
231+
)
232+
```
233+
234+
## Important notes
235+
236+
1. **Local execution**: Pod templates only apply to remote execution. When running locally, only your task code executes.
237+
238+
2. **Image building**: Flyte automatically builds and manages the image for your task environment. Images for sidecar containers must be pre-built and available in a registry.
239+
240+
3. **Primary container**: Your task code is automatically injected into the container matching `primary_container_name`. This container must be defined in the `pod_spec.containers` list.
241+
242+
4. **Lifecycle management**: Flyte monitors the primary container and terminates the entire pod when it exits, ensuring sidecar containers don't run indefinitely.
44243

45-
- **`primary_container_name`** (`str`, default: `"primary"`): Specifies the name of the main container that will run your task code. This must match the container name defined in your pod specification.
244+
## Best practices
46245

47-
- **`pod_spec`** (`Optional[V1PodSpec]`): A standard Kubernetes `V1PodSpec` object that defines the complete pod specification. This allows you to configure any pod-level setting including containers, volumes, security contexts, node selection, and more.
246+
1. **Start simple**: Begin with basic labels and annotations before adding complex sidecars
247+
2. **Test locally first**: Verify your task logic works locally before adding pod customizations
248+
3. **Use environment-specific templates**: Different environments (dev, staging, prod) may need different pod configurations
249+
4. **Set resource limits**: Always set resource requests and limits for sidecars to prevent cluster issues
250+
5. **Security**: Use image pull secrets and least-privilege service accounts
48251

49-
- **`labels`** (`Optional[Dict[str, str]]`): Key-value pairs used for organizing and selecting pods. Labels are used by Kubernetes selectors and can be queried to filter and manage pods.
252+
## Learn more
50253

51-
- **`annotations`** (`Optional[Dict[str, str]]`): Additional metadata attached to the pod that doesn't affect pod scheduling or selection. Annotations are typically used for storing non-identifying information like deployment revisions, contact information, or configuration details.
254+
- [Kubernetes Pods Documentation](https://kubernetes.io/docs/concepts/workloads/pods/)
255+
- [Kubernetes Python Client](https://github.com/kubernetes-client/python)
256+
- [V1PodSpec Reference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#podspec-v1-core)

content/user-guide/task-configuration/task-plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Plugins
3-
weight: 3
3+
weight: 20
44
variants: +flyte +serverless +byoc +selfmanaged
55
---
66

content/user-guide/task-programming/files-and-directories.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Files and directories
3-
weight: 90
3+
weight: 60
44
variants: +flyte +serverless +byoc +selfmanaged
55
---
66

content/user-guide/task-programming/other_features.md renamed to content/user-guide/task-programming/other-features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Other features
3-
weight: 9
3+
weight: 200
44
variants: +flyte +serverless +byoc +selfmanaged
55
---
66

0 commit comments

Comments
 (0)