Puppet Server 技巧的分配資源 – CPU 和 JVM 篇

2017-10-16 Puppet

最近 Puppet 大幅更新了 Document,在重新讀文件的時候看到新增的一篇「Puppet Server: Tuning Guide」主要講 Puppet Server 的調校,還有 Puppet Server 運作的方式,這對於有使用 Puppet 的人有很大的幫助。


CPU 和 JRuby、max-active-instances

Puppet Server 啟動時是由多個 JRuby 虛擬機所組成的,每個 JRuby 都在邏輯上隔離,並且一個 JRuby 一次處理一個請求來達到每個 HTTP request 都是獨立且安全的,避免當有異常的 HTTP request 請求進入時不會污染到其他的 JRuby 環境。

When Puppet Server starts up, it creates a pool of JRuby interpreters to use as workers when it needs need to execute some of the Puppet Ruby code. You can think of these almost as individual Ruby “virtual machines” that are controlled by Puppet Server; it’s not entirely dissimilar to the way that Passenger spawns several Ruby processes to hand off work to.

Puppet Server isolates these JRuby instances so that they will only be allowed to handle one request at a time. This ensures that we don’t encounter any concurrency issues, since the Ruby code is not thread-safe. When an HTTP request comes in to Puppet Server, and it determines that some Ruby code will need to be executed in order to handle the request, Puppet Server “borrows” a JRuby instance from the pool, uses it to do the work, and then “returns” it to the pool. If there are no JRuby instances available in the pool at the time a request comes in (presumably because all of the JRuby instances are already in use handling other requests), Puppet Server will block the request until one becomes available.


從上面得知 Puppet 針對每個 HTTP request 僅使用一個 JRuby,所以你可以透過 max-active-instances 來控制 JRuby 的數量,預設的 maximum  是 4,也就是每台 Puppet Server 最大同時 4 個 Puppet agent requests,如果超過的話當下的 HTTP request 會直接 timeout,這個問題在 Puppet 5.1 之後可以透過 retry requests 解決。

From a practical perspective, this setting basically controls how many Puppet agent runs Puppet Server can handle concurrently. The minimum value you can get away with here is 1, and if your installation is small enough that you’re unlikely to ever have more than one Puppet agent checking in with the server at exactly the same time, this is totally sufficient.


然後 JRuby 的處理跟 CPU core 有成正比的關係,如果你的 CPU 有 16 core,那預設 max-active-instances 為 4,只能使用到 4 core CPU,所以基本上 max-active-instances 這個值的設定是跟著你的 CPU core 數量跑 …,當然 max-active-instances 設定高於 CPU core 也不是什麼好事,這樣就直接浪費了 Memory 的使用量。

Assuming you have more than one CPU core in your machine, this situation means that you won’t be getting the maximum possible throughput from your Puppet Server installation. Increasing the value from 1 to 2 would mean that Puppet Server could now use a second CPU core to handle the requests from a second Puppet agent simultaneously.

It follows, then, that the maximum sensible value to use for this setting will be roughly the number of CPU cores you have in your server. Setting the value to something much higher than that won’t improve performance, because even if there are extra JRuby instances available in the pool to do work, they won’t be able to actually do any work if all of the CPU cores are already busy using JRuby instances to handle incoming agent requests.


JVM 和 JRuby、max-active-instances

承上了解每個 HTTP request 和 JRuby 有關係後,那麼 JRuby 當然也就和 JVM 息息相關,官方提供的資料是每個 JRuby 大約 512 MB,但是實際 JRuby 和你的 module、hiera-data 的大小有相對的關係,不過 1 JRuby = 512 MB 是比較標準的官方算法。

The most important factor when determining the max heap size for Puppet Server is the value of max-active-instances. Each JRuby instance needs to load up a copy of the Puppet Ruby code, and then needs some amount of memory overhead for all of the garbage that gets generated during a Puppet catalog compilation. Also, the memory requirements will vary based on how many Puppet modules you have in your module path, how much Hiera data you have, etc. At this time we estimate that a reasonable ballpark figure is about 512MB of RAM per JRuby instance, but that can vary depending on some characteristics of your Puppet codebase. For example, if you have a really high number of modules or a great deal of Hiera data, you might find that you need more than 512MB per JRuby instance.

You’ll also want to allocate a little extra heap to be used by the rest of the things going on in Puppet Server: the web server, etc. So, a good rule of thumb might be 512MB + (max-active-instances * 512MB).



講這麼多就是要問我該怎麼設定我的 Puppet Master 資源,舉例:

  • 如果我需要同時擁有 4 Node 來訪問 Puppet Master ?
    • 最少 CPU 4 core,JVM Heap Size 的 -Xmx 最少設為 2GB,整個 Linux Server 建議至少 4GB 以上。
  • 如果我有一台 CPU 8 core,記憶體 16 GB 的 Puppet Master,可以怎樣設定 ?
    • max-active-instances 設為 8 可完整利用全部的 CPU,JVM Heap Size -Xmx 可以設 4GB ~ 8GB,剩餘 8GB 可以留給其他 Service 使用。


希望從這篇可以讓大家更加了解 Puppet Master 的運作,以及評估使用量。


延伸閱讀:Puppet Server 技巧的分配資源 – JRuby 的效能


給 Mr. 沙先生一點建議



展開全部 | 收合全部


訂閱 Mr. 沙先生 的文章

輸入你的 email 用於訂閱

%d 位部落客按了讚: