【问题标题】:How to change the resources allocated to a container at run time?如何在运行时更改分配给容器的资源?
【发布时间】:2016-06-04 12:21:32
【问题描述】:

我知道,在使用带有 CPU 和内存的 -c-m 标志的 docker 进行配置时,我可以限制分配给容器的资源。

但是,有没有一种方法可以动态地将这些分配的资源更改为容器(在它们被配置之后),而无需重新部署具有更改资源的同一个容器?

【问题讨论】:

    标签: resources docker


    【解决方案1】:

    当时(Docker v1.11.1)有命令docker updateview docs)。有了它,您可以即时更改分配的资源。

    Usage:  docker update [OPTIONS] CONTAINER [CONTAINER...]
    
    Update configuration of one or more containers
    
      --blkio-weight          Block IO (relative weight), between 10 and 1000
      --cpu-shares            CPU shares (relative weight)
      --cpu-period            Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota             Limit CPU CFS (Completely Fair Scheduler) quota
      --cpuset-cpus           CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems           MEMs in which to allow execution (0-3, 0,1)
      --help                  Print usage
      --kernel-memory         Kernel memory limit
      -m, --memory            Memory limit
      --memory-reservation    Memory soft limit
      --memory-swap           Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --restart               Restart policy to apply when a container exits
    

    【讨论】:

      【解决方案2】:

      目前没有,但希望看到有人实现它:https://github.com/docker/docker/issues/6323

      【讨论】:

      【解决方案3】:

      Docker 1.10 或 1.11(2016 年第一季度)可能会出现这种情况:PR 15078 正在实施(2015 年 12 月)支持更改资源(包括 CPU),用于停止和运行容器。

      2016 年更新:它是 part of docker 1.10,并记录在 docker update (PR 15078) 中。

      我们决定允许设置我们所谓的资源,它现在由 cgroup 事物组成,因此下面是 PR #18073
      容器中唯一允许的可变元素位于 HostConfig 中,并且恰好位于 Resources 中(参见 struct)。

      resources := runconfig.Resources{
              BlkioWeight:       *flBlkioWeight,
              CpusetCpus:        *flCpusetCpus,    <====
              CpusetMems:        *flCpusetMems,    <====
              CPUShares:         *flCPUShares,     <====
              Memory:            flMemory,
              MemoryReservation: memoryReservation,
              MemorySwap:        memorySwap,
              KernelMemory:      kernelMemory,
              CPUPeriod:         *flCPUPeriod,
              CPUQuota:          *flCPUQuota,
          }
      
      • 命令应该是set(最后是update)。
      • 允许的更改作为标志传递:例如--memory=1Gb --cpushare=…(就像这个 PR 一样)。
      • Resources 结构的每个属性都有一个标志(不多也不少)。

      请注意,通过 docker set 进行更改应该会持续存在。
      即,这些更改将是永久性的(在容器的 JSON 中更新)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多