Search code examples
kuberneteskubernetes-helmkubernetes-pod

How to automatically configure Kubernetes port forwarding with Helm Charts?


Using Helm, how do I configure the Helm Chart to automatically include the port forwarding?

Documentation I have seen so far indicate I create a Helm chart, I run ...

helm install myhelmchart

... then forward the port manually...

  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=myhelmchart,app.kubernetes.io/instance=pouring-rat" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl port-forward $POD_NAME 8080:80

Solution

  • You can't: Helm's operation is limited to using its template language to render some set of YAML files, and submitting them to the Kubernetes server. It kind of only does kubectl apply and kubectl delete.

    One trick you might find useful, though, is that kubectl port-forward can take things other than pod names as of kubectl 1.10 (and this is functionality in the client, if you have a very old cluster you just need a new enough client). It will look up an appropriate pod name for you. So you can

    kubectl port-forward service/pouring-rat-nginx 8080:80
    

    I've found that kubectl port-forward works fine for lightweight testing and debugging and "if I send a curl request does it act the way I want". It also does things like routinely shut down after some idle time, and since it is tunneling TCP over HTTP it's not the fastest thing. Setting up a LoadBalancer-type service would be a better way to set up access from outside the cluster. Knobs like the type of service and any annotations you need to control the load balancer are good things to expose through Helm values.