Expose multiple workloads on the same host
This tutorial shows how to expose multiple workloads that share the same host on different paths.
You can either define a service at the root level, which is applied to all paths except the ones you've explicitly set service for at the rules level, or you can just define different services on each path separately without the need to define a root service.
The tutorial may be a follow-up to the Set up a custom domain for a workload tutorial.
Prerequisites
This tutorial is based on a sample HttpBin service deployment and a sample Function. To deploy or create those workloads, follow the Create a workload tutorial.
Root level service definition and multiple services definition on different paths
Follow the instructions to expose your instance of the HttpBin service and your sample Function on different paths with a service defined at the root level - HttpBin in the following example.
NOTE: The service definitions at the spec.rules level have higher precedence than the service definition at the spec.service level.
Export the following value as an environment variable:
Click to copyexport DOMAIN_TO_EXPOSE_WORKLOADS={DOMAIN_NAME}export GATEWAY=$NAMESPACE/httpbin-gatewayNOTE:
DOMAIN_NAME
is the domain that you own, for example, api.mydomain.com. If you don't want to use your custom domain, replaceDOMAIN_NAME
with a Kyma domain and$NAMESPACE/httpbin-gateway
with Kyma's default Gatewaykyma-system/kyma-gateway
To expose the instance of the HttpBin service and the instance of the sample Function, create an API Rule CR in your Namespace. In the following example, the services definition at the spec.rules level overwrites the service definition at the spec.service level. Run:
Click to copycat <<EOF | kubectl apply -f -apiVersion: gateway.kyma-project.io/v1beta1kind: APIRulemetadata:name: multiple-servicenamespace: $NAMESPACElabels:app: multiple-serviceexample: multiple-servicespec:host: multiple-service-example.$DOMAIN_TO_EXPOSE_WORKLOADSgateway: $GATEWAYservice:name: httpbinport: 8000rules:- path: /headersmethods: ["GET"]accessStrategies:- handler: noop- path: /functionmethods: ["GET"]accessStrategies:- handler: noopservice:name: functionport: 80EOFTo call the endpoints, send
GET
requests to the HttpBin service and the sample Function:Click to copycurl -ik -X GET https://multiple-service-example.$DOMAIN_TO_EXPOSE_WORKLOADS/headers # Send a GET request to the HttpBincurl -ik -X GET https://multiple-service-example.$DOMAIN_TO_EXPOSE_WORKLOADS/function # Send a GET request to the FunctionThese calls return the code 200 response.
Multiple services definition on different paths
Follow the instruction to expose your instance of the HttpBin service and your sample Function at the same time on different paths.
Export the following value as an environment variable:
Click to copyexport DOMAIN_TO_EXPOSE_WORKLOADS={DOMAIN_NAME}export GATEWAY=$NAMESPACE/httpbin-gatewayNOTE:
DOMAIN_NAME
is the domain that you own, for example, api.mydomain.com. If you don't want to use your custom domain, replaceDOMAIN_NAME
with a Kyma domain and$NAMESPACE/httpbin-gateway
with Kyma's default Gatewaykyma-system/kyma-gateway
To expose the instance of the HttpBin service and the instance of the sample Function, create an API Rule CR in your Namespace. Run:
Click to copycat <<EOF | kubectl apply -f -apiVersion: gateway.kyma-project.io/v1beta1kind: APIRulemetadata:name: multiple-servicenamespace: $NAMESPACElabels:app: multiple-serviceexample: multiple-servicespec:host: multiple-service-example.$DOMAIN_TO_EXPOSE_WORKLOADSgateway: $GATEWAYrules:- path: /headersmethods: ["GET"]accessStrategies:- handler: noopservice:name: httpbinport: 8000- path: /functionmethods: ["GET"]accessStrategies:- handler: noopservice:name: functionport: 80EOF
To call the endpoints, send
GET
requests to the HttpBin service and the sample Function:Click to copycurl -ik -X GET https://multiple-service-example.$DOMAIN_TO_EXPOSE_WORKLOADS/headers # Send a GET request to the HttpBincurl -ik -X GET https://multiple-service-example.$DOMAIN_TO_EXPOSE_WORKLOADS/function # Send a GET request to the FunctionThese calls return the code 200 response.