GKEService

A service hosted on a GKE Kubernetes Cluster.

Like all Services, this class configures itself across multiple Environments.

For more information see the official documentation.

Example Usage

Basic Usage

1
import launchflow as lf
2
3
cluster = lf.gcp.GKECluster("my-cluster")
4
service = lf.gcp.GKEService("my-service", cluster=cluster)

Custom Dockerfile Path

1
import launchflow as lf
2
3
cluster = lf.gcp.GKECluster("my-cluster")
4
service = lf.gcp.GKEService(
5
"my-service",
6
cluster=cluster,
7
dockerfile="docker/Dockerfile"m
8
build_directory="path/to/service_dir",
9
)

Use a Custom Node Pool

1
import launchflow as lf
2
3
cluster = lf.gcp.GKECluster("my-cluster")
4
node_pool = lf.gcp.NodePool("my-node-pool", cluster=cluster, machine_type="e2-micro")
5
6
service = lf.gcp.GKEService("my-service", cluster=cluster, node_pool=node_pool)

Deploy to GPU Enabled Node Pool

1
import launchflow as lf
2
3
cluster = lf.gcp.GKECluster("my-cluster", delete_protection=False)
4
node_pool = lf.gcp.NodePool(
5
name="node-pool",
6
cluster=cluster,
7
machine_type="n1-standard-2",
8
guest_accelerators=[lf.gcp.gke.GuestAccelerator(type="nvidia-tesla-t4", count=1)],
9
)

Service with a Startup Probe

1
import launchflow as lf
2
3
cluster = lf.gcp.GKECluster("my-cluster")
4
service = lf.gcp.GKEService(
5
"my-service",
6
cluster=cluster,
7
startup_probe=lf.kubernetes.StartupProbe(
8
path="/healthz",
9
initial_delay_seconds=5,
10
period_seconds=10,
11
),
12
)
13
service = lf.gcp.GKEService(
14
name="service",
15
cluster=cluster,
16
node_pool=node_pool,
17
tolerations=[lf.kubernetes.service.Toleration(key="nvidia.com/gpu", value="present", operator="Equal")],
18
)

Service with a Horizontal Pod Autoscaler

1
import launchflow as lf
2
3
cluster = lf.gcp.GKECluster("my-cluster")
4
service = lf.gcp.GKEService(
5
"my-service",
6
cluster=cluster,
7
container_resources=lf.kubernetes.service.ContainerResources(
8
requests=lf.kubernetes.service.ResourceQuantity(cpu="0.2"),
9
),
10
)
11
hpa = lf.kubernetes.HorizontalPodAutoscaler(
12
"hpa",
13
cluster=cluster,
14
target_name=service.name
15
)

Service with a Custom Domain Mapping

1
import launchflow as lf
2
3
cluster = lf.gcp.GKECluster("my-cluster")
4
service = lf.gcp.GKEService(
5
"my-service",
6
cluster=cluster,
7
domain="example.com"
8
service_type="NodePort",
9
)

initialization

Create a new GKE Service.

Args:

  • name (str): The name of the service.
  • cluster (GKECluster): The GKE cluster to deploy the service to.
  • build_directory (str): The directory to build the service from. This should be a relative path from the project root where your launchflow.yaml is defined.
  • build_ignore (List[str]): A list of files to ignore when building the service. This can be in the same syntax you would use for a .gitignore.
  • dockerfile (str): The Dockerfile to use for building the service. This should be a relative path from the build_directory.
  • node_pool (Optional[NodePool]): The node pool to deploy the service to.
  • container_port (int): The port the service will listen on inside the container.
  • host_port (Optional[int]): The port the service will listen on outside the container. If not provided, the service will not be exposed outside the cluster.
  • namespace (str): The Kubernetes namespace to deploy the service to. Defaults to default. The default namespace is connected to your LaunchFlow environment automatically, we recommend leaving this as default unless you need to deploy an isolated service.
  • service_type (Literal["ClusterIP", "NodePort", "LoadBalancer"]): The type of Kubernetes service to create. Defaults to LoadBalancer.
  • startup_probe (Optional[StartupProbe]): The startup probe for the service.
  • liveness_probe (Optional[LivenessProbe]): The liveness probe for the service.
  • readiness_probe (Optional[ReadinessProbe]): The readiness probe for the service.
  • environment_variables (Optional[Dict[str, str]]): A dictionary of environment variables to set on the service container.
  • container_resources (Optional[ContainerResources]): The container resources for the service. NOTE: If you are creating a HorizontalPodAutoscaler you will need to provide this.
  • tolerations (Optional[List[Toleration]]): A list of tolerations for the service.
  • domain (Optional[str]): The domain to use for the service. service_type must be NodePort to assign a custom domain.