Docker 容器资源限制配置 - Resources 结构体详解
Docker 容器资源限制配置 - Resources 结构体详解
Resources 结构体用于定义 Docker 容器的资源限制配置,它包含了各种资源限制参数,例如 CPU、内存、磁盘 IO、设备映射、ulimit 等。
// Resources contains container's resources (cgroups config, ulimits...)
type Resources struct {
// Applicable to all platforms
CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
Memory int64 // Memory limit (in bytes)
NanoCPUs int64 `json:"NanoCpus"` // CPU quota in units of 10<sup>-9</sup> CPUs.
// Applicable to UNIX platforms
CgroupParent string // Parent cgroup.
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
BlkioWeightDevice []*blkiodev.WeightDevice
BlkioDeviceReadBps []*blkiodev.ThrottleDevice
BlkioDeviceWriteBps []*blkiodev.ThrottleDevice
BlkioDeviceReadIOps []*blkiodev.ThrottleDevice
BlkioDeviceWriteIOps []*blkiodev.ThrottleDevice
CPUPeriod int64 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
CPURealtimePeriod int64 `json:"CpuRealtimePeriod"` // CPU real-time period
CPURealtimeRuntime int64 `json:"CpuRealtimeRuntime"` // CPU real-time runtime
CpusetCpus string // CpusetCpus 0-2, 0,1
CpusetMems string // CpusetMems 0-2, 0,1
Devices []DeviceMapping // List of devices to map inside the container
DeviceCgroupRules []string // List of rule to be added to the device cgroup
DeviceRequests []DeviceRequest // List of device requests for device drivers
// KernelMemory specifies the kernel memory limit (in bytes) for the container.
// Deprecated: kernel 5.4 deprecated kmem.limit_in_bytes.
KernelMemory int64 `json:",omitempty"`
KernelMemoryTCP int64 `json:",omitempty"` // Hard limit for kernel TCP buffer memory (in bytes)
MemoryReservation int64 // Memory soft limit (in bytes)
MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap
MemorySwappiness *int64 // Tuning container memory swappiness behaviour
OomKillDisable *bool // Whether to disable OOM Killer or not
PidsLimit *int64 // Setting PIDs limit for a container; Set `0` or `-1` for unlimited, or `null` to not change.
Ulimits []*units.Ulimit // List of ulimits to be set in the container
// Applicable to Windows
CPUCount int64 `json:"CpuCount"` // CPU count
CPUPercent int64 `json:"CpuPercent"` // CPU percent
IOMaximumIOps uint64 // Maximum IOps for the container system drive
IOMaximumBandwidth uint64 // Maximum IO in bytes per second for the container system drive
}
Resources 结构体字段说明
- CPUShares: 用于设置容器的 CPU 资源分配比例,值越大,容器分配到的 CPU 资源越多。
- Memory: 设置容器内存限制,单位为字节。
- NanoCPUs: 设置容器 CPU 配额,单位为 10-9 个 CPU。
- CgroupParent: 设置容器所属的 cgroup 父节点。
- BlkioWeight: 设置容器磁盘 IO 权重,值越大,容器分配到的磁盘 IO 资源越多。
- BlkioWeightDevice: 设置容器针对特定设备的磁盘 IO 权重。
- BlkioDeviceReadBps: 设置容器针对特定设备的磁盘读速度限制,单位为字节/秒。
- BlkioDeviceWriteBps: 设置容器针对特定设备的磁盘写速度限制,单位为字节/秒。
- BlkioDeviceReadIOps: 设置容器针对特定设备的磁盘读操作限制,单位为 IOPS/秒。
- BlkioDeviceWriteIOps: 设置容器针对特定设备的磁盘写操作限制,单位为 IOPS/秒。
- CPUPeriod: 设置 CPU CFS 调度周期。
- CPUQuota: 设置 CPU CFS 调度配额。
- CPURealtimePeriod: 设置 CPU 实时调度周期。
- CPURealtimeRuntime: 设置 CPU 实时调度运行时间。
- CpusetCpus: 设置容器可以使用的 CPU 核数。
- CpusetMems: 设置容器可以使用的内存节点。
- Devices: 设置容器可以使用的设备列表。
- DeviceCgroupRules: 设置容器设备 cgroup 规则。
- DeviceRequests: 设置容器对设备驱动程序的请求。
- KernelMemory: 设置容器内核内存限制,单位为字节。
- KernelMemoryTCP: 设置容器内核 TCP 缓冲区内存限制,单位为字节。
- MemoryReservation: 设置容器内存软限制,单位为字节。
- MemorySwap: 设置容器内存+swap 总使用量,设置为 -1 表示无限 swap。
- MemorySwappiness: 设置容器内存交换行为调整参数。
- OomKillDisable: 设置是否禁用 OOM Killer。
- PidsLimit: 设置容器进程数限制。
- Ulimits: 设置容器 ulimit 限制列表。
- CPUCount: 设置容器可以使用的 CPU 核数,适用于 Windows 平台。
- CPUPercent: 设置容器可以使用的 CPU 资源百分比,适用于 Windows 平台。
- IOMaximumIOps: 设置容器系统驱动器最大 IOPS,适用于 Windows 平台。
- IOMaximumBandwidth: 设置容器系统驱动器最大带宽,单位为字节/秒,适用于 Windows 平台。
资源限制示例
resources:
limits:
cpus: '2'
memory: '2Gi'
reservations:
cpus: '1'
memory: '1Gi'
上述示例中,我们限制了容器的 CPU 和内存资源:
limits.cpus: '2': 设置容器 CPU 资源上限为 2 个 CPU 核。limits.memory: '2Gi': 设置容器内存上限为 2 GB。reservations.cpus: '1': 设置容器 CPU 资源保证量为 1 个 CPU 核。reservations.memory: '1Gi': 设置容器内存保证量为 1 GB。
通过配置 Resources 结构体中的参数,可以根据实际需求对容器进行细粒度的资源限制,以提高资源利用率,保证容器稳定运行。
注意
CPUPercent字段用于设置容器可以使用的 CPU 资源的百分比。它表示容器可以使用的 CPU 资源相对于系统中剩余的 CPU 资源的比例。如果设置的值过大,超过了系统中剩余的 CPU 资源,容器可能无法获得足够的 CPU 资源,导致性能下降或运行失败。- 资源限制配置需要根据实际情况进行调整,建议进行测试以找到最佳配置。
- Docker 的资源限制配置功能基于 Linux cgroups 技术。
原文地址: https://www.cveoy.top/t/topic/fxcE 著作权归作者所有。请勿转载和采集!