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

Port and Protocol Specifications

Specify allowed ports and protocols (TCP, UDP, SCTP) in NetworkPolicy rules for fine-grained control.

Port and Protocol Specifications

What you'll learn: How to restrict traffic by specific ports and protocols in Kubernetes NetworkPolicies for precise network control.


Why Ports and Protocols Matter

So far, you've learned to control which Pods can communicate using label selectors, namespaces, and IP blocks. But what if you want to allow traffic only on specific ports or using certain protocols? That's where port and protocol specifications come in.

Think of it like apartment building security: you might allow a delivery person (identified by their uniform) to enter, but only through the loading dock door (port 80) and only during business hours. You're not just controlling who enters, but where and how.

Core Concept

Within your NetworkPolicy's ingress or egress rules, you can add a ports section that specifies:

  • Port number: The exact port (like 80 for HTTP, 443 for HTTPS, or 3306 for MySQL)
  • Protocol: The transport protocol — TCP, UDP, or SCTP

When you define these, Kubernetes only allows traffic matching both the port and protocol. If you omit the ports section entirely, all ports are allowed for that rule.

How It Works

The ports field is an array, so you can specify multiple port/protocol combinations in a single rule. Each entry can include:

  • protocol: Defaults to TCP if not specified
  • port: The port number or name (matching the container's port name)
  • endPort: (Optional) Creates a port range

You combine these port specifications with your existing pod selectors, namespace selectors, or IP blocks to create very granular rules like "allow TCP traffic on port 443 from Pods labeled app=frontend in the production namespace."

Key Takeaway:
Port and protocol specifications in NetworkPolicy rules let you control not just who can connect, but what services they can access, creating fine-grained network security boundaries.