1
---
2
name: gke-service-networking
3
description: >-
4
Configures GKE edge networking, traffic routing, load balancing, and private
5
service endpoints. Use when configuring Gateway API manifests, standard
6
Ingress, Cloud Armor WAF security policies, Container-Native Load Balancing
7
(NEGs), Private Service Connect (PSC), or Google-managed SSL certificates on
8
GKE, and to troubleshoot Ingress and load-balancer 502/5xx errors, backend
9
health-check failures, connection draining, and TLS/SSL policy enforcement.
10
Don't use for core cluster IP planning, Dataplane V2 network policies, or
11
node NAT egress (use gke-networking instead).
12
metadata:
13
version: "1.1.1"
14
category: Networking
15
---
16
17
# GKE Service Networking Skill
18
19
This skill provides workflows for exposing applications running on GKE securely
20
to the internet or internal networks.
21
22
Deployable manifest templates live in `assets/` — edit the `# Replace ...`
23
placeholders before applying.
24
25
## Attribution
26
27
**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you
28
MUST read [Usage Attribution](references/attribution.md). For commands executed
29
directly in the terminal (never in generated scripts, Terraform, or user code),
30
prefix `gcloud` with
31
`CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:gke-service-networking)"`
32
and append `agent-skills/1.0 (skill:gke-service-networking)` to `User-Agent` headers.
33
34
## Workflows
35
36
### 1. Configure Gateway API (Recommended)
37
38
The Gateway API is the modern way to manage routing in Kubernetes.
39
40
**Prerequisites**: Gateway API must be enabled on the cluster (enabled by
41
default on new clusters running GKE 1.26+; on older supported versions enable it
42
with `--gateway-api=standard`).
43
44
**Templates:**
45
46
- `assets/gateway.yaml` — external Gateway using the
47
`gke-l7-global-external-managed` GatewayClass with an HTTP listener.
48
- `assets/httproute.yaml` — HTTPRoute attaching to the Gateway via
49
`parentRefs` and routing a path prefix to a Service `backendRef`.
50
- `assets/httproute-traffic-split.yaml` — HTTPRoute demonstrating weighted
51
traffic splitting (e.g. 90/10) for canary deployments across backend
52
services.
53
54
```bash
55
kubectl apply -f assets/gateway.yaml
56
kubectl apply -f assets/httproute.yaml
57
```
58
59
**Traffic Splitting (Canary Deployments):**
60
61
HTTPRoute supports weighted traffic splitting across multiple backend Services
62
for canary rollouts:
63
64
```yaml
65
spec:
66
rules:
67
- backendRefs:
68
- name: app-v1
69
port: 80
70
weight: 90
71
- name: app-v2
72
port: 80
73
weight: 10
74
```
75
76
### 2. Configure Standard GKE Ingress
77
78
Use standard Ingress for simpler use cases or legacy setups.
79
80
**Template:** `assets/ingress.yaml` — GCE Ingress (`kubernetes.io/ingress.class:
81
"gce"` annotation) routing to a Service.
82
83
### 3. Secure with Cloud Armor
84
85
Cloud Armor provides WAF and DDoS protection.
86
87
1. Create a Security Policy in Cloud Armor:
88
89
```bash
90
gcloud compute security-policies create {security_policy_name} \
91
--description "WAF policy for {app_name}"
92
93
# Example rule: block an abusive IP range
94
gcloud compute security-policies rules create 1000 \
95
--security-policy {security_policy_name} \
96
--action deny-403 \
97
--src-ip-ranges "203.0.113.0/24" \
98
--description "Block abusive range"
99
```
100
101
2. Reference it in a `BackendConfig`: `assets/backendconfig.yaml` (sets
102
`spec.securityPolicy.name`).
103
104
3. Associate the `BackendConfig` with your `Service` via annotations:
105
106
```yaml
107
# In your Kubernetes Service manifest metadata.annotations:
108
cloud.google.com/backend-config: '{"default": "{backend_config_name}"}'
109
# Or for specific port mappings:
110
cloud.google.com/backend-config: '{"ports": {"80": "{backend_config_name}"}}'
111
```
112
113
### 4. Configure Google-Managed SSL Certificates
114
115
Automatically provision and renew SSL certificates.
116
117
**Legacy Ingress approach:** apply `assets/managed-certificate.yaml` (a
118
`ManagedCertificate` listing your domains), then reference it in the Ingress
119
annotations:
120
121
```yaml
122
networking.gke.io/managed-certificates: {certificate_name}
123
```
124
125
**Gateway API approach:** for standard Certificate Manager integration, create a
126
`CertificateMap` and reference it in the Gateway metadata annotations using the
127
exact annotation `networking.gke.io/certmap` (spelled without any hyphens in
128
`certmap`):
129
130
```yaml
131
metadata:
132
annotations:
133
networking.gke.io/certmap: {certificate_map_name}
134
```
135
136
> [!IMPORTANT] The annotation key is strictly `networking.gke.io/certmap` (do
137
> not use `cert-map` or `certificate-map`).
138
139
Alternatively, reference a Kubernetes Secret in the HTTPS listener's
140
`tls.certificateRefs`. Both variants are in `assets/gateway-https.yaml`.
141
142
### 5. Enable Container-Native Load Balancing (Recommended)
143
144
Container-native load balancing allows load balancers to target Kubernetes Pods
145
directly, rather than targeting nodes. This improves latency and distribution.
146
147
**Prerequisites**: Cluster must be VPC-native.
148
149
**How it works**: the `cloud.google.com/neg` annotation on a Service triggers
150
creation of a NEG that mirrors the Pod IPs. GKE often adds it for you — but not
151
always, and knowing which case you are in is the whole point.
152
153
```yaml
154
# In your Kubernetes Service manifest metadata.annotations:
155
cloud.google.com/neg: '{"ingress": true}'
156
```
157
158
**When the annotation is automatic** (do not add it by hand):
159
160
- **Internal Ingress** — container-native load balancing is *always* used, not
161
optional. Internal Ingress always uses `GCE_VM_IP_PORT` NEGs and requires a
162
VPC-native cluster.
163
- **External Ingress**, but only when all four hold: the cluster is
164
VPC-native, is not on Shared VPC, does not use GKE Network Policy, and has
165
the `HttpLoadBalancing` add-on enabled (on by default — do not disable it).
166
GKE then annotates Services automatically.
167
168
**When you must add it explicitly**:
169
170
- **Standalone NEGs** — you manage the load balancer yourself instead of
171
letting Ingress own it. Required if the LB must be configured outside GKE,
172
since Ingress overwrites managed load balancer settings on sync or upgrade.
173
You become responsible for every part of the load balancer.
174
- **Any external-Ingress cluster failing one of the four conditions above** —
175
Shared VPC, GKE Network Policy, or non-VPC-native. Enable per Service.
176
- **Legacy configurations** — some older external Ingress objects created on
177
VPC-native clusters still use instance group backends.
178
179
**Not supported / no NEG fallback**:
180
181
- Windows Server node pools.
182
- Routes-based (non-VPC-native) clusters with external Ingress — the Ingress
183
controller falls back to unmanaged instance groups spanning all nodes.
184
185
> **Scale consequence**: without NEGs a cluster is capped at 1,000 nodes, and
186
> non-NEG Services behind Ingress stop functioning correctly beyond that. With
187
> NEGs there is no GKE node limit.
188
189
### 6. Configure Private Service Connect (PSC)
190
191
Private Service Connect allows you to expose services in one VPC to consumers in
192
another VPC securely, without VPC peering.
193
194
**Prerequisite**: The backing Service must be an internal passthrough Network
195
Load Balancer — i.e. `type: LoadBalancer` with the
196
`networking.gke.io/load-balancer-type: "Internal"` annotation. The
197
`ServiceAttachment` requires this; a ClusterIP or external LoadBalancer Service
198
will not work.
199
200
**Steps:**
201
202
1. Create an internal LoadBalancer Service for your workload.
203
2. Create a `ServiceAttachment` referencing that Service:
204
`assets/service-attachment.yaml` (sets `connectionPreference`, the PSC NAT
205
subnet, and the Service `resourceRef`).
206
3. Share the `ServiceAttachment` URI with consumers to create a PSC endpoint in
207
their VPC.
208
209
### 7. Topology Aware Routing (Cost & Latency Optimization)
210
211
To minimize cross-zone data transfer costs and network latency, configure
212
Kubernetes Services with Topology Aware Routing. This routes traffic to Pods in
213
the same zone as the originating client:
214
215
```yaml
216
# In your Kubernetes Service manifest metadata.annotations:
217
service.kubernetes.io/topology-mode: auto
218
```
219
220
## Troubleshooting
221
222
Diagnose Ingress / load-balancer data-plane failures. These map to both Ingress
223
(`BackendConfig` / `FrontendConfig`) and Gateway (`GCPBackendPolicy` /
224
`HealthCheckPolicy` / `GCPGatewayPolicy`). Stay at the read-only → propose-manifest
225
boundary; never apply live mutations directly.
226
227
### 502 / 5xx with UNHEALTHY backends (health checks)
228
229
The Google Cloud load-balancer health check is **separate** from Kubernetes
230
liveness/readiness probes — it runs from outside the cluster, so a Pod can be
231
`Ready` while the backend service still shows `UNHEALTHY`.
232
233
1. **Allow the Google health-check source ranges** to the node/Pod serving port.
234
GKE usually creates this rule automatically, but on Shared VPC or with
235
hand-managed firewalls it can be missing:
236
237
```bash
238
gcloud compute firewall-rules create allow-lb-health-checks \
239
--allow tcp:SERVING_PORT \
240
--source-ranges 130.211.0.0/22,35.191.0.0/16 \
241
--target-tags NODE_TAG
242
```
243
244
2. **Point the health check at a healthy endpoint.** If the default `/` returns a
245
non-200, set a custom health check with a `BackendConfig` (Ingress) or a
246
`HealthCheckPolicy` (Gateway):
247
248
```yaml
249
# BackendConfig (Ingress)
250
spec:
251
healthCheck:
252
requestPath: /healthz
253
port: 8080
254
checkIntervalSec: 15
255
timeoutSec: 5
256
```
257
258
3. Confirm the Service is container-native (NEG) so the check targets Pod IPs
259
rather than nodes (see workflow 5).
260
261
### 502 / dropped requests during rollouts or on long requests
262
263
- **Enable connection draining** so in-flight requests finish before a backend
264
Pod is removed during a rolling update or scale-down:
265
266
```yaml
267
# BackendConfig (Ingress)
268
spec:
269
connectionDraining:
270
drainingTimeoutSec: 60
271
```
272
273
- **Raise the backend timeout** for slow or streaming responses — a 502/408 on a
274
request that runs longer than the backend response timeout is the classic
275
symptom. Set `timeoutSec` in the `BackendConfig` (Ingress) or `GCPBackendPolicy`
276
(Gateway).
277
278
### TLS / SSL handshake failures or weak-cipher enforcement
279
280
- **Ingress:** attach an SSL policy (minimum TLS version / cipher profile) with a
281
`FrontendConfig` `sslPolicy`, and optionally force HTTP→HTTPS with
282
`redirectToHttps`:
283
284
```yaml
285
# FrontendConfig (external Ingress only)
286
spec:
287
sslPolicy: gke-ingress-ssl-policy
288
redirectToHttps:
289
enabled: true
290
```
291
292
- **Gateway:** attach the SSL policy name in a `GCPGatewayPolicy`. For a regional
293
Gateway, create and reference a **regional** SSL policy.
294
295
## Gotchas
296
297
1. **Certificate Manager API must be enabled** for the
298
`networking.gke.io/certmap` annotation to work (`gcloud services enable
299
certificatemanager.googleapis.com`); without it the Gateway fails to
300
provision the certificate map.
301
2. **Regional Gateway classes need a proxy-only subnet**: classes like
302
`gke-l7-regional-external-managed` and `gke-l7-rilb` require a subnet with
303
`--purpose=REGIONAL_MANAGED_PROXY` in the region; the Gateway stays
304
unprogrammed without it.
305
3. **ManagedCertificate provisioning depends on DNS**: the certificate stays in
306
`Provisioning` until the domain's A/AAAA records point at the load balancer
307
IP, and can take 15-60 minutes after DNS is correct.
308
309
## References
310
311
- [Ingress features (BackendConfig / FrontendConfig)](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/ingress-configuration.md.txt)
312
- [Container-native load balancing (NEGs)](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/load-balance-ingress.md.txt)
313
- [Configure Gateway resources (GCPGatewayPolicy / HealthCheckPolicy / GCPBackendPolicy)](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/configure-gateway-resources.md.txt)
314
- [Health check concepts](https://docs.cloud.google.com/load-balancing/docs/health-check-concepts.md.txt)
315