Skip to content

Commit

Permalink
Add sample describing how to use KEDA with GKE. (#1532)
Browse files Browse the repository at this point in the history
* 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
jm-franc authored Nov 19, 2024
1 parent cb318f6 commit 5a49a33
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cost-optimization/gke-keda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Pub/Sub on Kubernetes Engine

[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](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.
4 changes: 4 additions & 0 deletions cost-optimization/gke-keda/cloud-pubsub/Dockerfile
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"]
32 changes: 32 additions & 0 deletions cost-optimization/gke-keda/cloud-pubsub/cloudbuild.yaml
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}'
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]
---
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]
---
63 changes: 63 additions & 0 deletions cost-optimization/gke-keda/cloud-pubsub/main.py
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()
2 changes: 2 additions & 0 deletions cost-optimization/gke-keda/cloud-pubsub/requirements.txt
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
28 changes: 28 additions & 0 deletions cost-optimization/gke-keda/ollama/helm-values-ollama.yaml
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
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]

0 comments on commit 5a49a33

Please sign in to comment.