0Pricing
Docker & Kubernetes for Developers · บทเรียน

การนำข้อกำหนดเครือข่ายไปใช้งาน

กำหนดและใช้ข้อกำหนดเครือข่ายเพื่อควบคุมการไหลของทราฟฟิกระหว่างพ็อด เพิ่มความปลอดภัยและการแยกส่วน

การนำข้อกำหนดเครือข่ายไปใช้งาน เป็นบทเรียน Docker & Kubernetes for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Docker & Kubernetes for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Secure Pod Traffic with Policies

In Kubernetes, by default, all pods can communicate with each other. While convenient, this 'flat' network model can be a security risk, especially in multi-service environments.

Network Policies act like firewalls for your pods, allowing you to define rules for how they can communicate both internally and externally.

Why Network Policies Matter

Imagine a backend database pod that only your API service should access. Without Network Policies, any other pod in the cluster could potentially connect to it.

  • Isolation: Prevent unauthorized access between services.
  • Security: Enforce least privilege, allowing only necessary connections.
  • Compliance: Meet security standards by segmenting network traffic.

What is a Network Policy?

A Network Policy is a Kubernetes resource that uses labels to select pods and then defines rules specifying what network traffic is allowed to and from those selected pods.

These policies are enforced by the Container Network Interface (CNI) plugin installed in your cluster (e.g., Calico, Cilium, Weave Net).

Anatomy of a Network Policy

A Network Policy is defined using a YAML manifest, just like other Kubernetes resources. Here's a basic structure:

  • apiVersion and kind: Identifies it as a Network Policy.
  • metadata: Contains its name and optional labels.
  • spec: Where the actual policy rules are defined.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: my-app-policy
spec:
  # Policy rules go here

Selecting Target Pods: podSelector

The spec.podSelector field is crucial. It defines which pods this policy will apply to. It uses label selectors to match pods within the same namespace where the policy is created.

An empty podSelector: {} selects all pods in the namespace.

spec:
  podSelector:
    matchLabels:
      app: my-app
  # ... other policy rules

Controlling Traffic Direction: policyTypes

The spec.policyTypes field specifies whether the policy applies to incoming (Ingress), outgoing (Egress), or both types of traffic for the selected pods.

  • Ingress: Controls traffic coming into the selected pods.
  • Egress: Controls traffic going out of the selected pods.

If omitted, it defaults to Ingress.

spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
    - Ingress
    - Egress

Defining Ingress Rules

The spec.ingress section defines rules for incoming traffic. Each rule specifies allowed sources, which can be:

  • Other pods (using podSelector).
  • Namespaces (using namespaceSelector).
  • IP CIDR blocks (using ipBlock).
  • Specific ports (using ports).

If a pod is selected by a Network Policy with policyTypes: [Ingress], all incoming traffic is denied by default, unless explicitly allowed by an ingress rule.

Defining Egress Rules

Similarly, the spec.egress section defines rules for outgoing traffic from the selected pods. It specifies allowed destinations, using the same selectors as ingress rules (to.podSelector, to.namespaceSelector, to.ipBlock, and ports).

If a pod is selected by a Network Policy with policyTypes: [Egress], all outgoing traffic is denied by default, unless explicitly allowed by an egress rule.

Example: Isolate a Backend Pod

Let's create a policy to isolate a pod labeled app: backend. This policy will deny all incoming traffic to it by default.

By just specifying policyTypes: [Ingress] and an empty ingress: [] array, we explicitly deny all incoming connections.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: backend-deny-all-ingress
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
    - Ingress
  ingress: [] # Denies all incoming traffic

Example: Allow Specific Ingress

Now, let's refine the previous policy. We still want to deny all ingress to our app: backend pod, but we'll add a rule to allow incoming traffic specifically from pods labeled app: frontend.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: backend-allow-frontend
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
      ports:
        - protocol: TCP
          port: 8080 # Assuming backend listens on 8080

Policy Check

You have an application pod labeled app: data-processor. You need to ensure it can only initiate connections to an external logging service at 192.168.1.10/32 on port 514, and no other outgoing traffic is allowed. Which key parts of a Network Policy would you configure?

Recap: Network Policies

You've learned how Kubernetes Network Policies provide crucial security and isolation for your applications!

  • Network Policies act as internal firewalls for pods.
  • They use podSelector to target specific pods.
  • policyTypes defines if rules apply to Ingress, Egress, or both.
  • ingress and egress sections define the actual allow rules, implicitly denying everything else.

By implementing Network Policies, you can significantly enhance the security posture of your Kubernetes applications.

คำถามที่พบบ่อย

บทเรียน “การนำข้อกำหนดเครือข่ายไปใช้งาน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การนำข้อกำหนดเครือข่ายไปใช้งาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Docker & Kubernetes for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การนำข้อกำหนดเครือข่ายไปใช้งาน”

กำหนดและใช้ข้อกำหนดเครือข่ายเพื่อควบคุมการไหลของทราฟฟิกระหว่างพ็อด เพิ่มความปลอดภัยและการแยกส่วน คุณปฏิบัติ Docker & Kubernetes for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Docker & Kubernetes for Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Docker & Kubernetes for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การนำข้อกำหนดเครือข่ายไปใช้งาน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Docker & Kubernetes for Developers นี้ได้ไหม

ได้ บทเรียน Docker & Kubernetes for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Kubernetes Ingress และการกำหนดเส้นทาง
  2. การนำข้อกำหนดเครือข่ายไปใช้งาน
  3. การค้นหาบริการและ DNS ใน K8s
  4. การสิ้นสุด TLS และการรักษาความปลอดภัยให้ Ingress ด้วย HTTPS
← กลับไปที่ Docker & Kubernetes for Developers