Course contentsShow
← Cloud and Infrastructure
Lesson 1241 of 2,98924. Kubernetes NetworkingFree lesson

IP Block Rules

Use ipBlock to allow or deny traffic to/from specific CIDR ranges and external endpoints.

IP Block Rules

What you'll learn: How to use ipBlock in Kubernetes NetworkPolicies to control traffic to and from specific IP address ranges and external endpoints.

Why IP Block Rules Matter

So far you've learned to control traffic between pods using pod and namespace selectors. But what about traffic to external services, third-party APIs, or on-premise systems? IP Block Rules let you specify CIDR ranges (IP address blocks) to allow or deny traffic beyond your cluster.

Understanding ipBlock

Think of ipBlock as a firewall rule that works with IP addresses instead of labels. You define a CIDR range (like 192.168.1.0/24) to match traffic, and optionally exclude specific IPs within that range using except.

Here's the structure within ingress or egress rules:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-external-api
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
  - Egress
  egress:
  - to:
    - ipBlock:
        cidr: 203.0.113.0/24
        except:
        - 203.0.113.5/32

This example allows backend pods to reach any IP in 203.0.113.0/24 except 203.0.113.5.

Common Use Cases

  • Allowing traffic to external APIs: Permit egress to specific cloud service IP ranges
  • Blocking certain public IPs: Deny access to known malicious addresses
  • On-premise integration: Allow traffic to corporate network CIDR blocks
  • Ingress filtering: Restrict which external IPs can reach your services

How It Works

The ipBlock field matches against the source IP (for ingress) or destination IP (for egress). Kubernetes uses CIDR notation, so you can be broad (0.0.0.0/0 for all IPs) or precise (10.0.1.5/32 for one IP).

Key Takeaway: Use ipBlock with CIDR notation to allow or deny NetworkPolicy traffic based on IP addresses, enabling control over external and non-pod endpoints.