Skip to content

Commit d965b81

Browse files
author
m1093782566
committed
add service topology kep
1 parent 04ff155 commit d965b81

File tree

2 files changed

+194
-1
lines changed

2 files changed

+194
-1
lines changed

keps/NEXT_KEP_NUMBER

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
31
1+
32
+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
---
2+
kep-number: 31
3+
title: Topology-aware service routing
4+
status: Pending
5+
authors:
6+
- "@m1093782566"
7+
owning-sig: sig-network
8+
reviewers:
9+
- "@thockin"
10+
- "@johnbelamaric"
11+
approvers:
12+
- "@thockin"
13+
creation-date: 2018-10-24
14+
last-updated: 2018-10-26
15+
---
16+
17+
# Topology-aware service routing
18+
19+
## Table of Contents
20+
21+
* [Motivation](#motivation)
22+
* [Goals](#goals)
23+
* [Non\-goals](#non-goals)
24+
* [User cases](#user-cases)
25+
* [Background](#background)
26+
* [Proposal](#proposal)
27+
* [Implementation History](#implementation-history)
28+
* [Service API changes](#service-api-changes)
29+
* [Endpoints API changes](#endpoints-api-changes)
30+
* [Endpoints Controller changes](#endpoints-controller-changes)
31+
* [Kube-proxy changes](#kube-proxy-changes)
32+
* [DNS changes](#dns-changes)
33+
34+
35+
## Motivation
36+
37+
Figure out a generic way to implement the "local service" route, say "topology aware routing of service".
38+
39+
Locality is defined by user, it can be any topology-related thing. "Local" means the "same topology level", e.g. same node, same rack, same failure zone, same failure region, same cloud provider etc. Two nodes are considered "local" if they have the same value for a particular label, called the "topology key".
40+
41+
### Goals
42+
43+
A generic way to support topology aware routing of services in arbitrary topological domains, e.g. node, rack, zone, region, etc. by node labels.
44+
45+
### Non-goals
46+
47+
* Scheduler spreading to implement this sort of topology guarantee
48+
* Dynamic Availability
49+
* Health-checking
50+
* Capacity-based or load-based spillover
51+
52+
### User cases
53+
54+
* Logging agents such as fluentd. Deploy fluentd as DaemonSet and applications only need to communicate with the fluentd in the same node.
55+
* For a sharded service that keeps per-node local information in each shard.
56+
* Authenticating proxies such as [aws-es-proxy](https://github.com/kopeio/aws-es-proxy).
57+
* In container identity wg, being able to give daemonset pods a unique identity per host is on the 2018 plan, and ensuring local pods can communicate to local node services securely is a key goal there. -- from @smarterclayton
58+
* Regional data costs in multi-AZ setup - for instance, in AWS, with a multi-AZ setup, half of the traffic will switch AZ, incurring regional data Transfer costs, whereas if something was local, it wouldn't hit the network.
59+
* Performance benefit (node local/rack local) is lower latency/higher bandwidth.
60+
61+
### Background
62+
63+
It's a pain point for multi-zone clusters deployment since cross-zone network traffic being charged, while in-zone is not. In addition, cross-node traffic may carry sensitive metadata from other nodes. Therefore, users always prefer the service backends that close to them, e.g. same zone, rack and host etc. for security, performance and cost concerns.
64+
65+
Kubernetes scheduler can constraining a pod to only be able to run on particular nodes/zones. However, Kubernetes service proxy just randomly picks an available backend for service routing and this one can be very far from the user, so we need a topology-aware service routing solution in Kubernetes. Basically, to find the nearest service backend. In other words, allowing people to configure if ALWAY reach a to local service backend. In this way, they can reduce network latency, improve security, save money and so on. However, because topology is arbitrary, zone, region, rack, generator, whatever, who knows? We should allow arbitrary locality.
66+
67+
`ExternalTrafficPolicy` was added in v1.4, but only for NodePort and external LB traffic. NodeName was added to `EndpointAddress` to allow kube-proxy to filter local endpoints for various future purposes.
68+
69+
Based on our experience of advanced routing setup and recent demo of enabling this feature in Kubernetes, this document would like to introduce a more generic way to support arbitrary service topology.
70+
71+
## Proposal
72+
73+
This proposal builds off of earlier requests to [use local pods only for kube-proxy loadbalancing](https://github.com/kubernetes/kubernetes/issues/7433) and [node-local service proposal](https://github.com/kubernetes/kubernetes/pull/28637). But, this document proposes that not only the particular "node-local" user case should be taken care, but also a more generic way should be figured out.
74+
75+
Locality is an "user-defined" thing. When we set topology key "hostname" for service, we expect node carries different node labels on the key "hostname".
76+
77+
Users can control the level of topology. For example, if someone run logging agent as a daemonset, he can set the "hard" topology requirement for same-host. If "hard" is not met, then just return "service not available".
78+
79+
And if someone set a "soft" topology requirement for same-host, say he "preferred" same-host endpoints and can accept other hosts when for some reasons local service's backend is not available on some host.
80+
81+
If multiple endpoints satisfy the "hard" or "soft" topology requirement, we will randomly pick one by default.
82+
83+
Routing decision is expected to be implemented by kube-proxy and kube-dns/coredns for headless service.
84+
85+
86+
## Implementation history
87+
88+
### Service API changes
89+
90+
Users need a way to declare what service is local and the definition of local backends for the particular service.
91+
92+
In this proposal, we give the service owner a chance to configure the service locality things. A new property would be introduced to `ServiceSpec`, say `topologyKeys` - it's a string slice and should be optional.
93+
94+
```go
95+
type ServiceSpec struct {
96+
// topologyKeys is a preference-order list of topology keys. If backends exist for
97+
// index [0], they will always be chosen; only if no backends exist for index [0] will backends for index [1] be considered.
98+
// If this field is specified and all indices have no backends, the service has no backends, and connections will fail. We say these requirements are hard.
99+
// In order to experss soft requirement, we may give a special node label key "" as it means "match all nodes".
100+
TopologyKeys []string `json:"topologyKeys" protobuf:"bytes,1,opt,name=topologyKeys"`
101+
}
102+
```
103+
104+
An example of `Service` with topology keys:
105+
106+
```yaml
107+
kind: Service
108+
metadata:
109+
name: service-local
110+
spec:
111+
topologyKeys: ["host", "zone"]
112+
```
113+
114+
115+
In our example above, we will firstly try to find the backends in the same host. If no backends match, we will then try the lucky of same zone. If finally we can't find any backends in the same host or same zone, then we say the service has no satisfied backends and connections will fail.
116+
117+
If we configure topologyKeys as ["host", ""], we just do the effort to find the backends in the same host and will not fail the connection if no matched backends found.
118+
119+
### Endpoints API changes
120+
121+
Although `NodeName` was already added to `EndpointAddress`, we want `Endpoints` to carry more node's topological informations so that allowing more topology information other than hostname.
122+
123+
This proposal will create a new `Topologies` field in `Endpoints.Subsets.Addresses` for identifying what topological domain the backend pod exists.
124+
125+
```go
126+
type EndpointAddress struct {
127+
// labels of node hosting the endpoint
128+
Topologies map[string]string
129+
}
130+
```
131+
132+
Please note that we only copy the labels that we know are needed by the topological constraints. In other words, only copying the labels which are used by `serviceSpec.topologyKeys` from node to endpoint.
133+
134+
### Endpoints Controller changes
135+
136+
Endpoint Controller will populate the `Topologyies` property for each `EndpointAddress`. We want `EndpointAddress.Topology` to tell the LB, such as kube-proxy what topological domain the endpoint exists.
137+
138+
Endpoints controller will need to watch Nodes for knowing labels of node hosting the endpoint and copy the node labels referenced in the service spec's topology constraints to EndpointAddress.
139+
140+
Endpoints Controller will also maintain an extra cache: `NodeToPodsCache`.
141+
`NodeToPodsCache` maps the node's name to the pods running on it. Node's add, delete and labels' change will trigger `NodeToPodsCache` re-index.
142+
143+
So, the new logic of endpoint controller might be like:
144+
145+
```go
146+
go watch Node
147+
// In each sync loop, for a given service, sync its endpoints
148+
for i, pod := range service backends; do
149+
node := nodeCache[pod.Spec.NodeName]
150+
endpointAddress := &v1.EndpointAddress {}
151+
// Copy all topology-related labels of node to all the endpoints running on it.
152+
// We can only include node labels referenced in the service spec's topology constraints
153+
for _, topologyKey := range service.TopologyKeys; do
154+
endpointAddress.Topologies[topoKey] = node.Labels[topologyKey]
155+
done
156+
endpoints.Subsets[i].Addresses = endpointAddress
157+
done
158+
```
159+
160+
### Kube-proxy changes
161+
162+
Kube-proxy will respect topology keys for each service, so kube-proxy on different nodes may create different proxy rules.
163+
164+
Kube-proxy will watch or periodically get(which approach has better performance?) its own node and will find the endpoints that are in the same topological domain as the node if `service.TopologyKeys` is not empty.
165+
166+
The new logic of kube-proxy might be like:
167+
168+
```go
169+
go watch/periodically get node with its nodename
170+
endpointsMeetRequirement := make([]endpointInfo, 0)
171+
for _, topologyKey := range service.TopologyKeys; do
172+
for i := range service's endpoints.Subsets; do
173+
ss := endpoints.Subsets[i]
174+
for j := range ss.Addresses; do
175+
// check if endpoint are in the same topological domain as the node running kube-proxy
176+
if ss.Addresses[j].TopologyKey[topologyKey] == node.Labels[topologyKey]; then
177+
endpointsMeetRequirement = append(endpointsMeetHardRequirement, endpoint)
178+
fi
179+
done
180+
// Randomly pick one if there are some endpoints(>=1) matched
181+
if len(endpointsMeetRequirement) != 0; then
182+
route request to an endpoint randomly
183+
return
184+
fi
185+
done
186+
done
187+
conection fails due to no mactch endpoint
188+
}
189+
```
190+
191+
### DNS changes
192+
193+
We need to consider this kind of topology support for headless service in kube-dns and coredns.

0 commit comments

Comments
 (0)