配置 Ingress

瞭解如何配置 Ingress 設定以符合工作負載需求。

保留來源 IP 位址

若要保留來源 IP 位址,您可以針對 VPC 叢集啟用 PROXY 通訊協定。 此選項適用於執行 4.13 版或更新版本的叢集。

PROXY 通訊協定提供了一種方便的方式,可跨多層 NAT 或 TCP 代理傳輸連線資訊,例如用戶端的位址。 有關 PROXY 通訊協定的詳細資訊,請參閱 HAProxy 規格

依預設,Ingress 控制器會接收只包含與負載平衡器相關聯的來源位址的連線。 您可以在 VPC 叢集裡啟用 PROXY 通訊協定,以配置負載平衡器來保留 Ingress 控制器所接收連線的原始用戶端位址。

啟用 PROXY 通訊協定

  1. 編輯 Ingress Controller 資源。

    oc -n openshift-ingress-operator edit ingresscontroller/default
    
  2. 在 Ingress 控制器資源中,尋找 spec.endpointPublishingStrategy.loadBalancer 區段並定義下列 providerParameters 值。

    endpointPublishingStrategy:
      loadBalancer:
        providerParameters:
          type: IBM
          ibm:
            protocol: PROXY
        scope: External
      type: LoadBalancerService
    
  3. 儲存並套用該資源。

停用 PROXY 通訊協定

  1. 編輯 Ingress Controller 資源。

    oc -n openshift-ingress-operator edit ingresscontroller/default
    
  2. 在 Ingress 控制器資源中,尋找 spec.endpointPublishingStrategy.loadBalancer 區段並定義下列 providerParameters 值。

    endpointPublishingStrategy:
      loadBalancer:
        providerParameters:
          type: IBM
          ibm:
            protocol: TCP
        scope: External
      type: LoadBalancerService
    
  3. 儲存並套用該資源。

使用註釋自訂 Ingress 遞送

如果您想要自訂應用程式的遞送規則,可以在您定義的 Ingress 資源中使用 路徑特定的 HAProxy 註釋

這些支援的註解格式為 haproxy.router.openshift.io/<annotation>router.openshift.io/<annotation>。IBM Cloud Kubernetes Service 註解 (ingress.bluemix.net/<annotation>) 和 NGINX 註解 (nginx.ingress.kubernetes.io/<annotation>) 不支援 Red Hat OpenShift 版本 4 中的 Ingress 控制器或 Ingress 資源。

啟用存取記載

HTTP 存取記錄包含傳入 HTTP 請求的相關資訊。 存取記錄在除錯複雜問題時相當有用,但路由器 OpenShift 預設並不支援存取記錄功能。 針對 OpenShift 叢集,您可以為路由器設定存取記錄功能,此設定會使路由器 Pod 中包含一個側車容器,該容器會執行伺服器 syslog 並將存取記錄寫入其 stdout. 檔案。

  1. 編輯 IngressController 設定,並將下列設定新增至 .spec

    logging:
      access:
        destination:
          type: Container
        httpCaptureHeaders:
          request:
          - maxLength: 256
            name: Host
        httpLogFormat: '{"time_date":"%t","client":"%ci","host":"%[capture.req.hdr(0)]","ssl_version":"%sslv","request_method":"%HM",
                       "request_uri":"%HU","status":%ST,"upstream_addr":"%si:%sp","request_time":%Tt,"upstream_connect_time":%Tc,
                       "upstream_header_time":%Tr,"termination_state":"%ts"}'
    

    編輯指令

    kubectl edit ingresscontroller -n openshift-ingress-operator default
    ingresscontroller.operator.openshift.io/default edited
    
  2. 檢查路由器 pod 是否執行兩個容器。

    kubectl get pod -n openshift-ingress -w
    

    輸出範例

    NAME                              READY   STATUS    RESTARTS   AGE
    router-default-66945cc7c4-4xlnh   2/2     Running   0          36s
    router-default-66945cc7c4-5cxpn   2/2     Running   0          36s
    
  3. 檢查存取記錄。

    kubectl logs -n openshift-ingress router-default-66945cc7c4-4xlnh -c logs
    

    輸出範例

    ...
    2025-01-28T12:29:07.038592+00:00 router-default-7fc484bbb8-qfm5d router-default-7fc484bbb8-qfm5d haproxy[41]:{"time_date":
    "28/Jan/2025:12:29:06.879","client":"10.5.207.79","host":"alb-autoscale-example-service-default.pvg-classic-z9g5zltmgb1ir
    -1e7743ca80a399c9cff4eaf617434c72-0000.us-south.stg.kube.appdomain.cloud","ssl_version":"TLSv1.3","request_method":
    "GET","request_uri":"/","status":200,"upstream_addr":"172.30.210.252:8080","request_time":159,"upstream_connect_time":1,
    "upstream_header_time":2,"termination_state":"--"}
    2025-01-28T12:29:09.572129+00:00 router-default-7fc484bbb8-qfm5d router-default-7fc484bbb8-qfm5d haproxy[41]: {"time_date":
    "28/Jan/2025:12:29:09.405","client":"10.5.207.79","host":"alb-autoscale-example-service-default.pvg-classic-z9g5zltmgb1ir
    -1e7743ca80a399c9cff4eaf617434c72-0000.us-south.stg.kube.appdomain.cloud","ssl_version":"TLSv1.3","request_method":"GET",
    "request_uri":"/","status":200,"upstream_addr":"172.30.210.252:8080","request_time":166,"upstream_connect_time":0,
    "upstream_header_time":3,"termination_state":"--"}
    ...
    

微調連線處理

clientTimeoutserverTimeout 參數是重要的設定,決定用戶端、Ingress 控制器和後端伺服器之間保持連線的時間。 這些超時在最佳化請求處理中扮演重要的角色,尤其是在處理長時間的用戶端連線、後端伺服器的延遲回應,以及保護寶貴的資源不被不必要地佔用時。

如果您預期客戶會維持連線開啟一段較長的時間,建議增加 clientTimeout 設定以因應這些情況。 相反地,如果您的後端伺服器因為高流量或處理負載而有較高的延遲,調整 serverTimeout 可以提供必要的餘量,讓伺服器在 Ingress 控制器終止連線前完成其要求處理。

您可以在 IngressController 資源中變更上述參數:

apiVersion: operator.openshift.io/v1
kind: IngressController
 ...
spec:
  tuningOptions:
    clientTimeout: 5s
    serverTimeout: 5s

有關調校選項的更多資訊,請參閱 文件 OpenShift

調整逾時

若您的叢集透過 Cloudflare IBM CloudCloud Internet Services (CIS) 公開,並使用網頁應用程式防火牆 (WAF) 或全域負載平衡功能,應將 clientTimeoutserverTimeout 設定為超過 900 秒的值,以避免過早終止連線。 如需詳細資訊,請參閱 Cloudflare 文件

  1. 識別您的入侵控制器:首先列出您的 IngressController 資源。 這可以透過指令來實現:

    oc get ingresscontrollers -n openshift-ingress-operator
    
  2. 更新逾時參數:若要修改特定 IngressControllerclientTimeoutserverTimeout 參數,可以執行修補命令。 例如,以下指令會將 default 入口控制器的逾時設定更新為 905 秒:

    oc patch ingresscontrollers default --patch '{"spec": {"tuningOptions":{"clientTimeout": "905s", "serverTimeout": "905s"}}}' --type=merge -n openshift-ingress-operator
    
  3. 僅 VPC 群集:如果您在虛擬私有雲 (VPC) 中運作,則必須調整 VPC 負載平衡器的閒置連線逾時與您的輸入控制器設定。 我們建議選擇比您的 Ingress Controller 超時設定更大的閒置連線超時。 以下指令示範如何將 router-default LoadBalancer 服務的閒置連線逾時更新為 910 秒:

    oc annotate svc -n openshift-ingress service.kubernetes.io/ibm-load-balancer-cloud-provider-vpc-idle-connection-timeout="910" router-default