Kubernetes

This feature was requested as part of Issue 345 . to add support for kubernetes’s service discovery provider.

The first thing you need to do is install the NuGet package that provides kubernetes support in Ocelot.

Install-Package Ocelot.Provider.Kubernetes

Then add the following to your ConfigureServices method.

s.AddOcelot()
 .AddKubernetes();

If you have services deployed in kubernetes you will normally use the naming service to access them.

The following example shows how to set up a ReRoute that will work in kubernetes. The most important thing is the ServiceName which is made up of the kubernetes service name. We also need to set up the ServiceDiscoveryProvider in GlobalConfiguration. The example here shows a typical configuration. It assumes kubernetes api server is running on 192.168.0.13 and that api service is on port 443.

  {
"ReRoutes": [
  {
    "DownstreamPathTemplate": "/api/values",
    "DownstreamScheme": "http",
    "UpstreamPathTemplate": "/values",
    "ServiceName": "downstreamservice",
    "UpstreamHttpMethod": [ "Get" ]
  }
],
"GlobalConfiguration": {
  "ServiceDiscoveryProvider": {
    "Host": "192.168.0.13",
    "Port": 443,
    "Token": "txpc696iUhbVoudg164r93CxDTrKRVWG",
    "Namespace": "dev",
    "Type": "kube"
  }
}

}

You use Ocelot to poll kubernetes for latest service information rather than per request. If you want to poll kubernetes for the latest services rather than per request (default behaviour) then you need to set the following configuration.


“ServiceDiscoveryProvider”: {
“Host”: “192.168.0.13”,
“Port”: 443, “Token”: “txpc696iUhbVoudg164r93CxDTrKRVWG”, “Namespace”: “dev”, “Type”: “pollkube” “PollingInterval”: 100

}

The polling interval is in milliseconds and tells Ocelot how often to call kubernetes for changes in service configuration.

Please note there are tradeoffs here. If you poll kubernetes it is possible Ocelot will not know if a service is down depending on your polling interval and you might get more errors than if you get the latest services per request. This really depends on how volatile your services are. I doubt it will matter for most people and polling may give a tiny performance improvement over calling kubernetes per request. There is no way for Ocelot to work these out for you.