0Pricing
Linux Networking & TCP/IP for Developers · Lektion

Netzwerkrichtlinien in Containern

Implementieren Sie Netzwerkrichtlinien in Kubernetes, um den Datenverkehr zwischen Pods zu steuern und Sicherheit sowie Isolation zu erhöhen.

Netzwerkrichtlinien in Containern ist eine kostenlose Linux Networking & TCP/IP for Developers-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Linux Networking & TCP/IP for Developers-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Linux Networking & TCP/IP for Developers-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What are Network Policies?

In Kubernetes, Network Policies are like firewalls for your pods. They control how groups of pods communicate with each other and with external network endpoints.

Think of them as a security layer that defines which connections are allowed or denied, enhancing isolation and security.

Why Use Network Policies?

Without Network Policies, all pods in a Kubernetes cluster can communicate with each other by default. This can be a significant security risk!

  • Isolation: Prevent unauthorized access between different application tiers (e.g., frontend talking directly to a sensitive database).
  • Security: Reduce the attack surface by only allowing necessary connections.
  • Compliance: Help meet regulatory requirements for network segmentation.

How Network Policies Function

Network Policies work by selecting specific pods and then defining rules for allowed inbound (ingress) and outbound (egress) traffic for those pods.

They are enforced by the cluster's Container Network Interface (CNI) plugin (like Calico or Cilium). If no policy selects a pod, all traffic to/from it is allowed by default.

Policy Structure: The Basics

A Network Policy is a Kubernetes resource defined in YAML. It specifies:

  • podSelector: Which pods the policy applies to.
  • policyTypes: Whether the policy affects ingress, egress, or both.
  • ingress/egress rules: The specific allowed connections.

Here's the basic YAML structure:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: my-policy
  namespace: default
spec:
  # ... rules go here ...

Targeting Pods with Selectors

The podSelector is crucial. It uses labels to identify the pods that this policy will apply to.

If podSelector is empty {}, the policy applies to ALL pods in its namespace. If omitted, the policy applies to no pods.

The policyTypes field specifies if the policy governs `Ingress`, `Egress`, or both. This helps the network plugin know which traffic directions to enforce.

spec:
  podSelector:
    matchLabels:
      app: my-app
      tier: backend
  policyTypes:
    - Ingress
    - Egress

Controlling Inbound Traffic (Ingress)

Ingress rules define what traffic is allowed to enter the selected pods. If an ingress rule is present, only traffic matching that rule is permitted; all other ingress traffic is denied.

You can specify allowed traffic based on:

  • from: The source of the traffic (e.g., IP block, namespace, or other pods).
  • ports: Specific destination ports on the selected pods.
ingress:
  - from:
      - podSelector:
          matchLabels:
            app: frontend
    ports:
      - protocol: TCP
        port: 80

Controlling Outbound Traffic (Egress)

Egress rules define what traffic is allowed to leave the selected pods. Similar to ingress, if an egress rule is present, only traffic matching that rule is permitted; all other egress traffic is denied.

You can specify allowed traffic based on:

  • to: The destination of the traffic (e.g., IP block, namespace, or other pods).
  • ports: Specific source ports on the selected pods.
egress:
  - to:
      - ipBlock:
          cidr: 10.0.0.0/24
    ports:
      - protocol: TCP
        port: 5432

Example: Default Deny Ingress

A common security practice is to implement a "default deny" policy. This means no traffic is allowed to a pod unless explicitly permitted.

To achieve this for ingress traffic, create a policy that selects the desired pods but has an empty ingress rule list. This explicitly denies all incoming traffic.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all-ingress
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: sensitive-app
  policyTypes:
    - Ingress
  ingress: [] # An empty list means no ingress is allowed

Example: Allow Specific Ingress

After a default deny, you can add more specific policies to allow necessary traffic. Here, we allow backend pods to receive traffic on port 80 from frontend pods.

This policy targets pods with app: backend and permits ingress from pods labeled app: frontend on TCP port 80.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend-to-backend
  namespace: default
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
      ports:
        - protocol: TCP
          port: 80

Policy Check

Consider a pod with label app: database. You want to block all incoming traffic to it, except from pods with label app: backend.

Recap: Network Policies

You've learned about Kubernetes Network Policies!

  • They provide firewall-like rules for pods.
  • They use podSelector to target pods and define ingress/egress rules.
  • By default, all pods can communicate; policies enforce restrictions.
  • They are essential for securing and isolating containerized applications.

Keep practicing with different policy rules to master container security!

Häufig gestellte Fragen

Ist die Lektion „Netzwerkrichtlinien in Containern“ kostenlos?

Ja — der vollständige Text von „Netzwerkrichtlinien in Containern“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Linux Networking & TCP/IP for Developers-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Linux Networking & TCP/IP for Developers-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Netzwerkrichtlinien in Containern“?

Implementieren Sie Netzwerkrichtlinien in Kubernetes, um den Datenverkehr zwischen Pods zu steuern und Sicherheit sowie Isolation zu erhöhen. Du übst Linux Networking & TCP/IP for Developers mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Linux Networking & TCP/IP for Developers zu starten?

Keine Vorkenntnisse erforderlich. Linux Networking & TCP/IP for Developers auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Netzwerkrichtlinien in Containern“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Linux Networking & TCP/IP for Developers-Lektion Code schreiben und ausführen?

Ja. Jede Linux Networking & TCP/IP for Developers-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Docker-Netzwerkmodi
  2. Grundlagen der Kubernetes-Netzwerke
  3. Netzwerkrichtlinien in Containern
  4. Service Discovery und DNS in Kubernetes
← Zurück zu Linux Networking & TCP/IP for Developers