# Proxy TCP Traffic by Port

Route TCP connections to a MySQL Service based on the incoming gateway port using either a Gateway API TCPRoute or an APISIX CRD stream route.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

1. Complete [Set Up Ingress Controller and Gateway](https://docs.api7.ai/ingress-controller/set-up-ingress-controller-and-gateway.md).
2. Install [MySQL Shell](https://dev.mysql.com/downloads/shell/) to initiate connections with MySQL server.

## Start an Example Upstream Service[​](#start-an-example-upstream-service "Direct link to Start an Example Upstream Service")

Create a Kubernetes manifest file for an example MySQL upstream service with the root password `my-secret-pw`:

mysql.yaml

```
apiVersion: apps/v1

kind: Deployment

metadata:

  name: mysql

  namespace: aic

  labels:

    app: mysql

spec:

  replicas: 1

  selector:

    matchLabels:

      app: mysql

  template:

    metadata:

      labels:

        app: mysql

    spec:

      containers:

        - name: mysql

          image: mysql:9.4

          env:

            - name: MYSQL_ROOT_PASSWORD

              value: "my-secret-pw"

          ports:

            - containerPort: 3306

          volumeMounts:

            - name: mysql-data

              mountPath: /var/lib/mysql

      volumes:

        - name: mysql-data

          emptyDir: {}

---

apiVersion: v1

kind: Service

metadata:

  name: mysql

  namespace: aic

spec:

  selector:

    app: mysql

  ports:

    - name: mysql

      port: 3306

      targetPort: 3306
```

Apply the configuration to your cluster:

```
kubectl apply -f mysql.yaml
```

## Enable Gateway Stream Proxy[​](#enable-gateway-stream-proxy "Direct link to Enable Gateway Stream Proxy")

The commands below add a listener to a fresh evaluation release. A `[0]` override replaces the whole listener list; `--reuse-values` does not append to it.

If the release already has listeners, export its complete values, append the new listener to the list, and upgrade with `-f stream-values.yaml` instead of the array override below. Keep the same namespace and installed chart version:

```
helm get values -n aic <release-name> --all -o yaml > stream-values.yaml
```

Upgrade your gateway to enable stream mode and set TCP port `9100`:

* APISIX Gateway
* API7 Gateway

Use the version in the installed release's `CHART` column from `helm list -n aic`, without the `apisix-` prefix.

```
helm upgrade -n aic apisix apisix/apisix \

  --version <installed-chart-version> \

  --reuse-values \

  --set "service.stream.enabled=true" \

  --set "service.stream.tcp[0]=9100"
```

Use the version in the installed release's `CHART` column from `helm list -n aic`, without the `gateway-` prefix. Do not use `APP VERSION`, which identifies the application rather than the chart.

```
# Replace 3.10.16 with the installed chart version.

GATEWAY_CHART_VERSION=3.10.16

helm upgrade -n aic api7-ee-3-gateway api7/gateway \

  --version "$GATEWAY_CHART_VERSION" \

  --reuse-values \

  --set "gateway.stream.enabled=true" \

  --set "gateway.stream.only=false" \

  --set "gateway.stream.tcp[0]=9100"
```

## Configure TCP Routing[​](#configure-tcp-routing "Direct link to Configure TCP Routing")

In this section, you will configure a route that listens for TCP traffic on port `9100`.

* Gateway API
* APISIX CRD

Update your Gateway manifest file to define a listener for TCP traffic:

gateway.yaml

```
apiVersion: gateway.networking.k8s.io/v1

kind: Gateway

metadata:

  namespace: aic

  name: apisix

spec:

  gatewayClassName: apisix

  listeners:

  - name: http

    protocol: HTTP

    port: 80

  - name: tcp

    protocol: TCP

    port: 9100

    allowedRoutes:

      kinds:

      - kind: TCPRoute

  infrastructure:

    parametersRef:

      group: apisix.apache.org

      kind: GatewayProxy

      name: apisix-config
```

Create a Kubernetes manifest for a TCPRoute:

tcp-route.yaml

```
apiVersion: gateway.networking.k8s.io/v1

kind: TCPRoute

metadata:

  name: stream-route-mysql

  namespace: aic

spec:

  parentRefs:

  - name: apisix

    sectionName: tcp

  rules:

  - backendRefs:

    - name: mysql

      port: 3306
```

When `listener_port_match_mode` is `explicit` or `auto`, the `sectionName: tcp` reference adds a `server_port` match for `9100`. The Gateway listener port must equal the physical gateway stream listener port. With the mode set to `off`, the controller does not add this match.

Apply the configuration to your cluster:

```
kubectl apply -f gateway.yaml -f tcp-route.yaml
```

Create a Kubernetes manifest file for a stream route:

tcp-route.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  name: stream-route-mysql

  namespace: aic

spec:

  ingressClassName: apisix

  stream:

    - name: stream-route-mysql

      protocol: TCP

      match:

        ingressPort: 9100

      backend:

        serviceName: mysql

        servicePort: 3306
```

Apply the configuration to your cluster:

```
kubectl apply -f tcp-route.yaml
```

## Set the Upstream Transport[​](#set-the-upstream-transport "Direct link to Set the Upstream Transport")

Use `tcp` for a plain TCP backend or `tls` when the gateway must establish TLS to the backend. The L4 schemes apply only to stream routes.

* Gateway API
* APISIX CRD

Attach a `BackendTrafficPolicy` to the backend Service:

mysql-upstream-policy.yaml

```
apiVersion: apisix.apache.org/v1alpha1

kind: BackendTrafficPolicy

metadata:

  name: mysql-transport

  namespace: aic

spec:

  targetRefs:

  - group: ""

    kind: Service

    name: mysql

    sectionName: mysql

  scheme: tcp
```

Apply the policy:

```
kubectl apply -f mysql-upstream-policy.yaml
```

Create an `ApisixUpstream` with the same name as the backend Service:

mysql-upstream.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  name: mysql

  namespace: aic

spec:

  ingressClassName: apisix

  scheme: tcp
```

Apply the upstream configuration:

```
kubectl apply -f mysql-upstream.yaml
```

To attach stream plugins to the TCPRoute, see [Apply Plugins to L4 Routes](https://docs.api7.ai/ingress-controller/apply-plugins-to-l4-routes.md).

## Verify[​](#verify "Direct link to Verify")

Expose the gateway’s service port to your local machine:

```
# replace with your gateway’s service name

kubectl port-forward svc/<gateway-service-name> -n aic 9100:9100 &
```

Connect with the MySQL server as root and key in the password `my-secret-pw` once prompted:

```
mysqlsh --sqlc --host=127.0.0.1 --port=9100 --user=root --password
```

If successful, MySQL Shell opens a classic protocol session and displays output similar to the following:

```
Creating a Classic session to 'root@127.0.0.1:9100'

Your MySQL connection id is 9

Server version: 9.4.0 MySQL Community Server - GPL
```
