×

无服务器应用程序被创建和部署为 Kubernetes 服务,由路由和配置定义,并包含在 YAML 文件中。要使用 OpenShift Serverless 部署无服务器应用程序,必须创建一个 Knative Service 对象。

示例 Knative Service 对象 YAML 文件
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: showcase (1)
  namespace: default (2)
spec:
  template:
    spec:
      containers:
        - image: quay.io/openshift-knative/showcase (3)
          env:
            - name: GREET (4)
              value: Ciao
1 应用程序的名称。
2 应用程序使用的命名空间。
3 应用程序的镜像。
4 示例应用程序打印出的环境变量。

您可以使用以下方法之一创建无服务器应用程序

  • 从 OpenShift Container Platform Web 控制台创建 Knative 服务。

    对于 OpenShift Container Platform,请参阅 使用开发者视角创建应用程序 以了解更多信息。

  • 使用 Knative (kn) CLI 创建 Knative 服务。

  • 使用 oc CLI 创建并应用 Knative Service 对象作为 YAML 文件。

使用 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

使用YAML创建无服务器应用程序

使用YAML文件创建Knative资源使用声明式API,使您可以以声明性和可重复的方式描述应用程序。要使用YAML创建无服务器应用程序,您必须创建一个定义Knative Service对象的YAML文件,然后使用oc apply应用它。

服务创建和应用程序部署后,Knative会为此版本的应用程序创建一个不可变的修订版。Knative还会执行网络编程以创建应用程序的路由、入口、服务和负载均衡器,并根据流量自动扩展您的Pod。

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

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

  • 安装OpenShift CLI (oc)。

步骤
  1. 创建一个包含以下示例代码的YAML文件

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: showcase
      namespace: default
    spec:
      template:
        spec:
          containers:
            - image: quay.io/openshift-knative/showcase
              env:
                - name: GREET
                  value: Bonjour
  2. 导航到包含YAML文件的目录,并通过应用YAML文件来部署应用程序

    $ oc apply -f <filename>

如果您不想切换到OpenShift Container Platform Web控制台中的**开发人员**视角,或使用Knative (kn) CLI或YAML文件,则可以使用OpenShift Container Platform Web控制台的**管理员**视角创建Knative组件。

使用管理员视角创建无服务器应用程序

无服务器应用程序被创建和部署为 Kubernetes 服务,由路由和配置定义,并包含在 YAML 文件中。要使用 OpenShift Serverless 部署无服务器应用程序,必须创建一个 Knative Service 对象。

示例 Knative Service 对象 YAML 文件
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: showcase (1)
  namespace: default (2)
spec:
  template:
    spec:
      containers:
        - image: quay.io/openshift-knative/showcase (3)
          env:
            - name: GREET (4)
              value: Ciao
1 应用程序的名称。
2 应用程序使用的命名空间。
3 应用程序的镜像。
4 示例应用程序打印出的环境变量。

服务创建和应用程序部署后,Knative会为此版本的应用程序创建一个不可变的修订版。Knative还会执行网络编程以创建应用程序的路由、入口、服务和负载均衡器,并根据流量自动扩展您的Pod。

前提条件

要使用**管理员**视角创建无服务器应用程序,请确保已完成以下步骤。

  • 已安装OpenShift Serverless Operator和Knative Serving。

  • 您已登录到Web控制台,并且处于**管理员**视角。

步骤
  1. 导航到**无服务器**→**Serving**页面。

  2. 在**创建**列表中,选择**服务**。

  3. 手动输入YAML或JSON定义,或通过将文件拖放到编辑器中。

  4. 单击**创建**。

使用离线模式创建服务

您可以以离线模式执行kn service命令,这样集群上就不会发生任何更改,而是服务描述符文件会在您的本地机器上创建。创建描述符文件后,您可以在将更改传播到集群之前修改该文件。

Knative CLI的离线模式只是一个技术预览功能。技术预览功能不受Red Hat生产服务级别协议(SLA)的支持,并且可能功能不完整。Red Hat不建议在生产环境中使用它们。这些功能可让您提前访问即将推出的产品功能,使客户能够在开发过程中测试功能并提供反馈。

有关Red Hat技术预览功能的支持范围的更多信息,请参阅技术预览功能支持范围

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

  • 您已安装 Knative (kn) CLI。

步骤
  1. 在离线模式下,创建一个本地Knative服务描述符文件

    $ kn service create showcase \
        --image quay.io/openshift-knative/showcase \
        --target ./ \
        --namespace test
    示例输出
    Service 'showcase' created in namespace 'test'.
    • --target ./标志启用离线模式,并将./指定为存储新目录树的目录。

      如果您没有指定现有目录,而是使用文件名,例如--target my-service.yaml,则不会创建目录树。只会创建当前目录中的服务描述符文件my-service.yaml

      文件名可以具有.yaml.yml.json扩展名。选择.json将以JSON格式创建服务描述符文件。

    • --namespace test选项将新服务置于test命名空间中。

      如果您不使用--namespace,并且已登录到OpenShift Container Platform集群,则会在当前命名空间中创建描述符文件。否则,将在default命名空间中创建描述符文件。

  2. 检查创建的目录结构

    $ tree ./
    示例输出
    ./
    └── test
        └── ksvc
            └── showcase.yaml
    
    2 directories, 1 file
    • 使用--target指定的当前./目录包含以指定的命名空间命名的新的test/目录。

    • test/目录包含以资源类型命名的ksvc目录。

    • ksvc目录包含根据指定的服务名称命名的描述符文件showcase.yaml

  3. 检查生成的服务器描述符文件

    $ cat test/ksvc/showcase.yaml
    示例输出
    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      creationTimestamp: null
      name: showcase
      namespace: test
    spec:
      template:
        metadata:
          annotations:
            client.knative.dev/user-image: quay.io/openshift-knative/showcase
          creationTimestamp: null
        spec:
          containers:
          - image: quay.io/openshift-knative/showcase
            name: ""
            resources: {}
    status: {}
  4. 列出有关新服务的信息

    $ kn service describe showcase --target ./ --namespace test
    示例输出
    Name:       showcase
    Namespace:  test
    Age:
    URL:
    
    Revisions:
    
    Conditions:
      OK TYPE    AGE REASON
    • --target ./选项指定包含命名空间子目录的目录结构的根目录。

      或者,您可以使用--target选项直接指定YAML或JSON文件名。可接受的文件扩展名为.yaml.yml.json

    • --namespace选项指定命名空间,它向kn传达包含必要服务描述符文件的子目录。

      如果您不使用--namespace,并且已登录到OpenShift Container Platform集群,kn将在以当前命名空间命名的子目录中搜索服务。否则,kn将在default/子目录中搜索。

  5. 使用服务描述符文件在集群上创建服务

    $ kn service create -f test/ksvc/showcase.yaml
    示例输出
    Creating service 'showcase' in namespace 'test':
    
      0.058s The Route is still working to reflect the latest desired specification.
      0.098s ...
      0.168s Configuration "showcase" is waiting for a Revision to become ready.
     23.377s ...
     23.419s Ingress has not yet been reconciled.
     23.534s Waiting for load balancer to be ready
     23.723s Ready to serve.
    
    Service 'showcase' created to latest revision 'showcase-00001' is available at URL:
    http://showcase-test.apps.example.com

验证您的无服务器应用程序部署

要验证您的无服务器应用程序是否已成功部署,您必须获取Knative创建的应用程序URL,然后向该URL发送请求并观察输出。OpenShift Serverless支持使用HTTP和HTTPS URL,但是oc get ksvc的输出始终使用http://格式打印URL。

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

  • 您已安装oc CLI。

  • 您已创建Knative服务。

前提条件
  • 安装OpenShift CLI (oc)。

步骤
  1. 查找应用程序URL

    $ oc get ksvc <service_name>
    示例输出
    NAME       URL                                   LATESTCREATED    LATESTREADY      READY   REASON
    showcase   http://showcase-default.example.com   showcase-00001   showcase-00001   True
  2. 向您的集群发出请求并观察输出。

    示例HTTP请求(使用HTTPie工具)
    $ http showcase-default.example.com
    示例HTTPS请求
    $ https showcase-default.example.com
    示例输出
    HTTP/1.1 200 OK
    Content-Type: application/json
    Server: Quarkus/2.13.7.Final-redhat-00003 Java/17.0.7
    X-Config: {"sink":"https://127.0.0.1:31111","greet":"Ciao","delay":0}
    X-Version: v0.7.0-4-g23d460f
    content-length: 49
    
    {
        "artifact": "knative-showcase",
        "greeting": "Ciao"
    }
  3. 可选。如果您的系统上没有安装HTTPie工具,则可以使用curl工具。

    示例HTTPS请求
    $ curl http://showcase-default.example.com
    示例输出
    {"artifact":"knative-showcase","greeting":"Ciao"}
  4. 可选。如果您收到有关证书链中自签名证书的错误,您可以向HTTPie命令添加--verify=no标志以忽略该错误

    $ https --verify=no showcase-default.example.com
    示例输出
    HTTP/1.1 200 OK
    Content-Type: application/json
    Server: Quarkus/2.13.7.Final-redhat-00003 Java/17.0.7
    X-Config: {"sink":"https://127.0.0.1:31111","greet":"Ciao","delay":0}
    X-Version: v0.7.0-4-g23d460f
    content-length: 49
    
    {
        "artifact": "knative-showcase",
        "greeting": "Ciao"
    }

    自签名证书不得用于生产部署。此方法仅用于测试目的。

  5. 可选。如果您的OpenShift Container Platform集群配置了由证书颁发机构(CA)签名的证书,但尚未为您的系统全局配置,则可以使用curl命令指定此证书。可以使用--cacert标志将证书路径传递给curl命令

    $ curl https://showcase-default.example.com --cacert <file>
    示例输出
    {"artifact":"knative-showcase","greeting":"Ciao"}