GKE VPC Flow Logs Preserve Kubernetes Workload Identity for Network Analysis

| 5 min read

Kubernetes flow logs are only useful if they retain workload identity

A network record that says one IP talked to another can be hard to act on in Kubernetes. Pods are created, moved, and removed; their IP addresses change with them. By the time you investigate an entry such as `10.4.2.18` connecting to `10.4.8.44`, the pod behind either address may have disappeared. Google Cloud’s VPC Flow Logs can attach GKE context to sampled network traffic, including the pod, workload, namespace, and service associated with an IP when the flow was recorded. That’s more than a convenience for troubleshooting: it lets you inspect L3/L4 traffic in workload terms without adding agents or sidecars. If you already use CNI or dataplane observability tools, flow logs complement them when you need deeper detail.

What GKE metadata adds

Enable VPC Flow Logs on a subnet hosting a GKE cluster, and records for traffic through the VPC can include the Kubernetes context associated with each IP at sampling time. That context remains available in the record after a pod has gone. The metadata can identify the cluster and its location; the pod, namespace, and owning controller, such as a Deployment, StatefulSet, or DaemonSet; and the Kubernetes service that received the traffic. Standard VPC fields also identify the node VM and zone at both ends of a flow. If you register GKE workloads in [App Hub](https://docs.cloud.google.com/app-hub/docs/overview?e=48754805), records can also include `apphubSource` and `apphubDestination` labels for the business application, environment, and criticality of each endpoint. That can help trace connections from cluster microservices to App Hub resources outside the cluster, including Cloud Run services.

Set up the prerequisites

Flow Analyzer reads from a log bucket upgraded to [Observability Analytics](https://cloud.google.com/network-intelligence-center/docs/flow-analyzer/enable-log-analytics), formerly Log Analytics. The `_Default` bucket is a common choice. The upgrade is permanent. ```sh gcloud logging buckets update _Default \ --location=global \ --enable-analytics ``` You can enable VPC Flow Logs across an organization with one configuration covering its VPC subnets, including subnets used by future clusters. This requires the Network Management Admin role. Set `include-all-metadata` to retain pod and namespace details. To manage cost and volume, you can also set a secondary sampling rate with `--flow-sampling`, an aggregation interval with `--aggregation-interval`, or limit recorded traffic with `--filter-expr`. ```sh gcloud network-management vpc-flow-logs-configs create CONFIG_NAME \ --location=global \ --organization=ORGANIZATION_ID \ --metadata=include-all-metadata ``` Autopilot clusters already have [intranode visibility](https://cloud.google.com/kubernetes-engine/docs/how-to/intranode-visibility) enabled. Standard clusters require an update: ```sh gcloud container clusters update CLUSTER_NAME \ --location=LOCATION \ --enable-intra-node-visibility ``` That change restarts control plane and node components and adds some per-node overhead, so test it on a non-production cluster first.

Use Flow Analyzer to follow the traffic

VPC Flow Logs write records to Cloud Logging, where you can search them in Logs Explorer or Log Analytics. For day-to-day investigation, [Flow Analyzer](https://docs.cloud.google.com/network-intelligence-center/docs/flow-analyzer/overview), part of Network Intelligence Center, presents the records in charts and tables. You can group and filter traffic by cluster, namespace, workload, pod, or service. The examples below use [Online Boutique](https://github.com/GoogleCloudPlatform/microservices-demo), an open-source microservices demo running on a GKE Autopilot cluster in `us-central1`. Its `loadgenerator` deployment sends synthetic shopper traffic to `frontend`. The frontend calls `productcatalogservice`, `cartservice`, `currencyservice`, `recommendationservice`, `shippingservice`, `adservice`, and `checkoutservice`.

Case 1: Replace IP pairs with workload names

When you’re investigating an incident or reviewing an architecture change, start with a basic question: which endpoints communicate, and how much traffic passes between them? Workload names make the answer much easier to read than temporary IP addresses. In Flow Analyzer: 1. Open **Network Intelligence Center > Flow Analyzer** and set the time range to **last 1 hour**. 2. Under **Source** and **Destination**, set **Organize flows by** to **GKE workload**. 3. Add the **GKE workload is known** filter to show pod-to-pod traffic. 4. Click **Run new query**. 1 *Grouping traffic by source and destination GKE workload in Flow Analyzer maps microservice dependencies without raw IP lookups.* Sort the resulting table by volume and it becomes a useful dependency map. In this example, `frontend` receives traffic from `loadgenerator`, representing shopper traffic, while `productcatalogservice` receives calls from both `recommendationservice` and `frontend`. Seeing workload names in each row also makes unexpected workload-to-workload connections easier to spot.

Case 2: Find the callers of a service

If a service is slow or about to change, you’ll want to know who depends on it and how much traffic each caller sends. To inspect `productcatalogservice`: 1. Set **Traffic aggregation** to **Client ↔ server (bidirectional)** so requests and responses between a pair are counted together. 2. Under **Server**, filter **GKE workload** to `productcatalogservice`. 3. Click **Run new query**, then select **Details** on the top row. 2 *Filtering by the `productcatalogservice` server workload and using bidirectional aggregation shows caller shares and a Details timeline.* The results show each caller’s share of traffic and a timeline for each client-server pair. If one caller’s traffic rises while the others remain steady, that gives you a clear place to begin investigating. For latency questions, switch the display mode to latency to rank those callers by round-trip time.

Case 3: Identify traffic crossing zones

In a regional GKE cluster, frequently communicating services placed in different zones send requests across zone boundaries, which can add inter-zone data transfer charges. VPC Flow Logs record the zone at both ends of each flow, so you can identify the pod pairs involved. 1. Switch from **Basic filters** to **SQL filters**, which allow comparisons between fields in the same flow. 2. Enter `src_gcp_zone <> dest_gcp_zone` in the **WHERE** box. 3. Set **Organize flows by** to **Source GCP zone**, **Source GKE pod**, **Destination GCP zone**, and **Destination GKE pod**.

Turn cross zone flows into a routing fix

The useful result here isn’t just a ranked list of network traffic. It’s a way to see which pods are talking across zones—and where a routing change might cut that traffic. In this cluster, the largest flows connect a frontend pod in us-central1-b with a currencyservice pod in us-central1-c. checkoutservice, shippingservice, and productcatalogservice pairs follow.

3
SQL filters using src_gcp_zone <> dest_gcp_zone and groups by multiple attributes to identify the pod pairs responsible for cross zone traffic.

To inspect the results further, open the underlying SQL query with Explore in Log Analytics. You can then customize it with joins or time buckets. For GKE, one practical response is to set trafficDistribution: PreferSameZone on the Service; older GKE versions use PreferClose. That tells the Service to favor healthy endpoints in the caller’s zone. Each zone also needs enough replicas to handle its share of traffic.

Know what the logs can and can’t show

VPC Flow Logs don’t include pod annotations for pod to internet traffic: the masquerade agent replaces the pod IP with the node IP before sampling. Keeping internal destinations in the non masquerade list preserves pod names for those flows. If ip-masq-agent runs with intranode visibility, include the cluster’s node and pod IP ranges in that list to avoid connectivity problems.

Network policy blocks are invisible to VPC Flow Logs because that traffic never reaches the VPC network. Use NetworkPolicy logging to investigate those cases.

Flow logs are sampled, so their byte and packet counts work best for ranking flows and tracking trends. They aren’t a per connection accounting record. Keep that limit in mind when judging the size of an individual flow.

Try the walkthroughs on your cluster

If VPC Flow Logs are already enabled for your clusters, upgrade the _Default bucket and open Flow Analyzer to repeat the three walkthroughs. The documentation covers setup, GKE record fields, traffic analysis, and troubleshooting:

Source: Nishant Kumar · cloud.google.com