管理服务

Snowpark Container Services 使您能够轻松部署、管理和扩展容器化应用程序。将应用程序镜像上传到账户中的存储库后,将应用程序容器作为服务或作业运行。本主题介绍如何使用服务。

服务是长期运行的,就像 Web 服务一样,不会自己结束。Snowflake 管理运行中的服务。例如,如果某个服务容器出于某种原因退出,Snowflake 会重新启动该容器,以便服务不间断地运行。如果您的服务需要更多的资源,例如更多的计算能力,Snowflake 会在计算池中预置额外的节点。

For more information about working with container services, see Snowpark Container Services: Working with services.

本主题介绍如何使用服务执行以下任务:

For common operations, such as listing or dropping, Snowflake CLI uses snow object commands as described in Managing Snowflake objects.

创建 Snowpark Container Services 服务

Snowpark Container Services 需要以下条件:

  • 计算池:Snowflake 在指定的计算池中运行您的服务。
  • A service specification file: This specification gives Snowflake the information needed to configure and run your service.

To create a service, enter a snow spcs service create command similar to the following:

snow spcs service create "job_1" --compute-pool "pool_1" --spec-path "/some-dir/spec_file.yaml"

For more information, see Managing Snowflake objects.

根据项目定义创建和部署服务

You can create a service from a snowflake.yml project definition file and then executing the snow spcs service deploy command.

The following shows a sample snowflake.yml project definition file:

definition_version: 2
entities:
  my_service:
    type: service
    identifier: my_service
    stage: my_stage
    compute_pool: my_compute_pool
    spec_file: spec.yml
    min_instances: 1
    max_instances: 2
    query_warehouse: my_warehouse
    auto_resume: true
    external_access_integrations:
      - my_external_access
    secrets:
        cred: my_cred_name
    artifacts:
      - spec.yml
    comment: "My service"
    tags:
      - name: test_tag
        value: test_value

下表描述了计算池项目定义的属性。

Compute pool project definition properties

PropertyDefinition

type

requiredstring

Must be service.

stage

requiredstring

Stage where the service specification file is located.

compute_pool

requiredstring

Compute pool where the service runs.

spec_file

requiredstring

Path to service specification file on the stage.

identifier

optionalstring

实体的 Snowflake 标识符。该值可以采用以下形式:

  • 字符串标识符文本
identifier: my-service

Both unquoted and quoted identifiers are supported. To use quoted identifiers, include the surrounding quotes in the YAML value (for example, ’”My Image Repository").

  • 对象
identifier:
  name: my-service
  schema: my-schema # optional
  database: my-db # optional

Note

An error occurs if you specify a schema or database and use a fully qualified name in the name property (such as mydb.schema1.my-app).

min_instances

optionalstring

要运行的最小服务实例数。

Default: 1

max_instances

optionalstring

Maximum number of service instances to run.

query_warehouse

optionalstring

Warehouse to use if a service container connects to Snowflake to execute a query without explicitly specifying a warehouse to use.

auto_resume

optionalstring

在调用服务函数或入口时是否自动恢复。

Default: True

external_access_integrations

optionalstring sequence

Names of external access integrations needed for this entity to access external networks.

secrets

optionaldictionary

Names and values of secrets variables so that you can use the variables to reference the secrets.

artifacts

optionalstring sequence

要添加到部署根的文件源和目标对的列表。您可以使用以下工件属性:

  • src: Path to the code source file or files
  • dest: Path to the directory to deploy the artifacts.

Destination paths that reference directories must end with a /. A glob pattern’s destination that does not end with a / results in an error. If omitted, dest defaults to the same string as src.

You can also pass in a string for each item instead of a dict, in which case the value is treated as both src and dest.

If src refers to just one file (not a glob), dest can refer to a target <path> or a <path/name>.

You can also pass in a string for each item instead of a dict, in which case the value is treated as both src and dest.

comment

optionalstring

Comments to associate with the compute pool.

tags

optionalTag sequence

Tag names and values for the compute pool. For more information, see Tag quotas

要创建和部署服务,请执行以下操作:

  1. 将当前目录更改为包含项目定义文件的目录。

  2. Run a snow spcs service deploy command similar to the following:

    snow spcs service deploy
    +---------------------------------------------------------------------+
    | key    | value                                                      |
    |--------+------------------------------------------------------------|
    | status | Service MY_SERVICE successfully created.                   |
    +---------------------------------------------------------------------+

暂停和恢复服务

To suspend a named service, enter a snow spcs service suspend command similar to the following:

snow spcs service suspend echo_service
+-------------------------------------------+
| key    | value                            |
|--------+----------------------------------|
| status | Statement executed successfully. |
+-------------------------------------------+

To resume a suspended service, enter a snow spcs service resume command similar to the following:

snow spcs service resume echo_service
+-------------------------------------------+
| key    | value                            |
|--------+----------------------------------|
| status | Statement executed successfully. |
+-------------------------------------------+

获取有关服务的状态信息

Note

当前角色必须对服务具有 MONITOR 权限才能获取服务状态。

列出所有服务

The snow spcs service list command returns an overview of all services, including the runtime state of the services, such as PENDING or RUNNING, and the upgrading status. To get the status of all services, enter a command similar to the following:

snow spcs service list
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|        |        |        |        |        |        |        |        |        |        |        |         | extern |         |        |         |        |         |        |        |         |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         | al_acc |         |        |         |        |         |        |        |         |        | managin | managi |
|        |        | databa |        |        |        |        | curren | target | min_in | max_in |         | ess_in |         |        |         |        | owner_r | query_ |        |         |        | g_objec | ng_obj |
|        |        | se_nam | schema |        | comput | dns_na | t_inst | _insta | stance | stance | auto_re | tegrat | created | update | resumed | commen | ole_typ | wareho |        | spec_di | is_upg | t_domai | ect_na |
| name   | status | e      | _name  | owner  | e_pool | me     | ances  | nces   | s      | s      | sume    | ions   | _on     | d_on   | _on     | t      | e       | use    | is_job | gest    | rading | n       | me     |
|--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+--------+---------+--------+---------+--------+---------+--------+--------+---------+--------+---------+--------|
| ECHO_S | RUNNIN | TEST00 | TEST_S | SYSADM | TUTORI | echo-s | 1      | 1      | 1      | 1      | true    | None   | 2024-10 | 2024-1 | None    | This   | ROLE    | COMPUT | false  | 52e62d1 | false  | None    | None   |
| ERVICE | G      | _DB    | CHEMA  | IN     | AL_COM | ervice |        |        |        |        |         |        | -16     | 0-16   |         | is a   |         | E_WH   |        | f19c720 |        |         |        |
|        |        |        |        |        | PUTE_P | .imhd. |        |        |        |        |         |        | 15:09:3 | 15:09: |         | test   |         |        |        | 6b5f4ef |        |         |        |
|        |        |        |        |        | OOL    | svc.sp |        |        |        |        |         |        | 0.49300 | 31.905 |         | servic |         |        |        | c069557 |        |         |        |
|        |        |        |        |        |        | cs.int |        |        |        |        |         |        | 0-07:00 | 000-07 |         | e      |         |        |        | 8b6c2b3 |        |         |        |
|        |        |        |        |        |        | ernal  |        |        |        |        |         |        |         | :00    |         |        |         |        |        | 806ad76 |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         |        |         |        |         |        |         |        |        | 67d78cc |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         |        |         |        |         |        |         |        |        | ce8b6ed |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         |        |         |        |         |        |         |        |        | 6501a8a |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         |        |         |        |         |        |         |        |        | 3       |        |         |        |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

获取命名服务的状态

To get the status of an individual service, enter a snow spcs service describe command similar to the following:

snow spcs service describe echo_service
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|        |        |        |        |        |        |        |        |        |        |        |         | extern |         |        |         |        |         |        |        |         |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         | al_acc |         |        |         |        |         |        |        |         |        | managin | managi |
|        |        | databa |        |        |        |        | curren | target | min_in | max_in |         | ess_in |         |        |         |        | owner_r | query_ |        |         |        | g_objec | ng_obj |
|        |        | se_nam | schema |        | comput | dns_na | t_inst | _insta | stance | stance | auto_re | tegrat | created | update | resumed | commen | ole_typ | wareho |        | spec_di | is_upg | t_domai | ect_na |
| name   | status | e      | _name  | owner  | e_pool | me     | ances  | nces   | s      | s      | sume    | ions   | _on     | d_on   | _on     | t      | e       | use    | is_job | gest    | rading | n       | me     |
|--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+--------+---------+--------+---------+--------+---------+--------+--------+---------+--------+---------+--------|
| ECHO_S | RUNNIN | TEST00 | TEST_S | SYSADM | TUTORI | echo-s | 1      | 1      | 1      | 1      | true    | None   | 2024-10 | 2024-1 | None    | This   | ROLE    | COMPUT | false  | 52e62d1 | false  | None    | None   |
| ERVICE | G      | _DB    | CHEMA  | IN     | AL_COM | ervice |        |        |        |        |         |        | -16     | 0-16   |         | is a   |         | E_WH   |        | f19c720 |        |         |        |
|        |        |        |        |        | PUTE_P | .imhd. |        |        |        |        |         |        | 15:09:3 | 15:09: |         | test   |         |        |        | 6b5f4ef |        |         |        |
|        |        |        |        |        | OOL    | svc.sp |        |        |        |        |         |        | 0.49300 | 31.905 |         | servic |         |        |        | c069557 |        |         |        |
|        |        |        |        |        |        | cs.int |        |        |        |        |         |        | 0-07:00 | 000-07 |         | e      |         |        |        | 8b6c2b3 |        |         |        |
|        |        |        |        |        |        | ernal  |        |        |        |        |         |        |         | :00    |         |        |         |        |        | 806ad76 |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         |        |         |        |         |        |         |        |        | 67d78cc |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         |        |         |        |         |        |         |        |        | ce8b6ed |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         |        |         |        |         |        |         |        |        | 6501a8a |        |         |        |
|        |        |        |        |        |        |        |        |        |        |        |         |        |         |        |         |        |         |        |        | 3       |        |         |        |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

列出实例和容器

You can list service’s instances and containers with the snow spcs service list-instances and snow spcs service list-containers commands, respectively.

To get the list of instances in the echo_service service, enter the following snow spcs service list-instances command:

snow spcs service list-instances echo_service
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| database_name | schema_name | service_name | instance_id | status | spec_digest                                                      | creation_time        | start_time           |
|---------------+-------------+--------------+-------------+--------+------------------------------------------------------------------+----------------------+----------------------|
| TEST00_DB     | TEST_SCHEMA | ECHO_SERVICE | 0           | READY  | 336c065739dd2b96e770f01804affdc7810e6df68a23b23052d851627abfbdf9 | 2024-10-10T06:06:30Z | 2024-10-10T06:06:30Z |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

To get the list of containers in the echo_service service, enter the following snow spcs service list-containers command:

snow spcs service list-containers echo_service
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| database_name | schema_name | service_name | instance_id | container_name | status | message | image_name                                | image_digest                              | restart_count | start_time           |
|---------------+-------------+--------------+-------------+----------------+--------+---------+-------------------------------------------+-------------------------------------------+---------------+----------------------|
| TEST00_DB     | TEST_SCHEMA | ECHO_SERVICE | 0           | main           | READY  | Running | org-test-account-00.registry.registry.sno | sha256:06c3d54edc24925abe398eda70d37eb6b8 | 0             | 2024-10-16T22:09:35Z |
|               |             |              |             |                |        |         | wflakecomputing.com/test00_db/test_schema | 7b1c4dd6211317592764e1e7d94498            |               |                      |
|               |             |              |             |                |        |         | /test00_repo/echo_service:latest          |                                           |               |                      |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

列出服务中的端点

To list the endpoints a named service, enter a snow spcs service list-endpoints command similar to the following:

snow spcs service list-endpoints echo_service
+--------------+------+----------+-----------------+-----------------------------------------+
| name         | port | protocol | ingress_enabled | ingress_url                             |
|--------------+------+----------+-----------------+-----------------------------------------|
| echoendpoint | 8000 | TCP      | true            | org-id-acct-id.snowflakecomputing.app   |
+--------------+------+----------+-----------------+-----------------------------------------+

列出与服务关联的服务角色

You can manage access to individual endpoints exposed by a service by defining service roles and permissions in the service specification. For more information about how to use service roles, see GRANT SERVICE ROLE.

To get a list of service roles created for a service, use the snow spcs service list-roles command, as shown:

snow spcs service list-roles my_service
+------------------------------------------------------------------+
| created_on                       | name                | comment |
|----------------------------------+---------------------+---------|
| 2024-10-09 16:48:52.980000-07:00 | ALL_ENDPOINTS_USAGE | None    |
+------------------------------------------------------------------+

设置和取消设置服务的属性或参数

Note

当前角色必须具有该服务的 OPERATE 权限才能设置属性。

To set a service’s property or parameter, enter a snow spcs service set command similar to the following:

snow spcs service set echo_service --min-instances 2 --max-instances 4
+-------------------------------------------+
| key    | value                            |
|--------+----------------------------------|
| status | Statement executed successfully. |
+-------------------------------------------+

要将服务的属性或参数重置为其默认值,请输入类似下面的命令:

snow spcs compute-pool unset tutorial_compute_pool --auto-resume
+-------------------------------------------+
| key    | value                            |
|--------+----------------------------------|
| status | Statement executed successfully. |
+-------------------------------------------+

显示命名服务的日志

Note

当前角色必须具有对服务的 MONITOR 权限才能显示日志。

To display local logs for a named service, enter a snow spcs service logs command similar to the following:

snow spcs service logs "service_1" --container-name "container_1" --instance-id "0"

升级命名服务

Note

当前角色必须具有对服务的 OPERATE 权限才能升级服务。

To upgrade a named service, enter a snow spcs service upgrade command similar to the following:

snow spcs service upgrade echo_service --spec-path spec.yml
+-------------------------------------------+
| key    | value                            |
|--------+----------------------------------|
| status | Statement executed successfully. |
+-------------------------------------------+