Proxy Requests to a Service
This tutorial shows how to configure a route with the Ingress Controller and verify requests through the gateway to a sample service in the cluster.
Prerequisites
Before you begin:
- Complete Set Up Ingress Controller and Gateway.
- If you used API7 Dashboard, replace the example namespace
aicwith the namespace shown in the generated manifests. - If you used API7 Dashboard, replace the example Gateway or IngressClass name
apisixwith the corresponding generated name. - Keep shared Kubernetes API identifiers, including
apisix.apache.organdApisixRoute, unchanged.
Deploy an Example Upstream Service
Create the following Kubernetes manifest to deploy an HTTPBIN service in the aic namespace:
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 route manifest that proxies requests to the HTTPBIN sample service:
- Gateway API
- Ingress
- APISIX CRD
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
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: aic
name: getting-started-ip
spec:
ingressClassName: apisix
rules:
- http:
paths:
- backend:
service:
name: httpbin
port:
number: 80
path: /ip
pathType: Exact
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: getting-started-ip
spec:
ingressClassName: apisix
http:
- name: getting-started-ip
match:
paths:
- /ip
backends:
- serviceName: httpbin
servicePort: 80
Apply the route manifest:
kubectl apply -f httpbin-route.yaml
Verify Routing
Expose the gateway’s service port to your local machine. Use aic for the APISIX local setup or the namespace selected in API7 Dashboard:
# replace with your gateway’s service name
kubectl port-forward svc/<gateway-service-name> -n <namespace> 18080:80 &
The example uses local port 18080 so it can run alongside a quickstart gateway on port 9080. If 18080 is already in use, choose another available local port and use it in the request below.
Send a request to the route:
curl "http://127.0.0.1:18080/ip"
You should receive an HTTPBIN response similar to the following. The origin value depends on the client address seen by HTTPBIN:
{
"origin": "127.0.0.1"
}
Delete the Route
Delete the route:
kubectl delete -f httpbin-route.yaml
Next Step
Continue to Common Use Cases to explore plugins for traffic control, authentication, transformation, and observability.