Ensure you have the Google Cloud SDK installed and authenticated before proceeding.
Table of Contents
- Prerequisites
- Create a GKE Cluster
- Deploy the Hello World App
- Define NetworkPolicy Manifests
- Launch Test Pods
- Verify Connectivity (Pre-Policy)
- Apply Ingress Policy
- Apply Egress Policy
- Cleanup
- References
1. Prerequisites
gcloudCLI installed and configured- A GKE cluster with NetworkPolicy enabled
kubectlconfigured to talk to your GKE cluster
2. Create a GKE Cluster
-
Set your compute zone:
-
Create a cluster named
gke-deep-divewith NetworkPolicy enabled: -
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:
4. Define NetworkPolicy Manifests
We will create two YAML files:| Policy File | Purpose |
|---|---|
ingress.yaml | Allow only selected pods to send traffic to the app |
egress.yaml | Restrict outbound traffic from selected pods |
4.1 Ingress Policy (ingress.yaml)
frontend can reach the web-hello-world service.
4.2 Egress Policy (egress.yaml)
frontend pods can:
- Connect to
web-hello-worldon port 8080 - Perform DNS lookups over port 53 (TCP/UDP)
5. Launch Test Pods
Create two interactive test pods for validating connectivity: Frontend Pod6. Verify Connectivity (Pre-Policy)
From both pods, run:7. Apply Ingress Policy
- In backend-pod,
curl http://web-hello-world:8080should now fail or timeout. - In frontend-pod,
curl http://web-hello-world:8080should still succeed.
8. Apply Egress Policy
- Exit and recreate frontend-pod to pick up new policies.
-
Apply egress policy:
- frontend-pod should still resolve DNS and reach the Hello World service.
curl http://google.comworks (DNS allowed).
8.1 Blocking DNS
To simulate a DNS block, remove the port-53 entries fromegress.yaml under egress: → second item, then reapply:
curl http://web-hello-world:8080by hostname now fails.curl http://<CLUSTER-IP>:8080still succeeds.- External lookups like
curl http://google.comfail due to DNS being blocked.
9. Cleanup
This action deletes your entire GKE cluster and cannot be undone.