Expose workloads in multiple Namespaces with a single APIRule definition
This tutorial shows how to expose service endpoints in multiple Namespaces using API Gateway Controller.
Prerequisites
Create a sample HttpBin service deployment and a sample Function in the separate Namespaces:
Create a Namespace for the HttpBin service and export its value as an environment variable. Run:
Click to copyexport NAMESPACE_HTTPBIN={NAMESPACE_HTTPBIN}kubectl create ns $NAMESPACE_HTTPBINkubectl label namespace $NAMESPACE_HTTPBIN istio-injection=enabled --overwriteCreate a different Namespace for the Function service and export its value as an environment variable. Run:
Click to copyexport NAMESPACE_FUNCTION={NAMESPACE_FUNCTION}kubectl create ns $NAMESPACE_FUNCTIONkubectl label namespace $NAMESPACE_FUNCTION istio-injection=enabled --overwriteDeploy an instance of the HttpBin service in its Namespace using the sample code:
Click to copykubectl -n $NAMESPACE_HTTPBIN create -f https://raw.githubusercontent.com/istio/istio/master/samples/httpbin/httpbin.yamlCreate a Function in its Namespace using the sample code:
Click to copykubectl -n $NAMESPACE_FUNCTION apply -f https://raw.githubusercontent.com/kyma-project/kyma/main/docs/03-tutorials/assets/function.yaml
Create a Namespace for the Gateway and APIRule CRs and export its value as the environment variable. Run:
Click to copyexport NAMESPACE={NAMESPACE}kubectl create ns $NAMESPACEkubectl label namespace $NAMESPACE istio-injection=enabled --overwriteDepending on whether you use your custom domain or a Kyma domain, export the necessary values as environment variables:
- Custom domain
- Kyma domain
Expose and access your workloads in multiple Namespaces
Expose the HttpBin and Function services in their respective Namespaces by creating an APIRule CR in its own Namespace. Run:
Click to copycat <<EOF | kubectl apply -f -apiVersion: gateway.kyma-project.io/v1beta1kind: APIRulemetadata:name: httpbin-and-functionnamespace: $NAMESPACEspec:host: httpbin-and-function.$DOMAIN_TO_EXPOSE_WORKLOADSgateway: $GATEWAYrules:- path: /headersmethods: ["GET"]service:name: httpbinnamespace: $NAMESPACE_HTTPBINport: 8000accessStrategies:- handler: noopmutators:- handler: noop- path: /functionmethods: ["GET"]service:name: functionnamespace: $NAMESPACE_FUNCTIONport: 80accessStrategies:- handler: noopmutators:- handler: noopEOFNOTE: If you are running Kyma on k3d, add
httpbin.kyma.local
to the entry with k3d IP in your system's/etc/hosts
file.Call the HttpBin endpoint by sending a
GET
request to the HttpBin service:Click to copycurl -ik -X GET https://httpbin-and-function.$DOMAIN_TO_EXPOSE_WORKLOADS/headersIf successful, the call returns the code
200 OK
response.Call the Function endpoint by sending a
GET
request to the Function service:Click to copycurl -ik -X GET https://httpbin-and-function.$DOMAIN_TO_EXPOSE_WORKLOADS/functionIf successful, the call returns the code
200 OK
response.