docker CPU限制參數
Option |
Description |
--cpus=<value> |
Specify how much of the available CPU resources a container can use. For instance, if the host machine has two CPUs and you set --cpus="1.5", the container is guaranteed at most one and a half of the CPUs. This is the equivalent of setting --cpu-period="100000" and --cpu-quota="150000". Available in Docker 1.13 and higher. |
--cpu-period=<value> |
Specify the CPU CFS scheduler period, which is used alongside--cpu-quota. Defaults to 100 micro-seconds. Most users do not change this from the default. If you use Docker 1.13 or higher, use --cpusinstead. |
--cpu-quota=<value> |
Impose a CPU CFS quota on the container. The number of microseconds per --cpu-period that the container is limited to before throttled. As such acting as the effective ceiling. If you use Docker 1.13 or higher, use --cpus instead. |
--cpuset-cpus |
Limit the specific CPUs or cores a container can use. A comma-separated list or hyphen-separated range of CPUs a container can use, if you have more than one CPU. The first CPU is numbered 0. A valid value might be 0-3 (to use the first, second, third, and fourth CPU) or 1,3 (to use the second and fourth CPU). |
--cpu-shares |
Set this flag to a value greater or less than the default of 1024 to increase or reduce the container’s weight, and give it access to a greater or lesser proportion of the host machine’s CPU cycles. This is only enforced when CPU cycles are constrained. When plenty of CPU cycles are available, all containers use as much CPU as they need. In that way, this is a soft limit. --cpu-shares does not prevent containers from being scheduled in swarm mode. It prioritizes container CPU resources for the available CPU cycles. It does not guarantee or reserve any specific CPU access. |
① --cpus指示容器可以使用的CPU數量。改參數指定的是百分比,並不是具體的個數。比如:主機有4個邏輯CPU,限制容器—cpus=2,那麼該容器最多可以使用的CPU時間是200%,但是4個CPU分配的時間可能是每個CPU各50%,而不一定是有其中2個CPU使用100%,而另2個CPU使用0%。
--cpus是docker 1.13之後纔出來的參數,目的是替代--cpu-period和--cpu-quota兩個參數,從而使配置更簡單。
② --cpu-period表示的是設置CPU時間週期,默認值是100000,單位是us,即0.1s。
③ --cpu-quota指示容器可以使用的最大的CPU時間,配合--cpu-period值使用。如果—cpu-quota=200000,即0.2s。那就是說在0.1s週期內改容器可以使用0.2s的CPU時間,顯然1個CPU是無法滿足要求的,需要至少2個CPU才能滿足。
④ --cpuset-cpus設置容器具體可以使用哪些個CPU。如--cpuset-cpus=”0,1,2”或者--cpuset-cpus=”0-2”,則容器會使用第0-2個CPU。
⑤ --cpu-shares,容器使用CPU的權重,默認值是1024,數值越大權重越大。該參數僅當有多個容器競爭同一個CPU時生效。對於單核CPU,如果容器A設置爲--cpu-shares=2048,容器B設置爲--cpus-shres=1024,僅當兩個容器需要使用的CPU時間超過整個CPU週期的時候,容器A會被分配66%的CPU時間,容器B被分配33%的CPU時間,大約是2:1;對於多核CPU,僅當多個容器競爭同一個CPU的時候該值生效。
kubernetes對CPU限制
第一種:資源對象LimitRange限制POD和Container的資源
apiVersion: v1 kind: LimitRange metadata: name: mylimits spec: limits: - max: cpu: "2" memory: 1Gi min: cpu: 200m memory: 6Mi type: Pod - default: cpu: 300m memory: 200Mi defaultRequest: cpu: 200m memory: 100Mi max: cpu: "2" memory: 1Gi min: cpu: 100m memory: 3Mi type: Container
第二種:定義pod時候限制資源
spec: containers: - image: gcr.io/google_containers/serve_hostname imagePullPolicy: Always name: kubernetes-serve-hostname resources: limits: cpu: "1" memory: 512Mi requests: cpu: "1" memory: 512Mi
如果兩者都配置?
先匹配pod裏的,再匹配namespace裏。
有些時候, 我們大部分容器遵循一個規則就好, 但有一小部分有特殊需求, 這個時候, 小部分的就需要單獨在容器的配置文件中指定. 這裏有一點要注意的是, 單獨在容器中配置的參數是不能大於指定的k8s資源限制, 否則會報錯, 容器無法啓動
PS: 對於一些java
項目, 必須設置java
虛擬機
的參數, 而且這個參數不能大於容器設置的限定值, 否則容器會因爲內存過大不停的重啓
其中:
limits.cpu <==> --cpu-quota # docker inspect中的CpuQuota值
requests.cpu <==> --cpu-shares # docker inspect中的CpuShares值
實驗對比
測試工具stress介紹
root@ustress-77b658748b-7856l:/# stress --help
`stress' imposes certain types of compute stress on your system
Usage: stress [OPTION [ARG]] ...
-?, --help show this help statement
--version show version statement
-v, --verbose be verbose
-q, --quiet be quiet
-n, --dry-run show what would have been done
-t, --timeout N timeout after N seconds
--backoff N wait factor of N microseconds before work starts
-c, --cpu N spawn N workers spinning on sqrt() #啓動N個進程,每個進程最多佔滿一個CPU
-i, --io N spawn N workers spinning on sync()
-m, --vm N spawn N workers spinning on malloc()/free()
--vm-bytes B malloc B bytes per vm worker (default is 256MB)
--vm-stride B touch a byte every B bytes (default is 4096)
--vm-hang N sleep N secs before free (default none, 0 is inf)
--vm-keep redirty memory instead of freeing and reallocating
-d, --hdd N spawn N workers spinning on write()/unlink()
--hdd-bytes B write B bytes per hdd worker (default is 1GB)
Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s
Note: Numbers may be suffixed with s,m,h,d,y (time) or B,K,M,G (size).
創建一個測試鏡像
FROM ubuntu:latest
RUN apt-get update && \
apt-get install stress
docker build -t reg.99bill.com/99bill/ustress .
創建一個kubernetes中deployment對象
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
appname: ustress
version: 0.0.6
name: ustress
namespace: default
spec:
replicas: 1
selector:
matchLabels:
appname: ustress
template:
metadata:
labels:
appname: ustress
version: 0.0.6
spec:
containers:
- image: reg.99bill.com/99bill/u-stress:latest
name: ustress
command: ['sh', '-c', 'stress -c 4']
resources:
limits:
cpu: 2 #實驗修改值
memory: 1G
requests:
cpu: 1 #實驗修改值
memory: 500M
terminationGracePeriodSeconds: 0
nodeName: 192.168.112.10
nodeSelector:
注:
① command: ['sh', '-c', 'stress -c 4'] 表示開啓4個佔用CPU的stress進程
② limits.cpu: 2 對應docker中"CpuQuota": 200000, "CpuPeriod": 100000默認值
③ requests.cpu:1對應docker中"CpuShares": 1024,
測試一:
limits.cpu: 4
requests.cpu: 0.5
結果驗證:
1. 查看docker容器參數值:
docker inspect e22896246184
512 = 0.5 * 1024
400000 = 4 * 100000
2. docker stats查看容器CPU使用率
由於設置了CPUQuota是CpuPeriod的4倍,所以容器可以使用400% CPU
3. 使用top查看進程與CPU
使用top命令查看4個stress進程,每個佔用100% CPU,總400%,可以看到有4個CPU被跑滿。
實驗二:
limits.cpu: 6
requests.cpu: 0.5
1. 查看docker容器參數值:
512 = 0.5 * 1024
600000 = 6 * 100000
2. docker stats查看容器CPU使用率
容器可以使用600%的CPU,現在只用400%
3. 使用top查看進程與CPU
實驗三:
limits.cpu: 1
requests.cpu: 0.5
1. 查看docker容器參數值:
docker inspect e22896246184
512 = 0.5 * 1024
100000 = 1 * 100000
2. docker stats查看容器CPU使用率
使用時間等於CpuPeriod,佔用100%
3. 使用top查看進程與CPU
從下圖可以看到,有4個CPU分別使用25%,加起來是100%。所以limits.cpu:1並不一定表示容器只會佔用1個CPU,而表示的是容器最多可以使用的CPU時間的比例。
實驗四:
limits.cpu: 0.5
requests.cpu: 0.5
1. 查看docker容器參數值
2. docker stats查看容器CPU使用率
3. 使用top查看進程與CPU