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

Kebijakan Jaringan dalam Kontainer

Terapkan kebijakan jaringan di Kubernetes untuk mengendalikan aliran lalu lintas antarpod, sekaligus meningkatkan keamanan dan isolasi.

Kebijakan Jaringan dalam Kontainer adalah pelajaran Linux Networking & TCP/IP for Developers gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Linux Networking & TCP/IP for Developers, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Linux Networking & TCP/IP for Developers mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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!

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Kebijakan Jaringan dalam Kontainer” gratis?

Ya — teks lengkap “Kebijakan Jaringan dalam Kontainer” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Linux Networking & TCP/IP for Developers, upgrade ke CoddyKit PRO. Kursus Linux Networking & TCP/IP for Developers mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Kebijakan Jaringan dalam Kontainer”?

Terapkan kebijakan jaringan di Kubernetes untuk mengendalikan aliran lalu lintas antarpod, sekaligus meningkatkan keamanan dan isolasi. Kamu berlatih Linux Networking & TCP/IP for Developers dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Linux Networking & TCP/IP for Developers?

Tidak diperlukan pengalaman sebelumnya. Linux Networking & TCP/IP for Developers di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.

Berapa lama pelajaran “Kebijakan Jaringan dalam Kontainer” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Linux Networking & TCP/IP for Developers ini?

Ya. Setiap pelajaran Linux Networking & TCP/IP for Developers menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Mode Jaringan Docker
  2. Dasar-Dasar Jaringan Kubernetes
  3. Kebijakan Jaringan dalam Kontainer
  4. Penemuan Layanan dan DNS di Kubernetes
← Kembali ke Linux Networking & TCP/IP for Developers