GKE - Google Kubernetes Engine
Managing Security Aspects
How to protect your GKE cluster components using Network Policies
Google Kubernetes Engine (GKE) provides built-in security features and supports Kubernetes Network Policies to safeguard your cluster. In this guide, you'll learn how to secure the control plane, worker nodes, instance metadata, pod access, and network traffic to maintain a robust security posture.
Securing the Control Plane
The control plane orchestrates your cluster, managing nodes, pods, and services. GKE secures it by running on a hardened Container-Optimized OS with SELinux, AppArmor, and kernel hardening. All communication with the API server is encrypted using TLS, and certificates are rotated automatically. Access is audited for traceability.
You can tighten control plane access even further:
- Authorized Networks: Restrict API endpoint access to whitelisted IP ranges.
- Private Clusters: Use private IPs for nodes and the API server, isolating them from the public internet.
- Identity and Access Management (IAM): Assign fine-grained roles and permissions to limit who can perform actions on the control plane.
Securing Worker Nodes
Worker nodes run your containerized workloads and must be locked down:
- Container-Optimized OS: Google-maintained, read-only root filesystem with a built-in firewall and no root SSH access.
- Autopilot Enforcement: Autopilot clusters apply these OS controls by default.
- Limited Accounts: Only essential user accounts exist with privileges scoped to required operations.
Node Upgrades
Regular updates close security gaps and ensure compatibility:
- Autopilot Clusters: Automatic node upgrades are enabled by default.
- Standard Clusters: Choose between automatic or manual upgrades with customizable windows.
# Upgrade a specific node pool in a cluster
gcloud container clusters upgrade CLUSTER_NAME \
--region=REGION \
--node-pool=POOL_NAME
Protecting Instance Metadata
By default, nodes fetch credentials and configuration from the Compute Engine metadata server. Pods shouldn't inherit node service account keys. Enabling Workload Identity filters metadata access, exposing only pod-level credentials.
Note
Workload Identity replaces node-level metadata access, preventing privilege escalation from pods to node credentials.
Managing Pod Access and Credentials
Apply the principle of least privilege to your workloads:
- Security Contexts: Define user IDs, restrict Linux capabilities, and disable privilege escalation.
- Workload Identity: Map Kubernetes ServiceAccounts to IAM ServiceAccounts for granular permissions.
- Binary Authorization: Enforce image signing and attestation before deployment.
apiVersion: v1
kind: Pod
metadata:
name: secure-app
spec:
containers:
- name: app
image: gcr.io/my-project/secure-app:latest
securityContext:
runAsUser: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
GKE Autopilot enforces these settings automatically. In Standard clusters, include securityContext fields in your Pod specs:
securityContext:
runAsNonRoot: true
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
Binary Authorization integrates with Artifact Registry to allow only trusted, signed images:
Network Security with Network Policies
Kubernetes Network Policies control traffic based on pod labels, enforcing a zero-trust network. For example, restrict an app=my-app
pod to only communicate with db=my-db
pods:
First, label your workloads:
kubectl label pods my-app app=my-app
kubectl label pods my-db db=my-db
Then, apply this NetworkPolicy:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-app-to-db
spec:
podSelector:
matchLabels:
db: my-db
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: my-app
Warning
Without a default deny policy, unintended traffic might still flow. Always set a global deny-all policy if no rules match.
Summary
By securing the control plane, hardening nodes, protecting metadata, enforcing least privilege, and applying Network Policies, you fortify your GKE cluster against threats.
Links and References
- Google Kubernetes Engine Overview
- Kubernetes NetworkPolicy
- Workload Identity Documentation
- Binary Authorization
Watch Video
Watch video content