github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/design/generic_resources.md (about)

     1  # Generic Resources
     2  
     3    * [Abstract](#abstract)
     4    * [Motivation](#motivation)
     5    * [Use Cases](#use-cases)
     6    * [Related Issues](#related-issues)
     7    * [Objectives](#objectives)
     8    * [Non-Objectives](#non-objectives)
     9    * [Proposed Changes](#proposed-changes)
    10  
    11  ##  Abstract
    12  
    13  This document describes the a solution to managing accountable node level
    14  resources unknown to docker swarm.
    15  
    16  ## Motivation
    17  
    18  Each node is different in its own way, some nodes might have access to
    19  accelerators, some nodes might have access to network devices and others
    20  might support AVX while others only support SSE.
    21  Swarmkit needs some simple way to account for these resources without having
    22  to implement them each time a new kind of resource comes into existence.
    23  
    24  While it is true that some resources can be advertised with labels, many
    25  resources have a shareable capacity and can’t be represented well as a label.
    26  
    27  The implementation we chose is to reuse a proven solution used by industry
    28  projects (mesos and kubernetes) which lead us to implement two kinds
    29  of generic resources:
    30    * Discrete (int64)
    31    * Set
    32    * Other types of resource like scalar can be extended
    33  
    34  Discrete resources are for use cases where only an unsigned is needed to account
    35  for the resource (see Linux Realtime).
    36  
    37  A set would mostly be used for every resource which would need an
    38  exclusive access to it.
    39  
    40  ## Constraints and Assumptions
    41  1. Future work might require new mechanisms to be made to allow generic resources
    42  to be cluster wide in order to satisfy other use cases (e.g: pool of licenses)
    43  2. Future work might require to add filters at the resource level
    44  2. Future work might require to share resources
    45  
    46  ## Use Cases
    47  
    48    * Exclusive access to discrete accelerators:
    49      * GPU devices
    50      * FPGA devices
    51      * MICs (Many-Integrated Core, such as Xeon Phi)
    52      * ...
    53    * Support for tracking additional cgroup quotas like cpu_rt_runtime.
    54      * [Linux Realtime](https://github.com/docker/docker/pull/23430)
    55    * PersistentDisks in GCE
    56    * Counting “slots” allowed access to a shared parallel file system.
    57  
    58  ## Related Issues
    59  
    60    * [Support abstract resource](https://github.com/docker/swarmkit/issues/594)
    61    * [Add new node filter to scheduler](https://github.com/docker/swarm/issues/2223)
    62    * [Add support for devices](https://github.com/docker/swarmkit/issues/1244)
    63    * [Resource Control](https://github.com/docker/swarmkit/issues/211)
    64    * [NVIDIA GPU support](https://github.com/docker/docker/issues/23917)
    65    * [Does Docker have plan to support allocating GPU](https://github.com/docker/docker/issues/24582)
    66    * [Docker Swarm to orchestrate "Swarm Cluster" which supports GPU](https://github.com/docker/docker/issues/24750)
    67    * [Use accelerator in docker container](https://github.com/docker/docker/issues/28642)
    68    * [Specify resource selectors](https://github.com/docker/swarmkit/issues/206)
    69  
    70  ## Objectives
    71  
    72  1. Associate multiple generic resources with a node
    73  2. Request some portion of available generic resources in the service
    74     during service creation
    75  3. Enable users to define and schedule generic resources in a vanilla swarmkit cluster
    76  
    77  ## Non-Objectives
    78  
    79  1. Solve how generic resources allocations are to be enforced or isolated.
    80  2. Solve how generic resources are discovered
    81  2. Solve how to filter at the resources level
    82  3. Solve how cluster-level generic resources should be advertised
    83  
    84  ## Proposed Changes
    85  
    86  ### Generic Resources request
    87  
    88  The services may only ask for generic resources as integers as the solution for asking for
    89  specific resources can be solved in many different ways (filters, multiple kinds
    90  of resources, ...) and should not be addressed in this PR.
    91  
    92  ```
    93  $ # Single resource
    94  $ swarmctl service create --name nginx --image nginx:latest --generic-resources "banana=2"
    95  $ # Multiple resources
    96  $ swarmctl service create --name nginx --image nginx:latest --generic-resources "banana=2,apple=3"
    97  ```
    98  
    99  ### Generic Resource advertising
   100  
   101  A node may advertise either an discrete number of resources or a set of resources.
   102  It is the scheduler's job to decide which resource to assign and keep track of which task
   103  owns which resource.
   104  
   105  ```
   106  $ swarmd -d $DIR --join-addr $IP --join-token $TOKEN --generic-node-resources "banana=blue,banana=red,banana=green,apple=8"
   107  ```
   108  
   109  ### Generic Resource communication
   110  
   111  As swarmkit is not responsible for exposing the resources to the container (or acquiring them),
   112  it needs a way to communicate how many generic resources were assigned (in the case of
   113  discrete resources) or / and what resources were selected (in the case of sets).
   114  
   115  The reference implementation of the executor exposes the resource value to
   116  software running in containers through environment variables.
   117  The exposed environment variable is prefixed with `DOCKER_RESOURCE_` and it's key
   118  uppercased.
   119  
   120  See example in the next section.
   121  
   122  **If we run `swarmctl inspect` we can see:**
   123  
   124  ```bash
   125  $ swarmctl node inspect node-with-generic-resources
   126  ID                        : 9toi8u8zo1qbkiw1d1nrsevdd
   127  Hostname                  : node-with-generic-resources
   128  Status:
   129    State                   : READY
   130    Availability            : ACTIVE
   131    Address                 : 127.0.0.1
   132  Platform:
   133    Operating System        : linux
   134    Architecture            : x86_64
   135  Resources:
   136    CPUs                    : 12
   137    Memory                  : 31 GiB
   138    apple                   : 3
   139    banana                  : red, blue, green
   140  Plugins:
   141    Network                 : [bridge host macvlan null overlay]
   142    Volume                  : [local nvidia-docker]
   143  Engine Version            : 1.13.1
   144  
   145  $ swarmctl service create --name nginx --image nginx:latest --generic-resources "banana=2,apple=2"
   146  $ swarmctl service inspect nginx
   147  ID                       : abxelhl822d8zyjqam3m3szb0
   148  Name                     : nginx
   149  Replicas                 : 1/1
   150  Template
   151   Container
   152    Image                  : nginx:latest
   153    Resources
   154      Reservations:
   155        banana             : 2
   156        apple              : 2
   157  
   158  Task ID                      Service    Slot    Image           Desired State    Last State                Node
   159  -------                      -------    ----    -----           -------------    ----------                ----
   160  6pbwd5qj7i0nsxlyi803qpf2x    nginx     1       nginx:latest    RUNNING          RUNNING 12 seconds ago    node-with-generic-resources
   161  
   162  $ # ssh to the node
   163  $ docker inspect $CONTAINER_ID --format '{{.Config.Env}}' | tr -s ' ' '\n'
   164  [DOCKER_RESOURCE_BANANA=red,blue
   165  DOCKER_RESOURCE_APPLE=2
   166  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
   167  NGINX_VERSION=1.13.0-1~stretch
   168  NJS_VERSION=1.13.0.0.1.10-1~stretch]
   169  
   170  
   171  ```