github.com/GoogleContainerTools/skaffold/v2@v2.13.2/docs-v2/content/en/docs/port-forwarding.md (about)

     1  ---
     2  title: "Port Forwarding"
     3  linkTitle: "Port Forwarding"
     4  weight: 46
     5  featureId: portforward
     6  aliases: [/docs/how-tos/portforward, /docs/pipeline-stages/port-forwarding]
     7  ---
     8  
     9  Skaffold has built-in support for forwarding ports from exposed Kubernetes resources on your cluster
    10  to your local machine when running in `dev`, `debug`, `deploy`, or `run` modes.
    11  
    12  ### Automatic Port Forwarding
    13  
    14  Skaffold supports automatic port forwarding the following classes of resources:
    15  
    16  - `user`: explicit port-forwards defined in the `skaffold.yaml` (called [_user-defined port forwards_](#UDPF))
    17  - `services`: ports exposed on services deployed by Skaffold.
    18  - `debug`: debugging ports as enabled by `skaffold debug` for Skaffold-built images.
    19  - `pods`: all `containerPort`s on deployed pods for Skaffold-built images.
    20  
    21  Skaffold enables certain classes of forwards by default depending on the Skaffold command used.
    22  These defaults can be overridden with the `--port-forward` flag, and port-forwarding can be
    23  disabled with `--port-forward=off`.
    24  
    25  Command-line                          | Default modes
    26  ------------------------------------- | -------------------
    27  `skaffold dev`                        | `user`
    28  `skaffold dev --port-forward`         | `user`, `services`
    29  `skaffold dev --port-forward=off`     | _no ports forwarded_
    30  `skaffold debug`                      | `user`, `debug`
    31  `skaffold debug --port-forward`       | `user`, `services`, `debug` <small>(<em>see note below</em>)</small>
    32  `skaffold debug --port-forward=off`   | _no ports forwarded_
    33  `skaffold deploy`                     | `off`
    34  `skaffold deploy --port-forward`      | `user`, `services`
    35  `skaffold run`                        | `off`
    36  `skaffold run --port-forward`         | `user`, `services`
    37  
    38  {{< alert title="Compatibility Note" >}}
    39  Note that `skaffold debug --port-forward` previously enabled the
    40  equivalent of `pods` as Skaffold did not have an equivalent of `debug`. 
    41  We have replaced `pods` as it caused confusion.
    42  {{< /alert >}}
    43  
    44  ### User-Defined Port Forwarding {#UDPF}
    45  
    46  Users can define additional resources to port forward in the skaffold config, to enable port forwarding for 
    47  
    48  * additional resource types supported by `kubectl port-forward` e.g.`Deployment`or `ReplicaSet`.
    49  * additional pods running containers which run images not built by Skaffold.
    50  
    51  For example:
    52  
    53  ```yaml
    54  portForward:
    55  - resourceType: deployment
    56    resourceName: myDep
    57    namespace: mynamespace
    58    port: 8080
    59    localPort: 9000 # *Optional*
    60  ```
    61  
    62  For this example, Skaffold will attempt to forward port 8080 to `localhost:9000`.
    63  If port 9000 is unavailable, Skaffold will forward to a random open port. 
    64  
    65  {{< alert title="Note about forwarding System Ports" >}}
    66  Skaffold will request matching local ports only when the remote port is `> 1023`. So a service on port `8080` would still map to port `8080` (if available), but a service on port `80` will be mapped to some port `≥ 1024`.
    67  
    68  User-defined port-forwards in the `skaffold.yaml` are unaffected and can bind to system ports.
    69  {{< /alert >}}
    70  
    71  {{< alert title="Note about user-defined port-forwarding for Docker deployments" >}}
    72  When [deploying to Docker]({{< relref "/docs/deployers/docker" >}}) with a user-defined port-forward in the `skaffold.yaml`, the `resourceType` of `portForward` must be set to `container`. Otherwise, Skaffold will not tell the Docker daemon to expose that port.
    73  {{< /alert >}}
    74  
    75  Skaffold will run `kubectl port-forward` on each of these resources in addition to the automatic port forwarding described above.
    76  Acceptable resource types include: `Service`, `Pod` and Controller resource type that has a pod spec: `ReplicaSet`, `ReplicationController`, `Deployment`, `StatefulSet`, `DaemonSet`, `Job`, `CronJob`. 
    77  
    78  
    79  | Field        | Values           | Mandatory  |
    80  | ------------- |-------------| -----|
    81  | resourceType     | `pod`, `service`, `deployment`, `replicaset`, `statefulset`, `replicationcontroller`, `daemonset`, `job`, `cronjob`, `container` | Yes | 
    82  | resourceName     | Name of the resource to forward.     | Yes | 
    83  | namespace  | The namespace of the resource to port forward.     | No. Defaults to current namespace, or `default` if no current namespace is defined | 
    84  | port | Port is the resource port that will be forwarded. | Yes |
    85  | address | Address is the address on which the forward will be bound. | No. Defaults to `127.0.0.1` |
    86  | localPort | LocalPort is the local port to forward too. | No. Defaults to value set for `port`. |
    87  
    88  
    89  Skaffold will run `kubectl port-forward` on all user defined resources.
    90  `kubectl port-forward` will select one pod created by that resource to forward too.
    91  
    92  For example, forwarding a deployment that creates 3 replicas could look like this:
    93  
    94  ```yaml
    95  portForward:
    96  - resourceType: deployment
    97    resourceName: myDep
    98    namespace: mynamespace
    99    port: 8080
   100    localPort: 9000
   101  ```
   102  
   103  ![portforward_deployment](/images/portforward.png)
   104  
   105  If you want the port forward to to be available from other hosts and not from the local host only, you can bind
   106  the port forward to the address `0.0.0.0`:
   107  
   108  ```yaml
   109  portForward:
   110  - resourceType: deployment
   111    resourceName: myDep
   112    namespace: mynamespace
   113    port: 8080
   114    address: 0.0.0.0
   115    localPort: 9000
   116  ```