“kubectl proxy” vs “kubectl port-forward” in Kubernetes

Introduction

You might have heard or used kubectl proxy and/or kubectl port-forward commands while working with Kubernetes cluster.
However, are you aware of how they work and when we should use the above commands?

Let’s deep dive and learn more about kubectl proxy and kubectl port-forward.

Kubectl Proxy

👉 It creates a proxy server or application-level gateway between localhost and the Kubernetes API server.
👉 It also allows serving static content over specified HTTP path.
👉 All incoming data enters through one port and gets forwarded to the remote Kubernetes API server port, except for the path matching the static content path.
👉 One of the most common use-case for using kubectl proxy is to access the kubernetes dashboard from your local and manage the resources from the dashboard.
👉 A simple command can be used to start a proxy server in your local which connects to the API server:

$ kubectl proxy -p 9090

👉 For other parameters which can be used with kubectl proxy, please refer to the kubectl reference.
👉 kubectl proxy works only for HTTP traffic.
👉 For more examples and other options of kubectl proxy, you can simply use the --help option:

$ kubectl proxy --help

Kubectl Port-Forward

👉 Kubectl port-forward is a method to access, interact and manage internal Kubernetes clusters directly from your local network.
👉 It forward one or more local ports to a pod. Even though can port-forward to any pod, service or deployment, a matching pod will be selected and forwarding will happen.
👉 It is mostly used for the purpose of getting access to internal cluster resources and debugging issues.
👉 Using kubectl port-forwarding command, you are mapping one of the local ports to the pod running in the Kubernetes cluster.
👉 Compared to kubectl proxykubectl port-forward is more generic as it can forward TCP traffic while kubectl proxy can only forward HTTP traffic.
👉 One simple example to forward all requests sent to local’s port 5050 to the pod’s port 8080:

$ kubectl port-forward pod/mypod 5050:8080

👉 For other parameters which can be used with kubectl port-forward, please refer to the kubectl reference.
👉 For more examples and other options of kubectl port-forward, you can simply use the --help option:

$ kubectl port-forward --help

I hope you’ve now better understanding of kubectl proxy and kubectl port-forward.

References

🔗 Kubectl Proxy
🔗 Kubectl port-forward

"Knowledge Sharing Is Powerful"
Scroll to Top