Skip to main content

Proxy Requests to a Service

This tutorial shows you how to create a basic route with the Ingress Controller that proxies requests to a sample service running in the cluster and verify that the routing functions correctly.

Prerequisite

  1. Complete Set Up Ingress Controller and Gateway.

Start an Example Upstream Service

Create the following Kubernetes manifest to deploy an HTTPBIN service in the aic namespace:

httpbin.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: aic
name: httpbin
spec:
replicas: 1
selector:
matchLabels:
app: httpbin
template:
metadata:
labels:
app: httpbin
spec:
containers:
- name: httpbin
image: kennethreitz/httpbin
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin
spec:
selector:
app: httpbin
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
type: ClusterIP

Apply the manifest:

kubectl apply -f httpbin.yaml

Create a Route

Create a Kubernetes manifest file for a route that will proxy requests to the HTTPBIN sample upstream service:

httpbin-route.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: getting-started-ip
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /ip
backendRefs:
- name: httpbin
port: 80

Apply the configurations to your cluster:

kubectl apply -f httpbin-route.yaml

Verify Routing

Expose the gateway’s service port to your local machine:

# replace with your gateway’s service name
kubectl port-forward svc/<gateway-service-name> 9080:80 &

Send a request to the route:

curl "http://127.0.0.1:9080/ip"

You should see a response of the following:

{
"origin": "127.0.0.1"
}

Delete the Route

To remove the previously created route, run the following command:

kubectl delete -f httpbin-route.yaml

Next Step

You have now learned the basics of proxying requests with a route. Next, you will explore the broader capabilities provided by the plugin ecosystem.