Stay organized with collections Save and categorize content based on your preferences.
Edit on GitHub
Report issue
Page history

Transparent proxy and filtering on Kubernetes

Author(s): @danisla ,   Published: 2017-09-23

Dan Isla | Solution Architect | Google

Contributed by Google employees.

There are many application environments and scenarios where you may want to filter and intercept all HTTP and HTTPS traffic out of a pod. Example use cases include isolating external access to specific HTTP or HTTPS paths and methods and in-flight altering of requests.

An example of HTTPS interception is filtering access to Cloud Storage buckets. Most proxy solutions do not support HTTPS inspection, so the filtering that can be done is limited at the IP/DNS level. For the case of a Cloud Storage bucket, the bucket name is embedded under the URL, so filtering is limited to all buckets under the domain, not any specific bucket.

You can transparently add request and response headers to requests between services for tracing purposes without the services having to explicitly set them.

Mitmproxy is an open-source tool that you can use to intercept and modify HTTP and HTTPS requests transparently using the Python scripting language.

This tutorial uses the tproxy-sidecar container to create firewall rules in the pod network to block egress traffic out of selected pods. The tproxy-podwatch controller watches for pod changes containing the "": "true" annotation and automatically add and removes the local firewall redirect rules to apply the transparent proxy to the pod.

architecture diagram

Figure 1. transparent proxy architecture diagram


  • Create a Kubernetes cluster with Google Kubernetes Engine.
  • Deploy the tproxy and the tproxy-podwatch pods using Helm.
  • Deploy example apps to test external access to a Cloud Storage bucket.

Before you begin

This tutorial assumes you already have a Google Cloud account and are familiar with the high-level concepts of Kubernetes Pods and Deployments.


This tutorial uses billable components of Google Cloud, including Google Kubernetes Engine.

Use the pricing calculator to estimate the costs for your environment.

Clone the source repository

  1. Open Cloud Shell.

  2. Clone the repository containing the code for this tutorial:

    git clone
    cd kubernetes-tproxy

    The remainder of this tutorial is run from the root of the cloned repository directory.

Create Kubernetes Engine cluster and install Helm

  1. Create the Kubernetes Engine cluster:

    gcloud container clusters create tproxy-example --zone us-central1-f

    This command also automatically configures the kubectl command to use the cluster.

  2. Install Helm in your Cloud Shell instance:

    curl -sL | tar -xvf - && sudo mv linux-amd64/helm /usr/local/bin/ && rm -Rf linux-amd64
  3. Initialize Helm:

    helm init

    This installs Tiller—which is the server-side component of Helm—in the Kubernetes cluster. The Tiller pod may take a minute to start.

  4. Verify that the client and server components have been deployed:

    helm version

    You should see the Client and Server versions in the output.

Install the Helm chart

Before installing the chart, you must first extract the certificates generated by mitmproxy. The generated CA certificate is used in the example pods to trust the proxy when making HTTPS requests.

  1. Extract the generated certificates using Docker:

    cd charts/tproxy
    docker run --rm -v ${PWD}/certs/:/home/mitmproxy/.mitmproxy mitmproxy/mitmproxy >/dev/null 2>&1
  2. Install the chart:

    helm install -n tproxy .

    The output of this command shows you how to augment your deployments to use the init container and trusted certificate configmap volume. Example output below:

    Add the init container spec below to your deployments:
        - name: tproxy
            imagePullPolicy: IfNotPresent
            privileged: true
                cpu: 500m
                memory: 128Mi
                cpu: 100m
                memory: 64Mi
    Add the volumes below to your deployments to use the trusted https tproxy:
        - name: ca-certs-debian
            name: tproxy-tproxy-root-certs
            - key: root-certs.crt
                path: ca-certificates.crt
  3. Get the status of the DaemonSet pods:

    kubectl get pods -o wide

    Notice in the example output below that there is a tproxy pod for each node:

    NAME                     READY     STATUS    RESTARTS   AGE       IP           NODE
    tproxy-tproxy-2h7lk   2/2       Running   0          21s   gke-tproxy-example-default-pool-1e70b38d-xchn
    tproxy-tproxy-4mvtf   2/2       Running   0          21s   gke-tproxy-example-default-pool-1e70b38d-hk89
    tproxy-tproxy-ljfq9   2/2       Running   0          21s   gke-tproxy-example-default-pool-1e70b38d-jsqd

The tproxy chart is now installed and ready to be used by pods with the init container and pod annotation.

Deploy example apps

Deploy the sample apps to demonstrate using and not using the init container to lock down external access from the pod.

  1. Change directories back to the repository root and deploy the example apps:

    cd ../../
    kubectl create -f examples/debian-app.yaml
    kubectl create -f examples/debian-app-locked-manual.yaml

    Note that the second deployment is the one that contains the init container and trusted certificate volume mount described in the chart post-installation notes.

  2. Get the logs for the pod without the tproxy annotation:

    kubectl logs --selector=app=debian-app,variant=unlocked --tail=10

    Example output: 200 200
    PING ( 56 data bytes
    64 bytes from icmp_seq=0 ttl=52 time=0.758 ms

    The output from the example app shows the status codes for the requests and the output of a ping command.

    Notice the following:

    • The request to succeeds with status code 200.
    • The request to the Cloud Storage bucket succeeds with status code 200.
    • The ping to succeeds.
  3. Get the logs for the pod with the tproxy annotation:

    kubectl logs --selector=app=debian-app,variant=locked --tail=4

    Example output: 418 200
    PING ( 56 data bytes
    ping: sending packet: Operation not permitted

    Notice the following:

    • The proxy blocks the request to with status code 418.
    • The proxy allows the request to the Cloud Storage bucket with status code 200.
    • The ping to is rejected.
  4. Inspect the logs from the mitmproxy DaemonSet pod to show the intercepted requests and responses:

    kubectl logs $(kubectl get pods -o wide | awk '/tproxy.*'$(kubectl get pods --selector=app=debian-app,variant=locked -o=jsonpath={.items..spec.nodeName})'/ {print $1}') -c tproxy-tproxy-mode --tail=10

    Note that the logs have to be retrieved from the tproxy pod that is running on the same node as the example app.

    Example output: clientconnect GET HTTP/2.0
                << 418 I'm a teapot 30b clientdisconnect clientconnect
    Streaming response from GET HTTP/2.0
                << 200  (content missing) clientdisconnect

    Notice that the proxy blocks the request to with status code 418.

Customizing the mitmproxy Python script

This tutorial uses a Python script to filter traffic to a specific Cloud Storage bucket. The Python script is installed as a ConfigMap resource, and mitmproxy can live-reload the script when it changes without restarting.

  1. Modify the script to change the "access denied" status code:

    cd charts/tproxy/
    sed -e 's/418/500/g' config/
  2. Upgrade the Helm chart with the change:

    helm upgrade tproxy .

    After about 30 seconds, the new script will be updated and in use by mitmproxy.

  3. Verify the output of the locked pod:

    kubectl logs --selector=app=debian-app,variant=locked --tail=4

    Example output: 500 200
    PING ( 56 data bytes
    ping: sending packet: Operation not permitted

    Notice that the proxy now blocks the request to with status code 500.


  1. Delete the sample apps:

    cd ../../
    kubectl delete -f examples/debian-app.yaml
    kubectl delete -f examples/debian-app-locked-manual.yaml
  2. Delete the tproxy helm release:

    helm delete --purge tproxy
  3. Delete the Kubernetes Engine cluster:

    gcloud container clusters delete tproxy-example --zone=us-central1-f

What's next?

Submit a tutorial

Share step-by-step guides

Submit a tutorial

Request a tutorial

Ask for community help

Submit a request

View tutorials

Search Google Cloud tutorials

View tutorials

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.