github.com/nginxinc/kubernetes-ingress@v1.12.5/docs-web/configuration/transportserver-resource.md (about)

     1  # TransportServer Resource
     2  
     3  The TransportServer resource allows you to configure TCP, UDP, and TLS Passthrough load balancing. The resource is implemented as a [Custom Resource](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/).
     4  
     5  This document is the reference documentation for the TransportServer resource. To see additional examples of using the resource for specific use cases, go to the [examples-of-custom-resources](https://github.com/nginxinc/kubernetes-ingress/blob/v1.12.5/examples-of-custom-resources) folder in our GitHub repo.
     6  
     7  > **Feature Status**: The TransportServer resource is available as a preview feature: it is suitable for experimenting and testing; however, it must be used with caution in production environments. Additionally, while the feature is in preview, we might introduce some backward-incompatible changes to the resource specification in the next releases.
     8  
     9  ## Contents
    10  
    11  - [TransportServer Resource](#transportserver-resource)
    12    - [Contents](#contents)
    13    - [Prerequisites](#prerequisites)
    14    - [TransportServer Specification](#transportserver-specification)
    15      - [Listener](#listener)
    16      - [Upstream](#upstream)
    17        - [Upstream.Healthcheck](#upstream-healthcheck)
    18        - [Upstream.Healthcheck.Match](#upstream-healthcheck-match)
    19      - [UpstreamParameters](#upstreamparameters)
    20      - [SessionParameters](#sessionparameters)
    21      - [Action](#action)
    22    - [Using TransportServer](#using-transportserver)
    23      - [Usings Snippets](#using-snippets)
    24      - [Validation](#validation)
    25        - [Structural Validation](#structural-validation)
    26        - [Comprehensive Validation](#comprehensive-validation)
    27    - [Customization via ConfigMap](#customization-via-configmap)
    28    - [Limitations](#limitations)
    29  
    30  ## Prerequisites
    31  
    32  * For TCP and UDP, the TransportServer resource must be used in conjunction with the [GlobalConfiguration resource](/nginx-ingress-controller/configuration/global-configuration/globalconfiguration-resource), which must be created separately.
    33  * For TLS Passthrough, make sure to enable the [`-enable-tls-passthrough`](/nginx-ingress-controller/configuration/global-configuration/command-line-arguments#cmdoption-enable-tls-passthrough) command-line argument of the Ingress Controller.
    34  
    35  ## TransportServer Specification
    36  
    37  The TransportServer resource defines load balancing configuration for TCP, UDP, or TLS Passthrough traffic. Below are a few examples:
    38  
    39  * TCP load balancing:
    40    ```yaml
    41    apiVersion: k8s.nginx.org/v1alpha1
    42    kind: TransportServer
    43    metadata:
    44      name: dns-tcp
    45    spec:
    46      listener:
    47        name: dns-tcp
    48        protocol: TCP
    49      upstreams:
    50      - name: dns-app
    51        service: dns-service
    52        port: 5353
    53      action:
    54        pass: dns-app
    55    ```
    56  * UDP load balancing:
    57    ```yaml
    58    apiVersion: k8s.nginx.org/v1alpha1
    59    kind: TransportServer
    60    metadata:
    61      name: dns-udp
    62    spec:
    63      listener:
    64        name: dns-udp
    65        protocol: UDP
    66      upstreams:
    67      - name: dns-app
    68        service: dns-service
    69        port: 5353
    70      upstreamParameters:
    71        udpRequests: 1
    72        udpResponses: 1
    73      action:
    74        pass: dns-app
    75    ```
    76  * TLS passthrough load balancing:
    77    ```yaml
    78    apiVersion: k8s.nginx.org/v1alpha1
    79    kind: TransportServer
    80    metadata:
    81      name: secure-app
    82    spec:
    83      listener:
    84        name: tls-passthrough
    85        protocol: TLS_PASSTHROUGH
    86      host: app.example.com
    87      upstreams:
    88      - name: secure-app
    89        service: secure-app
    90        port: 8443
    91      action:
    92        pass: secure-app
    93    ```
    94  
    95  ```eval_rst
    96  .. list-table::
    97     :header-rows: 1
    98  
    99     * - Field
   100       - Description
   101       - Type
   102       - Required
   103     * - ``listener``
   104       - The listener on NGINX that will accept incoming connections/datagrams.
   105       - `listener <#listener>`_
   106       - Yes
   107     * - ``host``
   108       - The host (domain name) of the server. Must be a valid subdomain as defined in RFC 1123, such as ``my-app`` or ``hello.example.com``. Wildcard domains like ``*.example.com`` are not allowed. Required for TLS Passthrough load balancing.
   109       - ``string``
   110       - No*
   111     * - ``upstreams``
   112       - A list of upstreams.
   113       - `[]upstream <#upstream>`_
   114       - Yes
   115     * - ``upstreamParameters``
   116       - The upstream parameters.
   117       - `upstreamParameters <#upstreamparameters>`_
   118       - No
   119     * - ``action``
   120       - The action to perform for a client connection/datagram.
   121       - `action <#action>`_
   122       - Yes
   123     * - ``ingressClassName``
   124       - Specifies which Ingress Controller must handle the TransportServer resource.
   125       - ``string``
   126       - No
   127     * - ``streamSnippets``
   128       - Sets a custom snippet in the ``stream`` context.
   129       - ``string``
   130       - No
   131     * - ``serverSnippets``
   132       - Sets a custom snippet in the ``server`` context.
   133       - ``string``
   134       - No
   135  ```
   136  
   137  \* -- Required for TLS Passthrough load balancing.
   138  
   139  ### Listener
   140  
   141  The listener field references a listener that NGINX will use to accept incoming traffic for the TransportServer. For TCP and UDP, the listener must be defined in the [GlobalConfiguration resource](/nginx-ingress-controller/configuration/global-configuration/globalconfiguration-resource). When referencing a listener, both the name and the protocol must match. For TLS Passthrough, use the built-in listener with the name `tls-passthrough` and the protocol `TLS_PASSTHROUGH`.
   142  
   143  An example:
   144  ```yaml
   145  listener:
   146    name: dns-udp
   147    protocol: UDP
   148  ```
   149  
   150  ```eval_rst
   151  .. list-table::
   152     :header-rows: 1
   153  
   154     * - Field
   155       - Description
   156       - Type
   157       - Required
   158     * - ``name``
   159       - The name of the listener.
   160       - ``string``
   161       - Yes
   162     * - ``protocol``
   163       - The protocol of the listener.
   164       - ``string``
   165       - Yes
   166  ```
   167  
   168  ### Upstream
   169  
   170  The upstream defines a destination for the TransportServer. For example:
   171  ```yaml
   172  name: secure-app
   173  service: secure-app
   174  port: 8443
   175  maxFails: 3
   176  maxConns: 100
   177  failTimeout: 30s
   178  loadBalancingMethod: least_conn
   179  ```
   180  
   181  ```eval_rst
   182  .. list-table::
   183     :header-rows: 1
   184  
   185     * - Field
   186       - Description
   187       - Type
   188       - Required
   189     * - ``name``
   190       - The name of the upstream. Must be a valid DNS label as defined in RFC 1035. For example, ``hello`` and ``upstream-123`` are valid. The name must be unique among all upstreams of the resource.
   191       - ``string``
   192       - Yes
   193     * - ``service``
   194       - The name of a `service <https://kubernetes.io/docs/concepts/services-networking/service/>`_. The service must belong to the same namespace as the resource. If the service doesn't exist, NGINX will assume the service has zero endpoints and close client connections/ignore datagrams.
   195       - ``string``
   196       - Yes
   197     * - ``port``
   198       - The port of the service. If the service doesn't define that port, NGINX will assume the service has zero endpoints and close client connections/ignore datagrams. The port must fall into the range ``1..65535``.
   199       - ``int``
   200       - Yes
   201     * - ``maxFails``
   202       - Sets the `number <https://nginx.org/en/docs/stream/ngx_stream_upstream_module.html#max_fails>`_ of unsuccessful attempts to communicate with the server that should happen in the duration set by the failTimeout parameter to consider the server unavailable. The default ``1``.
   203       - ``int``
   204       - No
   205     * - ``maxConns``
   206       - Sets the `number <https://nginx.org/en/docs/stream/ngx_stream_upstream_module.html#max_conns>`_ of maximum connections to the proxied server. Default value is zero, meaning there is no limit. The default is ``0``.
   207       - ``int``
   208       - No
   209     * - ``failTimeout``
   210       - Sets the `time <https://nginx.org/en/docs/stream/ngx_stream_upstream_module.html#fail_timeout>`_ during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable and the period of time the server will be considered unavailable. The default is ``10s``.
   211       - ``string``
   212       - No
   213     * - ``healthCheck``
   214       - The health check configuration for the Upstream. See the `health_check <https://nginx.org/en/docs/stream/ngx_stream_upstream_hc_module.html#health_check>`_ directive. Note: this feature is supported only in NGINX Plus.
   215       - `healthcheck <#upstream-healthcheck>`_
   216       - No
   217     * - ``loadBalancingMethod``
   218       - The method used to load balance the upstream servers. By default, connections are distributed between the servers using a weighted round-robin balancing method. See the `upstream <http://nginx.org/en/docs/stream/ngx_stream_upstream_module.html#upstream>`_ section for available methods and their details.
   219       - ``string``
   220       - No
   221  
   222  ```
   223  
   224  ### Upstream.Healthcheck
   225  
   226  The Healthcheck defines an [active health check](https://nginx.org/en/docs/stream/ngx_stream_upstream_hc_module.html?#health_check). In the example below we enable a health check for an upstream and configure all the available parameters:
   227  
   228  ```yaml
   229  name: secure-app
   230  service: secure-app
   231  port: 8443
   232  healthCheck:
   233    enable: true
   234    interval: 20s
   235    timeout: 30s
   236    jitter: 3s
   237    fails: 5
   238    passes: 5
   239    port: 8080
   240  ```
   241  
   242  Note: This feature is supported only in NGINX Plus.
   243  
   244  ```eval_rst
   245  .. list-table::
   246     :header-rows: 1
   247  
   248     * - Field
   249       - Description
   250       - Type
   251       - Required
   252     * - ``enable``
   253       - Enables a health check for an upstream server. The default is ``false``.
   254       - ``boolean``
   255       - No
   256     * - ``interval``
   257       - The interval between two consecutive health checks. The default is ``5s``.
   258       - ``string``
   259       - No
   260     * - ``timeout``
   261       - This overrides the timeout set by `proxy_timeout <http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_timeout>`_ which is set in `SessionParameters` for health checks. The default value is ``5s``.
   262       - ``string``
   263       - No
   264     * - ``jitter``
   265       - The time within which each health check will be randomly delayed. By default, there is no delay.
   266       - ``string``
   267       - No
   268     * - ``fails``
   269       - The number of consecutive failed health checks of a particular upstream server after which this server will be considered unhealthy. The default is ``1``.
   270       - ``integer``
   271       - No
   272     * - ``passes``
   273       - The number of consecutive passed health checks of a particular upstream server after which the server will be considered healthy. The default is ``1``.
   274       - ``integer``
   275       - No
   276     * - ``port``
   277       - The port used for health check requests. By default, the port of the upstream is used. Note: in contrast with the port of the upstream, this port is not a service port, but a port of a pod.
   278       - ``integer``
   279       - No
   280     * - ``match``
   281       - Controls the data to send and the response to expect for the healthcheck.
   282       - `match <#upstream-healthcheck-match>`_
   283       - No
   284  ```
   285  
   286  ### Upstream.Healthcheck.Match
   287   
   288  The match controls the data to send and the response to expect for the healthcheck:
   289  ```yaml
   290  match:
   291    send: 'GET / HTTP/1.0\r\nHost: localhost\r\n\r\n'
   292    expect: "~200 OK"
   293  ```
   294  
   295  Both `send` and `expect` fields can contain hexadecimal literals with the prefix `\x` followed by two hex digits, for example, `\x80`.
   296  
   297  See the [match](https://nginx.org/en/docs/stream/ngx_stream_upstream_hc_module.html#match) directive for details.
   298  
   299  ```eval_rst
   300  .. list-table::
   301     :header-rows: 1
   302  
   303     * - Field
   304       - Description
   305       - Type
   306       - Required
   307     * - ``send``
   308       - A string to send to an upstream server.
   309       - ``string``
   310       - No
   311     * - ``expect``
   312       - A literal string or a regular expression that the data obtained from the server should match. The regular expression is specified with the preceding ``~*`` modifier (for case-insensitive matching), or the ``~`` modifier (for case-sensitive matching). The Ingress Controller validates a regular expression using the RE2 syntax.
   313       - ``string``
   314       - No
   315  ```
   316  
   317  ### UpstreamParameters
   318  
   319  The upstream parameters define various parameters for the upstreams:
   320  ```yaml
   321  upstreamParameters:
   322    udpRequests: 1
   323    udpResponses: 1
   324    connectTimeout: 60s
   325    nextUpstream: true
   326    nextUpstreamTimeout: 50s
   327    nextUpstreamTries: 1
   328  ```
   329  
   330  ```eval_rst
   331  .. list-table::
   332     :header-rows: 1
   333  
   334     * - Field
   335       - Description
   336       - Type
   337       - Required
   338     * - ``udpRequests``
   339       - The number of datagrams, after receiving which, the next datagram from the same client starts a new session. See the `proxy_requests <https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_requests>`_ directive. The default is ``0``.
   340       - ``int``
   341       - No
   342     * - ``udpResponses``
   343       - The number of datagrams expected from the proxied server in response to a client datagram. See the `proxy_responses <https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_responses>`_ directive. By default, the number of datagrams is not limited.
   344       - ``int``
   345       - No
   346     * - ``connectTimeout``
   347       - The timeout for establishing a connection with a proxied server. See the `proxy_connect_timeout <http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_connect_timeout>`_ directive. The default is ``60s``.
   348       - ``string``
   349       - No
   350     * - ``nextUpstream``
   351       - If a connection to the proxied server cannot be established, determines whether a client connection will be passed to the next server. See the `proxy_next_upstream <http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_next_upstream>`_ directive. The default is ``true``.
   352       - bool
   353       - No
   354     * - ``nextUpstreamTries``
   355       - The number of tries for passing a connection to the next server. See the `proxy_next_upstream_tries <http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_next_upstream_tries>`_ directive. The default is ``0``.
   356       - ``int``
   357       - No
   358     * - ``nextUpstreamTimeout``
   359       - The time allowed to pass a connection to the next server. See the `proxy_next_upstream_timeout <http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_next_upstream_timeout>`_ directive. The default us ``0``.
   360       - ``string``
   361       - No
   362  ```
   363  
   364  ### SessionParameters
   365  
   366  The session parameters define various parameters for TCP connections and UDP sessions.
   367  ```yaml
   368  sessionParameters:
   369    timeout: 50s
   370  ```
   371  
   372  ```eval_rst
   373  .. list-table::
   374     :header-rows: 1
   375  
   376     * - Field
   377       - Description
   378       - Type
   379       - Required
   380     * - ``timeout``
   381       - The timeout between two succesive read or write operations on client or proxied server connections. See `proxy_timeout <http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_timeout>`_ directive. The default is ``10m``.
   382       - ``string``
   383       - No
   384  ```
   385  
   386  ### Action
   387  
   388  The action defines an action to perform for a client connection/datagram.
   389  
   390  In the example below, client connections/datagrams are passed to an upstream `dns-app`:
   391  ```yaml
   392  action:
   393    pass: dns-app
   394  ```
   395  
   396  ```eval_rst
   397  .. list-table::
   398     :header-rows: 1
   399  
   400     * - Field
   401       - Description
   402       - Type
   403       - Required
   404     * - ``pass``
   405       - Passes connections/datagrams to an upstream. The upstream with that name must be defined in the resource.
   406       - ``string``
   407       - Yes
   408  ```
   409  
   410  ## Using TransportServer
   411  
   412  You can use the usual `kubectl` commands to work with TransportServer resources, similar to Ingress resources.
   413  
   414  For example, the following command creates a TransportServer resource defined in `transport-server-passthrough.yaml` with the name `secure-app`:
   415  ```
   416  $ kubectl apply -f transport-server-passthrough.yaml
   417  transportserver.k8s.nginx.org/secure-app created
   418  ```
   419  
   420  You can get the resource by running:
   421  ```
   422  $ kubectl get transportserver secure-app
   423  NAME         AGE
   424  secure-app   46sm
   425  ```
   426  
   427  In the kubectl get and similar commands, you can also use the short name `ts` instead of `transportserver`.
   428  
   429  ### Using Snippets
   430  
   431  Snippets allow you to insert raw NGINX config into different contexts of NGINX configuration. In the example below, we use snippets to configure [access control](http://nginx.org/en/docs/stream/ngx_stream_access_module.html) in a TransportServer:
   432  
   433  ```yaml
   434  apiVersion: k8s.nginx.org/v1alpha1
   435  kind: TransportServer
   436  metadata:
   437    name: cafe
   438  spec:
   439    host: cafe.example.com
   440    serverSnippets: |
   441      deny  192.168.1.1;
   442      allow 192.168.1.0/24;
   443    upstreams:
   444    - name: tea
   445      service: tea-svc
   446      port: 80
   447  ```
   448  
   449  Snippets can also be specified for a stream. In the example below, we use snippets to [limit the number of connections](https://nginx.org/en/docs/stream/ngx_stream_limit_conn_module.html):
   450  
   451  ```yaml
   452  apiVersion: k8s.nginx.org/v1alpha1
   453  kind: TransportServer
   454  metadata:
   455    name: cafe
   456  spec:
   457    host: cafe.example.com
   458    streamSnippets: limit_conn_zone $binary_remote_addr zone=addr:10m;
   459    serverSnippets: limit_conn addr 1;
   460    upstreams:
   461    - name: tea
   462      service: tea-svc
   463      port: 80
   464  ```
   465  
   466  Snippets are intended to be used by advanced NGINX users who need more control over the generated NGINX configuration.
   467  
   468  However, because of the disadvantages described below, snippets are disabled by default. To use snippets, set the [`enable-snippets`](/nginx-ingress-controller/configuration/global-configuration/command-line-arguments#cmdoption-enable-snippets) command-line argument.
   469  
   470  Disadvantages of using snippets:
   471  * *Complexity*. To use snippets, you will need to:
   472    * Understand NGINX configuration primitives and implement a correct NGINX configuration.
   473    * Understand how the IC generates NGINX configuration so that a snippet doesn't interfere with the other features in the configuration.
   474  * *Decreased robustness*. An incorrect snippet makes the NGINX config invalid which will lead to a failed reload. This will prevent any new configuration updates, including updates for the other TransportServer resource until the snippet is fixed.
   475  * *Security implications*. Snippets give access to NGINX configuration primitives and those primitives are not validated by the Ingress Controller.
   476  
   477  
   478  > Note: during a period when the NGINX config includes an invalid snippet, NGINX will continue to operate with the latest valid configuration.
   479  
   480  > Note: to configure snippets in the `stream` context, use `stream-snippets` ConfigMap key.
   481  
   482  
   483  ### Validation
   484  
   485  Two types of validation are available for the TransportServer resource:
   486  * *Structural validation* by the `kubectl` and Kubernetes API server.
   487  * *Comprehensive validation* by the Ingress Controller.
   488  
   489  #### Structural Validation
   490  
   491  The custom resource definition for the TransportServer includes structural OpenAPI schema which describes the type of every field of the resource.
   492  
   493  If you try to create (or update) a resource that violates the structural schema (for example, you use a string value for the port field of an upstream), `kubectl` and Kubernetes API server will reject such a resource:
   494  * Example of `kubectl` validation:
   495      ```
   496      $ kubectl apply -f transport-server-passthrough.yaml
   497        error: error validating "transport-server-passthrough.yaml": error validating data: ValidationError(TransportServer.spec.upstreams[0].port): invalid type for org.nginx.k8s.v1alpha1.TransportServer.spec.upstreams.port: got "string", expected "integer"; if you choose to ignore these errors, turn validation off with --validate=false
   498      ```
   499  * Example of Kubernetes API server validation:
   500      ```
   501      $ kubectl apply -f transport-server-passthrough.yaml --validate=false
   502        The TransportServer "secure-app" is invalid: []: Invalid value: map[string]interface {}{ ... }: validation failure list:
   503        spec.upstreams.port in body must be of type integer: "string"
   504      ```
   505  
   506  If a resource is not rejected (it doesn't violate the structural schema), the Ingress Controller will validate it further.
   507  
   508  #### Comprehensive Validation
   509  
   510  The Ingress Controller validates the fields of a TransportServer resource. If a resource is invalid, the Ingress Controller will reject it: the resource will continue to exist in the cluster, but the Ingress Controller will ignore it.
   511  
   512  You can check if the Ingress Controller successfully applied the configuration for a TransportServer. For our example `secure-app` TransportServer, we can run:
   513  ```
   514  $ kubectl describe ts secure-app
   515  . . .
   516  Events:
   517    Type    Reason          Age   From                      Message
   518    ----    ------          ----  ----                      -------
   519    Normal  AddedOrUpdated  3s    nginx-ingress-controller  Configuration for default/secure-app was added or updated
   520  ```
   521  Note how the events section includes a Normal event with the AddedOrUpdated reason that informs us that the configuration was successfully applied.
   522  
   523  If you create an invalid resource, the Ingress Controller will reject it and emit a Rejected event. For example, if you create a TransportServer `secure-app` with a pass action that references a non-existing upstream, you will get  :
   524  ```
   525  $ kubectl describe ts secure-app
   526  . . .
   527  Events:
   528    Type     Reason    Age   From                      Message
   529    ----     ------    ----  ----                      -------
   530    Warning  Rejected  2s    nginx-ingress-controller  TransportServer default/secure-app is invalid and was rejected: spec.action.pass: Not found: "some-app"
   531  ```
   532  Note how the events section includes a Warning event with the Rejected reason.
   533  
   534  **Note**: If you make an existing resource invalid, the Ingress Controller will reject it and remove the corresponding configuration from NGINX.
   535  
   536  ## Customization via ConfigMap
   537  
   538  The [ConfigMap](/nginx-ingress-controller/configuration/global-configuration/configmap-resource) keys (except for `stream-snippets` and `stream-log-format`) do not affect TransportServer resources.
   539  
   540  ## Limitations
   541  
   542  The TransportServer resource is a preview feature. Currently, it comes with the following limitation:
   543  * When using TLS Passthrough, it is not possible to configure [Proxy Protocol](https://github.com/nginxinc/kubernetes-ingress/tree/v1.12.5/examples/proxy-protocol) for port 443 both for regular HTTPS and TLS Passthrough traffic.