How to check pod VPC and subnet

Hi All - I have pod running on my GKE cluster under nodes - Is there a command to find which VPC & Subnets the pods belongs to? instead of manual way?

Could you clarify on what you think the manual way is and a bit more about what you’re trying to achieve?

The answer is going to vary based on your goal due to how everything is supposed to be abstracted.

If you’re trying to control the internal network of your resources, you should be using Network Policies.

If you’re trying to just figure out the external IP for a service, the CCM from your cloud provider would handle the assignment and you can see if via kubectl get services.

If you’re just trying to reference a resource in your k8s yaml, you should be able to use the DNS services. However something to note about this, my experience with using DNS in GKE has been different from using DNS at Digital Ocean and Linode. DNS in your YAML will not resolve at Digital Ocean and Linode because they didn’t bother to setup DNS on the control plane for their managed clusters. By contrast, GKE just works.

Hi - Thanks for the response. My goal here is, suppose when I do “kubectl get pods -o wide” I get the pod IP address and to which nodepool it belongs to. In addition to that, is there a way where I can identify its corresponding VPC & subnet?

When I say manual way which means, to know pods corresponding VPC and Subnet - I am getting into respective network project → VPN networks → Subnets and then comparing the ip ranges of subnets vs the pods IP address that I am looking for and matching it…

Instead of above, is there an way I can find pods vpc and subnet by a command or a script?

The Cluster Networking documentation is probably worth a read along with the GKE Network Overview. The GKE documentation explicitly calls out that you shouldn’t rely on Pod IPs, because they’re ephemeral.

I didn’t find any GCP command but I hope this will help

#!/bin/bash

# Check if pod name is provided
if [ -z "$1" ]; then
  echo "Usage: $0 <pod-name>"
  exit 1
fi

POD_NAME=$1

# Get the node name where the pod is running
NODE_NAME=$(kubectl get pod $POD_NAME -o jsonpath='{.spec.nodeName}')

# Check if the pod exists
if [ -z "$NODE_NAME" ]; then
  echo "Pod $POD_NAME not found."
  exit 1
fi

# Get the zone of the node
NODE_ZONE=$(kubectl get node $NODE_NAME -o jsonpath='{.metadata.labels."topology\.kubernetes\.io/zone"}')

# Get the VPC and subnet information using gcloud
gcloud compute instances describe $NODE_NAME --zone=$NODE_ZONE --format="value(networkInterfaces.network,networkInterfaces.subnetwork)"

./get_pod_vpc_subnet.sh my-pod