Skip to content

Commit 3cb5373

Browse files
authored
feat(GKE): add reservation samples (#665)
1 parent 67a2337 commit 3cb5373

File tree

3 files changed

+327
-0
lines changed

3 files changed

+327
-0
lines changed

gke/autopilot/reservation/main.tf

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
data "google_client_config" "default" {}
18+
19+
resource "google_container_cluster" "default" {
20+
name = "gke-autopilot-cluster"
21+
location = "us-central1"
22+
23+
enable_autopilot = true
24+
25+
# Set `deletion_protection` to `true` will ensure that one cannot
26+
# accidentally delete this instance by use of Terraform.
27+
deletion_protection = false
28+
}
29+
30+
# [START gke_autopilot_reservation_specific_reservation]
31+
resource "google_compute_reservation" "specific_pod" {
32+
name = "specific-reservation-pod"
33+
zone = "us-central1-a"
34+
35+
specific_reservation {
36+
count = 1
37+
38+
instance_properties {
39+
machine_type = "c3-standard-4-lssd"
40+
41+
local_ssds {
42+
disk_size_gb = 375
43+
interface = "NVME"
44+
}
45+
}
46+
}
47+
48+
specific_reservation_required = true
49+
}
50+
# [END gke_autopilot_reservation_specific_reservation]
51+
52+
# [START gke_autopilot_reservation_specific_cluster]
53+
resource "google_compute_reservation" "specific_accelerator" {
54+
name = "specific-reservation-accelerator"
55+
zone = "us-central1-a"
56+
57+
specific_reservation {
58+
count = 1
59+
60+
instance_properties {
61+
#min_cpu_platform = "Intel Cascade Lake"
62+
machine_type = "g2-standard-4"
63+
64+
guest_accelerators {
65+
accelerator_count = 1
66+
accelerator_type = "nvidia-l4"
67+
}
68+
}
69+
}
70+
71+
specific_reservation_required = true
72+
}
73+
# [END gke_autopilot_reservation_specific_cluster]
74+
75+
provider "kubernetes" {
76+
host = "https://${google_container_cluster.default.endpoint}"
77+
token = data.google_client_config.default.access_token
78+
cluster_ca_certificate = base64decode(google_container_cluster.default.master_auth[0].cluster_ca_certificate)
79+
80+
ignore_annotations = [
81+
"^autopilot\\.gke\\.io\\/.*",
82+
"^cloud\\.google\\.com\\/cluster_autoscaler_.*"
83+
]
84+
}
85+
86+
# [START gke_autopilot_reservation_specific_pod]
87+
resource "kubernetes_pod_v1" "default_pod" {
88+
metadata {
89+
name = "specific-same-project-pod"
90+
}
91+
92+
spec {
93+
node_selector = {
94+
"cloud.google.com/compute-class" = "Performance"
95+
"cloud.google.com/machine-family" = "c3"
96+
"cloud.google.com/reservation-name" = google_compute_reservation.specific_pod.name
97+
"cloud.google.com/reservation-affinity" = "specific"
98+
}
99+
100+
container {
101+
name = "my-container"
102+
image = "k8s.gcr.io/pause"
103+
104+
resources {
105+
requests = {
106+
cpu = 2
107+
memory = "8Gi"
108+
ephemeral-storage = "1Gi"
109+
}
110+
}
111+
112+
security_context {
113+
allow_privilege_escalation = false
114+
run_as_non_root = false
115+
116+
capabilities {
117+
add = []
118+
drop = ["NET_RAW"]
119+
}
120+
}
121+
}
122+
123+
security_context {
124+
run_as_non_root = false
125+
supplemental_groups = []
126+
127+
seccomp_profile {
128+
type = "RuntimeDefault"
129+
}
130+
}
131+
}
132+
133+
depends_on = [
134+
google_compute_reservation.specific_pod
135+
]
136+
}
137+
# [END gke_autopilot_reservation_specific_pod]
138+
139+
# [START gke_autopilot_reservation_specific_accelerator]
140+
resource "kubernetes_pod_v1" "default_accelerator" {
141+
metadata {
142+
name = "specific-same-project-accelerator"
143+
}
144+
145+
spec {
146+
node_selector = {
147+
"cloud.google.com/compute-class" = "Accelerator"
148+
"cloud.google.com/gke-accelerator" = "nvidia-l4"
149+
"cloud.google.com/reservation-name" = google_compute_reservation.specific_accelerator.name
150+
"cloud.google.com/reservation-affinity" = "specific"
151+
}
152+
153+
container {
154+
name = "my-container"
155+
image = "k8s.gcr.io/pause"
156+
157+
resources {
158+
requests = {
159+
cpu = 2
160+
memory = "7Gi"
161+
ephemeral-storage = "1Gi"
162+
"nvidia.com/gpu" = 1
163+
164+
}
165+
limits = {
166+
"nvidia.com/gpu" = 1
167+
}
168+
}
169+
170+
security_context {
171+
allow_privilege_escalation = false
172+
run_as_non_root = false
173+
174+
capabilities {
175+
add = []
176+
drop = ["NET_RAW"]
177+
}
178+
}
179+
}
180+
181+
security_context {
182+
run_as_non_root = false
183+
supplemental_groups = []
184+
185+
seccomp_profile {
186+
type = "RuntimeDefault"
187+
}
188+
}
189+
}
190+
191+
depends_on = [
192+
google_compute_reservation.specific_accelerator
193+
]
194+
}
195+
# [END gke_autopilot_reservation_specific_accelerator]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: gke_autopilot_reservation
19+
spec:
20+
skip: true
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
# [START gke_standard_zonal_reservation_any_reservation]
18+
resource "google_compute_reservation" "any_reservation" {
19+
name = "any-reservation"
20+
zone = "us-central1-a"
21+
22+
specific_reservation {
23+
count = 3
24+
25+
instance_properties {
26+
machine_type = "e2-medium"
27+
}
28+
}
29+
}
30+
# [END gke_standard_zonal_reservation_any_reservation]
31+
32+
# [START gke_standard_zonal_reservation_any_cluster]
33+
resource "google_container_cluster" "default" {
34+
name = "gke-standard-zonal-cluster"
35+
location = "us-central1-a"
36+
37+
initial_node_count = 1
38+
39+
node_config {
40+
machine_type = "e2-medium"
41+
42+
reservation_affinity {
43+
consume_reservation_type = "ANY_RESERVATION"
44+
}
45+
}
46+
47+
depends_on = [
48+
google_compute_reservation.any_reservation
49+
]
50+
51+
# Set `deletion_protection` to `true` will ensure that one cannot
52+
# accidentally delete this instance by use of Terraform.
53+
deletion_protection = false
54+
}
55+
# [END gke_standard_zonal_reservation_any_cluster]
56+
57+
# [START gke_standard_zonal_reservation_any_node_pool]
58+
resource "google_container_node_pool" "any_node_pool" {
59+
name = "gke-standard-zonal-any-node-pool"
60+
cluster = google_container_cluster.default.name
61+
location = google_container_cluster.default.location
62+
63+
initial_node_count = 3
64+
node_config {
65+
machine_type = "e2-medium"
66+
67+
reservation_affinity {
68+
consume_reservation_type = "ANY_RESERVATION"
69+
}
70+
}
71+
}
72+
# [END gke_standard_zonal_reservation_any_node_pool]
73+
74+
# [START gke_standard_zonal_reservation_specific_reservation]
75+
resource "google_compute_reservation" "specific_reservation" {
76+
name = "specific-reservation"
77+
zone = "us-central1-a"
78+
79+
specific_reservation {
80+
count = 1
81+
82+
instance_properties {
83+
machine_type = "e2-medium"
84+
}
85+
}
86+
87+
specific_reservation_required = true
88+
}
89+
# [END gke_standard_zonal_reservation_specific_reservation]
90+
91+
# [START gke_standard_zonal_reservation_specific_node_pool]
92+
resource "google_container_node_pool" "specific_node_pool" {
93+
name = "gke-standard-zonal-specific-node-pool"
94+
cluster = google_container_cluster.default.name
95+
location = google_container_cluster.default.location
96+
97+
initial_node_count = 1
98+
node_config {
99+
machine_type = "e2-medium"
100+
101+
reservation_affinity {
102+
consume_reservation_type = "SPECIFIC_RESERVATION"
103+
key = "compute.googleapis.com/reservation-name"
104+
values = [google_compute_reservation.specific_reservation.name]
105+
}
106+
}
107+
108+
depends_on = [
109+
google_compute_reservation.specific_reservation
110+
]
111+
}
112+
# [END gke_standard_zonal_reservation_specific_node_pool]

0 commit comments

Comments
 (0)