Prompt
Need Kubernetes with network policies and RBAC
Latest observation
Absolutely — here’s a solid Kubernetes setup with both NetworkPolicies and RBAC. I’ll give you a practical baseline you can apply and adapt.
1) Prerequisites
Make sure your cluster supports NetworkPolicy enforcement.
Not all Kubernetes networking plugins do this by default. Common options:
- Calico
- Cilium
- Weave Net
- Antrea
Also ensure your namespace(s) have the workloads you want to protect.
2) Namespace setup
Example namespace:
apiVersion: v1
kind: Namespace
metadata:
name: secure-app
Apply:
kubectl apply -f namespace.yaml
3) NetworkPolicies
A. Default deny all ingress and egress
This is the starting point for a secure namespace.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: secure-app
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
This blocks all pod-to-pod traffic unless explicitly allowed.
B. Allow ingress only from same namespace app pods
Example: allow traffic to pods labeled app=web only from pods labeled app=frontend in the same namespace.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-web
namespace: secure-app
spec:
podSelector:
matchLabels:
app: web
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
C. Allow DNS egress
If you have default-deny egress, you usually need DNS access.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-egress
namespace: secure-app
spec:
podSelector: {}
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
Depending on your cluster, DNS pods may be labeled differently, especially with CoreDNS.
D. Allow egress to a database namespace
Example allowing app pods to talk to a database service in namespace data.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-web-to-db
namespace: secure-app
spec:
podSelector:
matchLabels:
app: web
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
name: data
podSelector:
matchLabels:
app: postgres
ports:
- protocol: TCP
port: 5432
4) RBAC
RBAC controls who can do what in the Kubernetes API, not network traffic.
A. ServiceAccount
Create a service account for your app:
apiVersion: v1
kind: ServiceAccount
metadata:
name: web-sa
namespace: secure-app
B. Role
Example Role allowing read-only access to pods, configmaps, and services in the namespace.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: web-readonly
namespace: secure-app
rules:
- apiGroups: [""]
resources: ["pods", "services", "configmaps"]
verbs: ["get", "list", "watch"]
C. RoleBinding
Bind the role to the service account:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: web-readonly-binding
namespace: secure-app
subjects:
- kind: ServiceAccount
name: web-sa
namespace: secure-app
roleRef:
kind: Role
name: web-readonly
apiGroup: rbac.authorization.k8s.io
5) Example Deployment using the ServiceAccount
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: secure-app
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
serviceAccountName: web-sa
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
6) Example of a more locked-down RBAC policy
If the app only needs to read its own configmaps and secrets, you can use:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: app-access
namespace: secure-app
rules:
- apiGroups: [""]
resources: ["configmaps", "secrets"]
resourceNames: ["app-config", "app-secret"]
verbs: ["get"]
7) Apply everything
kubectl apply -f namespace.yaml
kubectl apply -f serviceaccount.yaml
kubectl apply -f role.yaml
kubectl apply -f rolebinding.yaml
kubectl apply -f deployment.yaml
kubectl apply -f networkpolicy-default-deny.yaml
kubectl apply -f networkpolicy-dns.yaml
kubectl apply -f networkpolicy-app.yaml
8) Verify
Check RBAC:
kubectl auth can-i list pods --as=system:serviceaccount:secure-app:web-sa -n secure-app
Check NetworkPolicy:
Try to curl from a test pod in the namespace to unauthorized targets. Traffic should be blocked.
Example test pod:
kubectl run test --rm -it --image=busybox -n secure-app -- sh
Then inside:
wget -qO- http://<blocked-service>
9) Best practices
- Start with default deny all
- Add only the minimum required ingress and egress
- Use namespace labels carefully for inter-namespace policies
- Keep RBAC scoped to the namespace unless cluster-wide access is truly needed
- Avoid using
cluster-admin - Prefer ServiceAccounts for apps
- Review and rotate credentials regularly
If you want, I can also provide:
- a single combined YAML manifest
- a production-grade setup for frontend/api/db namespaces
- a Calico/Cilium-specific policy example