With the introduction of Swarm Mode came a transition in how cpu resources are reserved or limited. For the most part, the documentation around how to configure cpu resources available to a container or service is clear, so hats off to the documentation contributors.
Without digging into source code, I cannot find how the Swarm Mode cpu reservation impacts moby or whether there is a cgroup impact.
In compose v2, we could set the cpu reservation through, for example,
cpu_shares 256, which is the equivalent to
docker run --cpu-shares 256, which can be detected inside the container if necessary through
In compose v3, we have a new stanza to reserve cpus for a service:
version: '3' services: redis: image: redis:alpine deploy: resources: reservations: cpus: '0.25'
The equivalent command would be
docker service create --name redis --reserve-cpu 0.25 redis:alpine
Background aside, the documentation implies that this allows the service to request a share of CPU cycles for each replica of that service. However, it is not clear whether this is only used for scheduling (container placement) purposes or if this is implemented through cgroups the same way it was in the past. Or perhaps it achieves the goal of resource reservation through some other means?
I ask because I do not see a change to
/sys/fs/cgroup/cpu/cpu.shares inside the containers in Swarm Mode (running 18.03.01-ce) as I would have seen by applying
--cpu-shares directly in the past. There is a feature request that touches on this at https://github.com/moby/moby/issues/36388 but it is not clear from the documentation whether this should already be implemented and would therefore be considered a bug or if the current
--reserve-cpu implementation sets reservations some other way.