-
Notifications
You must be signed in to change notification settings - Fork 118
HTTP Traffic Splitting #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Example | ||
|
||
In this example we will deploy NGINX Kubernetes Gateway and configure traffic splitting for a simple cafe application. | ||
We will use `HTTPRoute` resources to split traffic between two versions of the application -- `coffee-v1` and `coffee-v2`. | ||
|
||
## Running the Example | ||
|
||
## 1. Deploy NGINX Kubernetes Gateway | ||
|
||
1. Follow the [installation instructions](/docs/installation.md) to deploy NGINX Gateway. | ||
|
||
1. Save the public IP address of NGINX Kubernetes Gateway into a shell variable: | ||
|
||
``` | ||
GW_IP=XXX.YYY.ZZZ.III | ||
``` | ||
|
||
1. Save the port of NGINX Kubernetes Gateway: | ||
|
||
``` | ||
GW_PORT=<port number> | ||
``` | ||
|
||
## 2. Deploy the Coffee Application | ||
|
||
1. Create the Cafe Deployments and Services: | ||
|
||
``` | ||
kubectl apply -f cafe.yaml | ||
``` | ||
|
||
1. Check that the Pods are running in the `default` namespace: | ||
|
||
``` | ||
kubectl -n default get pods | ||
NAME READY STATUS RESTARTS AGE | ||
coffee-v1-7c57c576b-rfjsh 1/1 Running 0 21m | ||
coffee-v2-698f66dc46-vcb6r 1/1 Running 0 21m | ||
``` | ||
|
||
## 3. Configure Routing | ||
|
||
1. Create the `Gateway`: | ||
|
||
``` | ||
kubectl apply -f gateway.yaml | ||
``` | ||
|
||
1. Create the `HTTPRoute` resources: | ||
|
||
``` | ||
kubectl apply -f cafe-route.yaml | ||
``` | ||
|
||
This `HTTPRoute` resource defines a route for the path `/coffee` that sends 80% of the requests to `coffee-v1` and 20% to `coffee-v2`. | ||
In this example, we use 80 and 20; however, the weights are calculated proportionally and do not need to sum to 100. | ||
For example, the weights of 8 and 2, 16 and 4, or 32 and 8 all evaluate to the same relative proportions. | ||
|
||
## 4. Test the Application | ||
|
||
To access the application, we will use `curl` to send requests to `/coffee`: | ||
|
||
``` | ||
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee | ||
``` | ||
|
||
80% of the responses will come from `coffee-v1`: | ||
|
||
``` | ||
Server address: 10.12.0.18:80 | ||
Server name: coffee-v1-7c57c576b-rfjsh | ||
``` | ||
|
||
20% of the responses will come from `coffee-v2`: | ||
|
||
``` | ||
Server address: 10.12.0.19:80 | ||
Server name: coffee-v2-698f66dc46-vcb6r | ||
``` | ||
|
||
### 5. Modify the Traffic Split Configuration | ||
|
||
Let's shift more of the traffic to `coffee-v2`. To do this we will update the `HTTPRoute` resource and change the weight | ||
of the `coffee-v2` backend to 80. Backends with equal weights will receive an equal share of traffic. | ||
|
||
1. Apply the updated `HTTPRoute` resource: | ||
|
||
``` | ||
kubectl apply -f cafe-route-equal-weight.yaml | ||
``` | ||
|
||
2. Test the application again: | ||
|
||
``` | ||
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee | ||
``` | ||
|
||
The responses will now be split evenly between `coffee-v1` and `coffee-v2`. | ||
|
||
We can continue modifying the weights of the backends to shift more and more traffic to `coffee-v2`. If there's an issue | ||
with `coffee-v2`, we can quickly shift traffic back to `coffee-v1`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: HTTPRoute | ||
metadata: | ||
name: cafe-route | ||
spec: | ||
parentRefs: | ||
- name: gateway | ||
sectionName: http | ||
hostnames: | ||
- "cafe.example.com" | ||
rules: | ||
- matches: | ||
- path: | ||
type: PathPrefix | ||
value: /coffee | ||
backendRefs: | ||
- name: coffee-v1 | ||
port: 80 | ||
weight: 80 | ||
- name: coffee-v2 | ||
port: 80 | ||
weight: 80 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: HTTPRoute | ||
metadata: | ||
name: cafe-route | ||
spec: | ||
parentRefs: | ||
- name: gateway | ||
sectionName: http | ||
hostnames: | ||
- "cafe.example.com" | ||
rules: | ||
- matches: | ||
- path: | ||
type: PathPrefix | ||
value: /coffee | ||
backendRefs: | ||
- name: coffee-v1 | ||
port: 80 | ||
weight: 80 | ||
- name: coffee-v2 | ||
port: 80 | ||
weight: 20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: coffee-v1 | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: coffee-v1 | ||
template: | ||
metadata: | ||
labels: | ||
app: coffee-v1 | ||
spec: | ||
containers: | ||
- name: coffee-v1 | ||
image: nginxdemos/nginx-hello:plain-text | ||
ports: | ||
- containerPort: 8080 | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: coffee-v1 | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: coffee-v1 | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: coffee-v2 | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: coffee-v2 | ||
template: | ||
metadata: | ||
labels: | ||
app: coffee-v2 | ||
spec: | ||
containers: | ||
- name: coffee-v2 | ||
image: nginxdemos/nginx-hello:plain-text | ||
ports: | ||
- containerPort: 8080 | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: coffee-v2 | ||
spec: | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: coffee-v2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: gateway.networking.k8s.io/v1beta1 | ||
kind: Gateway | ||
metadata: | ||
name: gateway | ||
labels: | ||
domain: k8s-gateway.nginx.org | ||
spec: | ||
gatewayClassName: nginx | ||
listeners: | ||
- name: http | ||
port: 80 | ||
protocol: HTTP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
) | ||
|
||
// executes the template with the given data. | ||
func execute(template *template.Template, data interface{}) []byte { | ||
var buf bytes.Buffer | ||
|
||
err := template.Execute(&buf, data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return buf.Bytes() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nginxinc/nginx-kubernetes-gateway/internal/nginx/config/http" | ||
) | ||
|
||
func TestExecute(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
t.Errorf("execute() panicked with %v", r) | ||
} | ||
}() | ||
|
||
bytes := execute(serversTemplate, []http.Server{}) | ||
if len(bytes) == 0 { | ||
t.Error("template.execute() did not generate anything") | ||
} | ||
} | ||
|
||
func TestExecutePanics(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Error("template.execute() did not panic") | ||
} | ||
}() | ||
|
||
_ = execute(serversTemplate, "not-correct-data") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.