|
| 1 | +--- |
| 2 | +reviewers: |
| 3 | +- maplain |
| 4 | +title: Service Internal Traffic Policy |
| 5 | +content_type: concept |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +<!-- overview --> |
| 10 | + |
| 11 | +{{< feature-state for_k8s_version="v1.21" state="alpha" >}} |
| 12 | + |
| 13 | +_Service Internal Traffic Policy_ enables internal traffic restrictions to only route |
| 14 | +internal traffic to endpoints within the node the traffic originated from. The |
| 15 | +"internal" traffic here refers to traffic originated from Pods in the current |
| 16 | +cluster. This can help to reduce costs and improve performance. |
| 17 | + |
| 18 | +<!-- body --> |
| 19 | + |
| 20 | +## Using Service Internal Traffic Policy |
| 21 | + |
| 22 | +You can enable Internal Traffic Policy for a Service by setting the |
| 23 | +`spec.internalTrafficPolicy` to "Local". This tells kube-proxy to only use node |
| 24 | +local endpoints for cluster internal traffic. Importantly, for pods on nodes with |
| 25 | +no endpoints for a given Service, the Service will behave as if it has zero |
| 26 | +endpoints (for Pods on this node) even if the service does have endpoints on other |
| 27 | +nodes. |
| 28 | + |
| 29 | +The following example shows what a Service looks like when internalTrafficPolicy |
| 30 | +is set to "Local": |
| 31 | + |
| 32 | +```yaml |
| 33 | +apiVersion: v1 |
| 34 | +kind: Service |
| 35 | +metadata: |
| 36 | + name: my-service |
| 37 | +spec: |
| 38 | + selector: |
| 39 | + app: MyApp |
| 40 | + ports: |
| 41 | + - protocol: TCP |
| 42 | + port: 80 |
| 43 | + targetPort: 9376 |
| 44 | + internalTrafficPolicy: Local |
| 45 | +``` |
| 46 | +
|
| 47 | +## How it Works |
| 48 | +
|
| 49 | +kube-proxy filters the endpoints it routes to based on the |
| 50 | +`spec.internalTrafficPolicy` setting. When it's "Local", only node local |
| 51 | +endpoints are considered. When it's "Cluster" or missing, all endpoints are |
| 52 | +considered. |
| 53 | +When the feature gate `ServiceInternalTrafficPolicy` is on, |
| 54 | +`spec.internalTrafficPolicy` defaults to "Cluster". |
| 55 | + |
| 56 | +## Constraints |
| 57 | + |
| 58 | +* Service Internal Traffic Policy is not used when `externalTrafficPolicy` is set |
| 59 | + to "Local" on a Service. It is possible to use both features in the same cluster |
| 60 | + on different Services, just not on the same Service. |
| 61 | + |
| 62 | +## {{% heading "whatsnext" %}} |
| 63 | + |
| 64 | +* Read about [enabling Topology Aware Hints](/docs/tasks/administer-cluster/enabling-topology-aware-hints) |
| 65 | +* Read about [Service External Traffic Policy](/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip) |
| 66 | +* Read [Connecting Applications with Services](/docs/concepts/services-networking/connect-applications-service/) |
0 commit comments