Skip to content

Commit

Permalink
Add example for custom compute classes reservations. (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrqq authored Nov 26, 2024
1 parent 5be2e51 commit 69724d8
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 0 deletions.
6 changes: 6 additions & 0 deletions autoscaling/custom-compute-classes/reservations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Custom compute classes reservations example

[![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_workspace=autoscaling/custom-compute-classes/reservations)

This example shows how to utilize custom compute classes in order to consume reservation capacity for the workloads.
Learn more about custom compute classes - https://cloud.google.com/kubernetes-engine/docs/concepts/about-custom-compute-classes
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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.

# [START gke_autoscaling_ccc_any_reservation]
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: ccc-reservations
spec:
nodePoolAutoCreation:
enabled: true
priorities:
- machineType: ${MACHINE_TYPE}
reservations:
affinity: AnyBestEffort
whenUnsatisfiable: DoNotScaleUp
# [END gke_autoscaling_ccc_any_reservation]
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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.

# [START gke_autoscaling_ccc_gpu_reservation]
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: ccc-reservations
spec:
nodePoolAutoCreation:
enabled: true
priorities:
- machineType: ${MACHINE_TYPE}
gpu:
type: ${ACCELERATOR_TYPE}
count: ${ACCELERATOR_COUNT}
reservations:
specific:
- name: ${RESERVATION_NAME}
affinity: Specific
whenUnsatisfiable: DoNotScaleUp
# [END gke_autoscaling_ccc_gpu_reservation]
---

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
#
# 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.

# [START gke_autoscaling_ccc_local_shared_spot]
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: ccc-reservations
spec:
nodePoolAutoCreation:
enabled: true
priorities:
- machineType: ${MACHINE_TYPE}
reservations:
specific:
- name: ${RESERVATION_NAME}
affinity: Specific
- machineType: ${MACHINE_TYPE}
reservations:
specific:
- name: ${SHARED_RESERVATION_NAME
project: ${SHARED_RESERVATION_PROJECT}
affinity: Specific
- machineType: ${MACHINE_TYPE}
spot: true
whenUnsatisfiable: DoNotScaleUp
# [END gke_autoscaling_ccc_local_shared_spot]
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.

# [START gke_autoscaling_ccc_specific_reservation]
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: ccc-reservations
spec:
nodePoolAutoCreation:
enabled: true
priorities:
- machineType: ${MACHINE_TYPE}
reservations:
specific:
- name: ${RESERVATION_NAME}
- name: ${SHARED_RESERVATION_NAME
project: ${SHARED_RESERVATION_PROJECT
affinity: Specific
whenUnsatisfiable: DoNotScaleUp
# [END gke_autoscaling_ccc_specific_reservation]
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.

# [START gke_autoscaling_ccc_ssd_reservation]
apiVersion: cloud.google.com/v1
kind: ComputeClass
metadata:
name: ccc-reservations
spec:
nodePoolAutoCreation:
enabled: true
priorities:
- machineType: ${MACHINE_TYPE}
storage:
localSSDCount: ${LOCAL_SSD_COUNT}
reservations:
specific:
- name: ${RESERVATION_NAME}
affinity: Specific
whenUnsatisfiable: DoNotScaleUp
# [END gke_autoscaling_ccc_ssd_reservation]
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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.

# [START gke_autoscaling_ccc_reservations_deployment_gpu]
apiVersion: apps/v1
kind: Deployment
metadata:
name: ccc-reservations
spec:
replicas: 1
selector:
matchLabels:
app: ccc-reservations
template:
metadata:
labels:
app: ccc-reservations
spec:
nodeSelector:
cloud.google.com/compute-class: ccc-reservations
containers:
- name: ccc-reservations
image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
resources:
requests:
cpu: 500m
memory: 500Mi
nvidia.com/gpu: ${ACCELERATOR_QUANTITY}
limits:
cpu: 500m
memory: 500Mi
nvidia.com/gpu: ${ACCELERATOR_QUANTITY}
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- ccc-reservations
topologyKey: "kubernetes.io/hostname"
# [END gke_autoscaling_ccc_reservations_deployment_gpu]
---

54 changes: 54 additions & 0 deletions autoscaling/custom-compute-classes/reservations/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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.

# [START gke_autoscaling_ccc_reservations_deployment]
apiVersion: apps/v1
kind: Deployment
metadata:
name: ccc-reservations
spec:
replicas: 1
selector:
matchLabels:
app: ccc-reservations
template:
metadata:
labels:
app: ccc-reservations
spec:
nodeSelector:
cloud.google.com/compute-class: ccc-reservations
containers:
- name: ccc-reservations
image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
resources:
requests:
cpu: 500m
memory: 500Mi
limits:
cpu: 500m
memory: 500Mi
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- ccc-reservations
topologyKey: "kubernetes.io/hostname"
# [END gke_autoscaling_ccc_reservations_deployment]
---

24 changes: 24 additions & 0 deletions autoscaling/custom-compute-classes/reservations/reset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#
# 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.

set -euo pipefail

DEPLOYMENT_MANIFEST="${2:-deployment.yaml}"

echo "Removing deployment at ${DEPLOYMENT_MANIFEST}"
kubectl delete -f "${DEPLOYMENT_MANIFEST}" --ignore-not-found=true
echo "Waiting for nodes to get downscaled..."
kubectl wait --for=delete node -l cloud.google.com/compute-class=ccc-reservations --timeout=1h
35 changes: 35 additions & 0 deletions autoscaling/custom-compute-classes/reservations/schedule.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
#
# 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.

set -euo pipefail

COMPUTECLASS_MANIFEST="${1}"
DEPLOYMENT_MANIFEST="${2}"
SCALE_UP_TIMEOUT="${3:-300s}"
SCALE_UP_BATCH="${4:-1}"

echo "Going to use deployment at ${DEPLOYMENT_MANIFEST}, compute class at ${COMPUTECLASS_MANIFEST} with batch scale up size of ${SCALE_UP_BATCH} and timeout: ${SCALE_UP_TIMEOUT}"

kubectl apply -f "${COMPUTECLASS_MANIFEST}"
kubectl apply -f "${DEPLOYMENT_MANIFEST}"

while :; do
kubectl rollout status -f "${DEPLOYMENT_MANIFEST}" --timeout "${SCALE_UP_TIMEOUT}"
replicas=$(kubectl get -f "${DEPLOYMENT_MANIFEST}" -o "jsonpath={.status.replicas}")
newReplicas=$(expr "${replicas}" + "${SCALE_UP_BATCH}")
echo "Scaling up deployment from ${replicas} to ${newReplicas}"
kubectl scale -f "${DEPLOYMENT_MANIFEST}" --current-replicas="${replicas}" --replicas="${newReplicas}"
done

0 comments on commit 69724d8

Please sign in to comment.