網站首頁 編程語言 正文
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- Docker-資源限制
- 1.Cgroup簡介
- cgroups有四大功能:
- 2.CPU資源限制
- 查看CPU使用率
- 查看默認值
- 測試CPU使用率
- 設置CPU資源占用比(設置多個容器時才有效)
- 對內存使用進行限制
- 對磁盤IO配額控制(blkio)的限制
Docker-資源限制
1.Cgroup簡介
_cgroups,是一個非常強大的linux內核工具,他不僅可以限制被namespace隔離起來的資源,
還可以為資源設置權重、計算使用量、操控進程啟停等等。所以cgroups (Control groups) 實現了對資源的配額和度量。
cgroups有四大功能:
-
資源限制:可以對任務使用的資源總額進行限制;
-
先級分配:通過分配的cpu時間片數量以及磁盤I0帶寬大小,實際上相當于控制了任務運行優先級;
-
資源統計:可以統計系統的資源使用量,如cpu時長, 內存用量等;
-
任務控制: cgroup可以對任務執行掛起、恢復等操作。
2.CPU資源限制
Linux通過CFS ( Completely Fair Scheduler, 完全公平調度器)來調度各個進程對CPU的使用。CFS默認的調度周期是100ms。
可以設置每個容器進程的調度周期,以及在這個周期內各個容器最多能使用多少CPU時間。
使用–cpu-period即可設置調度周期,使用–cpu-quota即可設置在每個周期內容器能使用的CPU時間。兩者可以配合使用。
CFS周期的有效范圍是1ms~1s, 對應的–cpu-period的數值范圍是1000~1000000。
容器的CPU 配額必須不小于1ms,即–cpu-quota 的值必須>= 1000。
查看CPU使用率
查看默認值
[root@zbx-agent01 ~]# cd /sys/fs/cgroup/cpu/docker
[root@zbx-agent01 docker]# cat cpu.cfs_period_us cpu.cfs_quota_us
100000
-1
#cfs_period_us表示一個cpu帶寬,單位為微秒。系統總CPU帶寬: cpu核心數 * cfs_period_us
#cfs_quota_us表示Cgroup可以使用的cpu的帶寬,單位為微秒。cfs_quota_us為-1,表示使用的CPU不受cgroup限制。cfs_quota_us的最小值為1ms(1000),最大值為1s。
測試CPU使用率
docker run -it --name test01 debian:latest
vi cpu.sh
#!/bin/bash
i=0
while true
do
let i++
done
chmod +x cpu.sh
./cpu.sh &
另一個終端使用top查看
設置CPU資源占用比(設置多個容器時才有效)
[root@zbx-agent01 docker]# docker rm -f `docker ps -a -q`
f349c2bece0d
e619b609448d
a4d35efaa9bf
cafbe0660514
22500512a0ba
7dc4c280b0fc
46188e2f028a
5fce3f28819e
12d35de6f836
08d9d0a77dab
0a14ae5189c1
edb1704cf372
9c4612185d64
2f80db9d227c
[root@zbx-agent01 docker]# docker run -itd --name test01 --cpu-shares 1024 centos:latest
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
0929cc3ba6d952b977eeda4d8a4118715e015442c629de4d0018eac9dbdb8947
[root@zbx-agent01 docker]# docker run -itd --name test02 --cpu-shares 512 centos:latest
c371f718811a0ba6de50fa29aabeff3baf5f7050cf919649da451f3ba636c653
#分別進入容器,進行壓力測試
docker exec -it test01/02 bash
yum install -y epel-release
yum install stress -y
stress -c 4 #產生四個進程,每個進程都反復不停的計算隨機數的平方根
#查看容器的運行狀態(動態更新)
docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PID
#########################################如果容器使用的是centos8可能無法使用yum安裝,按下步驟恢復
cd /etc/yum.repos.d/
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum clean all
yum makecache
由上圖可以看見兩個容器之間的cpu使用比率接近于2:1
(cpu使用率超過100的原因:當單個處理器的使用率達到100%時。對于多個處理器,這個數字將超過100%,您需要乘以主機可用
的處理器數量,才能找到理論上限,由于該虛擬機我分配了兩核心,所以理論使用上線為200%)
對內存使用進行限制
[root@zbx-agent01 docker]# docker run -itd --name test03 -m 512m centos:latest
[root@zbx-agent01 docker]# docker stats
對磁盤IO配額控制(blkio)的限制
[root@zbx-agent01 docker]# docker run -itd --name test04 --device-read-bps /dev/sda:1M centos:latest bash
95e690836d8d6ab58b0445cce1ca1b6d0e6069e5954ae46aa02aa5a3a52379b3
[root@zbx-agent01 docker]# docker exec -it test05 bash
[root@d42da1e99eaa /]# dd if=/dev/zero of=test.txt bs=2M count=5 oflag=direct
5+0 records in
5+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 10.0024 s, 1.0 MB/s
原文鏈接:https://blog.csdn.net/weixin_66946305/article/details/125732050
- 上一篇:Docker之Harbor私有倉庫
- 下一篇:Docker 數據管理
相關推薦
- 2022-11-05 go?module化?import?調用本地模塊?tidy的方法_Golang
- 2023-07-26 webpack原理之開發第一個loader
- 2022-07-06 python如何刪除字符串最后一個字符_python
- 2022-10-23 Android?手寫RecyclerView實現列表加載_Android
- 2022-08-04 如何利用python實現列表嵌套字典取值_python
- 2023-12-09 出現錯誤:SLF4J: Class path contains multiple SLF4J bin
- 2024-02-25 maven打包測試jar包沖突
- 2021-12-02 Android創建淡入淡出動畫的詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支