Skip to content

Commit 1e4b4d2

Browse files
authored
HTTPS Termination (#140)
* HTTPS Termination This commit adds support for HTTPS listeners with a TLS mode of Terminate. Multiple HTTPS listeners are supported provided their hostnames do not conflict. Additionally, a gateway can have an HTTP and HTTPS listener with the same hostname. Limitations: - HTTPS listeners must listen on port 443 - Supports a single reference to a Kubernetes Secret of type kubernetes.io/tls - Secret must be in the same namespace as the Gateway - Secret must be created before the HTTPRoutes are created - Secret rotation is not supported - SNI enforcement is not implemented
1 parent ab761af commit 1e4b4d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3900
-305
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ You can deploy NGINX Kubernetes Gateway on an existing Kubernetes 1.16+ cluster.
9393
NAME READY STATUS RESTARTS AGE
9494
nginx-gateway-5d4f4c7db7-xk2kq 2/2 Running 0 112s
9595
```
96-
1. Create the Gateway resource:
97-
98-
```
99-
kubectl apply -f deploy/manifests/gateway.yaml
100-
```
10196
10297
## Expose NGINX Kubernetes Gateway
10398

deploy/manifests/nginx-gateway.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rules:
1313
- ""
1414
resources:
1515
- services
16+
- secrets
1617
verbs:
1718
- list
1819
- watch
@@ -80,7 +81,7 @@ spec:
8081
initContainers:
8182
- image: busybox:1.34 # FIXME(pleshakov): use gateway container to init the Config with proper main config
8283
name: nginx-config-initializer
83-
command: [ 'sh', '-c', 'echo "load_module /usr/lib/nginx/modules/ngx_http_js_module.so; events {} pid /etc/nginx/nginx.pid; http { include /etc/nginx/conf.d/*.conf; js_import /usr/lib/nginx/modules/njs/httpmatches.js; server { default_type text/html; return 404; } }" > /etc/nginx/nginx.conf && mkdir /etc/nginx/conf.d && chown 1001:0 /etc/nginx/conf.d' ]
84+
command: [ 'sh', '-c', 'echo "load_module /usr/lib/nginx/modules/ngx_http_js_module.so; events {} pid /etc/nginx/nginx.pid; http { include /etc/nginx/conf.d/*.conf; js_import /usr/lib/nginx/modules/njs/httpmatches.js; }" > /etc/nginx/nginx.conf && mkdir /etc/nginx/conf.d /etc/nginx/secrets && chown 1001:0 /etc/nginx/conf.d /etc/nginx/secrets' ]
8485
volumeMounts:
8586
- name: nginx-config
8687
mountPath: /etc/nginx
@@ -105,6 +106,8 @@ spec:
105106
ports:
106107
- name: http
107108
containerPort: 80
109+
- name: https
110+
containerPort: 443
108111
volumeMounts:
109112
- name: nginx-config
110113
mountPath: /etc/nginx

deploy/manifests/service/loadbalancer.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ spec:
1111
targetPort: 80
1212
protocol: TCP
1313
name: http
14+
- port: 443
15+
targetPort: 443
16+
protocol: TCP
17+
name: https
1418
selector:
1519
app: nginx-gateway

examples/advanced-routing/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ The cafe application consists of four services: `coffee-v1-svc`, `coffee-v2-svc`
4646

4747
## 3. Configure Routing
4848

49+
1. Create the `Gateway`:
50+
51+
```
52+
kubectl apply -f gateway.yaml
53+
```
54+
4955
1. Create the `HTTPRoute` resources:
5056

5157
```

examples/advanced-routing/cafe-routes.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ metadata:
55
spec:
66
parentRefs:
77
- name: gateway
8-
namespace: nginx-gateway
98
sectionName: http
109
hostnames:
1110
- "cafe.example.com"
@@ -41,7 +40,6 @@ metadata:
4140
spec:
4241
parentRefs:
4342
- name: gateway
44-
namespace: nginx-gateway
4543
sectionName: http
4644
hostnames:
4745
- "cafe.example.com"

deploy/manifests/gateway.yaml renamed to examples/advanced-routing/gateway.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ apiVersion: gateway.networking.k8s.io/v1alpha2
22
kind: Gateway
33
metadata:
44
name: gateway
5-
namespace: nginx-gateway
65
labels:
76
domain: k8s-gateway.nginx.org
87
spec:

examples/cafe-example/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ In this example we deploy NGINX Kubernetes Gateway, a simple web application, an
3939

4040
## 3. Configure Routing
4141

42+
1. Create the `Gateway`:
43+
44+
```
45+
kubectl apply -f gateway.yaml
46+
```
47+
4248
1. Create the `HTTPRoute` resources:
4349

4450
```

examples/cafe-example/cafe-routes.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ metadata:
55
spec:
66
parentRefs:
77
- name: gateway
8-
namespace: nginx-gateway
98
sectionName: http
109
hostnames:
1110
- "cafe.example.com"
@@ -21,7 +20,6 @@ metadata:
2120
spec:
2221
parentRefs:
2322
- name: gateway
24-
namespace: nginx-gateway
2523
sectionName: http
2624
hostnames:
2725
- "cafe.example.com"
@@ -41,7 +39,6 @@ metadata:
4139
spec:
4240
parentRefs:
4341
- name: gateway
44-
namespace: nginx-gateway
4542
sectionName: http
4643
hostnames:
4744
- "cafe.example.com"

examples/cafe-example/gateway.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: gateway.networking.k8s.io/v1alpha2
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
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# HTTPS Termination Example
2+
3+
In this example we expand on the simple [cafe-example](../cafe-example) by adding HTTPS termination to our routes.
4+
5+
## Running the Example
6+
7+
## 1. Deploy NGINX Kubernetes Gateway
8+
9+
1. Follow the [installation instructions](https://github.com/nginxinc/nginx-kubernetes-gateway/blob/main/README.md#run-nginx-gateway) to deploy NGINX Gateway.
10+
11+
1. Save the public IP address of NGINX Kubernetes Gateway into a shell variable:
12+
13+
```
14+
GW_IP=XXX.YYY.ZZZ.III
15+
```
16+
17+
1. Save the HTTPS port of NGINX Kubernetes Gateway:
18+
19+
```
20+
GW_HTTPS_PORT=port
21+
```
22+
23+
## 2. Deploy the Cafe Application
24+
25+
1. Create the coffee and the tea deployments and services:
26+
27+
```
28+
kubectl apply -f cafe.yaml
29+
```
30+
31+
1. Check that the Pods are running in the `default` namespace:
32+
33+
```
34+
kubectl -n default get pods
35+
NAME READY STATUS RESTARTS AGE
36+
coffee-6f4b79b975-2sb28 1/1 Running 0 12s
37+
tea-6fb46d899f-fm7zr 1/1 Running 0 12s
38+
```
39+
40+
## 3. Configure HTTPS Termination and Routing
41+
42+
1. Create a secret with a TLS certificate and key:
43+
```
44+
kubectl apply -f cafe-secret.yaml
45+
```
46+
47+
The TLS certificate and key in this secret are used to terminate the TLS connections for the cafe application.
48+
**Important**: This certificate and key are for demo purposes only.
49+
50+
1. Create the `Gateway` resource:
51+
```
52+
kubectl apply -f gateway.yaml
53+
```
54+
55+
This [gateway](./gateway.yaml) configures an `https` listener is to terminate TLS connections using the `cafe-secret` we created in the step 1.
56+
57+
1. Create the `HTTPRoute` resources:
58+
```
59+
kubectl apply -f cafe-routes.yaml
60+
```
61+
62+
To configure HTTPS termination for our cafe application, we will bind the `https` listener to our `HTTPRoutes` in [cafe-routes.yaml](./cafe-routes.yaml) using the [`parentRef`](https://gateway-api.sigs.k8s.io/v1alpha2/references/spec/#gateway.networking.k8s.io%2fv1alpha2.ParentReference) field:
63+
64+
```yaml
65+
parentRefs:
66+
- name: gateway
67+
namespace: default
68+
sectionName: https
69+
```
70+
71+
## 4. Test the Application
72+
73+
To access the application, we will use `curl` to send requests to the `coffee` and `tea` services.
74+
Since our certificate is self-signed, we'll use curl's `--insecure` option to turn off certificate verification.
75+
76+
To get coffee:
77+
78+
```
79+
curl --resolve cafe.example.com:$GW_HTTPS_PORT:$GW_IP https://cafe.example.com:$GW_HTTPS_PORT/coffee --insecure
80+
Server address: 10.12.0.18:80
81+
Server name: coffee-7586895968-r26zn
82+
```
83+
84+
To get tea:
85+
86+
```
87+
curl --resolve cafe.example.com:$GW_HTTPS_PORT:$GW_IP https://cafe.example.com:$GW_HTTPS_PORT/tea --insecure
88+
Server address: 10.12.0.19:80
89+
Server name: tea-7cd44fcb4d-xfw2x
90+
```

0 commit comments

Comments
 (0)