Sobes.tech
Junior — Middle

What functions does NetworkPolicy perform in Kubernetes for managing network traffic?

sobes.tech AI

Answer from AI

NetworkPolicy in Kubernetes is used to manage network traffic between pods and other network entities within the cluster. Main functions:

  • Control of incoming and outgoing traffic: allows defining which pods or IP addresses can send or receive traffic.
  • Isolation of network resources: helps restrict access between pods to enhance security.
  • Filtering by labels and namespaces: rules apply to pods with specific labels or in certain namespaces.
  • Support for various protocols and ports: traffic can be restricted by protocols (TCP, UDP) and ports.

Example of a simple NetworkPolicy that allows incoming traffic only from pods with the label app=frontend:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend
spec:
  podSelector:
    matchLabels:
      role: backend
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend