Skip to main content

Proxy Requests to a Service

This tutorial shows how to proxy requests through the Ingress Controller to a sample service in the cluster and verify the route.

Prerequisite​

  1. Complete Set Up Ingress Controller and Gateway.

The manifests below use values from the APISIX local setup. If you used API7 Dashboard, make the following substitutions:

APISIX exampleAPI7 replacement
aic namespaceNamespace shown in the Dashboard-generated manifests
apisix Gateway or IngressClassCorresponding name shown in the generated Gateway or IngressClass manifest

Keep shared Kubernetes API identifiers, including apisix.apache.org and ApisixRoute, unchanged.

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.