1.4. 将 Service Mesh 与 OpenShift Serverless 集成


1.4.1. 验证安装先决条件

在安装和配置 Service Mesh 与 Serverless 集成前,请验证是否满足先决条件。

流程

  1. 检查冲突网关:

    示例命令

    $ oc get gateway -A -o jsonpath='{range .items[*]}{@.metadata.namespace}{"/"}{@.metadata.name}{" "}{@.spec.servers}{"\n"}{end}' | column -t
    Copy to Clipboard Toggle word wrap

    输出示例

    knative-serving/knative-ingress-gateway  [{"hosts":["*"],"port":{"name":"https","number":443,"protocol":"HTTPS"},"tls":{"credentialName":"wildcard-certs","mode":"SIMPLE"}}]
    knative-serving/knative-local-gateway    [{"hosts":["*"],"port":{"name":"http","number":8081,"protocol":"HTTP"}}]
    Copy to Clipboard Toggle word wrap

    这个命令不应该返回绑定端口 的网关 :443hosts: ["*"],除了作为另一个 Service Mesh 实例一部分的 knative-serving Gateways 的网关。

    注意

    Serverless 是 Serverless 一部分的网格必须不同,最好只为 Serverless 工作负载保留。这是因为额外的配置(如 Gateways )可能会影响 Serverless 网关 knative-local-gatewayknative-ingress-gateway。Red Hat OpenShift Service Mesh 只允许一个网关在同一端口(端口:443 )上声明通配符主机绑定(hosts:["*"])。如果另一个网关已绑定此配置,则必须为 Serverless 工作负载创建一个单独的网格。

  2. 检查 Red Hat OpenShift Service Mesh istio-ingressgateway 是否作为类型 NodePortLoadBalancer 公开:

    示例命令

    $ oc get svc -A | grep istio-ingressgateway
    Copy to Clipboard Toggle word wrap

    输出示例

    istio-system   istio-ingressgateway  ClusterIP  172.30.46.146 none>   15021/TCP,80/TCP,443/TCP     9m50s
    Copy to Clipboard Toggle word wrap

    此命令不应返回 NodePortLoadBalancer 类型的 Service 对象。

    注意

    应该使用 OpenShift 路由通过 OpenShift Ingress 调用集群外部 Knative 服务。不支持直接访问 Service Mesh,比如使用类型为 NodePortLoadBalancerService 对象公开 istio-ingressgateway

1.4.2. 安装和配置 Service Mesh

要将 Serverless 与 Service Mesh 集成,您需要使用特定的配置安装 Service Mesh。

流程

  1. 使用以下配置在 istio-system 命名空间中创建 ServiceMeshControlPlane 资源:

    重要

    如果您有一个现有的 ServiceMeshControlPlane 对象,请确保应用了相同的配置。

    apiVersion: maistra.io/v2
    kind: ServiceMeshControlPlane
    metadata:
      name: basic
      namespace: istio-system
    spec:
      profiles:
      - default
      security:
        dataPlane:
          mtls: true 
    1
    
      techPreview:
        meshConfig:
          defaultConfig:
            terminationDrainDuration: 35s 
    2
    
      gateways:
        ingress:
          service:
            metadata:
              labels:
                knative: ingressgateway 
    3
    
      proxy:
        networking:
          trafficControl:
            inbound:
              excludedPorts: 
    4
    
              - 8444 # metrics
              - 8022 # serving: wait-for-drain k8s pre-stop hook
    Copy to Clipboard Toggle word wrap
    1
    在网格中强制实施严格的 mTLS。只允许使用有效客户端证书调用。
    2
    Serverless 对于 Knative 服务有 30 秒安全终止。Istio-proxy 需要更长的终止持续时间,以确保不会丢弃请求。
    3
    仅为 ingress 网关定义特定选择器,以仅针对 Knative 网关。
    4
    这些端口由 Kubernetes 和集群监控调用,它们不是网格的一部分,无法使用 mTLS 调用。因此,这些端口不包括在网格中。
  2. 将您要与 Service Mesh 集成的命名空间作为成员添加到 ServiceMeshMemberRoll 对象中:

    servicemesh-member-roll.yaml 配置文件示例

    apiVersion: maistra.io/v1
    kind: ServiceMeshMemberRoll
    metadata:
      name: default
      namespace: istio-system
    spec:
      members: 
    1
    
        - knative-serving
        - knative-eventing
        - your-OpenShift-projects
    Copy to Clipboard Toggle word wrap

    1
    要与 Service Mesh 集成的命名空间列表。
    重要

    此命名空间列表必须包含 knative-servingknative-eventing 命名空间。

  3. 应用 ServiceMeshMemberRoll 资源:

    $ oc apply -f servicemesh-member-roll.yaml
    Copy to Clipboard Toggle word wrap
  4. 创建必要的网关,以便 Service Mesh 可以接受流量。以下示例使用带有 ISTIO_MUTUAL 模式(mTLS)的 knative-local-gateway 对象:

    istio-knative-gateways.yaml 配置文件示例

    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: knative-ingress-gateway
      namespace: knative-serving
    spec:
      selector:
        knative: ingressgateway
      servers:
        - port:
            number: 443
            name: https
            protocol: HTTPS
          hosts:
            - "*"
          tls:
            mode: SIMPLE
            credentialName: <wildcard_certs> 
    1
    
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
     name: knative-local-gateway
     namespace: knative-serving
    spec:
     selector:
       knative: ingressgateway
     servers:
       - port:
           number: 8081
           name: https
           protocol: HTTPS 
    2
    
         tls:
           mode: ISTIO_MUTUAL 
    3
    
         hosts:
           - "*"
    ---
    apiVersion: v1
    kind: Service
    metadata:
     name: knative-local-gateway
     namespace: istio-system
     labels:
       experimental.istio.io/disable-gateway-port-translation: "true"
    spec:
     type: ClusterIP
     selector:
       istio: ingressgateway
     ports:
       - name: http2
         port: 80
         targetPort: 8081
    Copy to Clipboard Toggle word wrap

    1
    包含通配符证书的 secret 名称。
    2 3
    knative-local-gateway 对象提供 HTTPS 流量,并期望所有客户端使用 mTLS 发送请求。这意味着,只有来自 Service Mesh 的流量才可以进行。Service Mesh 外部的工作负载必须通过 OpenShift 路由使用外部域。
  5. 应用 Gateway 资源:

    $ oc apply -f istio-knative-gateways.yaml
    Copy to Clipboard Toggle word wrap

1.4.3. 安装和配置 Serverless

安装 Service Mesh 后,您需要使用特定的配置安装 Serverless。

流程

  1. 使用以下 KnativeServing 自定义资源安装 Knative Serving,该资源启用 Istio 集成:

    knative-serving-config.yaml 配置文件示例

    apiVersion: operator.knative.dev/v1beta1
    kind: KnativeServing
    metadata:
      name: knative-serving
      namespace: knative-serving
    spec:
      ingress:
        istio:
          enabled: true 
    1
    
      deployments: 
    2
    
      - name: activator
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: autoscaler
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      config:
        istio: 
    3
    
          gateway.knative-serving.knative-ingress-gateway: istio-ingressgateway.<your-istio-namespace>.svc.cluster.local
          local-gateway.knative-serving.knative-local-gateway: knative-local-gateway.<your-istio-namespace>.svc.cluster.local
    Copy to Clipboard Toggle word wrap

    1
    启用 Istio 集成。
    2
    为 Knative Serving data plane pod 启用 sidecar 注入。
    3
    如果您的 istio 没有在 istio-system 命名空间中运行,则需要使用正确的命名空间设置这两个标记。
  2. 应用 KnativeServing 资源:

    $ oc apply -f knative-serving-config.yaml
    Copy to Clipboard Toggle word wrap
  3. 使用以下 KnativeEventing 对象安装 Knative Eventing,它启用了 Istio 集成:

    knative-eventing-config.yaml 配置文件示例

    apiVersion: operator.knative.dev/v1beta1
    kind: KnativeEventing
    metadata:
      name: knative-eventing
      namespace: knative-eventing
    spec:
      config:
        features:
          istio: enabled 
    1
    
      workloads: 
    2
    
      - name: pingsource-mt-adapter
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: imc-dispatcher
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: mt-broker-ingress
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: mt-broker-filter
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
    Copy to Clipboard Toggle word wrap

    1
    启用 Eventing Istio 控制器,为每个 InMemoryChannelKafkaChannel 服务创建一个 DestinationRule
    2
    为 Knative Eventing pod 启用 sidecar 注入。
  4. 应用 KnativeEventing 资源:

    $ oc apply -f knative-eventing-config.yaml
    Copy to Clipboard Toggle word wrap
  5. 使用以下 KnativeKafka 自定义资源安装 Knative Kafka,该资源启用 Istio 集成:

    knative-kafka-config.yaml 配置文件示例

    apiVersion: operator.serverless.openshift.io/v1alpha1
    kind: KnativeKafka
    metadata:
      name: knative-kafka
      namespace: knative-eventing
    spec:
      channel:
        enabled: true
        bootstrapServers: <bootstrap_servers> 
    1
    
      source:
        enabled: true
      broker:
        enabled: true
        defaultConfig:
          bootstrapServers: <bootstrap_servers> 
    2
    
          numPartitions: <num_partitions>
          replicationFactor: <replication_factor>
        sink:
          enabled: true
      workloads: 
    3
    
      - name: kafka-controller
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: kafka-broker-receiver
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: kafka-broker-dispatcher
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: kafka-channel-receiver
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: kafka-channel-dispatcher
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: kafka-source-dispatcher
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
      - name: kafka-sink-receiver
        labels:
          "sidecar.istio.io/inject": "true"
        annotations:
          "sidecar.istio.io/rewriteAppHTTPProbers": "true"
    Copy to Clipboard Toggle word wrap

    1 2
    Apache Kafka 集群 URL,如 my-cluster-kafka-bootstrap.kafka:9092
    3
    为 Knative Kafka pod 启用 sidecar 注入。
  6. 应用 KnativeEventing 对象:

    $ oc apply -f knative-kafka-config.yaml
    Copy to Clipboard Toggle word wrap
  7. 安装 ServiceEntry 以告知 Service Mesh KnativeKafka 组件和 Apache Kafka 集群之间的通信:

    kafka-cluster-serviceentry.yaml 配置文件示例

    apiVersion: networking.istio.io/v1alpha3
    kind: ServiceEntry
    metadata:
      name: kafka-cluster
      namespace: knative-eventing
    spec:
      hosts: 
    1
    
        - <bootstrap_servers_without_port>
      exportTo:
        - "."
      ports: 
    2
    
        - number: 9092
          name: tcp-plain
          protocol: TCP
        - number: 9093
          name: tcp-tls
          protocol: TCP
        - number: 9094
          name: tcp-sasl-tls
          protocol: TCP
        - number: 9095
          name: tcp-sasl-tls
          protocol: TCP
        - number: 9096
          name: tcp-tls
          protocol: TCP
      location: MESH_EXTERNAL
      resolution: NONE
    Copy to Clipboard Toggle word wrap

    1
    Apache Kafka 集群主机列表,如 my-cluster-kafka-bootstrap.kafka
    2
    Apache Kafka 集群监听程序端口。
    注意

    spec.ports 中列出的端口是 examplecategories 端口。实际值取决于 Apache Kafka 集群的配置方式。

  8. 应用 ServiceEntry 资源:

    $ oc apply -f kafka-cluster-serviceentry.yaml
    Copy to Clipboard Toggle word wrap

1.4.4. 验证集成

安装启用了 Istio 的 Service Mesh 和 Serverless 后,您可以验证集成是否正常工作。

流程

  1. 创建一个启用了 sidecar 注入并使用 pass-through 路由的 Knative Service:

    knative-service.yaml 配置文件示例

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: <service_name>
      namespace: <namespace> 
    1
    
      annotations:
        serving.knative.openshift.io/enablePassthrough: "true" 
    2
    
    spec:
      template:
        metadata:
          annotations:
            sidecar.istio.io/inject: "true" 
    3
    
            sidecar.istio.io/rewriteAppHTTPProbers: "true"
        spec:
          containers:
          - image: <image_url>
    Copy to Clipboard Toggle word wrap

    1
    属于服务网格 member roll 的命名空间。
    2
    指示 Knative Serving 生成直通启用的路由,以便您已生成的证书直接通过 ingress 网关提供。
    3
    将 Service Mesh sidecar 注入 Knative 服务 pod。
    重要

    始终将来自此示例中的注解添加到所有 Knative Service 中,以使它们与 Service Mesh 一起工作。

  2. 应用 Service 资源:

    $ oc apply -f knative-service.yaml
    Copy to Clipboard Toggle word wrap
  3. 使用 CA 信任的安全连接访问无服务器应用程序:

    $ curl --cacert root.crt <service_url>
    Copy to Clipboard Toggle word wrap

    例如,运行:

    示例命令

    $ curl --cacert root.crt https://hello-default.apps.openshift.example.com
    Copy to Clipboard Toggle word wrap

    输出示例

    Hello Openshift!
    Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat