Linux系統(tǒng)非Docker環(huán)境如何限制CPU使用率

2020年9月14日14:50:53 發(fā)表評論 3,222 ℃

微服務時代,有時候為了方便,經(jīng)常在一臺高配置服務器運行很多服務,這個時候CPU資源搶占就成了一個非常頭痛的問題。閑時不會出現(xiàn)什么問題,比如一臺16核服務器,當某一個時刻某個微服出現(xiàn)高并發(fā)訪問時,此服務會搶占大量的CPU資源,從而影響了此服務器其他服務的正常訪問,比如下圖的情況:

Linux系統(tǒng)非Docker環(huán)境如何限制CPU使用率

docker容器在資源限制做的完善,kubernetes集群還可以通過資源使用率自動擴縮容,但是在非docker環(huán)境的時候,要實現(xiàn)CPU限制還是比較麻煩的,好在Linux有個命令可以做這樣的事情,它就是cpulimit,安裝也比較簡單。今天阿湯博客就簡單介紹一下cpulimit命令。

Ubuntu/Debian系統(tǒng):

apt-get install cpulimit -y

RedHat/CentOS系統(tǒng):(先安裝好epel源)

yum install cpulimit -y

cpulimit使用也非常簡單,看下幫助信息:

Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 1600 (required)
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

主要說明下- l參數(shù),單核cpu的使用率范圍為0%-100%,4核CPU的使用率范圍為0%-400%,16核CPU的使用率范圍為0%-1600%,以此類推,此值是一個絕對值。

- e參數(shù)可以根據(jù)進程路徑限制。

下面通過wrk壓測工具,對cpulimit命令做一下測試。

首先限制為200%:

cpulimit -p 26546 -l 200

看下測試結(jié)果:

Linux系統(tǒng)非Docker環(huán)境如何限制CPU使用率

再限制為800%:

cpulimit -p 26546 -l 800

看下測試結(jié)果:

Linux系統(tǒng)非Docker環(huán)境如何限制CPU使用率

實際壓測試過程中,該進程的CPU使用率可能會超過cpulimit的限制,但是不會超過太多,這是因為是cpu使用率在動態(tài)變動,cpulimit不能非常及時準確的調(diào)整,偶爾會超過一點限制。這也說明和docker的CPU限制有點區(qū)別。

【騰訊云】云服務器、云數(shù)據(jù)庫、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評論

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: