Expose an external application
This example shows how we can expose a service located outside the Kubernetes cluster using an Ingress.
Installation
Follow the deployment documentation to install the Kong Ingress Controller on the Kubernetes cluster.
Installing the Gateway APIs
If you wish to use the Gateway APIs examples, follow the supplemental Gateway APIs installation instructions.
Testing connectivity to Kong Gateway
Ensure that the PROXY_IP
environment variable is
set to contain the IP address or URL pointing to Kong Gateway.
The deployment guide that you used to install the Kong Ingress Controller on the Kubernetes cluster provides the instructions to configure this environment variable.
If everything is set correctly, a request to Kong Gateway returns
a HTTP 404 Not Found
status code:
curl -i $PROXY_IP
The results should look like this:
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Content-Length: 48
X-Kong-Response-Latency: 0
Server: kong/3.0.0
{"message":"no Route matched with those values"}
This is expected because Kong Gateway doesn’t know how to proxy the request yet.
Create a configuration group
Ingress and Gateway APIs controllers need a configuration that indicates which set of routing configuration they should recognize. This allows multiple controllers to coexist in the same cluster. Before creating individual routes, you need to create a class configuration to associate routes with:
Kong Ingress Controller recognizes the kong
IngressClass and
konghq.com/kic-gateway-controller
GatewayClass
by default. Setting the CONTROLLER_INGRESS_CLASS
or
CONTROLLER_GATEWAY_API_CONTROLLER_NAME
environment variable to
another value overrides these defaults.
Create a Kubernetes Service
First we need to create a Kubernetes Service type=ExternalName using the hostname of the application we want to expose:
echo "
kind: Service
apiVersion: v1
metadata:
name: proxy-to-httpbin
spec:
ports:
- protocol: TCP
port: 80
type: ExternalName
externalName: httpbin.org
" | kubectl apply -f -
Response:
service/echo created
Create an Ingress to expose the service at the path /httpbin
Test the Service
curl -si http://kong.example/httpbin/anything --resolve kong.example:80:$PROXY_IP
Response:
HTTP/1.1 200 OK
Date: Thu, 15 Dec 2022 21:31:47 GMT
Content-Type: application/json
Content-Length: 341
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Kong-Upstream-Latency: 2
X-Kong-Proxy-Latency: 1
Via: kong/3.1.1
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.86.0",
"X-Amzn-Trace-Id": "Root=1-639b9243-7cdb670008b8189a5948d619"
},
"json": null,
"method": "GET",
"origin": "136.25.153.9",
"url": "http://httpbin.org/anything"
}