Skip to content

Commit

Permalink
Method 2
Browse files Browse the repository at this point in the history
  • Loading branch information
boruszak committed Nov 2, 2023
1 parent ac15715 commit 3255c98
Showing 1 changed file with 79 additions and 5 deletions.
84 changes: 79 additions & 5 deletions website/content/docs/k8s/multiport/traffic-split.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,39 @@ This page describes the process for splitting TCP, HTTP, and gRPC traffic betwee

## Prerequisites

Splitting traffic between two ports of a multi-port service requires the [v2 catalog API](/consul/docs/architecture/catalog/v2). For guidance on enabling the v2 catalog, deploying multi-port services, and applying traffic permissions to them, refer to [configure multi-port services](/consul/docs/k8s/multiport/configure).
Splitting traffic between two ports of a multi-port service requires the [v2 catalog API](/consul/docs/architecture/catalog/v2).

In addition, how you define a multi-port service affects how Services are addressed in Kubernetes. The instructions on this page offer examples for two configuration methods:

- **Method 1**: Define a single Kubernetes Service that exposes multiple ports
- **Method 2**: Define multiple Kubernetes Services that expose individual ports

For guidance on enabling the v2 catalog, deploying multi-port services using these methods, and applying traffic permissions to the services, refer to [configure multi-port services](/consul/docs/k8s/multiport/configure).

## Overview

Complete the following steps to implement a split in TCP traffic between two services:

1. Define the resource's behavior in a custom resource definition (CRD).
1. Apply the resource to your cluster.
1. Output the resource path with the `consul resource read` command.

## Define route resource

The following example splits traffic for the `api` service. TCP traffic for services registered to the Consul catalog that are available at the `api-workload` port is split so that 50% of the traffic routes to the service at the `api-workload` port and 50% routes to the service at the `admin-workload` port.
The following example splits traffic for the services in the `api` Pod.

<Tabs>

<Tab heading="Method 1" group="method1">

TCP traffic for services registered to the Consul catalog that are available at the `admin` port is split so that 50% of the traffic routes to the service at the `api` port and 50% routes to the service at the `admin` port.

<CodeBlockConfig filename="api-split.yaml">

```yaml
apiVersion: mesh.consul.hashicorp.com/v2beta1
kind: TCPRoute
metadata:
name: api
name: api-split
spec:
parentRefs:
- ref:
Expand Down Expand Up @@ -68,6 +80,51 @@ spec:
weight: 50
```
</CodeBlockConfig>
</Tab>
<Tab heading="Method 2" group="method2">
TCP traffic for services registered to the Consul catalog that are available at the `api-admin` port is split so that 50% of the traffic routes to the service at the `api` port and 50% routes to the service at the `api-admin` port.

<CodeBlockConfig filename="api-split.yaml">

```yaml
apiVersion: mesh.consul.hashicorp.com/v2beta1
kind: TCPRoute
metadata:
name: api-split
spec:
parentRefs:
- ref:
type:
group: catalog
groupVersion: v2beta1
kind: Service
name: api-admin
port: "admin"
rules:
- backendRefs:
- backendRef:
ref:
type:
group: catalog
groupVersion: v2beta1
kind: Service
name: api
port: "api"
weight: 50
- backendRef:
ref:
type:
group: catalog
groupVersion: v2beta1
kind: Service
name: api-admin
port: "admin"
weight: 50
```

</CodeBlockConfig>

## Apply the resource
Expand All @@ -78,10 +135,27 @@ Use the `kubectl` command to apply the resource to your Consul cluster.
$ kubectl apply -f api-split.yaml
```

Then, open a shell session in the `web` container and test the `api` service on port 90.
<Tabs>

<Tab heading="Method 1" group="method1">

Then, open a shell session in the `web` container and test the `api` service on port 90.

```shell-session
$ kubectl exec -it ${WEB_POD} -c web -- curl api:90
```

</Tab>

<Tab heading="Method 2" group="method2">

Then, open a shell session in the `web` container and test the `api-admin` service on port 90.

```shell-session
$ kubectl exec -it ${WEB_POD} -c web -- curl api-admin:90
```

</Tab>
</Tabs>

Half of the traffic should respond with the `hello world` response from port 80, instead of port 90's response of `hello world from 9090 admin`. Repeat the command several times to verify that you receive both responses.

0 comments on commit 3255c98

Please sign in to comment.