×

使用 Knative CLI 创建无服务器应用程序

使用 Knative (kn) CLI 创建无服务器应用程序比直接修改 YAML 文件提供更简化和直观的用户界面。您可以使用kn service create 命令创建一个基本的无服务器应用程序。

先决条件
  • OpenShift Serverless Operator 和 Knative Serving 已安装在您的集群上。

  • 您已安装 Knative (kn) CLI。

  • 您已创建了一个项目,或有权访问具有适当角色和权限以在 OpenShift Container Platform 中创建应用程序和其他工作负载的项目。

步骤
  • 创建一个 Knative 服务

    $ kn service create <service-name> --image <image> --tag <tag-value>

    其中

    • --image 是应用程序的镜像 URI。

    • --tag 是一个可选标志,可用于向使用该服务创建的初始修订版添加标签。

      示例命令
      $ kn service create showcase \
          --image quay.io/openshift-knative/showcase
      示例输出
      Creating service 'showcase' in namespace 'default':
      
        0.271s The Route is still working to reflect the latest desired specification.
        0.580s Configuration "showcase" is waiting for a Revision to become ready.
        3.857s ...
        3.861s Ingress has not yet been reconciled.
        4.270s Ready to serve.
      
      Service 'showcase' created with latest revision 'showcase-00001' and URL:
      http://showcase-default.apps-crc.testing

使用 Knative CLI 更新无服务器应用程序

在构建服务的过程中,您可以使用kn service update 命令进行命令行的交互式会话。与kn service apply 命令相比,使用kn service update 命令时,您只需要指定要更新的更改,而不需要指定 Knative 服务的完整配置。

示例命令
  • 通过添加新的环境变量来更新服务

    $ kn service update <service_name> --env <key>=<value>
  • 通过添加新的端口来更新服务

    $ kn service update <service_name> --port 80
  • 通过添加新的请求和限制参数来更新服务

    $ kn service update <service_name> --request cpu=500m --limit memory=1024Mi --limit cpu=1000m
  • latest 标签分配给修订版

    $ kn service update <service_name> --tag <revision_name>=latest
  • 将服务的最新READY 修订版的标签从testing 更新为staging

    $ kn service update <service_name> --untag testing --tag @latest=staging
  • 向接收 10% 流量的修订版添加test 标签,并将其余流量发送到服务的最新READY 修订版

    $ kn service update <service_name> --tag <revision_name>=test --traffic test=10,@latest=90

应用服务声明

您可以使用kn service apply 命令声明性地配置 Knative 服务。如果服务不存在,则会创建它;否则,将使用已更改的选项更新现有服务。

kn service apply 命令对于 shell 脚本或持续集成管道特别有用,因为用户通常希望在一个命令中完全指定服务的狀態以声明目标狀態。

使用kn service apply 时,必须提供 Knative 服务的完整配置。这与kn service update 命令不同,后者只需要您在命令中指定要更新的选项。

示例命令
  • 创建服务

    $ kn service apply <service_name> --image <image>
  • 向服务添加环境变量

    $ kn service apply <service_name> --image <image> --env <key>=<value>
  • 从 JSON 或 YAML 文件读取服务声明

    $ kn service apply <service_name> -f <filename>

使用 Knative CLI 描述无服务器应用程序

您可以使用kn service describe命令来描述 Knative 服务。

示例命令
  • 描述服务

    $ kn service describe --verbose <service_name>

    --verbose标志是可选的,但可以包含在内以提供更详细的描述。常规输出和详细输出之间的区别如下例所示。

    --verbose标志的示例输出
    Name:       showcase
    Namespace:  default
    Age:        2m
    URL:        http://showcase-default.apps.ocp.example.com
    
    Revisions:
      100%  @latest (showcase-00001) [1] (2m)
            Image:  quay.io/openshift-knative/showcase (pinned to aaea76)
    
    Conditions:
      OK TYPE                   AGE REASON
      ++ Ready                   1m
      ++ ConfigurationsReady     1m
      ++ RoutesReady             1m
    包含--verbose标志的示例输出
    Name:         showcase
    Namespace:    default
    Annotations:  serving.knative.dev/creator=system:admin
                  serving.knative.dev/lastModifier=system:admin
    Age:          3m
    URL:          http://showcase-default.apps.ocp.example.com
    Cluster:      http://showcase.default.svc.cluster.local
    
    Revisions:
      100%  @latest (showcase-00001) [1] (3m)
            Image:  quay.io/openshift-knative/showcase (pinned to aaea76)
            Env:    GREET=Bonjour
    
    Conditions:
      OK TYPE                   AGE REASON
      ++ Ready                   3m
      ++ ConfigurationsReady     3m
      ++ RoutesReady             3m
  • 以 YAML 格式描述服务

    $ kn service describe <service_name> -o yaml
  • 以 JSON 格式描述服务

    $ kn service describe <service_name> -o json
  • 仅打印服务 URL

    $ kn service describe <service_name> -o url