# Port Forwarding

### Regular port-forwarding

The port-forward command allows you to forward traffic from a local port to any destination that the mirrord targeted pod has access to, in a similar way to `kubectl port-forward`. The traffic is forwarded as-if it was coming from the target pod, meaning it has access to destinations that might be outside the cluster, like third-party APIs, depending on what's accessible by the target pod.

{% hint style="warning" %}
To use port-forwarding with MySQL databases, at least version 3.177.0 of mirrord is required.
{% endhint %}

You can use the command like so:

```bash
mirrord port-forward --target <target-path> -L <local port>:<remote address>:<remote port>
```

For example, to forward traffic from localhost:8080 to an in-cluster service py-serv listening on port 80:

```bash
mirrord port-forward -L 8080:py-serv:80
```

### Reverse port-forwarding

It also allows for reverse port forwarding, where traffic is redirected from a port on the target pod or workload to a local port, like so:

```bash
mirrord port-forward --target <target-path> -R <remote port>:<local port>
```

For example, to forward traffic from an in-cluster deployment py-serv listening on port 80 to localhost:8080:

```bash
mirrord port-forward --target deployment/py-serv -R 80:8080
```

In addition, multiple ports can be forwarded in one direction or both directions simultaneously in the same command by providing each source and destination as a separate `-L` or `-R` argument.

Regular port forwarding with an `-L` can be done in targetless mode and does not require specifying any target. Reverse port forwarding always requires a target.

### More details

* The local port component of the `-L` argument is optional, and without it the same port will be used locally as on the remote.
* The same is true of the `-R` argument: if one port number is provided, it will be used for both local and remote ports.
* Port-forwarding only supports TCP, not UDP.
* The remote address can be an IPv4 address or a hostname - hostnames are resolved in the cluster.
* In regular port forwarding (`-L`) connections are made lazily and hostname resolution is attempted only data is sent to the local port.
* Reverse forwarding (`-R`) can read the `feature.network.incoming` section of a mirrord config file when the file is passed to the command with `-f`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://metalbear.com/mirrord/docs/using-mirrord/port-forwarding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
