Skip to content

Commit 6440451

Browse files
committed
Use a separate example for testing VSR functionality
1 parent ef5dd51 commit 6440451

File tree

11 files changed

+352
-0
lines changed

11 files changed

+352
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Cross-Namespace Configuration
2+
3+
In this example we use the [VirtualServer and
4+
VirtualServerRoute](https://docs.nginx.com/nginx-ingress-controller/configuration/virtualserver-and-virtualserverroute-resources/)
5+
resources to configure load balancing for the modified cafe application from the [Basic
6+
Configuration](../basic-configuration/) example. We have put the load balancing configuration as well as the deployments
7+
and services into multiple namespaces. Instead of one namespace, we now use three: `tea`, `coffee`, and `cafe`.
8+
9+
- In the tea namespace, we create the tea deployment, service, and the corresponding load-balancing configuration.
10+
- In the coffee namespace, we create the coffee deployment, service, and the corresponding load-balancing configuration.
11+
- In the cafe namespace, we create the cafe secret with the TLS certificate and key and the load-balancing configuration
12+
for the cafe application. That configuration references the coffee and tea configurations.
13+
14+
## Prerequisites
15+
16+
1. Follow the [installation](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/)
17+
instructions to deploy the Ingress Controller with custom resources enabled.
18+
1. Save the public IP address of the Ingress Controller into a shell variable:
19+
20+
```console
21+
IC_IP=XXX.YYY.ZZZ.III
22+
```
23+
24+
1. Save the HTTPS port of the Ingress Controller into a shell variable:
25+
26+
```console
27+
IC_HTTPS_PORT=<port number>
28+
```
29+
30+
## Step 1 - Create Namespaces
31+
32+
Create the required tea, coffee, and cafe namespaces:
33+
34+
```console
35+
kubectl create -f namespaces.yaml
36+
```
37+
38+
## Step 2 - Deploy the Cafe Application
39+
40+
1. Create the tea deployment and service in the tea namespace:
41+
42+
```console
43+
kubectl create -f tea.yaml
44+
```
45+
46+
1. Create the coffee deployment and service in the coffee namespace:
47+
48+
```console
49+
kubectl create -f coffee.yaml
50+
```
51+
52+
## Step 3 - Configure Load Balancing and TLS Termination
53+
54+
1. Create the VirtualServerRoute resource for tea in the tea namespace:
55+
56+
```console
57+
kubectl create -f tea-virtual-server-route.yaml
58+
```
59+
60+
1. Create the VirtualServerRoute resource for coffee in the coffee namespace:
61+
62+
```console
63+
kubectl create -f coffee-virtual-server-route.yaml
64+
```
65+
66+
1. Create the secret with the TLS certificate and key in the cafe namespace:
67+
68+
```console
69+
kubectl create -f cafe-secret.yaml
70+
```
71+
72+
1. Create the VirtualServer resource for the cafe app in the cafe namespace:
73+
74+
```console
75+
kubectl create -f cafe-virtual-server.yaml
76+
```
77+
78+
## Step 4 - Test the Configuration
79+
80+
1. Check that the configuration has been successfully applied by inspecting the events of the VirtualServerRoutes and
81+
VirtualServer:
82+
83+
```console
84+
kubectl describe virtualserverroute tea -n tea
85+
```
86+
87+
```text
88+
. . .
89+
Events:
90+
Type Reason Age From Message
91+
---- ------ ---- ---- -------
92+
Warning NoVirtualServersFound 2m nginx-ingress-controller No VirtualServer references VirtualServerRoute tea/tea
93+
Normal AddedOrUpdated 1m nginx-ingress-controller Configuration for tea/tea was added or updated
94+
```
95+
96+
```console
97+
kubectl describe virtualserverroute coffee -n coffee
98+
```
99+
100+
```text
101+
. . .
102+
Events:
103+
Type Reason Age From Message
104+
---- ------ ---- ---- -------
105+
Warning NoVirtualServersFound 2m nginx-ingress-controller No VirtualServer references VirtualServerRoute coffee/coffee
106+
Normal AddedOrUpdated 1m nginx-ingress-controller Configuration for coffee/coffee was added or updated
107+
```
108+
109+
```console
110+
kubectl describe virtualserver cafe -n cafe
111+
```
112+
113+
```text
114+
. . .
115+
Events:
116+
Type Reason Age From Message
117+
---- ------ ---- ---- -------
118+
Normal AddedOrUpdated 1m nginx-ingress-controller Configuration for cafe/cafe was added or updated
119+
```
120+
121+
1. Access the application using curl. We'll use curl's `--insecure` option to turn off certificate verification of our
122+
self-signed certificate and `--resolve` option to set the IP address and HTTPS port of the Ingress Controller to the
123+
domain name of the cafe application:
124+
125+
To get coffee:
126+
127+
```console
128+
curl --resolve cafe.example.com:$IC_HTTPS_PORT:$IC_IP https://cafe.example.com:$IC_HTTPS_PORT/coffee --insecure
129+
```
130+
131+
```text
132+
Server address: 10.16.1.193:80
133+
Server name: coffee-7dbb5795f6-mltpf
134+
...
135+
```
136+
137+
If your prefer tea:
138+
139+
```console
140+
curl --resolve cafe.example.com:$IC_HTTPS_PORT:$IC_IP https://cafe.example.com:$IC_HTTPS_PORT/tea --insecure
141+
```
142+
143+
```text
144+
Server address: 10.16.0.157:80
145+
Server name: tea-7d57856c44-674b8
146+
...
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: k8s.nginx.org/v1
2+
kind: Policy
3+
metadata:
4+
name: api-key-policy
5+
namespace: cafe
6+
spec:
7+
apiKey:
8+
suppliedIn:
9+
header:
10+
- "X-header-name"
11+
query:
12+
- "queryName"
13+
clientSecret: api-key-client-secret
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: api-key-client-secret
5+
namespace: cafe
6+
type: nginx.org/apikey
7+
data:
8+
client1: cGFzc3dvcmQ= # password
9+
client2: YW5vdGhlci1wYXNzd29yZA== # another-password
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: cafe-secret
5+
namespace: cafe
6+
type: kubernetes.io/tls
7+
data:
8+
tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURMakNDQWhZQ0NRREFPRjl0THNhWFdqQU5CZ2txaGtpRzl3MEJBUXNGQURCYU1Rc3dDUVlEVlFRR0V3SlYKVXpFTE1Ba0dBMVVFQ0F3Q1EwRXhJVEFmQmdOVkJBb01HRWx1ZEdWeWJtVjBJRmRwWkdkcGRITWdVSFI1SUV4MApaREViTUJrR0ExVUVBd3dTWTJGbVpTNWxlR0Z0Y0d4bExtTnZiU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E9ucTJBNWVpS2ErOGpSQVJZS0pMWU5kZkR1d29seHZHNmJaaGtQaS80RXRUCjNZMThzUUtCZ0h0S2JrKzdsTkpWZXN3WEU1Y1VHNkVEVXNEZS8yVWE3ZlhwN0ZjanFCRW9hcDFMU3crNlRYcDAKWmdybUtFOEFSek00NytFSkhVdmlpcS9udXBFMTVnMGtKVzNzeWhwVTl6WkxPN2x0QjBLSWtPOVpSY21Vam84UQpjcExsSE1BcWJMSjhXWUdKQ2toaVd4eWFsNmhZVHlXWTRjVmtDMHh0VGwvaFVFOUllTktvCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apiVersion: k8s.nginx.org/v1
2+
kind: VirtualServer
3+
metadata:
4+
name: cafe
5+
namespace: cafe
6+
spec:
7+
host: cafe.example.com
8+
tls:
9+
secret: cafe-secret
10+
server-snippets: |
11+
# snippet defined in VS server block
12+
proxy_set_header X-VS-Name "Cafe";
13+
routes:
14+
# - path: /tea
15+
# route: tea/tea
16+
# policies:
17+
# - name: rate-limit-policy
18+
# - path: /coffee
19+
# route: coffee/coffee
20+
- path: /
21+
routeSelector:
22+
matchLabels:
23+
app: cafe
24+
# route: tea
25+
policies:
26+
- name: api-key-policy
27+
location-snippets: |
28+
# snippet defined in VS
29+
proxy_set_header X-VS-Name "Cafe";
30+
errorPages:
31+
- codes: [ 502, 503 ]
32+
redirect:
33+
code: 301
34+
url: https://nginx.org
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: k8s.nginx.org/v1
2+
kind: VirtualServerRoute
3+
metadata:
4+
name: coffee
5+
namespace: coffee
6+
labels:
7+
app: cafe
8+
route: coffee
9+
spec:
10+
host: cafe.example.com
11+
upstreams:
12+
- name: coffee
13+
service: coffee-svc
14+
port: 80
15+
subroutes:
16+
- path: /coffee
17+
action:
18+
pass: coffee
19+
policies:
20+
- name: rate-limit-policy
21+
location-snippets: |
22+
# snippet defined in VSR
23+
proxy_set_header X-VSR-Name "Coffee";
24+
errorPages:
25+
- codes: [404]
26+
return:
27+
code: 200
28+
body: "Original resource not found, but success!"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: coffee
5+
namespace: coffee
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: coffee
11+
template:
12+
metadata:
13+
labels:
14+
app: coffee
15+
spec:
16+
containers:
17+
- name: coffee
18+
image: nginxdemos/nginx-hello:plain-text
19+
ports:
20+
- containerPort: 8080
21+
---
22+
apiVersion: v1
23+
kind: Service
24+
metadata:
25+
name: coffee-svc
26+
namespace: coffee
27+
spec:
28+
ports:
29+
- port: 80
30+
targetPort: 8080
31+
protocol: TCP
32+
name: http
33+
selector:
34+
app: coffee
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: cafe
5+
---
6+
apiVersion: v1
7+
kind: Namespace
8+
metadata:
9+
name: tea
10+
---
11+
apiVersion: v1
12+
kind: Namespace
13+
metadata:
14+
name: coffee
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: k8s.nginx.org/v1
2+
kind: Policy
3+
metadata:
4+
name: rate-limit-policy
5+
namespace: coffee
6+
spec:
7+
rateLimit:
8+
rate: 1r/s
9+
key: ${binary_remote_addr}
10+
zoneSize: 10M
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: k8s.nginx.org/v1
2+
kind: VirtualServerRoute
3+
metadata:
4+
name: tea
5+
namespace: tea
6+
labels:
7+
route: tea
8+
app: cafe
9+
spec:
10+
host: cafe.example.com
11+
upstreams:
12+
- name: tea
13+
service: tea-svc
14+
port: 80
15+
subroutes:
16+
- path: /tea
17+
action:
18+
pass: tea
19+
# location-snippets: |
20+
# # snippet defined in VSR
21+
# proxy_set_header X-VSR-Name "Tea";

0 commit comments

Comments
 (0)