java启动限制cpu_kubernetes cpu限制参数说明

docker CPU限制参数

Option

Description

--cpus=

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=

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=

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的资源

48304ba5e6f9fe08f3fa1abda7d326ab.png

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

48304ba5e6f9fe08f3fa1abda7d326ab.png

第二种:定义pod时候限制资源

48304ba5e6f9fe08f3fa1abda7d326ab.png

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

48304ba5e6f9fe08f3fa1abda7d326ab.png

如果两者都配置?

先匹配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

7d85cd92785bfe5d36ee9fc62ac0ae70.png

2. docker stats查看容器CPU使用率

由于设置了CPUQuota是CpuPeriod的4倍,所以容器可以使用400% CPU

e0edf788ff87402698225d446e397f87.png

3. 使用top查看进程与CPU

使用top命令查看4个stress进程,每个占用100% CPU,总400%,可以看到有4个CPU被跑满。

686248289f8497ee61e3b596a2d5ada5.png

aa60a71e54158c6887848aecd01b9534.png

实验二:

limits.cpu: 6

requests.cpu: 0.5

1. 查看docker容器参数值:

512 = 0.5 * 1024

600000 = 6 * 100000

70c5a1f1f97234cb664d66d74c69a324.png

2. docker stats查看容器CPU使用率

容器可以使用600%的CPU,现在只用400%

3f08aa335c76b9a6ccb725a72e47eeac.png

3. 使用top查看进程与CPU

dcbce84beb1f1e6773646f6f30a87566.png

a0e35edb7fad7fc5696f51d85616ed60.png

实验三:

limits.cpu: 1

requests.cpu: 0.5

1. 查看docker容器参数值:

docker inspect e22896246184

512 = 0.5 * 1024

100000 = 1 * 100000

c117da29c0cbb1ee94920da6169dd98c.png

2. docker stats查看容器CPU使用率

使用时间等于CpuPeriod,占用100%

0170edf1907783cfbe40e665684bcfd9.png

3. 使用top查看进程与CPU

eac4f40705e378bbbb9a41f86eb8b1ad.png

从下图可以看到,有4个CPU分别使用25%,加起来是100%。所以limits.cpu:1并不一定表示容器只会占用1个CPU,而表示的是容器最多可以使用的CPU时间的比例。

609e1a7216c632834cad638a3d805147.png

实验四:

limits.cpu: 0.5

requests.cpu: 0.5

1. 查看docker容器参数值

73f03475457289d573c4dc2d5572a362.png

2. docker stats查看容器CPU使用率

5279ae778b2ada874e4b103fa7d4a968.png

3. 使用top查看进程与CPU

28b2063ce97d84e496cddda57eec5875.png

ae691473e4cd7d55e64d7dc76c04eb23.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用kubernetes-client java获取pod容器内存与CPU使用率,可以按照以下步骤进行操作。 首先,你需要在Java项目中添加kubernetes-client的依赖,例如在pom.xml文件中添加以下代码: ```xml <dependencies> <dependency> <groupId>io.kubernetes</groupId> <artifactId>client-java</artifactId> <version>9.0.0</version> </dependency> </dependencies> ``` 接下来,你可以使用以下代码获取pod的相关信息: ```java import io.kubernetes.client.apis.CoreV1Api; import io.kubernetes.client.custom.Quantity; import io.kubernetes.client.models.V1Container; import io.kubernetes.client.models.V1ContainerStatus; import io.kubernetes.client.models.V1NamespaceList; import io.kubernetes.client.models.V1Pod; import io.kubernetes.client.models.V1PodList; import io.kubernetes.client.models.V1PodMetrics; import io.kubernetes.client.models.V1PodMetricsList; import io.kubernetes.client.util.Config; import java.util.Map; public class KubernetesClientExample { public static void main(String[] args) throws Exception { // 创建Kubernetes客户端 io.kubernetes.client.Configuration config = Config.defaultClientConfig(); io.kubernetes.client.apis.CoreV1Api api = new CoreV1Api(); // 获取pod列表 V1PodList podList = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); for (V1Pod pod : podList.getItems()) { // 获取pod所属的命名空间和名称 String namespace = pod.getMetadata().getNamespace(); String name = pod.getMetadata().getName(); // 获取pod的容器列表和相关状态信息 for (V1Container container : pod.getSpec().getContainers()) { String containerName = container.getName(); V1ContainerStatus containerStatus = pod.getStatus().getContainerStatuses().stream() .filter(status -> status.getName().equals(containerName)) .findFirst().orElse(null); if (containerStatus != null) { // 获取容器的CPU和内存使用率 Map<String, Quantity> usage = containerStatus.getUsage(); Quantity cpuUsage = usage.get("cpu"); Quantity memoryUsage = usage.get("memory"); System.out.println("Namespace: " + namespace + ", Pod: " + name + ", Container: " + containerName + ", CPU Usage: " + cpuUsage + ", Memory Usage: " + memoryUsage); } } } } } ``` 上述代码中,我们首先创建了Kubernetes的客户端,然后通过CoreV1Api实例来获取pod列表。对于每个pod,我们遍历其容器列表,并获取容器的名称以及相关状态信息。在状态信息中,我们可以找到容器的CPU和内存使用率,以及其他相关指标。 需要注意的是,这里获取的是当前时刻的使用率数据,如果你想要定时获取容器的使用率信息,可以使用定时任务等方法来实现。 这是一个简单的示例,你可以根据实际需求和项目结构进行相应的扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值