Network Policies in k8s

Saurabh AdhauSaurabh Adhau
4 min read

Introduction

Network Policies in Kubernetes are crucial for securing and managing network traffic within a cluster. They allow administrators to define rules that control traffic flow to and from pods, providing a way to enforce network segmentation and isolation.

Key Concepts of Network Policies

  1. Network Policy Resource

  2. Selectors

  3. Ingress and Egress Rules

  4. Network Plugins

Network Policy Resource

A Network Policy is a Kubernetes resource that defines how groups of pods are allowed to communicate with each other and other network endpoints. Network Policies use selectors to specify the target pods and rules to control traffic.

Example of a Simple Network Policy

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-frontend
  namespace: default
spec:
  podSelector:
    matchLabels:
      role: frontend
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: backend
    ports:
    - protocol: TCP
      port: 80

In this example:

  • The policy applies to pods with the label role: frontend.

  • It allows inbound traffic from pods with the label role: backend on port 80/TCP.

Selectors

Selectors define which pods the Network Policy applies to. There are two main types of selectors:

  1. Pod Selectors: Used to target specific pods within the same namespace.

  2. Namespace Selectors: Used to target pods in other namespaces.

Example of a Pod Selector

podSelector:
  matchLabels:
    app: my-app

Example of a Namespace Selector

namespaceSelector:
  matchLabels:
    environment: production

Ingress and Egress Rules

Network Policies can define ingress (incoming) and egress (outgoing) traffic rules.

Ingress Rules

Ingress rules control incoming traffic to the selected pods. They can specify the sources of traffic and the allowed ports and protocols.

Example of an Ingress Rule:

ingress:
- from:
  - podSelector:
      matchLabels:
        role: backend
  ports:
  - protocol: TCP
    port: 80

Egress Rules

Egress rules control outgoing traffic from the selected pods. They can specify the destinations of traffic and the allowed ports and protocols.

Example of an Egress Rule:

egress:
- to:
  - ipBlock:
      cidr: 10.0.0.0/24
  ports:
  - protocol: TCP
    port: 53

Network Plugins

Network Policies are implemented by network plugins, such as Calico, Cilium, and Weave. Ensure that the chosen network plugin supports Network Policies and is correctly configured in your cluster.

Use Case 1: Isolating Sensitive Applications

Consider a scenario where you have a database pod that should only be accessible by the backend application pods. You can create a Network Policy to enforce this isolation.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-isolation
  namespace: default
spec:
  podSelector:
    matchLabels:
      role: database
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: backend
    ports:
    - protocol: TCP
      port: 5432

Use Case 2: Restricting Traffic to External Services

You may want to restrict pods in your development environment from accessing external services except for necessary updates. This can be achieved with egress rules.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: restrict-external
  namespace: dev
spec:
  podSelector:
    matchLabels:
      environment: dev
  policyTypes:
  - Egress
  egress:
  - to:
    - ipBlock:
        cidr: 192.168.1.0/24
    ports:
    - protocol: TCP
      port: 443

Use Case 3: Enforcing Zero Trust Networking

Implementing zero trust networking involves ensuring that only explicitly allowed communications are permitted. Here’s an example of enforcing such a policy for frontend and backend services.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: zero-trust
  namespace: default
spec:
  podSelector:
    matchLabels:
      role: frontend
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: backend
    ports:
    - protocol: TCP
      port: 80
  egress:
  - to:
    - podSelector:
        matchLabels:
          role: backend
    ports:
    - protocol: TCP
      port: 80

Benefits of Network Policies

  • Enhanced Security: By defining strict rules for traffic flow, you reduce the attack surface and protect sensitive data.

  • Improved Compliance: Network policies help in meeting regulatory requirements by enforcing network segmentation and access controls.

  • Microservices Management: Simplifies the management of microservices by defining clear communication rules between services.

Conclusion

Network Policies in Kubernetes are a powerful tool for managing and securing network traffic within a cluster. By leveraging selectors, ingress, and egress rules, administrators can enforce fine-grained access controls, ensuring that only authorized traffic flows between pods. This not only enhances the security posture of the cluster but also aids in compliance and effective management of microservices. Whether isolating sensitive applications, restricting access to external services, or implementing zero trust networking, Network Policies provide the necessary framework to achieve these goals.

0
Subscribe to my newsletter

Read articles from Saurabh Adhau directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Adhau
Saurabh Adhau

As a DevOps Engineer, I thrive in the cloud and command a vast arsenal of tools and technologies: ☁️ AWS and Azure Cloud: Where the sky is the limit, I ensure applications soar. 🔨 DevOps Toolbelt: Git, GitHub, GitLab – I master them all for smooth development workflows. 🧱 Infrastructure as Code: Terraform and Ansible sculpt infrastructure like a masterpiece. 🐳 Containerization: With Docker, I package applications for effortless deployment. 🚀 Orchestration: Kubernetes conducts my application symphonies. 🌐 Web Servers: Nginx and Apache, my trusted gatekeepers of the web.