-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[k8s] Optimize ingress creation to avoid nginx hot-reloads #3263
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,39 +57,55 @@ def _open_ports_using_ingress( | |
ports: List[int], | ||
provider_config: Dict[str, Any], | ||
) -> None: | ||
# Check if an ingress controller exists | ||
if not network_utils.ingress_controller_exists(): | ||
raise Exception( | ||
'Ingress controller not found. ' | ||
'Install Nginx ingress controller first: ' | ||
'https://github.com/kubernetes/ingress-nginx/blob/main/docs/deploy/index.md.' # pylint: disable=line-too-long | ||
) | ||
|
||
for port in ports: | ||
service_name = f'{cluster_name_on_cloud}-skypilot-service--{port}' | ||
ingress_name = f'{cluster_name_on_cloud}-skypilot-ingress--{port}' | ||
path_prefix = _PATH_PREFIX.format( | ||
cluster_name_on_cloud=cluster_name_on_cloud, port=port) | ||
# Prepare service names, ports, for template rendering | ||
service_details = [ | ||
(f'{cluster_name_on_cloud}-skypilot-service--{port}', port, | ||
_PATH_PREFIX.format(cluster_name_on_cloud=cluster_name_on_cloud, | ||
port=port).rstrip('/').lstrip('/')) | ||
for port in ports | ||
] | ||
|
||
# Generate ingress and services specs | ||
# We batch ingress rule creation because each rule triggers a hot reload of | ||
# the nginx controller. If the ingress rules are created sequentially, | ||
# it could lead to multiple reloads of the Nginx-Ingress-Controller within | ||
# a brief period. Consequently, the Nginx-Controller pod might spawn an | ||
# excessive number of sub-processes. This surge triggers Kubernetes to kill | ||
# and restart the Nginx due to the podPidsLimit parameter, which is | ||
# typically set to a default value like 1024. | ||
# To avoid this, we change ingress creation into one object containing | ||
# multiple rules. | ||
content = network_utils.fill_ingress_template( | ||
namespace=provider_config.get('namespace', 'default'), | ||
service_details=service_details, | ||
ingress_name=f'{cluster_name_on_cloud}-skypilot-ingress', | ||
selector_key='skypilot-cluster', | ||
selector_value=cluster_name_on_cloud, | ||
) | ||
|
||
content = network_utils.fill_ingress_template( | ||
namespace=provider_config.get('namespace', 'default'), | ||
path_prefix=path_prefix, | ||
service_name=service_name, | ||
service_port=port, | ||
ingress_name=ingress_name, | ||
selector_key='skypilot-cluster', | ||
selector_value=cluster_name_on_cloud, | ||
) | ||
# Create or update services based on the generated specs | ||
for service_name, service_spec in content['services_spec'].items(): | ||
network_utils.create_or_replace_namespaced_service( | ||
namespace=provider_config.get('namespace', 'default'), | ||
service_name=service_name, | ||
service_spec=content['service_spec'], | ||
) | ||
network_utils.create_or_replace_namespaced_ingress( | ||
namespace=provider_config.get('namespace', 'default'), | ||
ingress_name=ingress_name, | ||
ingress_spec=content['ingress_spec'], | ||
service_spec=service_spec, | ||
) | ||
|
||
# Create or update the single ingress for all services | ||
network_utils.create_or_replace_namespaced_ingress( | ||
namespace=provider_config.get('namespace', 'default'), | ||
ingress_name=f'{cluster_name_on_cloud}-skypilot-ingress', | ||
ingress_spec=content['ingress_spec'], | ||
) | ||
Comment on lines
+102
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, if we are changing the ingress for all the ports in batch, will this cause the other ports being unavailable for a while when a new port is added? Also, just to confirm, when adding a new port, the original endpoint for the existing ports do not change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that's true. Nginx will do a hot reload whenever a new ingress rule is created. As a result, ports may be unavailable for a short duration before getting restotred.
Yes, the original endpoint will remain the same, since the k8s service remains the same. |
||
|
||
|
||
def cleanup_ports( | ||
cluster_name_on_cloud: str, | ||
|
@@ -128,17 +144,20 @@ def _cleanup_ports_for_ingress( | |
ports: List[int], | ||
provider_config: Dict[str, Any], | ||
) -> None: | ||
# Delete services for each port | ||
for port in ports: | ||
service_name = f'{cluster_name_on_cloud}-skypilot-service--{port}' | ||
ingress_name = f'{cluster_name_on_cloud}-skypilot-ingress--{port}' | ||
network_utils.delete_namespaced_service( | ||
namespace=provider_config.get('namespace', 'default'), | ||
service_name=service_name, | ||
) | ||
network_utils.delete_namespaced_ingress( | ||
namespace=provider_config.get('namespace', 'default'), | ||
ingress_name=ingress_name, | ||
) | ||
|
||
# Delete the single ingress used for all ports | ||
ingress_name = f'{cluster_name_on_cloud}-skypilot-ingress' | ||
network_utils.delete_namespaced_ingress( | ||
namespace=provider_config.get('namespace', 'default'), | ||
ingress_name=ingress_name, | ||
) | ||
|
||
|
||
def query_ports( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add the PR description in the comments here or some other places in the code for why we batch the rules? : )