متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 128Kubernetes · Network

How to write a Kubernetes NetworkPolicy

By default every Pod in a cluster can talk to every other Pod — a hacked frontend can reach the database of another team directly. NetworkPolicies are a firewall for Pods: you deny all in a namespace, then allow only the connections the app needs (frontend → api → database, plus DNS). This how-to writes that set of policies and tests them.

  • 5 الدروس
  • 1 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

128.1

The quick answer

netpol/default-deny.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: shop
spec:
  podSelector: {}
  policyTypes: [Ingress, Egress]
kubectl apply -f netpol/
kubectl get networkpolicy -n shop

Then add allow policies (step 3). Important: your CNI must support NetworkPolicies (Calico, Cilium — yes; plain Flannel — no).

الأوامر في هذا الدرس

128.2

Step by step

  1. Deny all in and out (quick answer). From now on nothing works in shop — so apply it together with the allow rules.
  2. Allow DNS for all Pods (otherwise names do not resolve):
netpol/allow-dns.yaml
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 }
  1. Allow the app's own paths — api may reach the database on 5432:
netpol/api-to-db.yaml
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).

  1. Allow the ingress controller to reach the frontend (namespaceSelector for the ingress namespace), and any outside APIs the app calls (ipBlock).
  2. Labels are everything — policies select Pods by labels; check them:
kubectl get pods -n shop --show-labels

الأوامر في هذا الدرس

128.3

Check 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 out

The api reaches the database, the frontend does not.

الأوامر في هذا الدرس

128.4

Common problems

ProblemCauseFix
Policies have no effect at allThe CNI does not enforce them.Use Calico/Cilium (or your cloud's policy option).
Everything broke after deny-allDNS and app paths not allowed yet.Apply allow-dns and the app policies together with deny-all.
Names do not resolveDNS egress missing.The allow-dns policy (UDP and TCP 53).
A rule does not matchWrong labels, or namespaceSelector and podSelector combined wrongly.One - from: item with both = AND; two items = OR. Check labels.
Health checks failThe kubelet/ingress controller can not reach the Pods.Allow the ingress controller namespace; node traffic depends on the CNI.
Pods can still reach the internetNo egress policy on them.Deny-all must include Egress.

128.5

Go further

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).

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل