README ¶
Routing across Knative Services
This example shows how to map multiple Knative services to different paths under a single domain name using the Istio VirtualService concept. Istio is a general-purpose reverse proxy, therefore these directions can also be used to configure routing based on other request data such as headers, or even to map Knative and external resources under the same domain name.
In this sample, we set up two web services: "Search" service and "Login"
service, which simply read in an env variable 'SERVICE_NAME' and prints
"${SERVICE_NAME} is called"
. We'll then create a VirtualService with host
"example.com", and define routing rules in the VirtualService so that
"example.com/search" maps to the Search service, and "example.com/login" maps
to the Login service.
Prerequisites
- A Kubernetes cluster with Knative Serving installed.
- Install Docker.
- Acquire a domain name.
- In this example, we use example.com. If you don't have a domain name, you can modify your hosts file (on Mac or Linux) to map example.com to your cluster's ingress IP.
- Check out the code:
go get -d github.com/knative/docs/serving/samples/knative-routing-go
Setup
Build the application container and publish it to a container registry:
- Move into the sample directory:
cd $GOPATH/src/github.com/knative/docs
- Set your preferred container registry:
export REPO="gcr.io/<YOUR_PROJECT_ID>"
This example shows how to use Google Container Registry (GCR). You will need a Google Cloud Project and to enable the Google Container Registry API.
- Use Docker to build your application container:
docker build \
--tag "${REPO}/serving/samples/knative-routing-go" \
--file=serving/samples/knative-routing-go/Dockerfile .
- Push your container to a container registry:
docker push "${REPO}/serving/samples/knative-routing-go"
-
Replace the image reference path with our published image path in the configuration file (
serving/samples/knative-routing-go/sample.yaml
):- Manually replace:
image: github.com/knative/docs/serving/samples/knative-routing-go
withimage: <YOUR_CONTAINER_REGISTRY>/serving/samples/knative-routing-go
Or
- Use run this command:
perl -pi -e "s@github.com/knative/docs@${REPO}@g" serving/samples/knative-routing-go/sample.yaml
- Manually replace:
Deploy the Service
Deploy the Knative Serving sample:
kubectl apply --filename serving/samples/knative-routing-go/sample.yaml
Exploring the Routes
A shared Gateway "knative-shared-gateway" is used within Knative service mesh for serving all incoming traffic. You can inspect it and its corresponding Kubernetes service with:
- Check the shared Gateway:
kubectl get Gateway --namespace knative-serving --output yaml
- Check the corresponding Kubernetes service for the shared Gateway:
kubectl get svc knative-ingressgateway --namespace istio-system --output yaml
- Inspect the deployed Knative services with:
kubectl get service.serving.knative.dev
You should see 2 Knative services: search-service and login-service.
Access the Services
- Find the shared Gateway IP and export as an environment variable:
export GATEWAY_IP=`kubectl get svc knative-ingressgateway --namespace istio-system \
--output jsonpath="{.status.loadBalancer.ingress[*]['ip']}"`
- Find the "Search" service route and export as an environment variable:
export SERVICE_HOST=`kubectl get route search-service --output jsonpath="{.status.domain}"`
- Make a curl request to the service:
curl http://${GATEWAY_IP} --header "Host:${SERVICE_HOST}"
You should see: Search Service is called !
- Similarly, you can also directly access "Login" service with:
export SERVICE_HOST=`kubectl get route login-service --output jsonpath="{.status.domain}"`
curl http://${GATEWAY_IP} --header "Host:${SERVICE_HOST}"
You should see: Login Service is called !
Apply Custom Routing Rule
- Apply the custom routing rules defined in
routing.yaml
file with:
kubectl apply --filename serving/samples/knative-routing-go/routing.yaml
- The
routing.yaml
file will generate a new VirtualService "entry-route" for domain "example.com". View the VirtualService:
kubectl get VirtualService entry-route --output yaml
-
Send a request to the "Search" service and the "Login" service by using corresponding URIs. You should get the same results as directly accessing these services.
- Get the ingress IP:
export GATEWAY_IP=`kubectl get svc knative-ingressgateway --namespace istio-system \ --output jsonpath="{.status.loadBalancer.ingress[*]['ip']}"`
- Send a request to the Search service:
curl http://${GATEWAY_IP}/search --header "Host:example.com"
- Send a request to the Login service:
curl http://${GATEWAY_IP}/login --header "Host:example.com"
How It Works
When an external request with host "example.com" reaches
"knative-shared-gateway" Gateway, the "entry-route" VirtualService will check
if it has /search
or /login
URI. If the URI matches, then the host of
request will be rewritten into the host of "Search" service or "Login" service
correspondingly. This resets the final destination of the request.
The request with updated host will be forwarded to "knative-shared-gateway"
Gateway again. The Gateway proxy checks the updated host, and forwards it to
"Search" or "Login" service according to its host setting.
Clean Up
To clean up the sample resources:
kubectl delete --filename serving/samples/knative-routing-go/sample.yaml
kubectl delete --filename serving/samples/knative-routing-go/routing.yaml
Documentation ¶
There is no documentation for this package.