Skip to content

Commit 7961bd3

Browse files
author
Kate Osborn
committed
Add traffic splitting example
1 parent 435fbfc commit 7961bd3

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

examples/traffic-splitting/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Example
2+
3+
In this example we will deploy NGINX Kubernetes Gateway and configure traffic splitting for a simple cafe application.
4+
We will use `HTTPRoute` resources to split traffic between two versions of the application -- `coffee-v1` and `coffee-v2`.
5+
6+
## Running the Example
7+
8+
## 1. Deploy NGINX Kubernetes Gateway
9+
10+
1. Follow the [installation instructions](/docs/installation.md) to deploy NGINX Gateway.
11+
12+
1. Save the public IP address of NGINX Kubernetes Gateway into a shell variable:
13+
14+
```
15+
GW_IP=XXX.YYY.ZZZ.III
16+
```
17+
18+
1. Save the port of NGINX Kubernetes Gateway:
19+
20+
```
21+
GW_PORT=<port number>
22+
```
23+
24+
## 2. Deploy the Coffee Application
25+
26+
1. Create the Cafe Deployments and Services:
27+
28+
```
29+
kubectl apply -f cafe.yaml
30+
```
31+
32+
1. Check that the Pods are running in the `default` namespace:
33+
34+
```
35+
kubectl -n default get pods
36+
NAME READY STATUS RESTARTS AGE
37+
coffee-v1-7c57c576b-rfjsh 1/1 Running 0 21m
38+
coffee-v2-698f66dc46-vcb6r 1/1 Running 0 21m
39+
```
40+
41+
## 3. Configure Routing
42+
43+
1. Create the `Gateway`:
44+
45+
```
46+
kubectl apply -f gateway.yaml
47+
```
48+
49+
1. Create the `HTTPRoute` resources:
50+
51+
```
52+
kubectl apply -f cafe-route.yaml
53+
```
54+
55+
This `HTTPRoute` resource defines a route for the path `/coffee` that sends 80% of the requests to `coffee-v1` and 20% to `coffee-v2`.
56+
57+
## 4. Test the Application
58+
59+
To access the application, we will use `curl` to send requests to `/coffee`:
60+
61+
```
62+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee
63+
```
64+
65+
80% of the responses will come from `coffee-v1`:
66+
67+
```
68+
Server address: 10.12.0.18:80
69+
Server name: coffee-v1-7c57c576b-rfjsh
70+
```
71+
72+
20% of the responses will come from `coffee-v2`:
73+
74+
```
75+
Server address: 10.12.0.19:80
76+
Server name: coffee-v2-698f66dc46-vcb6r
77+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: gateway.networking.k8s.io/v1beta1
2+
kind: HTTPRoute
3+
metadata:
4+
name: cafe-route
5+
spec:
6+
parentRefs:
7+
- name: gateway
8+
sectionName: http
9+
hostnames:
10+
- "cafe.example.com"
11+
rules:
12+
- matches:
13+
- path:
14+
type: PathPrefix
15+
value: /coffee
16+
backendRefs:
17+
- name: coffee-v1
18+
port: 80
19+
weight: 80
20+
- name: coffee-v2
21+
port: 80
22+
weight: 20

examples/traffic-splitting/cafe.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: coffee-v1
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: coffee-v1
10+
template:
11+
metadata:
12+
labels:
13+
app: coffee-v1
14+
spec:
15+
containers:
16+
- name: coffee-v1
17+
image: nginxdemos/nginx-hello:plain-text
18+
ports:
19+
- containerPort: 8080
20+
---
21+
apiVersion: v1
22+
kind: Service
23+
metadata:
24+
name: coffee-v1
25+
spec:
26+
ports:
27+
- port: 80
28+
targetPort: 8080
29+
protocol: TCP
30+
name: http
31+
selector:
32+
app: coffee-v1
33+
---
34+
apiVersion: apps/v1
35+
kind: Deployment
36+
metadata:
37+
name: coffee-v2
38+
spec:
39+
replicas: 1
40+
selector:
41+
matchLabels:
42+
app: coffee-v2
43+
template:
44+
metadata:
45+
labels:
46+
app: coffee-v2
47+
spec:
48+
containers:
49+
- name: coffee-v2
50+
image: nginxdemos/nginx-hello:plain-text
51+
ports:
52+
- containerPort: 8080
53+
---
54+
apiVersion: v1
55+
kind: Service
56+
metadata:
57+
name: coffee-v2
58+
spec:
59+
ports:
60+
- port: 80
61+
targetPort: 8080
62+
protocol: TCP
63+
name: http
64+
selector:
65+
app: coffee-v2
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: gateway.networking.k8s.io/v1beta1
2+
kind: Gateway
3+
metadata:
4+
name: gateway
5+
labels:
6+
domain: k8s-gateway.nginx.org
7+
spec:
8+
gatewayClassName: nginx
9+
listeners:
10+
- name: http
11+
port: 80
12+
protocol: HTTP

0 commit comments

Comments
 (0)