Skip to content

Kubernetes Service port-forwarding

Kubernetes service port-forwarding is a networking mechanism used to expose a local port to a specific Kubernetes Service.^[400-devops__06-Kubernetes__k8s-learning__08-collect__README.md] This allows a user to access an application running inside the cluster as if it were running locally on their machine.

Usage

To initiate a port-forward session to a Kubernetes Service, the kubectl port-forward command is used with the following syntax^[400-devops__06-Kubernetes__k8s-learning__08-collect__README.md]:

[kubectl port-forward](<./kubectl-port-forward.md>) service/svc-myapp 8080:80 --address 0.0.0.0

In this example, traffic is forwarded from the local machine to the service.^[400-devops__06-Kubernetes__k8s-learning__08-collect__README.md] The command includes flags to bind to all network interfaces (--address 0.0.0.0) rather than just the default localhost.^[400-devops__06-Kubernetes__k8s-learning__08-collect__README.md]

Verification

Once the forwarding is established, the connection can be verified by sending requests to the mapped local port.^[400-devops__06-Kubernetes__k8s-learning__08-collect__README.md] For example, using a loop to curl the endpoint demonstrates that traffic is reaching the application pods.^[400-devops__06-Kubernetes__k8s-learning__08-collect__README.md]

Sources

^[400-devops__06-Kubernetes__k8s-learning__08-collect__README.md]