原地更新应用程序#
您可以在 Serve 应用程序上线后,通过更新配置文件中的设置并使用 serve deploy 命令重新部署它来更新它们。在重新部署的配置文件中,您可以添加新的部署设置或删除旧的部署设置。这是因为 serve deploy 是幂等的,这意味着您的 Serve 应用程序的配置始终与您成功部署的最新配置匹配(或遵循),而无论您之前部署了什么配置文件。
轻量级配置更新#
轻量级配置更新会修改正在运行的部署副本,而无需关闭它们并重新启动,因此在部署更新期间的停机时间更短。对于每个部署,修改以下值被视为轻量级配置更新,并且不会关闭该部署的副本。
num_replicasautoscaling_configuser_configmax_ongoing_requestsgraceful_shutdown_timeout_sgraceful_shutdown_wait_loop_shealth_check_period_shealth_check_timeout_s
更新用户配置#
此示例使用了来自生产指南的文本摘要和翻译应用程序。每个单独的部署都包含一个 reconfigure() 方法。此方法允许您通过更新 user_config 来对部署进行轻量级更新。
首先,让我们部署图。确保使用 CLI 命令 ray stop 停止任何之前的 Ray 集群,以便进行此示例。
$ ray start --head
$ serve deploy serve_config.yaml
然后向应用程序发送请求
import requests
english_text = (
"It was the best of times, it was the worst of times, it was the age "
"of wisdom, it was the age of foolishness, it was the epoch of belief"
)
response = requests.post("http://127.0.0.1:8000/", json=english_text)
french_text = response.text
print(french_text)
# 'c'était le meilleur des temps, c'était le pire des temps .'
通过更改 Translator 用户配置中的 language 属性,将文本翻译的目标语言从法语更改为德语。
...
applications:
- name: default
route_prefix: /
import_path: text_ml:app
runtime_env:
pip:
- torch
- transformers
deployments:
- name: Translator
num_replicas: 1
user_config:
language: german
...
在不停止 Ray 集群的情况下,使用 serve deploy 重新部署应用程序。
$ serve deploy serve_config.yaml
...
我们可以使用 serve status 检查我们的部署。一旦应用程序的 status 恢复为 RUNNING,我们就可以再次尝试我们的请求。
$ serve status
proxies:
cef533a072b0f03bf92a6b98cb4eb9153b7b7c7b7f15954feb2f38ec: HEALTHY
applications:
default:
status: RUNNING
message: ''
last_deployed_time_s: 1694041157.2211847
deployments:
Translator:
status: HEALTHY
replica_states:
RUNNING: 1
message: ''
Summarizer:
status: HEALTHY
replica_states:
RUNNING: 1
message: ''
语言已更新。现在返回的文本是德语而不是法语。
import requests
english_text = (
"It was the best of times, it was the worst of times, it was the age "
"of wisdom, it was the age of foolishness, it was the epoch of belief"
)
response = requests.post("http://127.0.0.1:8000/", json=english_text)
german_text = response.text
print(german_text)
# 'Es war die beste Zeit, es war die schlimmste Zeit .'
代码更新#
更改部署配置中的以下值将触发重新部署并重启该部署的所有副本。
ray_actor_optionsplacement_group_bundlesplacement_group_strategy
更改应用程序级别的以下配置值也视为代码更新,并且应用程序中的所有部署都将被重启。
import_pathruntime_env
警告
虽然您可以通过部署一个全新的部署图(使用不同的 import_path 和不同的 runtime_env)来更新您的 Serve 应用程序,但在生产环境中不推荐这样做。
对于大规模代码更新的最佳实践是启动一个新的 Ray 集群,使用 serve deploy 将更新后的代码部署到该集群,然后将流量从旧集群切换到新集群。