Kubernetes 用の LoadBalancer

このプロジェクトには、Kubernetes エンドポイントまたは Kubernetes サービスに基づく負荷分散用の Spring Cloud ロードバランサーが含まれています。プロジェクトに含めるには、次の依存関係を追加します。Fabric8 実装

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-kubernetes-fabric8-loadbalancer</artifactId>
</dependency>

Kubernetes Java クライアントの実装

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-kubernetes-client-loadbalancer</artifactId>
</dependency>

ロードバランサが動作する「モード」には、POD と SERVICE の 2 つがあり、プロパティで示されます。(default being POD)

spring.cloud.kubernetes.loadbalancer.mode=SERVICE

または

spring.cloud.kubernetes.loadbalancer.mode=POD

POD モードでは、DiscoveryClient を使用してロードバランサー名に一致するすべてのサービスを検索します。例: 次のような構成の場合:

@Bean
@LoadBalanced
WebClient.Builder client() {
	return WebClient.builder();
}

その WebClient を使用して service-a (英語)  にリクエストを発行すると、service-a を使用してこの値で DiscoveryClient::getInstances を呼び出します。これは DiscoveryClient を使用しているため、それに固有のすべての構成が適用されます。これはドキュメントの関連部分で説明されています。

一方、SERVICE モードを使用する場合、状況は少し異なりますが、検出クライアント設定とよく似ています。例: service-a という名前のサービスをどの名前空間で検索するかという質問に答えるには、次のいずれかの設定を使用します。

spring.cloud.kubernetes.discovery.all-namespaces
spring.cloud.kubernetes.discovery.namespaces

すべての名前空間を検索するか、いわゆる「選択的名前空間」を検索するかを指定します。上記のいずれも指定されていない場合は、名前空間の解決が起動します。

Once we find all the services, we need to know what port to call them by. If the service in question has a single port defined, that is what we will use, no matter of its name. If there are no ports defined, this service will not be considered for load balancing and will be skipped.

If there are more then one ports defined, we will try to match its name to the value of the property (http by default):

spring.cloud.kubernetes.loadbalancer.portName

In case such a match is found, that port number will be used. Otherwise, the "first" port from the list will be used. This last option is non-deterministic and care must be taken.

Once we know the port, we know how to call that service. The URL will have the form:

service-a.<SERVICE_NAMESPACE>.svc.<DOMAIN>:<FOUND_PORT>

<SERVICE_NAMESPACE> is the namespace where the service resides, DOMAIN is the value of the property (by default it is equal to cluster.local):

spring.cloud.kubernetes.loadbalancer.clusterDomain

and <FOUND_PORT> is the port of the service that we have chosen described in the process above.

If a service needs to be accessed over HTTPS, you need to explicitly configure that. The rules for that are exactly the same as for the discovery implementation and can be found in the relevant part of the documentation regarding discovery-client.