OpenAI 分享 Kubernetes 扩展至 7500 节点经验
OpenAI 发布技术博客,总结将单一 Kubernetes 集群扩展至 7,500 个节点的工程实践与教训。文章重点阐述了针对大规模机器学习工作负载的特殊优化策略,旨在帮助研究团队在不修改代码的前提下实现基础设施的快速迭代与扩容。尽管提供了具体的架构调整思路,但作为 2021 年的回顾性内容,其时效性与直接可复用性对当前读者有限。
报道正文
本文目录15 个章节
将单个 Kubernetes 集群扩展至如此规模的情况并不多见,需要特别谨慎的处理,但其优势在于构建了一套简洁的基础设施,使我们的机器学习研究团队能够更快速地推进工作并实现规模化扩展,而无需修改代码。
自我们上一篇关于扩展至 2,500 个节点的文章发布以来,我们持续扩展基础设施以满足研究人员的需求,在此过程中也积累了许多额外的经验教训。本文总结了这些经验,以便 Kubernetes 社区的其他成员从中受益,并在文末列出了我们目前仍面临、计划接下来解决的一些问题。
我们的工作负载
在深入探讨之前,有必要先描述一下我们的工作负载。我们在 Kubernetes 上运行的应用程序和硬件与典型公司所遇到的情况有很大不同。因此,我们所面临的问题及相应的解决方案,未必适合你的环境!
一个大型机器学习作业会跨越多个节点,并且当它能够独占每个节点上的所有硬件资源时,运行效率最高。这使得 GPU 之间可以通过NVLink直接通信,或者 GPU 可以通过GPUDirect直接与网卡(NIC)通信。因此,对于我们的许多工作负载而言,单个 Pod 会占用整个节点。NUMA、CPU 或 PCIe 资源的争用不会影响调度决策。装箱(bin-packing)或碎片化也不是常见问题。我们当前的集群具备全对分带宽(full bisection bandwidth),因此我们也无需考虑机架或网络拓扑结构。这意味着,尽管我们拥有大量节点,但调度器承受的压力相对较低。
话虽如此,kube-scheduler 的负载压力具有突发性。一个新作业可能包含数百个同时创建的 Pod,随后又回落到相对较低的变动率。
我们最大的作业运行的是 MPI,作业内的所有 Pod 都参与同一个 MPI communicator。如果其中任何一个参与的 Pod 死亡,整个作业就会停止并需要重启。作业会定期保存检查点(checkpoint),重启后将从最后一个检查点恢复。因此,我们将这些 Pod 视为_半有状态(semi-stateful)_——被终止的 Pod 可以被替换且工作可以继续,但这种操作会造成中断,应尽可能减少此类情况的发生。
我们并不太依赖 Kubernetes 的负载均衡。我们的 HTTPS 流量很少,不需要进行 A/B 测试、蓝绿部署或金丝雀发布。Pod 之间通过 MPI over SSH 直接使用 Pod IP 地址通信,而不是通过 Service Endpoint。服务“发现”机制也很有限;我们仅在作业启动时进行一次查找,以确定哪些 Pod 参与 MPI 通信。
大多数作业会与某种形式的 Blob 存储交互。它们通常要么从 Blob 存储流式读取数据集的分片或直接读写检查点,要么将其缓存到快速的本地临时磁盘上。我们在某些需要 POSIX 语义的场景下使用少量 PersistentVolume,但 Blob 存储的可扩展性远强于前者,且无需执行缓慢的卸载/挂载操作。
最后,我们的工作性质从根本上说是研究性的,这意味着工作负载本身在不断变化。虽然超级计算团队致力于提供我们认为达到“生产级”质量标准的计算基础设施,但运行在该集群上的应用程序生命周期很短,其开发者迭代速度极快。新的使用模式随时可能出现,挑战我们对趋势和适当权衡的假设。我们需要一个可持续的系统,同时也允许我们在情况发生变化时快速响应。
网络
随着集群中节点和 Pod 数量的增加,我们发现 Flannel 难以扩展到所需的吞吐量水平。因此,我们转而使用 Azure VMSS 的 IP 配置原生 Pod 网络技术以及相关的 CNI 插件。这使我们的 Pod 能够获得主机级别的网络吞吐量。
我们转向使用基于别名(alias-based)的 IP 寻址的另一个原因是,在我们最大的集群中,任何时刻可能同时使用约 200,000 个 IP 地址。当我们测试基于路由(route-based)的 Pod 网络时,发现可有效使用的路由数量存在显著限制。
避免封装会增加对底层 SDN 或路由引擎的要求,但它保持了我们的网络设置简单。添加 VPN 或隧道功能无需额外的适配器。我们不必担心由于网络某部分 MTU 较低而导致的报文分片问题。网络策略和流量监控非常直观;数据包的源和目的地没有任何歧义。
我们使用主机上的 iptables 标记功能来跟踪每个 Namespace 和 Pod 的网络资源使用情况。这使研究人员能够可视化其网络使用模式。特别是,由于我们的许多实验具有不同的互联网通信和 Pod 间通信模式,因此能够调查瓶颈可能出现的位置通常非常有用。
iptables 的 mangle 规则可用于任意标记符合特定条件的数据包。以下是我们用于检测流量是内部流量还是发往互联网的流量的规则。FORWARD 规则涵盖来自 Pod 的流量,而 INPUT 和 OUTPUT 规则涵盖来自主机的流量:
一旦标记完成,iptables 将启动计数器以跟踪匹配此规则的字节数和数据包数量。你可以直接使用 iptables 命令查看这些计数器:
我们使用一个名为 iptables-exporter(opens in a new window) 的开源 Prometheus exporter,将这些统计数据纳入我们的监控系统。这是一种简单的方法,用于跟踪符合各种不同条件类型的数据包。
我们网络模型的一个独特之处在于,我们将节点、Pod 和服务网络的 CIDR 范围完全暴露给研究人员。我们采用中心辐射型(hub and spoke)网络模型,并使用原生的节点和 Pod CIDR 范围来路由该流量。研究人员连接到中心节点,从而可以访问任何单个集群(即辐射节点)。但集群之间无法相互通信。这确保了集群保持隔离,没有可能破坏故障隔离的跨集群依赖关系。
我们使用一个“NAT”主机来转换来自集群外部流量的服务网络 CIDR 范围。这种设置为我们的研究人员在如何选择以及如何配置其实验所需的网络方面提供了极大的灵活性。
API 服务器
Kubernetes API 服务器和 etcd 是健康运行集群的关键组件,因此我们特别关注这些系统所承受的压力。我们使用由 kube-prometheus(opens in a new window) 提供的 Grafana 仪表板,以及额外的内部开发仪表板。我们发现,对 API 服务器上 HTTP 状态码 429(请求过多)和 5xx(服务器错误)的发生速率进行告警,作为问题的高层信号是非常有用的。
尽管有些团队在 Kubernetes 集群内部运行 API Server,但我们始终将其部署在集群外部。etcd 和 API Server 都运行在各自专用的节点上。我们最大的集群配置了 5 个 API Server 和 5 个 etcd 节点,以分散负载并最大限度地减少单个节点故障带来的影响。自我们在上一篇博客文章中将 Kubernetes Events 拆分到独立的 etcd 集群后,etcd 再未出现过显著问题。API Server 是无状态的,通常很容易在具备自愈能力的实例组或规模集(scaleset)中运行。由于 etcd 集群的故障极为罕见,我们尚未尝试构建针对 etcd 集群的自动化自愈机制。
API Server 会占用相当多的内存,且其内存用量通常与集群中的节点数量呈线性增长关系。在我们拥有 7,500 个节点的集群中,观察到每个 API Server 最多使用 70GB 堆内存,因此幸运的是,这在未来很长一段时间内仍应处于硬件能力范围之内。
对 API Server 的一大压力来源是对 Endpoints 的 WATCH 操作。有一些服务(例如 kubelet 和 node-exporter),集群中的每个节点都是其成员。当节点被添加或从集群中移除时,这些 WATCH 会被触发。而且,由于通常每个节点本身都会通过 kube-proxy 监听 kubelet 服务,这些响应所需的数量和带宽将达到 级别,极其庞大,偶尔甚至达到 1GB/s 或更高。Kubernetes 1.17 推出的 EndpointSlices(opens in a new window) 带来了巨大收益,将这一负载降低了 1000 倍。
总体而言,我们会密切关注任何随集群规模扩展而增长的 API Server 请求。我们尽量避免让 DaemonSet 直接与 API Server 交互。在确实需要每个节点监听变更的场景下,引入一个中间缓存服务(例如 Datadog Cluster Agent(opens in a new window))似乎是一种良好的模式,可以避免出现集群范围的瓶颈。
随着集群规模的扩大,我们对集群的实际自动扩缩容操作有所减少。但偶尔也会遇到因一次性过度扩容而导致的问题。新节点加入集群时会生成大量请求,如果一次性添加数百个节点,可能会使 API Server 的容量过载。即使只是将扩容过程平滑处理几秒钟,也有助于避免宕机事故。
使用 Prometheus 和 Grafana 进行时间序列指标监控
[注:本文篇幅超长,以上为核心前篇精译,后续内容保留原文呈现]
We use Prometheus to collect time-series metrics and Grafana for graphs, dashboards, and alerts. We started with a deployment ofkube-prometheus(opens in a new window)that collects a wide variety of metrics and good dashboards for visualization. Over time we’ve added many of our own dashboards, metrics, and alerts.
As we added more and more nodes, we struggled with the sheer amount of metrics being collected by Prometheus. While kube-prometheus exposes a lot of useful data, some of it we weren’t actually ever looking at, and some was just too granular to collect, store, and query effectively. We usePrometheus rules(opens in a new window)to “drop” some of these metrics from being ingested.
For a while we struggled with a problem where Prometheus would consume more and more memory until eventually crashing the container in an Out-Of-Memory error (OOM). This seemed to occur even after throwing enormous amounts of memory capacity at the application. What’s worse was, when it did crash, it would take many hours on startup replaying write-ahead-log files before it was usable again.
Eventually wetracked down the source of these OOMs(opens in a new window)to be an interaction between Grafana and Prometheus, where Grafana would use the/api/v1/seriesAPI on Prometheus with a query of{le!=""}(Basically, “give me all the histogram metrics”). The implementation of/api/v1/serieswas unbounded in both time and space—for a query with a lot of results, this would continue to consume ever-more memory and time. It also continues to grow even after the requester has given up and closed the connection. For us, there was never enough memory, and Prometheus would eventually crash. Wepatched(opens in a new window)Prometheus to contain this API within a Context to enforce a timeout, which fixed it entirely.
While Prometheus crashed far less often, in times when we did need to restart it, WAL replay remained an issue. It would often take many hours to replay through all WAL logs before Prometheus was up collecting new metrics and servicing queries. With help fromRobust Perception(opens in a new window), we found that applying aGOMAXPROCS=24had a big improvement. Prometheus tries to use all cores when during WAL replay, and for servers with a large number of cores, the contention kills all performance.
We’re exploring new options to increase our monitoring capacity, described in the “Unsolved problems” section below.
Healthchecks
With a cluster this large, we of course rely on automation to detect and remove misbehaving nodes from the cluster. Over time we have built up a number of healthcheck systems.
Passive healthchecks
Some healthchecks are passive, always running on all nodes. These monitor basic system resources such as network reachability, bad or full disks, or GPU errors. GPUs exhibit problems a number of different ways, but an easy common one is an “Uncorrectable ECC error.” Nvidia’s Data Center GPU Manager (DCGM) tools make it easy to query for this and a number of other “Xid” errors. One way we track these errors is viadcgm-exporter(opens in a new window)to ingest the metrics into Prometheus, our monitoring system. This will appear as theDCGM_FI_DEV_XID_ERRORSmetric and be set to the error code that has most recently occurred. Additionally, theNVML Device Query API(opens in a new window)exposes more detailed information about the health and operation of a GPU.
Once we detect an error, they can often be fixed by resetting the GPU or system, though in some cases it does lead to the underlying GPU needing to be physically replaced.
Another form of healthcheck tracks maintenance events from the upstream cloud provider. Each of the major cloud providers expose a way to know if the current VM is due for an upcoming maintenance event that will eventually cause a disruption. The VM may need to be rebooted so an underlying hypervisor patch can be applied or the physical node swapped out for other hardware.
These passive healthchecks run constantly in the background on all nodes. If a healthcheck starts failing, the node is automatically cordoned so no new pods are to be scheduled on the node. For more serious healthcheck failures, we will also attempt a pod eviction to request all currently-running pods to exit immediately. It’s still up to the pod itself, configurable via a Pod Disruption Budget, to decide if it wants to allow this eviction to occur. Eventually, either after all pods have terminated, or 7 days has elapsed (part of our SLA), we will forcibly terminate the VM.
Active GPU tests
Unfortunately not all GPU problems manifest as error codes visible through DCGM. We’ve built up our own library of tests that exercise GPUs to catch additional problems and ensure that the hardware and driver is behaving as expected. These tests can’t be run in the background—they require exclusive use of a GPU for several seconds or minutes to run.
We first run these tests on nodes upon boot, in a system we call “preflight.” All nodes join the cluster with a “preflight” taint and label applied. This taint prevents normal pods from being scheduled on the node. A DaemonSet is configured to run preflight test pods on all nodes with this label. Upon successful completion of the test, the test itself removes the taint and label and the node is then available for general use.
We also then run these tests periodically during the lifetime of a node. We run this as a CronJob, allowing it to land on any available node in the cluster. This is admittedly a bit random and uncontrolled about which nodes get tested, but we’ve found that over time it provides sufficient coverage with minimal coordination or disruption.
Quotas & resource usage
As we scaled up our clusters, researchers started to find themselves having difficulty getting all of the capacity that they were allocated. Traditional job scheduling systems have a lot of different features available to fairly run work between competing teams, which Kubernetes does not have. Over time, we took inspiration from those job scheduling systems and build several capabilities in a Kubernetes-native way.
Team taints
We have a service in each cluster, “team-resource-manager” that has multiple functions. Its data source is a ConfigMap that specifies tuples of (node selector, team label to apply, allocation amount) for all of the research teams that have capacity in a given cluster. It reconciles this with the current nodes in the cluster, tainting the appropriate number of nodes withopenai.com/team=teamname:NoSchedule.
team-resource-manager” also has an admission webhook service, such that as each job is submitted, a corresponding toleration is applied based on the submitter’s team membership. Using taints allows us to constrain the Kubernetes pod scheduler flexibly, such as allowing a “any” toleration for lower priority pods, which allows teams to borrow each other’s capacity without requiring heavyweight coordination.
CPU & GPU balloons
In addition to using cluster-autoscaler to dynamically scale our VM-backed clusters, we use it to remediate (remove & re-add) unhealthy members within the cluster. We do this by setting the “min size” of the cluster to zero, and the “max size” of the cluster to the capacity available. However, cluster-autoscaler, if it sees idle nodes, will attempt to scale down to only needed capacity. For multiple reasons (VM spin up latency, pre-allocated costs, the API server impacts mentioned above) this idle-scaling isn’t ideal.
So, we introduced a balloon Deployment for both our CPU-only and GPU hosts. This Deployment contains a ReplicaSet with “max size” number of low-priority pods. These pods occupy resources within a node, so the autoscaler doesn’t consider them as idle. However since they’re low priority, the scheduler can evict them immediately to make room for actual work. (We chose to use a Deployment instead of a DaemonSet, to avoid the DaemonSet being considered idle workload on a node.)
One thing of note, we use pod anti-affinity to ensure the pods would evenly distribute across the nodes. Earlier versions of the Kubernetes scheduler had an performance issue with pod anti-affinity. This has been corrected since Kubernetes 1.18.
Gang scheduling
Our experiments often involve one or more StatefulSets, each operating a different portion of the training effort. For Optimizers, researchers need all members of the StatefulSet to be scheduled, before any training can be done (as we often use MPI to coordinate between optimizer members, and MPI is sensitive to group membership changes).
However, Kubernetes by default won’t necessarily prioritize fulfilling all requests from one StatefulSet over another. For example if two experiments each requested 100% of the cluster’s capacity, instead of scheduling all of one experiment or the other, Kubernetes might schedule only half of each experiment’s pods, leading to a deadlock where neither experiment can make progress.
We tried a few things needing a custom scheduler, but ran into edge cases that caused conflicts with how normal pods were scheduled. Kubernetes 1.18 introduced a plugin architecture for the core Kubernetes scheduler, making it much easier to add features like this natively. We recently landed on theCoscheduling plugin(opens in a new window)as a good way to solve this problem.
Unsolved problems
There are many problems still to address as we scale up our Kubernetes clusters. A few of them include:
Metrics
At our scale we’ve had many difficulties with Prometheus’s built-in TSDB storage engine being slow to compact, and needing long times needed to replay the WAL (Write-Ahead-Log) any time it restarts. Queries also tend to result in “query processing would load too many samples” errors. We’re in the process of migrating to a different Prometheus-compatible storage and query engine. Look forward to a future blog post about how it goes!
Pod network traffic shaping
As we scale up our clusters, each pod is calculated to have a certain amount of Internet bandwidth available. The aggregate Internet bandwidth requirements per person have become substantial, and our researchers now have the ability to unintentionally put a significant resource strain on other locations on the Internet, such as datasets for download and software packages to install.
Conclusions
We’ve found Kubernetes to be an exceptionally flexible platform for our research needs. It has the ability to scale up to meet the most demanding workloads we’ve put on it. There are many areas yet though where it needs improvement, and the Supercomputing team at OpenAI will continue to explore how Kubernetes can scale. If this kind of work seems interesting, you should considerapplyingat OpenAI!