这里仅针对docker本身,不涉及任何编排工具compose或者k8s等。

按照惯例,官文撸起来。

重要的部分是一些选项,用来限制资源大小。

Memory
Most of these options take a positive integer, followed by a suffix of b, k, m, g, to indicate bytes, kilobytes, megabytes, or gigabytes.

-m or --memory= The maximum amount of memory the container can use. If you set this option, the minimum allowed value is 4m (4 megabyte).
--oom-kill-disable By default, if an out-of-memory (OOM) error occurs, the kernel kills processes in a container. To change this behavior, use the --oom-kill-disable option. Only disable the OOM killer on containers where you have also set the -m/--memory option. If the -m flag is not set, the host can run out of memory and the kernel may need to kill the host system’s processes to free memory.

多数选项值都是正整数,单位是b, k, m, g,分别表示 bytes, kilobytes, megabytes, 和 gigabytes。

-m or --memory= 指定一个容器能使用的最大内存,默认不允许小于4m。

--oom-kill-disable 默认如果发生OOM异常,内核会kill掉容器中的进程。可以使用这个选项来改变这种行为。只在设置了-m内存限制的容器上有效。如果没有设置-m,宿主机会OOM,然后内核会需要kill宿主机的进程来释放内存。


CPU
By default, each container’s access to the host machine’s CPU cycles is unlimited. You can set various constraints to limit a given container’s access to the host machine’s CPU cycles. Most users use and configure the default CFS scheduler.

The CFS is the Linux kernel CPU scheduler for normal Linux processes. Several runtime flags allow you to configure the amount of access to CPU resources your container has. When you use these settings, Docker modifies the settings for the container’s cgroup on the host machine.

--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.
--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资源是没有限制的。多数用户使用默认的CFS调度器来配置。

CFS是linux用于进程的内核cpu调度器。有一些运行时选项可以用来限制容器可以使用的cpu资源,这些配置会修改宿主机上容器的cgroup设置。

--cpus= 指定一个容器可用的cpu资源的大小。例如,如果宿主机有两个cpu,那么--cpus="1.5"表示可以使用1.5个cpu。

--cpuset-cpus 限制一个容器能使用指定的cpu或cpu核心。可以用逗号分隔符表示单独的哪几个,或者连字符(中横线)表示范围。从0开始计数,第一个cpu记为0。1, 3表示使用第二个和第四个cpu。1-3表示使用第二三四个cpu。

一个简单示例:

# If you have 1 CPU,  the following command guarantees the container at most 50% of the CPU every second.

docker run -it --cpus=".5" debian:jessie /bin/bash

参考:

https://docs.docker.com/config/containers/resource_constraints/

docker的memory和cpu资源限制的更多相关文章

  1. 控制 Memory 和 CPU 资源的使用

    Resource Governor的出现,解决了在一台SQL Server实例上,管理多用户工作负载和资源隔离的需求,它允许管理员限制系统处理Requsts时所耗费的CPU 和 Memory资源的数量 ...

  2. Docker技术入门与实战 第二版-学习笔记-5-容器-命令及限制内存与cpu资源

    1.启动容器 启动容器有两种方式: 基于镜像新建一个容器并启动 将在终止状态(stopped)的容器重新启动 1)新建并启动——docker run 比如在启动ubuntu:14.04容器,并输出“H ...

  3. docker高级应用之cpu与内存资源限制(转)

    时间:2015-06-09 14:01:52      阅读:1581      评论:0      收藏:0      [点我收藏+] 标签:docker资源限制   docker cpu限制    ...

  4. Docker CPU 资源限制——CPU分片功能测试

    之前的一篇随笔——Docker CPU 资源限制 中介绍了针对COU的某个或某几个核的控制,今天介绍下CPU分片功能,即CPU占比. 测试步骤 1.下载CPU测试image.agileek/cpuse ...

  5. Docker CPU 资源限制——CPU固定核功能测试

    Docker使用Linux cgroup来实现资源的限制,对于CPU的限制有两种方法: 1.cpuset CPU Set限定容器使用某个固定的CPU核.使用默认的libcontainer引擎时,可以通 ...

  6. 理解Docker(4):Docker 容器使用 cgroups 限制资源使用

    本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  7. docker cgroup技术之cpu和cpuset

    在centos7的/sys/fs/cgroup下面可以看到与cpu相关的有cpu,cpuacct和cpuset 3个subsystem.cpu用于对cpu使用率的划分:cpuset用于设置cpu的亲和 ...

  8. spark docker java kubernetes 获取cpu内核/线程数问题

    升级服务从spark2.3.0-hadoop2.8 至 spark2.4.0 hadoop3.0 一日后导致spark streaming kafka消费数据积压 服务不是传统的部署在yarn上,而是 ...

  9. 一个表缺失索引发的CPU资源瓶颈案例

    背景 近几日,公司的应用团队反应业务系统突然变慢了,之前是一直比较正常.后与业务部门沟通了解详情,得知最近生意比较好,同时也在做大的促销活动,使得业务数据处理的量出现较大的增长,最终系统在处理时出现瓶 ...

随机推荐

  1. Linux下路由配置梳理

    在日常运维作业中,经常会碰到路由表的操作.下面就linux运维中的路由操作做一梳理:---------------------------------------------------------- ...

  2. ajax多级菜单栏

    1.jsp 首先ajax查询数据 <script type="text/javascript"> function targetlist() { $.ajax({ ur ...

  3. idea 设置注释

    idea和eclipse的注释还是有一些差别的. idea: 类头注释:打开file->setting->Editor->Filr and Code Templates->In ...

  4. HDOJ2007_平方和与立方和

    应该注意到一个细节是题目中没有说明输入的两个数据一定是先小后大的关系,所以需要做一次判断.其他的比较简单. HDOJ2007_平方和与立方和 #include<iostream> #inc ...

  5. OpenCV Code: Operations on Mat element

    cv::Mat img = cv::imread("image.jpg"); Method 1: img.at<uchar>(i, j, k) Method 2: im ...

  6. C语言复制文件的两种简单的方法【从根本解决问题】

    网上的方法大致有这样几种: 1.使用操作系统提供的复制文件的API 2.使用C语言本身提供的复制文件的函数 3.直接读写文件,从文件角度来操作,从而直接将一个文件复制 这里我们使用的就是这第三种. 复 ...

  7. Linux初学笔记---关于进程管理等

    菜鸟初学: 1. 查看进程用的命令: ps 具体用法 ps -A ro ps -e 显示所有进程 ps -u root 显示root 用户的进程 ps -u root -N 显示非root用户的进程 ...

  8. [转帖]Marvell兵败中国4G 创始人去职未来几何

    Marvell兵败中国4G 创始人去职未来几何 (2016-04-12 09:08:30) 2016年的帖子. http://blog.sina.com.cn/s/blog_1542ef86c0102 ...

  9. ubuntu ftp服务

    apt-get install vsftpd apt-get update vi /etc/apt/sources.list vim  /etc/vsftpd.conf service vsftpd ...

  10. 远程连接db2数据库

    在db2数据库中,需要理解catalog(编目)这个概念,理解前先说下db2数据库的体系结构:由系统(节点)也就是主机,下面是实例,实例下面是数据库,然后是表空间,再是数据库对象.现在假设你有一个数据 ...