هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
namespace: shop
spec:
podSelector: {}
policyTypes: [Ingress, Egress]Then add allow policies (step 3). Important: your CNI must support NetworkPolicies (Calico, Cilium — yes; plain Flannel — no).
Step by step
- Deny all in and out (quick answer). From now on nothing works in
shop— so apply it together with the allow rules. - Allow DNS for all Pods (otherwise names do not resolve):
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: shop
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 }- Allow the app's own paths — api may reach the database on 5432:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-from-api
namespace: shop
spec:
podSelector:
matchLabels: { app: postgres }
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchLabels: { app: api }
ports:
- { protocol: TCP, port: 5432 }
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-to-db
namespace: shop
spec:
podSelector:
matchLabels: { app: api }
policyTypes: [Egress]
egress:
- to:
- podSelector:
matchLabels: { app: postgres }
ports:
- { protocol: TCP, port: 5432 }With deny-all for both directions you need two sides: the database accepts from api (Ingress), and the api may send to the database (Egress).
- Allow the ingress controller to reach the frontend (
namespaceSelectorfor the ingress namespace), and any outside APIs the app calls (ipBlock). - Labels are everything — policies select Pods by labels; check them:
kubectl get pods -n shop --show-labelsCheck that it worked
Test allowed and blocked paths from inside:
kubectl exec deploy/api -n shop -- nc -zv -w 3 postgres 5432
postgres (10.244.1.23:5432) open
kubectl exec deploy/frontend -n shop -- nc -zv -w 3 postgres 5432
nc: postgres (10.244.1.23:5432): Operation timed outThe api reaches the database, the frontend does not.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Policies have no effect at all | The CNI does not enforce them. | Use Calico/Cilium (or your cloud's policy option). |
| Everything broke after deny-all | DNS and app paths not allowed yet. | Apply allow-dns and the app policies together with deny-all. |
| Names do not resolve | DNS egress missing. | The allow-dns policy (UDP and TCP 53). |
| A rule does not match | Wrong labels, or namespaceSelector and podSelector combined wrongly. | One - from: item with both = AND; two items = OR. Check labels. |
| Health checks fail | The kubelet/ingress controller can not reach the Pods. | Allow the ingress controller namespace; node traffic depends on the CNI. |
| Pods can still reach the internet | No egress policy on them. | Deny-all must include Egress. |
Go further
- Command pages: kubectl.
- Next how-tos: Give a user read-only access with RBAC, Apply Pod Security Standards, Open a shell in a Pod to test the network, Debug DNS inside Kubernetes.
- Learn it: CKA — NetworkPolicy basics, CKA — Default deny, CKA — Allow rules, CKA — Test policies, CKS — NetworkPolicies.
- Practise: Lab: services, DNS and a default-deny NetworkPolicy.
1Why must you allow DNS after a default-deny egress policy?
Otherwise Pods can not resolve service names, and nothing works.
2In a from: list, what is the difference between one item with namespaceSelector and podSelector, and two separate items?
One item = both must match (AND); two items = either may match (OR).