Securing Kubernetes Databases with Network Policies: A Practical Guide

·

3 min read

Introduction

In the world of Kubernetes security, protecting sensitive resources like databases is crucial. This article explores how to use Network Policies to secure database access within a Kubernetes cluster, a common challenge faced by DevOps engineers.

Why Network Policies Matter

Network Policies in Kubernetes serve as firewall rules that control pod-to-pod communication. Here's why they're essential:

  • Security from Internal Threats: Even within your organization's cluster, not all pods should have access to sensitive databases

  • Breach Containment: If a namespace gets compromised, Network Policies prevent lateral movement to sensitive resources

  • Access Control: Ensure only authorized applications can communicate with your databases

Understanding Network Policy Types

Network Policies primarily handle two types of traffic:

  1. Ingress Traffic: Controls inbound connections to pods (focus of this guide)

  2. Egress Traffic: Controls outbound connections from pods (e.g., restricting access to external websites)

Prerequisites for Network Policies

Before implementing Network Policies, ensure:

  • Your Kubernetes distribution supports Network Policies

  • A compatible Container Network Interface (CNI) plugin is installed (e.g., Calico)

  • You're using one of these environments:

    • EKS cluster (version ≥ 1.27)

    • Enterprise Kubernetes distributions (OpenShift, Rancher, Tanzu)

    • Properly configured local setup with necessary network plugins

Practical Implementation

Scenario: Securing a Redis Database

Let's walk through a practical example of securing a Redis database using Network Policies.

  1. First, create the secure namespace:
kubectl create ns secure-namespace
  1. Deploy Redis database:
# db.yaml
apiVersion: v1
kind: Pod
metadata:
  name: redis
  labels:
    app: redis
spec:
  containers:
  - name: redis
    image: redis
    ports:
    - containerPort: 6379
  1. Create a Network Policy:
# network-policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: redis-network-policy
spec:
  podSelector:
    matchLabels:
      app: redis
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: redis-member

Network Policy Configuration Options

Network Policies offer three main ways to control access:

  1. Pod Selector: Control access based on pod labels

  2. Namespace Selector: Restrict access based on namespace

  3. IP Block: Control access based on IP address ranges

Testing the Network Policy

Without the Network Policy:

  • Pods from any namespace can connect to the Redis database

  • This poses a security risk

After applying the Network Policy:

  • Only pods with the specified label (role: redis-member) can connect

  • All other connection attempts are blocked

Best Practices

  1. Default Deny: Start with restrictive policies and gradually allow necessary access

  2. Label Strategy: Develop a clear labeling strategy for your pods

  3. Regular Testing: Regularly verify that your Network Policies are working as intended

  4. Documentation: Maintain clear documentation of your Network Policy configurations

Conclusion

Network Policies are a crucial security feature in Kubernetes that helps protect sensitive resources like databases. By implementing them properly, you can ensure that only authorized pods can access your databases, significantly reducing the risk of unauthorized access and potential security breaches.

Additional Resources