Skip to content

loadbalancer: Keep backends around that are terminating and not serving - #40969

Merged
joamaki merged 1 commit into
cilium:mainfrom
joamaki:pr/joamaki/lb-graceful-termination-of-not-serving
Aug 15, 2025
Merged

joamaki merged 1 commit into
cilium:mainfrom
joamaki:pr/joamaki/lb-graceful-termination-of-not-serving

Conversation

@joamaki

@joamaki joamaki commented Aug 6, 2025 •

Copy link
Copy Markdown
Contributor

This fixes handling of terminating endpoints that are marked as not serving. These were skipped by ParseEndpointSliceV1 which lead to them being removed from the backend BPF maps and disrupting connections too early when a terminating pod stopped signalling readiness (conditions became {ready: false, serving: false, terminating: true}.

To fix this we:

  1. introduce BackendStateTerminatingNotServing for terminating backends that are to be kept in BPF maps, but not load-balanced to.
  2. remove the skipping of non-serving backends from ParseEndpointSliceV1. In case the conditions are the unexpected {ready: false, serving: false, terminating: false} the backend is simply quarantined.
Kubernetes endpoints that are terminating are retained in the backends BPF state regardless of the "serving" condition to avoid connection disruptions when a pod no longer signals readiness to process new connections.

@maintainer-s-little-helper maintainer-s-little-helper Bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Aug 6, 2025
@joamaki

joamaki commented Aug 6, 2025

Copy link
Copy Markdown
Contributor Author

/test

@joamaki
joamaki force-pushed the pr/joamaki/lb-graceful-termination-of-not-serving branch 6 times, most recently from 4ab9dc9 to 3a56eac Compare August 6, 2025 16:45
@joamaki
joamaki force-pushed the pr/joamaki/lb-graceful-termination-of-not-serving branch from 3a56eac to 5fe283a Compare August 8, 2025 10:04
@joamaki joamaki added the release-note/bug This PR fixes an issue in a previous release of Cilium. label Aug 8, 2025
@maintainer-s-little-helper maintainer-s-little-helper Bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Aug 8, 2025
@joamaki joamaki added the needs-backport/1.18 This PR / issue needs backporting to the v1.18 branch label Aug 8, 2025
@joamaki

joamaki commented Aug 8, 2025

Copy link
Copy Markdown
Contributor Author

/test

@joamaki
joamaki force-pushed the pr/joamaki/lb-graceful-termination-of-not-serving branch from 5fe283a to 42fd2c8 Compare August 8, 2025 12:37
@joamaki

joamaki commented Aug 8, 2025

Copy link
Copy Markdown
Contributor Author

/test

@joamaki
joamaki marked this pull request as ready for review August 11, 2025 09:30
@joamaki
joamaki requested review from a team as code owners August 11, 2025 09:30
@joamaki
joamaki enabled auto-merge August 11, 2025 09:31
@borkmann
borkmann requested a review from aojea August 11, 2025 15:10
A terminating pod that no longer signals readiness will be seen as an
endpoint with {ready: false, serving: false, terminating: true}. These
were skipped leading to the backend being removed from the backends map
which causes connection disruptions.

Fix this by keeping the non-serving terminating backends in the backends BPF
map, but not using it as a fallback backend if only terminating backends exists.

This is implemented by:
- Including all conditions in [k8s.Backend], not just Terminating.
- Adding a new backend state (TerminatingNotServing) used with Terminating&!Serving conditions.
- Not considering TerminatingNotServing backends when selecting the backends.

Signed-off-by: Jussi Maki <[email protected]>
@joamaki
joamaki force-pushed the pr/joamaki/lb-graceful-termination-of-not-serving branch from 803742a to 1059edd Compare August 15, 2025 09:31
@joamaki

joamaki commented Aug 15, 2025

Copy link
Copy Markdown
Contributor Author

/test

@joamaki
joamaki added this pull request to the merge queue Aug 15, 2025
Merged via the queue into cilium:main with commit 6f41c98 Aug 15, 2025
@joamaki
joamaki deleted the pr/joamaki/lb-graceful-termination-of-not-serving branch August 15, 2025 12:57
@giorio94 giorio94 removed the needs-backport/1.18 This PR / issue needs backporting to the v1.18 branch label Aug 18, 2025
@giorio94

Copy link
Copy Markdown
Member

Dropped the needs-backport label for the moment, given #41199

@WarheadsSE

Copy link
Copy Markdown

👋 In light of #41234 fixing up the state concern, resolving the need to revert in #41199 ... Should the needs-backport be added back, in conjunction with the fixup?

There are many interested in the results of these changes.

@joamaki

joamaki commented Oct 2, 2025

Copy link
Copy Markdown
Contributor Author

Will need to look at #41244 first and then it’ll make sense to backport. Will try to get to this next week.

@joamaki

joamaki commented Oct 14, 2025

Copy link
Copy Markdown
Contributor Author

Opened #42170 to fix #41244. Once this is in I'll do a backport of this PR, #41234 and #42170 to v1.18.

@tamilmani1989

Copy link
Copy Markdown
Contributor

Opened #42170 to fix #41244. Once this is in I'll do a backport of this PR, #41234 and #42170 to v1.18.

@joamaki is it feasible to backport to 1.17?

@joamaki

joamaki commented Nov 11, 2025

Copy link
Copy Markdown
Contributor Author

Opened #42170 to fix #41244. Once this is in I'll do a backport of this PR, #41234 and #42170 to v1.18.

@joamaki is it feasible to backport to 1.17?

No it is not since v1.17 has the old LB control-plane.

@cilium-release-bot cilium-release-bot Bot moved this to Released in cilium v1.19.0 Feb 3, 2026
ankitaojha123 added a commit to ankitaojha123/cilium that referenced this pull request Sep 16, 2026
In Cilium 1.18, commit c09922f (PR cilium#40969) retained terminating backends
in BPF maps to allow graceful TCP connection draining.

However, because connectionless UDP datagrams do not contain TCP SYN flags,
subsequent UDP packets from client sockets with existing Conntrack entries
evaluated `!state->syn` as true. This trapped UDP traffic on terminating pods
for the duration of the termination grace period (20–45s), causing packet
loss during rolling updates and pod deletions.

This change adds `tuple->nexthdr != IPPROTO_UDP` to the connection draining
check in both `lb4_local()` and `lb6_local()`. As a result:
1. TCP connections continue to gracefully drain to terminating backends.
2. UDP packets immediately bypass draining, select an active healthy backend,
   and update the Conntrack entry.
3. Fallback to terminating backends when no active backends exist (KEP-1669)
   remains fully preserved when `svc->count == 0`.

Additionally, extends `tc_nodeport_lb_terminating_backend.c` to verify that
subsequent UDP datagrams redirect to active backends when active backends
are present.

Fixes cilium#48714

Signed-off-by: Ankita Ojha <[email protected]>
ankitaojha123 added a commit to ankitaojha123/cilium that referenced this pull request Sep 17, 2026
In Cilium 1.18, commit c09922f (PR cilium#40969) retained terminating backends
in BPF maps to allow graceful TCP connection draining.

However, because connectionless UDP datagrams do not contain TCP SYN flags,
subsequent UDP packets from client sockets with existing Conntrack entries
evaluated `!state->syn` as true. This trapped UDP traffic on terminating pods
for the duration of the termination grace period (20–45s), causing packet
loss during rolling updates and pod deletions.

This change adds `tuple->nexthdr != IPPROTO_UDP` to the connection draining
check in both `lb4_local()` and `lb6_local()`. As a result:
1. TCP connections continue to gracefully drain to terminating backends.
2. UDP packets immediately bypass draining, select an active healthy backend,
   and update the Conntrack entry.
3. Fallback to terminating backends when no active backends exist (KEP-1669)
   remains fully preserved when `svc->count == 0`.

Tests:
- Updated `tc_nodeport_lb_terminating_backend.c` to use TCP, verifying that
  existing TCP connections continue draining to terminating backends.
- Added `tc_nodeport_lb_terminating_backend_udp.c` to verify that UDP datagrams
  immediately redirect to active backends when available, and fall back when
  no active backends remain.

Fixes cilium#48714

Signed-off-by: Ankita Ojha <[email protected]>
ankitaojha123 added a commit to ankitaojha123/cilium that referenced this pull request Sep 18, 2026
In Cilium 1.18, commit c09922f (PR cilium#40969) retained terminating backends
in BPF maps to allow graceful TCP connection draining.

However, because connectionless UDP datagrams do not contain TCP SYN flags,
subsequent UDP packets from client sockets with existing Conntrack entries
evaluated `!state->syn` as true. This trapped UDP traffic on terminating pods
for the duration of the termination grace period (20–45s), causing packet
loss during rolling updates and pod deletions.

This change adds `tuple->nexthdr != IPPROTO_UDP` to the connection draining
check in both `lb4_local()` and `lb6_local()`. As a result:
1. TCP connections continue to gracefully drain to terminating backends.
2. UDP packets immediately bypass draining, select an active healthy backend,
   and update the Conntrack entry.
3. Fallback to terminating backends when no active backends exist (KEP-1669)
   remains fully preserved when `svc->count == 0`.

Tests:
- Updated `tc_nodeport_lb_terminating_backend.c` to use TCP, verifying that
  existing TCP connections continue draining to terminating backends.
- Added `tc_nodeport_lb_terminating_backend_udp.c` to verify that UDP datagrams
  immediately redirect to active backends when available, and fall back when
  no active backends remain.

Fixes cilium#48714

Signed-off-by: Ankita Ojha <[email protected]>
ankitaojha123 added a commit to ankitaojha123/cilium that referenced this pull request Sep 18, 2026
In Cilium 1.18, PR cilium#40969 retained terminating backends in BPF maps
to allow graceful TCP connection draining.

However, because connectionless UDP datagrams do not contain TCP SYN flags,
subsequent UDP packets from client sockets with existing Conntrack entries
evaluated `!state->syn` as true. This trapped UDP traffic on terminating pods
for the duration of the termination grace period (20–45s), causing packet
loss during rolling updates and pod deletions.

This change adds `tuple->nexthdr != IPPROTO_UDP` to the connection draining
check in both `lb4_local()` and `lb6_local()`. As a result:
1. TCP connections continue to gracefully drain to terminating backends.
2. UDP packets immediately bypass draining, select an active healthy backend,
   and update the Conntrack entry.
3. Fallback to terminating backends when no active backends exist (KEP-1669)
   remains fully preserved when `svc->count == 0`.

Tests:
- Updated `tc_nodeport_lb_terminating_backend.c` to use TCP, verifying that
  existing TCP connections continue draining to terminating backends.
- Added `tc_nodeport_lb_terminating_backend_udp.c` to verify that UDP datagrams
  immediately redirect to active backends when available, and fall back when
  no active backends remain.

Fixes cilium#48714

Signed-off-by: Ankita Ojha <[email protected]>
ankitaojha123 added a commit to ankitaojha123/cilium that referenced this pull request Sep 21, 2026
In Cilium 1.18, PR cilium#40969 retained terminating backends in BPF maps
to allow graceful TCP connection draining.

However, because connectionless UDP datagrams do not contain TCP SYN flags,
subsequent UDP packets from client sockets with existing Conntrack entries
evaluated `!state->syn` as true. This trapped UDP traffic on terminating pods
for the duration of the termination grace period (20–45s), causing packet
loss during rolling updates and pod deletions.

This change adds `tuple->nexthdr != IPPROTO_UDP` to the connection draining
check in both `lb4_local()` and `lb6_local()`. As a result:
1. TCP connections continue to gracefully drain to terminating backends.
2. UDP packets immediately bypass draining, select an active healthy backend,
   and update the Conntrack entry.
3. Fallback to terminating backends when no active backends exist (KEP-1669)
   remains fully preserved when `svc->count == 0`.

Tests:
- Updated `tc_nodeport_lb_terminating_backend.c` to use TCP, verifying that
  existing TCP connections continue draining to terminating backends.
- Added `tc_nodeport_lb_terminating_backend_udp.c` to verify that UDP datagrams
  immediately redirect to active backends when available, and fall back when
  no active backends remain.

Fixes cilium#48714

Signed-off-by: Ankita Ojha <[email protected]>
ankitaojha123 added a commit to ankitaojha123/cilium that referenced this pull request Sep 21, 2026
In Cilium 1.18, PR cilium#40969 retained terminating backends in BPF maps
to allow graceful TCP connection draining.

However, because connectionless UDP datagrams do not contain TCP SYN flags,
subsequent UDP packets from client sockets with existing Conntrack entries
evaluated `!state->syn` as true. This trapped UDP traffic on terminating pods
for the duration of the termination grace period (20–45s), causing packet
loss during rolling updates and pod deletions.

This change adds `tuple->nexthdr != IPPROTO_UDP` to the connection draining
check in both `lb4_local()` and `lb6_local()`. As a result:
1. TCP connections continue to gracefully drain to terminating backends.
2. UDP packets immediately bypass draining, select an active healthy backend,
   and update the Conntrack entry.
3. Fallback to terminating backends when no active backends exist (KEP-1669)
   remains fully preserved when `svc->count == 0`.

Tests:
- Updated `tc_nodeport_lb_terminating_backend.c` to use TCP, verifying that
  existing TCP connections continue draining to terminating backends.
- Added `tc_nodeport_lb_terminating_backend_udp.c` to verify that UDP datagrams
  immediately redirect to active backends when available, and fall back when
  no active backends remain.

Fixes cilium#48714

Signed-off-by: Ankita Ojha <[email protected]>
ankitaojha123 added a commit to ankitaojha123/cilium that referenced this pull request Sep 22, 2026
In Cilium 1.18, PR cilium#40969 retained terminating backends in BPF maps
to allow graceful TCP connection draining.

However, because connectionless UDP datagrams do not contain TCP SYN
flags, subsequent UDP packets from client sockets with existing
Conntrack entries evaluated `!state->syn` as true. This trapped UDP
traffic on terminating pods for the duration of the termination grace
period (20–45s), causing packet loss during rolling updates and pod
deletions.

This change adds `tuple->nexthdr != IPPROTO_UDP` to the connection
draining check in both `lb4_local()` and `lb6_local()`. As a result:
1. TCP connections continue to gracefully drain to terminating backends.
2. UDP packets immediately bypass draining, select an active healthy
   backend, and update the Conntrack entry.
3. Fallback to terminating backends when no active backends exist
   (KEP-1669) remains fully preserved when `svc->count == 0`.

Tests:
- Updated `tc_nodeport_lb_terminating_backend.c` to use TCP, verifying
  that existing TCP connections continue draining to terminating backends.
- Added `tc_nodeport_lb_terminating_backend_udp.c` to verify that UDP
  datagrams immediately redirect to active backends when available, and
  fall back when no active backends remain.

Fixes cilium#48714

Signed-off-by: Ankita Ojha <[email protected]>
pull Bot pushed a commit to TKaxv-7S/cilium that referenced this pull request Sep 24, 2026
In Cilium 1.18, PR cilium#40969 retained terminating backends in BPF maps
to allow graceful TCP connection draining.

However, because connectionless UDP datagrams do not contain TCP SYN
flags, subsequent UDP packets from client sockets with existing
Conntrack entries evaluated `!state->syn` as true. This trapped UDP
traffic on terminating pods for the duration of the termination grace
period (20–45s), causing packet loss during rolling updates and pod
deletions.

This change adds `tuple->nexthdr != IPPROTO_UDP` to the connection
draining check in both `lb4_local()` and `lb6_local()`. As a result:
1. TCP connections continue to gracefully drain to terminating backends.
2. UDP packets immediately bypass draining, select an active healthy
   backend, and update the Conntrack entry.
3. Fallback to terminating backends when no active backends exist
   (KEP-1669) remains fully preserved when `svc->count == 0`.

Tests:
- Updated `tc_nodeport_lb_terminating_backend.c` to use TCP, verifying
  that existing TCP connections continue draining to terminating backends.
- Added `tc_nodeport_lb_terminating_backend_udp.c` to verify that UDP
  datagrams immediately redirect to active backends when available, and
  fall back when no active backends remain.

Fixes cilium#48714

Signed-off-by: Ankita Ojha <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug This PR fixes an issue in a previous release of Cilium.

Projects

No open projects
Status: Released

Development

Successfully merging this pull request may close these issues.

9 participants