Configure a TCP Layer-4 Route
Note: This document does not apply to Higress versions ≥ 2.2.0 and < 2.2.4. If your Higress version falls within this range, it is recommended to upgrade to 2.2.4 or higher.
Prerequisites
Section titled “Prerequisites”- Higress is installed in the higress-system namespace in K8s, with the gateway’s Service name being higress-gateway;
- The goal is to configure a layer-4 route for the tcp-echo service in the default namespace, with the service listening on port 9000 and the gateway also listening on port 9000;
- The route is configured using Gateway API CRD, which requires Higress to be configured to support Gateway API in advance. Reference documentation: Document
Configuration Steps
Section titled “Configuration Steps”The specific configuration steps differ slightly across Higress versions. Please pay close attention.
1. Create GatewayClass
Section titled “1. Create GatewayClass”When the Higress version is ≥ 2.2.0, this step is not required. Please skip directly to the next step.
- Create a
gatewayclass.yamlfile with the following content:apiVersion: gateway.networking.k8s.io/v1kind: GatewayClassmetadata:name: higressspec:controllerName: "higress.io/gateway-controller" - Execute the command to apply the configuration to the K8s cluster:
Terminal window kubectl apply -f gatewayclass.yaml
2. Create Gateway
Section titled “2. Create Gateway”- Create a
gateway.yamlfile with the following content:apiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: higress-gatewaynamespace: higress-systemspec:gatewayClassName: higresslisteners:- name: default-tcpprotocol: TCPport: 9000allowedRoutes:namespaces:from: Allkinds:- kind: TCPRoute - Execute the command to apply the configuration to the K8s cluster:
Terminal window kubectl apply -f gateway.yaml
3. Modify the higress-gateway Service
Section titled “3. Modify the higress-gateway Service”- Execute the command to enter the edit state of the
higress-gatewayService:Terminal window kubectl edit service higress-gateway -n higress-system - Add a description for port
9000to thespec.portslist. The configuration after the addition should look like this:...ports:- name: http2port: 80protocol: TCPtargetPort: 80- name: httpsport: 443protocol: TCPtargetPort: 443# --- Configuration added here ---- name: tcpport: 9000protocol: TCPtargetPort: 9000# -----------------------------... - Save the edits and exit the editor.
4. Create TCPRoute
Section titled “4. Create TCPRoute”- Create a
tcproute.yamlfile with the following content:- Higress ≥ 2.2.4 + Gateway API 1.6.0
apiVersion: gateway.networking.k8s.io/v1kind: TCPRoutemetadata:name: tcp-echonamespace: defaultspec:parentRefs:- name: higress-gatewaynamespace: higress-systemport: 9000rules:- backendRefs:- name: tcp-echoport: 9000
- Higress < 2.2.4 + Gateway API ≤ 1.4.0
apiVersion: gateway.networking.k8s.io/v1alpha2kind: TCPRoutemetadata:name: tcp-echonamespace: defaultspec:parentRefs:- name: higress-gatewaynamespace: higress-systemport: 9000rules:- backendRefs:- name: tcp-echoport: 9000
- Higress ≥ 2.2.4 + Gateway API 1.6.0
- Execute the command to apply the configuration to the K8s cluster:
Terminal window kubectl apply -f tcproute.yaml
5. Configuration Verification
Section titled “5. Configuration Verification”Configuration complete. We can verify whether the route is working properly by using telnet to connect to port 9000 of the higress-gateway service or by other means.