1
---
2
name: gke-workload-security
3
description: >-
4
Audits, configures, and hardens workload-level security controls for Google
5
Kubernetes Engine (GKE) applications and namespaces. Covers running security
6
audits (`audit_cluster.sh`), enforcing Network Policies (default-deny and Dataplane
7
V2 logging), isolating high-risk pods inside GKE Sandbox (`gVisor`), enforcing Pod
8
Security Standards (`restricted` labeling) and pod securityContext, and mounting Secret Manager secrets via
9
CSI (`SecretProviderClass`). Use when auditing workload security posture, isolating
10
namespaces, applying pod security standards, or
11
configuring network policies and secret volume mounts. Don't use for Workload Identity (use gke-workload-identity), cluster-wide
12
control plane security, RBAC hardening, Binary Authorization, Shielded Nodes,
13
or enabling platform-level GKE add-ons (use gke-platform-security instead).
14
metadata:
15
version: "1.0.3"
16
category: Security
17
---
18
19
# GKE Workload Security
20
21
> **Routing Note:** For Workload Identity IAM and KSA/GSA bindings, use the `gke-workload-identity` skill. For cluster-level security flags (`--database-encryption-key`, `--security-posture`, RBAC, Shielded Nodes, Binary Authorization), use the `gke-platform-security` skill.
22
23
This skill provides workflows and best practices for securing GKE workloads. It
24
covers security auditing, Identity and Access Management (Workload Identity),
25
Network Security (Network Policies), and Node Security.
26
27
## Attribution
28
29
**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you
30
MUST read [Usage Attribution](references/attribution.md). For commands executed
31
directly in the terminal (never in generated scripts, Terraform, or user code),
32
prefix `gcloud` with
33
`CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:gke-workload-security)"`
34
and append `agent-skills/1.0 (skill:gke-workload-security)` to `User-Agent` headers.
35
36
## Workflows
37
38
### 1. Security Audit
39
40
Assess the current security posture of your cluster using the provided audit
41
script.
42
43
**Prerequisites:**
44
45
- `gcloud` CLI authenticated.
46
- `jq` command-line JSON processor installed.
47
48
**Capabilities:**
49
50
- Checks for Workload Identity.
51
- Verifies Network Policy is enabled.
52
- Checks if Shielded Nodes are enabled.
53
- Checks if Binary Authorization is enabled.
54
- Checks for Private Cluster configuration.
55
56
**Command:**
57
58
```bash
59
scripts/audit_cluster.sh <cluster-name> <region> <project-id>
60
```
61
62
### 2. Configure Workload Identity
63
64
Workload Identity allows Kubernetes Service Accounts (KSAs) to impersonate
65
Google Service Accounts (GSAs). Use this method when workloads require a
66
dedicated GSA to access Google Cloud APIs; for most workloads, direct IAM
67
principal binding (covered in the `gke-workload-identity` skill) is the
68
recommended method.
69
70
**Steps:**
71
72
1. **Create Namespace and KSA:**
73
74
```bash
75
kubectl create namespace workload-identity-test-ns
76
kubectl create serviceaccount <ksa-name> \
77
--namespace workload-identity-test-ns
78
```
79
80
2. **Bind KSA to GSA:**
81
82
```bash
83
gcloud iam service-accounts add-iam-policy-binding <gsa-name>@<project-id>.iam.gserviceaccount.com \
84
--role roles/iam.workloadIdentityUser \
85
--member "serviceAccount:<project-id>.svc.id.goog[workload-identity-test-ns/<ksa-name>]"
86
```
87
88
3. **Annotate KSA:**
89
90
```bash
91
kubectl annotate serviceaccount <ksa-name> \
92
--namespace workload-identity-test-ns \
93
iam.gke.io/gcp-service-account=<gsa-name>@<project-id>.iam.gserviceaccount.com
94
```
95
96
4. **Verify Example Pod:** Use existing asset
97
`assets/workload-identity-pod.yaml` to test the configuration. Update the
98
`<ksa-name>` in the file first.
99
100
```bash
101
kubectl apply -f assets/workload-identity-pod.yaml -n workload-identity-test-ns
102
```
103
104
### 3. Implement Network Policies
105
106
Control traffic flow between Pods using Network Policies. By default, all
107
traffic is allowed.
108
109
**Enable Network Policy Enforcement:**
110
111
```bash
112
gcloud container clusters update <cluster-name> \
113
--update-addons=NetworkPolicy=ENABLED \
114
--region <region>
115
```
116
117
> [!NOTE] If your cluster uses Dataplane V2 (`--enable-dataplane-v2`), Network
118
> Policy enforcement is built-in and this step is not required (and may fail).
119
120
**Apply Default Deny Policy:** Isolate namespaces by denying all ingress and
121
egress traffic by default.
122
123
**Replace `<target-namespace>` with the namespace you want to isolate.**
124
125
```bash
126
kubectl apply -f assets/default-deny-netpol.yaml -n <target-namespace>
127
```
128
129
### 4. GKE Sandbox (gVisor) Pod Isolation
130
131
Run untrusted workloads in a sandbox for extra kernel isolation. *(Note:
132
Enabling Shielded Nodes (`--enable-shielded-nodes`) and GKE Sandbox
133
(`--enable-gke-sandbox`) at the cluster control plane level are platform-level
134
actions covered in the `gke-platform-security` skill.)*
135
136
**Run a Sandboxed Pod:** Add `runtimeClassName: gvisor` to your Pod spec:
137
138
```yaml
139
apiVersion: v1
140
kind: Pod
141
metadata:
142
name: sandboxed-pod
143
spec:
144
runtimeClassName: gvisor
145
containers:
146
- name: app
147
image: nginx
148
```
149
150
### 5. Pod Security Standards
151
152
Enforce security policies on namespaces using labels.
153
154
**Enforce Restricted Profile:**
155
156
```bash
157
kubectl label --overwrite ns <namespace> \
158
pod-security.kubernetes.io/enforce=restricted \
159
pod-security.kubernetes.io/enforce-version=latest
160
```
161
162
> [!NOTE] Using `latest` ensures you use the policies corresponding to the
163
> cluster's current version. You can pin it to a specific version (e.g.,
164
> `v1.30`) to lock down the namespace to policies of a specific release.
165
166
### 6. Secret Manager Integration (CSI Driver)
167
168
Mount secrets from Google Cloud Secret Manager directly as volumes in your pods.
169
170
**Prerequisites**: Secret Manager CSI driver must be enabled on the cluster.
171
172
**Example SecretProviderClass:**
173
174
```yaml
175
apiVersion: secrets-store.csi.x-k8s.io/v1
176
kind: SecretProviderClass
177
metadata:
178
name: my-secret-provider
179
spec:
180
provider: gcp
181
parameters:
182
secrets: |
183
- resourceName: "projects/<project-id>/secrets/my-secret/versions/latest"
184
fileName: "my-secret-file"
185
```
186
187
**Example Pod Spec excerpt:**
188
189
```yaml
190
spec:
191
containers:
192
- name: my-app
193
volumeMounts:
194
- name: secrets-store-inline
195
mountPath: "/mnt/secrets"
196
readOnly: true
197
volumes:
198
- name: secrets-store-inline
199
csi:
200
driver: secrets-store.csi.k8s.io
201
readOnly: true
202
volumeAttributes:
203
secretProviderClass: "my-secret-provider"
204
```
205
206
### 7. Enable Network Policy Logging
207
208
If using GKE Dataplane V2, you can log allowed and denied connections.
209
210
**Steps:**
211
212
1. Configure the `NetworkLogging` custom resource.
213
214
**Example NetworkLogging Manifest:**
215
216
```yaml
217
apiVersion: networking.gke.io/v1alpha1
218
kind: NetworkLogging
219
metadata:
220
name: default
221
spec:
222
cluster:
223
allow:
224
log: true
225
delegate: true
226
deny:
227
log: true
228
delegate: true
229
```
230
231
This will log connection details to Cloud Logging.
232
233
## Best Practices
234
235
1. **Least Privilege:** Always use Workload Identity with minimal IAM roles.
236
Avoid using Node default service accounts.
237
2. **Network Isolation:** Use Network Policies to restrict Pod-to-Pod
238
communication. Enable Network Policy Logging for visibility.
239
3. **Image Security:** Use Binary Authorization to ensure only trusted images
240
are deployed.
241
4. **Secret Management**: Use Secret Manager CSI driver instead of default
242
Kubernetes secrets for sensitive data.
243
5. **Pod Security**: Enforce `baseline` or `restricted` Pod Security Standards
244
on all non-system namespaces.
245
6. **Policy Enforcement**: Consider using **Policy Controller** (Gatekeeper) to
246
enforce custom security and compliance policies across the cluster.
247
248
## Resources
249
250
- [Workload Identity Federation for GKE](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity)
251
- [GKE Network Policies](https://cloud.google.com/kubernetes-engine/docs/how-to/network-policy)
252
- [Pod Security Standards in GKE](https://cloud.google.com/kubernetes-engine/docs/how-to/podsecurityadmission)
253
- [Google Secret Manager CSI Driver](https://cloud.google.com/secret-manager/docs/secret-manager-managed-csi-component)
254
- [GKE Dataplane V2 Network Logging](https://cloud.google.com/kubernetes-engine/docs/how-to/network-policy-logging)
255