跳到内容

配置入口网关

Knative 使用共享的入口网关来服务 Knative 服务网格中的所有传入流量,即 knative-serving 命名空间下的 knative-ingress-gateway 网关。默认情况下,我们使用 istio-system 命名空间下的 Istio 网关服务 istio-ingressgateway 作为其底层服务。您可以按照以下步骤替换服务和网关。

替换默认的 istio-ingressgateway 服务

步骤 1:创建网关服务和部署实例

您需要先创建网关服务和部署实例来处理流量。假设您将默认的 istio-ingressgateway 自定义为 custom-ingressgateway,如下所示。

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  components:
    ingressGateways:
      - name: custom-ingressgateway
        enabled: true
        namespace: custom-ns
        label:
          istio: custom-gateway

步骤 2:更新 Knative 网关

更新 knative-serving 命名空间下的网关实例 knative-ingress-gateway

kubectl edit gateway knative-ingress-gateway -n knative-serving

将标签选择器替换为您的服务的标签

istio: ingressgateway

对于前面提到的 custom-ingressgateway 服务示例,它应该更新为

istio: custom-gateway

如果服务端口发生变化(与 istio-ingressgateway 的端口相比),请相应地更新网关中的端口信息。

步骤 3:更新网关 ConfigMap

  1. 更新 knative-serving 命名空间下的网关 configmap config-istio

    kubectl edit configmap config-istio -n knative-serving
    

    此命令将打开您的默认文本编辑器,并允许您编辑 config-istio ConfigMap。

    apiVersion: v1
    data:
      _example: |
        ################################
        #                              #
        #    EXAMPLE CONFIGURATION     #
        #                              #
        ################################
        # ...
        external-gateways: |
          - name: knative-ingress-gateway
            namespace: knative-serving
            service: istio-ingressgateway.istio-system.svc.cluster.local
    
  2. 编辑文件以添加包含您的服务完整限定 URL 的 external-gateways 字段。对于前面提到的 custom-ingressgateway 服务示例,它应该更新为

    apiVersion: v1
    data:
      external-gateways: |
        - name: knative-ingress-gateway
          namespace: knative-serving
          service: custom-ingressgateway.custom-ns.svc.cluster.local
    kind: ConfigMap
    [...]
    

替换 knative-ingress-gateway 网关

到目前为止,我们已经对网关服务进行了自定义,但我们也可能希望使用自己的网关。我们可以按照以下步骤将默认网关替换为我们自己的网关。

步骤 1:创建网关

假设您要将默认的 knative-ingress-gateway 网关替换为 custom-ns 中的 knative-custom-gateway。首先,创建 knative-custom-gateway 网关

  1. 使用以下模板创建一个 YAML 文件

    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: knative-custom-gateway
      namespace: custom-ns
    spec:
      selector:
        istio: <service-label>
      servers:
      - port:
          number: 80
          name: http
          protocol: HTTP
        hosts:
        - "*"
    
    其中 <service-label> 是用于选择服务的标签,例如 ingressgateway

  2. 通过运行以下命令应用 YAML 文件

    kubectl apply -f <filename>.yaml
    
    其中 <filename> 是您在前面步骤中创建的文件名。

步骤 2:更新网关 ConfigMap

  1. 更新 knative-serving 命名空间下的网关 configmap config-istio

    kubectl edit configmap config-istio -n knative-serving
    

    此命令将打开您的默认文本编辑器,并允许您编辑 config-istio ConfigMap。

    apiVersion: v1
    data:
      _example: |
        ################################
        #                              #
        #    EXAMPLE CONFIGURATION     #
        #                              #
        ################################
        # ...
        external-gateways: |
          - name: knative-ingress-gateway
            namespace: knative-serving
            service: istio-ingressgateway.istio-system.svc.cluster.local
    
  2. 编辑文件以添加包含自定义网关的 external-gateways 字段。对于前面提到的 knative-custom-gateway 示例,它应该更新为

    apiVersion: v1
    data:
      external-gateways: |
        - name: knative-custom-gateway
          namespace: custom-ns
          service: istio-ingressgateway.istio-system.svc.cluster.local
    kind: ConfigMap
    [...]
    

配置格式应为

  external-gateways: |
    - name: <gateway-name>
      namespace: <gateway-namespace>
      service: <fully-qualified-url-of-istio-ingress-service>

我们使用分析和 Cookie 来了解网站流量。有关您使用我们网站的信息将与 Google 共享,以用于该目的。 了解更多。