Version note: examples use the Kubernetes v1.35 documentation snapshot, not necessarily the newest release. Check current support and component compatibility. These commands were not tested on a live cluster.
What this guide covers
Kubernetes has separate controls for API permissions, Pod security settings and network traffic. RBAC controls API actions. Pod Security admission checks Pod settings. NetworkPolicy restricts traffic only when the network plugin enforces it.
Before you start
Use an isolated test cluster with a NetworkPolicy-capable network plugin. Flannel alone does not supply that enforcement. Do not install a second competing network plugin onto an existing cluster to try this example.
Step 1: Create a restricted workload
Save this as security.yaml and apply it:
apiVersion: v1
kind: Namespace
metadata:
name: secure-lab
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: v1.35
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/audit-version: v1.35
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/warn-version: v1.35
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: restricted-worker
namespace: secure-lab
automountServiceAccountToken: false
---
apiVersion: v1
kind: Pod
metadata:
name: restricted-worker
namespace: secure-lab
labels:
app: restricted-worker
spec:
serviceAccountName: restricted-worker
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
runAsUser: 10000
seccompProfile:
type: RuntimeDefault
containers:
- name: worker
image: busybox:1.37.0
command: ["sh", "-c", "sleep 3600"]
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
resources:
requests:
cpu: 10m
memory: 16Mi
limits:
memory: 32Mi
The namespace requires the restricted Pod Security profile. The ServiceAccount and Role grant only the actions listed. The container runs without root or unnecessary Linux capabilities.
Step 2: Apply network restrictions
Save this as network-policy.yaml and apply it:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
namespace: secure-lab
spec:
podSelector: {}
policyTypes: ["Ingress", "Egress"]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-cluster-dns
namespace: secure-lab
spec:
podSelector:
matchLabels:
app: restricted-worker
policyTypes: ["Egress"]
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
Read the selectors and ports carefully. This example denies traffic by default and adds a DNS exception. DNS labels and ports must match your cluster’s actual DNS setup.
Step 3: Test allowed and denied operations
Run:
kubectl -n secure-lab get networkpolicies
kubectl -n secure-lab exec restricted-worker -- nslookup kubernetes.default.svc.cluster.local
kubectl -n secure-lab exec restricted-worker -- wget -T 3 -qO- https://example.com
kubectl auth can-i get secrets --namespace=secure-lab \
--as=system:serviceaccount:secure-lab:restricted-worker
The intended DNS lookup should work. The external web request should fail because it has no egress exception. The account should not be allowed to read Secrets. Check that a failed request is due to policy rather than an unrelated outage.
Step 4: Verify each control independently
Try a known permitted operation as well as a denied one. A NetworkPolicy object can exist while doing nothing if the plugin does not enforce it. Likewise, API permissions do not restrict ordinary network packets.
Check the result
Keep a short allow/deny checklist for API access, Pod admission and traffic. Each needs its own observed result; one denied command does not establish complete isolation.
Conversation
0 commentsNo comments yet. Start the conversation below.