Skip to main content
In this tutorial, you’ll learn how to use Kubernetes Network Policies to control traffic between pods and services on Google Kubernetes Engine (GKE). We’ll walk through creating a GKE cluster with network policy support, deploying a sample application, and crafting both ingress and egress policies to enforce communication rules.
Ensure you have the Google Cloud SDK installed and authenticated before proceeding.

Table of Contents

  1. Prerequisites
  2. Create a GKE Cluster
  3. Deploy the Hello World App
  4. Define NetworkPolicy Manifests
  5. Launch Test Pods
  6. Verify Connectivity (Pre-Policy)
  7. Apply Ingress Policy
  8. Apply Egress Policy
  9. Cleanup
  10. References

1. Prerequisites

  • gcloud CLI installed and configured
  • A GKE cluster with NetworkPolicy enabled
  • kubectl configured to talk to your GKE cluster

2. Create a GKE Cluster

  1. Set your compute zone:
  2. Create a cluster named gke-deep-dive with NetworkPolicy enabled:
  3. Verify the cluster status:
    You should see output similar to:

3. Deploy the Hello World App

Deploy a sample “Hello World” service (app=web-hello-world) on port 8080:
Confirm the pod and service are up:
Expected service entry:

4. Define NetworkPolicy Manifests

We will create two YAML files:

4.1 Ingress Policy (ingress.yaml)

This policy ensures only pods labeled frontend can reach the web-hello-world service.

4.2 Egress Policy (egress.yaml)

With this egress policy, frontend pods can:
  • Connect to web-hello-world on port 8080
  • Perform DNS lookups over port 53 (TCP/UDP)

5. Launch Test Pods

Create two interactive test pods for validating connectivity: Frontend Pod
Backend Pod

6. Verify Connectivity (Pre-Policy)

From both pods, run:
Expected result: both succeed, as no policies are enforced yet.

7. Apply Ingress Policy

  • In backend-pod, curl http://web-hello-world:8080 should now fail or timeout.
  • In frontend-pod, curl http://web-hello-world:8080 should still succeed.

8. Apply Egress Policy

  1. Exit and recreate frontend-pod to pick up new policies.
  2. Apply egress policy:
  • frontend-pod should still resolve DNS and reach the Hello World service.
  • curl http://google.com works (DNS allowed).

8.1 Blocking DNS

To simulate a DNS block, remove the port-53 entries from egress.yaml under egress: → second item, then reapply:
  • curl http://web-hello-world:8080 by hostname now fails.
  • curl http://<CLUSTER-IP>:8080 still succeeds.
  • External lookups like curl http://google.com fail due to DNS being blocked.

9. Cleanup

This action deletes your entire GKE cluster and cannot be undone.

10. References

Watch Video