New to Claude Skills? Learn how to install them →

mukul975 on GitHub

Implementing Network Policies for Kubernetes

Free

Enforce security with Kubernetes NetworkPolicies.

Get this skill

Free · Opens the source repo

What Implementing Network Policies for Kubernetes does

Implementing Network Policies for Kubernetes is a skill designed for developers and security engineers who need to enforce network segmentation within Kubernetes clusters. By utilizing Kubernetes NetworkPolicies, this skill allows users to define ingress and egress rules that control the flow of traffic between pods, namespaces, and external endpoints. It is particularly useful when deploying a zero-trust architecture, as it helps to prevent lateral movement of workloads, enhancing overall security posture.

The skill provides a series of YAML templates that can be applied to Kubernetes clusters, ensuring that only the necessary traffic is allowed between applications. The default-deny-all policy serves as a foundational security measure, blocking all traffic unless explicitly allowed. Additional policies can be created to permit DNS egress for service discovery, allow specific applications to communicate, and restrict egress to external services. This granularity in control is essential for maintaining a secure environment in cloud-native applications.

Users will benefit from the clear, step-by-step workflows provided in the skill, which guide them through the implementation of various network policies. These workflows cover common scenarios such as allowing frontend-backend communication, restricting access to cloud metadata, and setting up cross-namespace policies for monitoring tools. The skill is ideal for those looking to enhance their Kubernetes security architecture or conduct security assessments that require robust network policy implementations.

To use this skill effectively, users should have a Kubernetes cluster configured with a NetworkPolicy-supporting CNI plugin, such as Calico or Cilium, and a basic understanding of Kubernetes pod labels and selectors. This ensures that they can tailor the network policies to their specific application needs and compliance requirements.

When to use it

Use this skill when deploying Kubernetes applications that require strict network segmentation and security controls.

When not to use it

This skill may not be suitable for environments without a Kubernetes cluster or where CNI plugins like Calico or Cilium are not supported.

What you can build with it

Deploying a New Application

When deploying a new application in a Kubernetes cluster, use this skill to set up network policies that enforce strict traffic controls from the start.

Enhancing Security Posture

If your organization is moving towards a zero-trust architecture, implement this skill to create network policies that prevent lateral movement within the cluster.

Conducting Security Assessments

Use this skill during security assessments to ensure that network policies are correctly implemented and that compliance requirements are met.

How to install Implementing Network Policies for Kubernetes

View source

1. Install with the skills CLI

npx skills add mukul975/anthropic-cybersecurity-skills/implementing-network-policies-for-kubernetes --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by mukul975

Implementing Network Policies for Kubernetes

Overview

Kubernetes NetworkPolicies provide pod-level network segmentation by defining ingress and egress rules that control traffic flow between pods, namespaces, and external endpoints. Combined with CNI plugins like Calico or Cilium, network policies enforce zero-trust microsegmentation to prevent lateral movement within the cluster.

When to Use

  • When deploying or configuring implementing network policies for kubernetes capabilities in your environment
  • When establishing security controls aligned to compliance requirements
  • When building or improving security architecture for this domain
  • When conducting security assessments that require this implementation

Prerequisites

  • Kubernetes cluster with NetworkPolicy-supporting CNI (Calico, Cilium, Antrea)
  • kubectl configured with admin access
  • Understanding of pod labels and selectors

Workflow

Step 1: Default Deny All Traffic

# default-deny-all.yaml - Apply to every namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: production
spec:
  podSelector: {}  # Applies to all pods
  policyTypes:
    - Ingress
    - Egress

Step 2: Allow DNS Egress (Required for Service Discovery)

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: production
spec:
  podSelector: {}
  policyTypes:
    - Egress
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: kube-system
      ports:
        - protocol: UDP
          port: 53
        - protocol: TCP
          port: 53

Step 3: Application-Specific Policies

# Allow frontend to reach backend only
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: backend-allow-frontend
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: frontend
      ports:
        - protocol: TCP
          port: 8080
---
# Allow backend to reach database only
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: database-allow-backend
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: database
  policyTypes:
    - Ingress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: backend
      ports:
        - protocol: TCP
          port: 5432

Step 4: Cross-Namespace Policies

# Allow monitoring namespace to scrape metrics
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-monitoring-scrape
  namespace: production
spec:
  podSelector: {}
  policyTypes:
    - Ingress
  ingress:
    - from:
        - namespaceSelector:
            matchLabels:
              purpose: monitoring
      ports:
        - protocol: TCP
          port: 9090  # Prometheus metrics port

Step 5: Egress Restrictions

# Restrict egress to specific external services
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: restrict-egress
  namespace: production
spec:
  podSelector:
    matchLabels:
      app: backend
  policyTypes:
    - Egress
  egress:
    - to:
        - podSelector:
            matchLabels:
              app: database
      ports:
        - protocol: TCP
          port: 5432
    - to:  # Allow external API
        - ipBlock:
            cidr: 203.0.113.0/24
      ports:
        - protocol: TCP
          port: 443
    - to:  # DNS
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: kube-system
      ports:
        - protocol: UDP
          port: 53

Step 6: Block Cloud Metadata Access

# Prevent SSRF to cloud metadata service
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: block-metadata
  namespace: production
spec:
  podSelector: {}
  policyTypes:
    - Egress
  egress:
    - to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except:
              - 169.254.169.254/32  # AWS/GCP metadata
              - 100.100.100.200/32  # Azure metadata

Validation Commands

# Verify policies are applied
kubectl get networkpolicies -n production

# Test connectivity (should be blocked)
kubectl run test-pod --image=busybox --restart=Never -n production -- wget -qO- --timeout=2 http://database-service:5432
# Expected: timeout (blocked by policy)

# Test allowed traffic
kubectl run frontend-test --image=busybox --labels=app=frontend --restart=Never -n production -- wget -qO- --timeout=2 http://backend-service:8080
# Expected: connection succeeds

References

Frequently asked questions about Implementing Network Policies for Kubernetes

Similar skills