diff --git a/cmd/uncloud/service/run.go b/cmd/uncloud/service/run.go index 1c02565a..cb355b71 100644 --- a/cmd/uncloud/service/run.go +++ b/cmd/uncloud/service/run.go @@ -28,6 +28,7 @@ type runOptions struct { machines []string memory dockeropts.MemBytes mode string + shmSize dockeropts.MemBytes name string privileged bool publish []string @@ -78,8 +79,12 @@ func NewRunCommand(groupID string) *cobra.Command { "Placement constraint by machine names, limiting which machines the service can run on. Can be specified "+ "multiple times or as a comma-separated list of machine names. (default is any suitable machine)") cmd.Flags().VarP(&opts.memory, "memory", "", - "Maximum amount of memory a service container can use. Value is a positive integer with optional unit suffix "+ - "(b, k, m, g). Default unit is bytes if no suffix specified.\n"+ + "Maximum amount of memory a service container can use. Value is a positive integer with optional unit suffix (b, k, m, g).\n"+ + "Default unit is bytes if no suffix specified.\n"+ + "Examples: 1073741824, 1024m, 1g (all equal 1 gibibyte)") + cmd.Flags().VarP(&opts.shmSize, "shm-size", "", + "Maximum amount of shared memory (mounted at /dev/shm) a service container can use. Value is a positive integer\n"+ + "with optional unit suffix (b, k, m, g). Default unit is bytes if no suffix specified.\n"+ "Examples: 1073741824, 1024m, 1g (all equal 1 gibibyte)") cmd.Flags().StringVarP(&opts.name, "name", "n", "", "Assign a name to the service. A random name is generated if not specified.") @@ -224,9 +229,10 @@ func prepareServiceSpec(opts runOptions) (api.ServiceSpec, error) { Privileged: opts.privileged, PullPolicy: opts.pull, Resources: api.ContainerResources{ - CPU: opts.cpu.Value(), - Memory: opts.memory.Value(), - Ulimits: ulimits, + CPU: opts.cpu.Value(), + Memory: opts.memory.Value(), + SharedMemory: opts.shmSize.Value(), + Ulimits: ulimits, }, User: opts.user, VolumeMounts: mounts, diff --git a/internal/machine/docker/server.go b/internal/machine/docker/server.go index 41ae5391..9a3e637b 100644 --- a/internal/machine/docker/server.go +++ b/internal/machine/docker/server.go @@ -642,6 +642,7 @@ func (s *Server) CreateServiceContainer( RestartPolicy: container.RestartPolicy{ Name: container.RestartPolicyUnlessStopped, }, + ShmSize: spec.Container.Resources.SharedMemory, Sysctls: spec.Container.Sysctls, } diff --git a/pkg/api/resources.go b/pkg/api/resources.go index 15bf9e5b..12744a67 100644 --- a/pkg/api/resources.go +++ b/pkg/api/resources.go @@ -21,6 +21,8 @@ type ContainerResources struct { Devices []DeviceMapping // DeviceReservations requests for access to things like GPUs. DeviceReservations []container.DeviceRequest + // SharedMemory is the size of the shared memory (in bytes) mounted at /dev/shm in the container. + SharedMemory int64 // Ulimits defines the resource limits for the container. Ulimits map[string]Ulimit } diff --git a/pkg/client/compose/service.go b/pkg/client/compose/service.go index bf0ffae7..d6530040 100644 --- a/pkg/client/compose/service.go +++ b/pkg/client/compose/service.go @@ -177,6 +177,7 @@ func resourcesFromCompose(service types.ServiceConfig) api.ContainerResources { CPU: int64(service.CPUS * 1e9), Memory: int64(service.MemLimit), MemoryReservation: int64(service.MemReservation), + SharedMemory: int64(service.ShmSize), Ulimits: ulimitsFromCompose(service.Ulimits), } diff --git a/pkg/client/compose/service_test.go b/pkg/client/compose/service_test.go index b65ff9fc..e490a9d6 100644 --- a/pkg/client/compose/service_test.go +++ b/pkg/client/compose/service_test.go @@ -133,6 +133,7 @@ func TestServiceSpecFromCompose(t *testing.T) { CPU: 0.5 * api.Core, Memory: 100 * units.MiB, MemoryReservation: 50 * units.MiB, + SharedMemory: 256 * units.MiB, Ulimits: map[string]api.Ulimit{ "nofile": {Soft: 20000, Hard: 40000}, "nproc": {Soft: 65535, Hard: 65535}, diff --git a/pkg/client/compose/testdata/compose-full-spec.yaml b/pkg/client/compose/testdata/compose-full-spec.yaml index 13bd83e6..d345a240 100644 --- a/pkg/client/compose/testdata/compose-full-spec.yaml +++ b/pkg/client/compose/testdata/compose-full-spec.yaml @@ -34,6 +34,7 @@ services: max-file: 3 mem_limit: 100M mem_reservation: 50M + shm_size: 268435456 privileged: true pull_policy: always scale: 3 diff --git a/website/docs/8-compose-file-reference/1-support-matrix.md b/website/docs/8-compose-file-reference/1-support-matrix.md index 8f9e9dde..000a52c1 100644 --- a/website/docs/8-compose-file-reference/1-support-matrix.md +++ b/website/docs/8-compose-file-reference/1-support-matrix.md @@ -43,6 +43,7 @@ If you rely on a specific Compose feature that is not supported by Uncloud, plea | `pull_policy` | ✅ Supported | `always`, `missing`, `never` | | `secrets` | ❌ Not supported | Use configs or environment variables | | `security_opt` | ❌ Not supported | | +| `shm_size` | ✅ Supported | Shared memory size | | `stop_grace_period` | ✅ Supported | Time to wait after SIGTERM before SIGKILL | | `storage_opt` | ❌ Not supported | | | `sysctls` | ✅ Supported | Namespaced kernel parameters | diff --git a/website/docs/9-cli-reference/uc_run.md b/website/docs/9-cli-reference/uc_run.md index bb05f274..75b61abc 100644 --- a/website/docs/9-cli-reference/uc_run.md +++ b/website/docs/9-cli-reference/uc_run.md @@ -16,7 +16,8 @@ uc run IMAGE [COMMAND...] [flags] Format: VAR=value or just VAR to use the value from the local environment. -h, --help help for run -m, --machine strings Placement constraint by machine names, limiting which machines the service can run on. Can be specified multiple times or as a comma-separated list of machine names. (default is any suitable machine) - --memory bytes Maximum amount of memory a service container can use. Value is a positive integer with optional unit suffix (b, k, m, g). Default unit is bytes if no suffix specified. + --memory bytes Maximum amount of memory a service container can use. Value is a positive integer with optional unit suffix (b, k, m, g). + Default unit is bytes if no suffix specified. Examples: 1073741824, 1024m, 1g (all equal 1 gibibyte) --mode string Replication mode of the service: either 'replicated' (a specified number of containers across the machines) or 'global' (one container on every machine). (default "replicated") -n, --name string Assign a name to the service. A random name is generated if not specified. @@ -31,6 +32,9 @@ uc run IMAGE [COMMAND...] [flags] -p 53:5353/udp@host Bind UDP port 5353 to host port 53 --pull string Pull image from the registry before running service containers ('always', 'missing', 'never'). (default "missing") --replicas uint Number of containers to run for the service. Only valid for a replicated service. (default 1) + --shm-size bytes Maximum amount of shared memory (mounted at /dev/shm) a service container can use. Value is a positive integer + with optional unit suffix (b, k, m, g). Default unit is bytes if no suffix specified. + Examples: 1073741824, 1024m, 1g (all equal 1 gibibyte) --ulimit strings Set resource limits for service containers. Can be specified multiple times. Format: type=soft_limit[:hard_limit]. If hard limit is not specified, soft limit is used for both. Examples: diff --git a/website/docs/9-cli-reference/uc_service_run.md b/website/docs/9-cli-reference/uc_service_run.md index b89114c5..5b39358e 100644 --- a/website/docs/9-cli-reference/uc_service_run.md +++ b/website/docs/9-cli-reference/uc_service_run.md @@ -16,7 +16,8 @@ uc service run IMAGE [COMMAND...] [flags] Format: VAR=value or just VAR to use the value from the local environment. -h, --help help for run -m, --machine strings Placement constraint by machine names, limiting which machines the service can run on. Can be specified multiple times or as a comma-separated list of machine names. (default is any suitable machine) - --memory bytes Maximum amount of memory a service container can use. Value is a positive integer with optional unit suffix (b, k, m, g). Default unit is bytes if no suffix specified. + --memory bytes Maximum amount of memory a service container can use. Value is a positive integer with optional unit suffix (b, k, m, g). + Default unit is bytes if no suffix specified. Examples: 1073741824, 1024m, 1g (all equal 1 gibibyte) --mode string Replication mode of the service: either 'replicated' (a specified number of containers across the machines) or 'global' (one container on every machine). (default "replicated") -n, --name string Assign a name to the service. A random name is generated if not specified. @@ -31,6 +32,9 @@ uc service run IMAGE [COMMAND...] [flags] -p 53:5353/udp@host Bind UDP port 5353 to host port 53 --pull string Pull image from the registry before running service containers ('always', 'missing', 'never'). (default "missing") --replicas uint Number of containers to run for the service. Only valid for a replicated service. (default 1) + --shm-size bytes Maximum amount of shared memory (mounted at /dev/shm) a service container can use. Value is a positive integer + with optional unit suffix (b, k, m, g). Default unit is bytes if no suffix specified. + Examples: 1073741824, 1024m, 1g (all equal 1 gibibyte) --ulimit strings Set resource limits for service containers. Can be specified multiple times. Format: type=soft_limit[:hard_limit]. If hard limit is not specified, soft limit is used for both. Examples: