3.3. Kubernetes Gateway API を使用してゲートウェイ経由で Egress トラフィックを送信する
Kubernetes Gateway API を使用して、送信 HTTP トラフィックを Egress ゲートウェイ経由で送信します。
前提条件
- Istio コントロールプレーンをインストールした。
-
IstioおよびIstioCNIリソースを設定した。
手順
オプション: {k8} Gateway API カスタムリソース定義 (CRD) を有効にします。
注記Kubernetes 1.28 および OpenShift Container Platform 4.18 以前のバージョンの Red Hat OpenShift Service Mesh では、Kubernetes Gateway API CRD はデフォルトでは使用できないため、使用する前に CRD を有効にする必要があります。OpenShift Container Platform 4.19 以降のバージョンでは、CRD がデフォルトで有効になっています。
Kubernetes Gateway API CRD を有効にする、
gateway-cr.yamlという名前の YAML ファイルを作成します。Kubernetes Gateway カスタムリソース (CR) ファイルの例
apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: info-gateway spec: gatewayClassName: istio listeners: - name: http port: 80 protocol: HTTP allowedRoutes: namespaces: from: Same --- apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: info spec: parentRefs: - name: info-gateway rules: - matches: - path: type: Exact value: /productpage - path: type: PathPrefix value: /static - path: type: Exact value: /login - path: type: Exact value: /logout - path: type: PathPrefix value: /api/v1/products backendRefs: - name: productpage port: 9080以下のコマンドを実行して、YAML ファイルを適用します。
$ oc apply -f gateway-cr.yaml
次のコマンドを実行して、
Egress-gatewayという namespace を作成します。$ oc create namespace egress-gateway次のコマンドを実行して、その namespace に
istio-injectionラベルを適用します。$ oc label namespace egress-gateway istio-injection=enabledEgress ゲートウェイを定義する、
egress -gateway-cr.yamlという名前の YAML ファイルを作成します。Egress ゲートウェイ CR ファイルの例
# ServiceEntry to allow traffic to httpbin.org apiVersion: networking.istio.io/v1 kind: ServiceEntry metadata: name: httpbin-ext spec: hosts: - httpbin.org ports: - number: 80 name: http protocol: HTTP location: MESH_EXTERNAL resolution: DNS --- # Gateway API Gateway for egress apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: httpbin-egress-gateway annotations: networking.istio.io/service-type: ClusterIP spec: gatewayClassName: istio listeners: - name: http hostname: httpbin.org port: 80 protocol: HTTP allowedRoutes: namespaces: from: Same --- # HTTPRoute to direct traffic from sidecars to the egress gateway apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: direct-httpbin-to-egress-gateway spec: parentRefs: - kind: ServiceEntry group: networking.istio.io name: httpbin-ext rules: - backendRefs: - name: httpbin-egress-gateway-istio port: 80 --- # HTTPRoute to forward traffic from the egress gateway to httpbin.org apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: forward-httpbin-from-egress-gateway spec: parentRefs: - name: httpbin-egress-gateway hostnames: - httpbin.org rules: - backendRefs: - kind: Hostname group: networking.istio.io name: httpbin.org port: 80以下のコマンドを実行して、YAML ファイルを適用します。
$ oc apply -f egress-gateway-cr.yaml
検証
次のコマンドを実行して、ゲートウェイ設定のステータスを確認します。
$ oc describe gateway -n egress-gateway目的の出力の
Status列には、Programmedが表示されます。次のコマンドを実行して、
Egress-gatewaynamespace にcurlPod を作成します。$ oc run test-pod --image=curlimages/curl:latest -n egress-gateway --rm -it --restart=Never -- shcurlクライアントを使用し、次のコマンドを入力して Egress ゲートウェイ経由でhttpbin.orgにアクセスできることを確認します。$ curl -v http://httpbin.org/get目的の出力には、設定されたゲートウェイを通過する Egress トラフィックのルートを示す
httpbin.orgからのレスポンスが表示されます。