-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample describing how to use KEDA with GKE. (#1532)
* Add sample describing how to use KEDA with GKE. * Move the README file to the top-level directory for this sample. * Update copyright dates. * Add missing license headers. * Fix tags and remove reference to deprecated GCR.
- Loading branch information
Showing
9 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Pub/Sub on Kubernetes Engine | ||
|
||
[](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleCloudPlatform/kubernetes-engine-samples&cloudshell_tutorial=README.md&cloudshell_workspace=cost-optimization/gke-keda/) | ||
|
||
This repository contains source code, Docker image build file and Kubernetes | ||
manifests for KEDA on Kubernetes Engine tutorial. Please follow the tutorial | ||
at https://cloud.google.com/kubernetes-engine/docs/tutorials/scale-to-zero-using-keda. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM python:3.13-slim | ||
COPY . . | ||
RUN pip install -r requirements.txt | ||
ENTRYPOINT ["python", "-u", "main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# This file and other cloudbuild.yaml files are used to ensure that | ||
# our public Docker images such as us-docker.pkg.dev/google-samples/containers/gke/pubsub-sample:v1 | ||
# are rebuilt and updated upon changes to the repository. | ||
|
||
steps: | ||
- name: 'gcr.io/cloud-builders/docker' | ||
args: | ||
- 'build' | ||
- '-t' | ||
- 'us-docker.pkg.dev/google-samples/containers/gke/keda-pubsub-sample:v1' | ||
- '-t' | ||
- 'us-docker.pkg.dev/google-samples/containers/gke/keda-pubsub-sample:sample-public-image-v1-${SHORT_SHA}' | ||
- '.' | ||
dir: 'cost-optimization/gke-keda/cloud-pubsub' | ||
|
||
images: | ||
- 'us-docker.pkg.dev/google-samples/containers/gke/keda-pubsub-sample:v1' | ||
- 'us-docker.pkg.dev/google-samples/containers/gke/keda-pubsub-sample:sample-public-image-v1-${SHORT_SHA}' |
43 changes: 43 additions & 0 deletions
43
cost-optimization/gke-keda/cloud-pubsub/deployment/keda-pubsub-scaledobject.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
apiVersion: keda.sh/v1alpha1 | ||
kind: TriggerAuthentication | ||
metadata: | ||
name: keda-auth | ||
namespace: keda-pubsub | ||
spec: | ||
podIdentity: | ||
provider: gcp | ||
--- | ||
# [START gke_deployment_keda_pubsub_scaledobject_pubsub] | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledObject | ||
metadata: | ||
name: keda-pubsub | ||
namespace: keda-pubsub | ||
spec: | ||
pollingInterval: 5 # We set those to low values to quickly trigger scale-to-zero. | ||
cooldownPeriod: 10 | ||
maxReplicaCount: 5 | ||
scaleTargetRef: | ||
name: keda-pubsub | ||
triggers: | ||
- type: gcp-pubsub | ||
authenticationRef: | ||
name: keda-auth | ||
metadata: | ||
subscriptionName: "projects/${PROJECT_ID}/subscriptions/keda-echo-read" | ||
# [END gke_deployment_keda_pubsub_scaledobject_pubsub] | ||
--- |
47 changes: 47 additions & 0 deletions
47
cost-optimization/gke-keda/cloud-pubsub/deployment/keda-pubsub-with-workload-identity.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: keda-pubsub | ||
--- | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
namespace: keda-pubsub | ||
name: keda-pubsub-sa | ||
--- | ||
# [START gke_deployment_keda_pubsub_with_workflow_identity_deployment_pubsub] | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: keda-pubsub | ||
namespace: keda-pubsub | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: keda-pubsub | ||
template: | ||
metadata: | ||
labels: | ||
app: keda-pubsub | ||
spec: | ||
serviceAccountName: keda-pubsub-sa | ||
containers: | ||
- name: subscriber | ||
image: us-docker.pkg.dev/google-samples/containers/gke/keda-pubsub-sample:v1 | ||
# [END gke_deployment_keda_pubsub_with_workflow_identity_deployment_pubsub] | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Pub/Sub pull example on Google Kubernetes Engine. | ||
This program pulls messages from a Cloud Pub/Sub topic and | ||
prints to standard output. | ||
""" | ||
|
||
import datetime | ||
import time | ||
|
||
# [START gke_keda_pubsub_pull] | ||
# [START container_keda_pubsub_pull] | ||
from google import auth | ||
from google.cloud import pubsub_v1 | ||
|
||
|
||
def main(): | ||
"""Continuously pull messages from subsciption""" | ||
|
||
# read default project ID | ||
_, project_id = auth.default() | ||
subscription_id = 'keda-echo-read' | ||
|
||
subscriber = pubsub_v1.SubscriberClient() | ||
subscription_path = subscriber.subscription_path( | ||
project_id, subscription_id) | ||
|
||
def callback(message: pubsub_v1.subscriber.message.Message) -> None: | ||
"""Process received message""" | ||
print(f"Received message: ID={message.message_id} Data={message.data}") | ||
print(f"[{datetime.datetime.now()}] Processing: {message.message_id}") | ||
time.sleep(3) | ||
print(f"[{datetime.datetime.now()}] Processed: {message.message_id}") | ||
message.ack() | ||
|
||
streaming_pull_future = subscriber.subscribe( | ||
subscription_path, callback=callback) | ||
print(f"Pulling messages from {subscription_path}...") | ||
|
||
with subscriber: | ||
try: | ||
streaming_pull_future.result() | ||
except Exception as e: | ||
print(e) | ||
# [END container_keda_pubsub_pull] | ||
# [END gke_keda_pubsub_pull] | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
google-cloud-pubsub~=2.23.0 | ||
google-auth~=2.34.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
ollama: | ||
gpu: | ||
enabled: true | ||
type: 'nvidia' | ||
number: 1 | ||
models: | ||
- gemma:7b | ||
- llama2:7b | ||
persistentVolume: | ||
enabled: true | ||
size: 100Gi | ||
storageClass: "premium-rwo" | ||
service: | ||
port: 11434 |
40 changes: 40 additions & 0 deletions
40
cost-optimization/gke-keda/ollama/keda-ollama-httpscaledobject.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# [START gke_deployment_keda_ollama_httpscaledobject_ollama] | ||
kind: HTTPScaledObject | ||
apiVersion: http.keda.sh/v1alpha1 | ||
metadata: | ||
namespace: ollama | ||
name: ollama | ||
spec: | ||
hosts: | ||
- ollama.ollama | ||
pathPrefixes: | ||
- / | ||
scaleTargetRef: | ||
name: ollama | ||
kind: Deployment | ||
apiVersion: apps/v1 | ||
service: ollama | ||
# Must match the value from ollama-values.yaml | ||
port: 11434 | ||
replicas: | ||
min: 0 | ||
max: 2 | ||
scaledownPeriod: 3600 | ||
scalingMetric: | ||
requestRate: | ||
targetValue: 20 | ||
# [END gke_deployment_keda_ollama_httpscaledobject_ollama] |