0Pricing
Docker & Kubernetes for Developers · 강의

네트워크 정책 구현

보안과 격리를 강화할 수 있도록 네트워크 정책을 정의하고 적용하여 파드 간 트래픽 흐름을 제어합니다.

네트워크 정책 구현은(는) CoddyKit의 무료 Docker & Kubernetes for Developers 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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.

자주 묻는 질문

“네트워크 정책 구현” 강의는 무료인가요?

네 — “네트워크 정책 구현” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Docker & Kubernetes for Developers 강의 전체를 잠금 해제할 수 있습니다. Docker & Kubernetes for Developers 강의에는 총 4개의 강의가 포함되어 있습니다.

“네트워크 정책 구현”에서 뭘 배우나요?

보안과 격리를 강화할 수 있도록 네트워크 정책을 정의하고 적용하여 파드 간 트래픽 흐름을 제어합니다. 브라우저에서 직접 실행하는 실습 코드로 Docker & Kubernetes for Developers을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Docker & Kubernetes for Developers을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Docker & Kubernetes for Developers은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“네트워크 정책 구현” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Docker & Kubernetes for Developers 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Docker & Kubernetes for Developers 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Kubernetes Ingress 및 라우팅
  2. 네트워크 정책 구현
  3. K8s 서비스 검색 및 DNS
  4. TLS 종료 및 HTTPS로 Ingress 보호하기
← Docker & Kubernetes for Developers(으)로 돌아가기