×

您可以使用单个 Knative 服务部署多容器 Pod。此方法对于将应用程序职责分离成更小、更专业的部件非常有用。

配置多容器服务

默认情况下启用多容器支持。您可以通过在服务中指定多个容器来创建多容器 Pod。

步骤
  1. 修改您的服务以包含其他容器。只有一个容器可以处理请求,因此请为恰好一个容器指定 `ports`。这是一个包含两个容器的示例配置

    多容器配置
    apiVersion: serving.knative.dev/v1
    kind: Service
    ...
    spec:
      template:
        spec:
          containers:
            - name: first-container (1)
              image: gcr.io/knative-samples/helloworld-go
              ports:
                - containerPort: 8080 (2)
            - name: second-container (3)
              image: gcr.io/knative-samples/helloworld-java
    1 第一个容器配置。
    2 第一个容器的端口规范。
    3 第二个容器配置。

探测多容器服务

您可以为多个容器指定就绪探针和存活性探针。此功能默认情况下未启用,您必须使用 `KnativeServing` 自定义资源 (CR) 来配置它。

步骤
  1. 通过在 `KnativeServing` CR 中启用 `multi-container-probing` 功能来为您的服务配置多容器探测。

    多容器探测配置
    ...
    spec:
      config:
        features:
          "multi-container-probing": enabled (1)
    ...
    1 已启用 multi-container-probing 功能
  2. 应用更新的 `KnativeServing` CR。

    $ oc apply -f <filename>
  3. 修改您的多容器服务以包含指定的探针。

    多容器探测
    apiVersion: serving.knative.dev/v1
    kind: Service
    ...
    spec:
     template:
       spec:
         containers:
           - name: first-container
             image: ghcr.io/knative/helloworld-go:latest
             ports:
               - containerPort: 8080
             readinessProbe: (1)
               httpGet:
                 port: 8080
           - name: second-container
             image: gcr.io/knative-samples/helloworld-java
             readinessProbe: (2)
               httpGet:
                 port: 8090
    1 第一个容器的就绪探针
    2 第二个容器的就绪探针