服务规范参考
The Snowpark Container Services specification is in YAML (https://yaml.org/spec/ (https://yaml.org/spec/)). It gives Snowflake the necessary information to configure and run your service. You provide the specification at the time of creating a service.
一般语法为:
Note that the spec and serviceRoles are the top-level fields in the specification.
-
spec: Use this field to provide specification details. It includes these top-level fields:- spec.containers (required): A list of one or more application containers. Your containerized application must have at least one container.
- spec.endpoints (optional): A list of endpoints that the service exposes. You might choose to make an endpoint public, allowing network ingress access to the service.
- spec.volumes (optional): A list of storage volumes for the containers to use.
- spec.logExporters (optional): This field manages the level of container logs exported to the event table in your account.
- spec.resourceManagement (optional): Defines autoscaling policies that control how Snowflake scales service instances up, down, or suspends them based on metrics.
-
serviceRoles: Use this field to define one or more service roles. The service role is the mechanism you use to manage privileges to endpoints the service exposes.
一般准则
-
The following format guidelines apply for the
namefields (container, endpoint, and volume names):- 最多可包含 63 个字符。
- Can contain a sequence of lowercase alphanumeric or
-characters. - 必须以字母字符开头。
- 必须以字母数字字符结尾。
-
Customers should ensure that no personal data, sensitive data, export-controlled data, or other regulated data is entered as metadata in the specification file. For more information, see Metadata Fields in Snowflake.
The following sections explain each of the top-level spec fields.
spec.containers field (required)¶
Use the spec.containers field to describe each of the OCI (https://opencontainers.org/) containers in your application.
请注意以下事项:
- 当您创建服务时,Snowflake 会在指定计算池中的单个节点上运行这些容器,共享相同的网络接口。
- 您可以选择运行多个服务实例,实现传入请求的负载均衡。Snowflake 可能选择在指定计算池中的同一节点或不同节点上运行这些服务实例。给定实例的所有容器始终在一个节点上运行。
- 目前,Snowpark Container Services 需要 linux/amd64 平台镜像。
以下各节介绍了容器字段的类型。
containers.name and containers.image fields¶
对于每个容器,仅名称和镜像是必填字段。
nameis the image name. This name can be used to identify a specific container for the purposes of observability (for example, logs, metrics).imageis the name of the image you uploaded to a Snowflake image repository in your Snowflake account.
例如:
containers.command and containers.args fields¶
Use these optional fields to control what executable is started in your container and the arguments that are passed to that executable. You can configure defaults for these at the time of creating the image, typically in a Dockerfile. By using these service specification fields, you can change these defaults (and thus change the container behavior) without having to rebuild your container image:
containers.commandoverrides theDockerfileENTRYPOINT. This allows you to run a different executable in the container.containers.argsoverrides theDockerfileCMD. This allows you to provide different arguments to the command (the executable).
示例
Your Dockerfile includes the following code:
These Dockerfile entries execute the python3 command
and pass two arguments: main.py and Bob. You can override
these values in the specification file as follows:
-
To override the ENTRYPOINT, add the
containers.commandfield in the specification file: -
To override the argument “Bob”, add the
containers.argsfield in the specification file:
containers.env field¶
Use the containers.env field to define container environment variables. All processes in the container have access to these
environment variables:
示例
In Tutorial 1, the application code
(echo_service.py) reads the environment variables as shown:
Note that the example passes default values for the variables to the getenv function. If the environment variables are not defined, these defaults are used.
CHARACTER_NAME: When the Echo service receives an HTTP POST request with a string (for example, “Hello”), the service returns “I said Hello”. You can overwrite this default value in the specification file. For example, set the value to “Bob”; the Echo service returns a “Bob said Hello” response.SERVER_PORT: In this default configuration, the Echo service listens on port 8080. You can override the default value and specify another port.
以下服务规范将替换这两个环境变量值:
Note that, because you changed the port number your service listens on,
the specification must also update the endpoint (endpoints.port field value) as shown.
containers.readinessProbe field¶
Use the containers.readinessProbe field to identify a readiness probe in your application. Snowflake
continuously calls this probe to determine when your application is ready to serve requests, and will
stop routing traffic if the probe starts failing.
Snowflake 在指定的端口和路径上向指定的就绪情况探测发出 HTTP GET 请求,并查找您的服务以返回 HTTP 200 OK 状态,以确保只有正常运行的容器才能提供流量。
使用以下字段提供所需信息:
port: The network port on which the service is listening for the readiness probe requests. You need not declare this port as an endpoint.path: Snowflake makes HTTP GET requests to the service with this path.
示例
In Tutorial 1, the application code (echo_python.py) implements
the following readiness probe:
Accordingly, the specification file includes the containers.readinessProbe
field:
就绪情况探测指定的端口可以不是已配置的端点。服务可以仅出于就绪情况探测的目的来监听不同的端口。
containers.volumeMounts field¶
Because the spec.volumes and spec.containers.volumeMounts fields work together,
they are explained together in one section. For more information,
see field (optional).
containers.resources field¶
A compute pool defines a set of available resources (CPU, memory, and storage) and Snowflake determines where in the compute pool to run your services.
It is recommended that you explicitly indicate resource requirements for the specific container and set appropriate limits in the specification. Note that the resources you specify are constrained by the instance family of the nodes in your compute pool. For more information, see CREATE COMPUTE POOL.
Use containers.resources field to specify explicit resource requirements for the specific application container:
containers.resources.requests: The requests you specify should be the average resource usage you anticipate by your service. Snowflake uses this information to determine placement of the service instance in the compute pool. Snowflake ensures that the sum of the resource requests placed on a given node fits within the available resources on the node.containers.resources.limits: The limits you specify direct Snowflake to not allocate resources more than the specified limits. Thus, you can prevent cost overruns.
您可以为以下资源指定请求和限制:
-
memory: This is the memory required for your application container. You can use either decimal or binary units to express the values. For example, 2G represents a request for 2,000,000,000 bytes and 2Gi represents a request for 2 x 1024 x 1024 x 1024 bytes.When specifying memory, a unit is required. For example,
100Mor5Gi. The supported units are: M, Mi, G, Gi. -
cpu: This refers to virtual core (vCPU) units. For example, 1 CPU unit is equivalent to 1 vCPU. Fractional requests are allowed, such as 0.5, which can also be expressed as 500m. -
nvidia.com/gpu: If GPUs are required, they must be requested, and there must also be alimitspecified for the same quantity. If your container does not specify requests and limits for GPU capacity, it cannot access any GPUs. The number of GPUs you can request is limited by the maximum GPUs supported by theINSTANCE_TYPEyou choose when creating a compute pool.
resource.requests and resource.limits are relative to the node capacity (vCPU and memory) of the instance family of the associated compute pool.
-
如果没有提供资源需求(CPU、内存或两者),Snowflake 会为您派生一个:
- For
cpu, the derived value is either 0.5 or thecpulimit you provided, whichever is greater. - For
memory, the derived value is either 0.5 GiB or thememorylimit you provided, whichever is greater.
- For
-
If a resource limit (cpu, memory, or both) is not provided, Snowflake defaults the limits to the node capacity for the instance family of the associated compute pool.
-
If you do provide
resource.limitsand they exceed the node capacity, Snowflake will cap the limit to the node capacity. -
Snowflake evaluates these resource requirements independently for
cpuandmemory.
请注意,如果 Snowflake 理论上无法在给定的计算池上安排服务,CREATE SERVICE 将会失败。理论上不可能假设计算池具有允许的最大节点数,并且计算池上没有运行其他服务。也就是说,Snowflake 无法在计算池限制内分配请求的资源。如果理论上可行,但所需资源正在使用中,那么 CREATE SERVICE 将会成功。一些服务实例会报告状态,表明由于资源不足,无法安排服务,直到资源可用为止。
示例 1
In the following specification, the containers.resources field
describes the resource requirements for the container:
In this example, Snowflake is asked to allocate at least 2 GB of memory, one GPU, and a half CPU core for the container. At the same time, the container is not allowed to use more than 4 GB of memory and one GPU.
示例 2
假设:
-
You create a compute pool of two nodes; each node has 27 GB of memory and one GPU:
-
您创建一个服务,该服务要求 Snowflake 运行该服务的两个实例:
Both
MIN_INSTANCESandMAX_INSTANCESare set to 2. Therefore, Snowflake will run two instances of the service.
现在,考虑以下场景:
-
If your service does not explicitly include resource requirements in your application specification, Snowflake decides whether to run these instances on the same node or different nodes in the compute pool.
-
You do include resource requirements in the service specification and request 15 GB of memory for the container:
Your compute pool node has 27 GB of memory, and Snowflake cannot run two containers on the same node. Snowflake will run the two service instances on separate nodes in the compute pool.
-
You include resource requirements in the service specification and request 2 GB of memory and one GPU for the container:
您正在为每个容器请求 1 个 GPU,并且每个节点只有 1 个 GPU。在这种情况下,尽管内存不是问题,但 Snowflake 无法在一个节点上安排两个服务实例。此需求迫使 Snowflake 在两个单独的计算池节点上运行两个服务实例。
containers.secrets field¶
Use the containers.secrets field in your service specification to provide Snowflake-managed credentials to your application containers. Start by storing the credentials in Snowflake secret objects. Then, in the service specification, reference the secret object and specify where to place the credentials inside the container.
The following is a summary of how to use the containers.secrets fields:
-
Specify Snowflake secret: Use the
snowflakeSecretfield to specify either a Snowflake secret object name or object reference. Object references are applicable when using Snowpark Container Services to create a Native App (an app with containers).- Use
secretKeyRefto provide the name of the key in the Snowflake secret.
- Use
-
Specify the secret placement in the application container: Use the
envVarNamefield to pass the secret as environment variables ordirectoryPathto write the secrets to local container files.
For more information, see Passing credentials to a container using Snowflake secrets.
请注意,创建服务的角色(所有者角色)需要对引用的密钥具有 READ 权限。
spec.endpoints field (optional)¶
Use the spec.endpoints field to specify a list of TCP network ports that your application exposes.
A service might expose zero to many endpoints. Use the following fields to describe an endpoint:
-
name: Unique name of the endpoint. The name is used to identify the endpoint in service function and service role specification. -
port: The network port on which your service is listening. You must specify this field or theportRangefield. -
portRange: The network port range on which your application is listening. You must specify this field or theportfield.Ports defined in
portRangecan only be accessed by directly calling service instance IP addresses. To get service instance IP addresses, use theinstances.prefixed DNS name.For more information, see Service-to-service communications.
Note that you can only specify the
portRangefield if theprotocolfield is set to TCP and thepublicfield is false. -
public: If you want this endpoint to be accessible from outside the Snowpark Container Services network, set this field totrue. Public endpoints only support the “HTTP” value for theprotocolfield. -
protocol: The protocol that the endpoint supports. The supported values are TCP and HTTP. By default, the protocol is HTTP. When specifying theprotocol, the following apply:- When this endpoint is public or the target of a service function (see Using a service), the protocol must be HTTP or HTTPS.
-
corsSettings: The fields underendpointsallow you to configure Snowflake support for CORS on HTTP requests to public endpoints.corsSettings.Access-Control-Allow-Origin: Specifies the origins for which Snowflake responds with the provided CORS allow and expose response headers. The value must be a valid URL with no path specified, for example,https://example.com/, https://example.com:12345, for security reasons the “*” wildcard is not allowed forAccess-Control-Allow-Origin.- Snowflake 支持以下 CORS 响应标头:
corsSettings.Access-Control-Allow-Methods: Specifies the value of the HTTPAccess-Control-Allow-MethodsCORS response header. This tells the browsers what HTTP methods (GET, POST, etc.) they should allow when sending requests to this endpoint.corsSettings.Access-Control-Allow-Headers: Specifies the value of the HTTPAccess-Control-Allow-HeadersCORS response header. This tells the browsers what HTTP headers they should allow when sending requests to this endpoint.corsSettings.Access-Control-Expose-Headers: Specifies the value of the HTTPAccess-Control-Expose-HeadersCORS response header. This tells the browsers what HTTP headers they should allow when exposing responses from this endpoint.
Note
Snowflake 对公共访问执行身份验证和授权检查,只允许有权限的 Snowflake 用户使用该服务。对端点的公共访问需要 Snowflake 身份验证。经过身份验证的用户还必须对该服务端点具有授权(用户对访问该端点的角色拥有使用权限)。
示例
The following is the application specification used in Tutorial 1:
This application container exposes one endpoint.
It also includes the optional public field to enable access
to the endpoint from outside of Snowflake (internet access). By default, public is false.
spec.volumes field (optional)¶
This section explains both spec.volumes and spec.containers.volumeMounts specification fields because they’re closely related.
spec.volumesdefines a shared file system. These volumes can be made available in your containers.spec.containers.volumeMountdefines where a volume appears in specific containers.
Note that, the volumes field is specified at the spec level, but since multiple containers can share the same volume, volumeMounts becomes a spec.containers-level field.
使用这些字段来描述卷和卷挂载。
-
spec.volumes: Use the following fields to describe a volume:-
所有卷类型的必填字段:
name: Unique name of the volume. It is referred to byspec.containers.volumeMounts.name.source: This can belocal,memory,block,stage, or"@<stagename>"(which is deprecated). The next section explains these volume types.size(required only for thememoryandblockvolume types): For memory and block volumes, this is the size of the volume in bytes. For block storage, the value must always be an integer, specified using the Gi unit suffix. For example,5Gimeans5*1024*1024*1024bytes.
-
For the
blocktype volume, you can specify these optional fields:blockConfig.initialContents.fromSnapshot,blockConfig.iops,blockConfig.throughput,blockConfig.encryption,snapshotOnDelete, andsnapshotDeleteAfter. For more information, see Specifying block storage in service specification. -
For the
stagetype volume,nameis a required field. It identifies the stage. You can also specify the optional fieldsstageConfig.resourcesandstageConfig.metadataCache. For more information, see Using Snowflake stage volumes with services.
-
-
spec.containers.volumeMounts: Each container can have zero or more volume mounts.containers.volumeMountsis also a list. That is, each container can have multiple volume mounts. Use the following fields to describe a volume mount:name: The name of the volume to mount. A single container can reference the same volume multiple times.mountPath: The file path to where the volume for the container should be mounted.
关于支持的卷类型
Snowflake 支持应用程序容器使用以下这些卷类型:本地、内存、块和 Snowflake 暂存区。
- Local volume: Containers in a service instance can use a local disk to share files. For example, if your application has two containers—an application container and a log analyzer— the application can write logs to the local volume, and the log analyzer can read the logs.
请注意,如果您正在运行一个服务的多个实例,则只有属于一个服务实例的容器才能共享卷。属于不同服务实例的容器不共享卷。
- 内存: 您可以使用 RAM 支持的文件系统供容器使用。
- Block: Containers can also use block storage volumes. For more information, see Using block storage volumes with services.
- Snowflake stage: You can also give containers convenient access to files on a Snowflake stage in your account. For more information, see Using Snowflake stage volumes with services.
示例
机器学习应用程序包括以下两个容器:
- An
appcontainer for the main application - A
logger-agentcontainer that collects logs and uploads them to Amazon S3
这些容器使用以下两个卷:
localvolume: This application writes logs that the log agent reads.- Snowflake stage,
@model_stage: The main application reads files from this stage.
In the following example specification, the app container mounts both the
logs and models volumes, and the logging-agent container
mounts only the logs volume:
If multiple instances of the service are running, the
logging-agent and the app containers within a service instance
share the logs volume. The logs volume is not shared across
service instances.
If, in addition to these volumes, your app container also uses a 2-GB memory volume, revise the specification
to include the volume in the volumes list and also add another volume mount in the app containers volumeMounts list:
Note that when you specify memory as the volume source, you must also specify the volumes.size field to indicate the
memory size. For information about the memory size units you can specify, see 关于单位.
关于挂载卷上的文件权限
挂载 Snowflake 暂存区或块存储卷的容器通常以根用户身份运行。但是,有时您的容器可能会以非根用户身份运行。例如:
- 如果应用程序使用第三方库,则该库将使用非根用户在容器内运行应用程序代码。
- 出于安全性等其他原因,您可以在容器内以非根用户身份运行应用程序。
为避免与文件用户权限相关的潜在错误,将容器的 UID(用户 ID)和 GID(组 ID)设置为规范的一部分非常重要。这对于使用特定用户和组在容器内启动或运行应用程序的容器来说,这一点尤为重要。通过设置适当的 UID 和 GID,您可以使用以非根用户身份运行的容器。例如:
Snowflake 使用此信息以适当的权限挂载暂存区。
要获取容器的 UID 和 GID,请执行以下步骤:
-
Run the container locally using
docker run. -
Look up the container ID using the
docker container listcommand. Partial sample output: -
Run the
docker idcommand inside the container to get the UID and GID:
示例输出:
spec.logExporters field (optional)¶
Snowflake collects your applications output to standard output or standard error. For more information, see
Accessing local container logs. Use spec.logExporters to configure which of these outputs Snowflake exports to your event table.
The supported logLevel values are:
INFO(default): Export all the user logs.ERROR: Export only the error logs. Snowflake exports only the logs from stderr stream.NONE: Do not export logs to the event table.
spec.platformMonitor field (optional)¶
Individual services publish metrics. These Snowflake-provided metrics are also referred to as the platform metrics. You add the spec.platformMonitor field in the specification to direct Snowflake to send metrics from the service to the event table configured for your account. The target use case for this is to observe resource utilization of a specific service.
group_N refers to a predefined metrics groups that you are interested in. While the service is running, Snowflake logs metrics from specified groups to the event table. You can then query the metrics from the event table. For more information, see Monitoring Services.
spec.resourceManagement field (optional)¶
Use the spec.resourceManagement field to define autoscaling policies that
control how Snowflake scales service instances up, scales them down, or suspends
them based on metrics. Policies can use Snowflake-provided platform metrics or
custom metrics your service emits.
For the full field reference, syntax details, and examples, see Scaling services.
关于单位
服务规范在多个地方采用数值。支持使用多种单位来表示这些值。对于大值和小值,您可使用二进制和十进制单位,如下所示。在以下列表中,“#”表示整数值。
-
二进制单位:
numberKimeansnumber*1024. For example, 4Ki is equivalent to 4096.numberMimeansnumber10241024.numberGimeansnumber10241024*1024.
-
十进制单位:
numberkmeansnumber*1000. For example, 4k is equivalent to 4000.numberMmeansnumber10001000.numberGmeannumber10001000*1000.
-
小数单位:
numbermmeansnumber*0.001. For example,cpu: 500mis equivalent tocpu: 0.5.
capabilities field (optional)¶
In the capabilities top-level field in the specification, use the securityContext.executeAsCaller field to indicate the application intends to use caller’s rights.
By default, executeAsCaller is false.
serviceRoles field (optional)¶
Use the serviceRoles top-level field in the specification to define one or more service roles. For each service role, provide a name and a list of one or more endpoints (defined in the spec.endpoints) you want the service role to grant USAGE privilege on.
请注意以下事项:
- Both the
nameandendpointsare required. - 服务角色名称须遵循以下格式:
- Must contain alphanumeric or
_characters. - 必须以字母字符开头。
- 必须以字母数字字符结尾。
- Must contain alphanumeric or
For more information, see Managing service-related privileges.