3.2. Istio API を使用してゲートウェイ経由で Egress トラフィックを誘導する
Istio API を使用して、ゲートウェイインジェクションを使用してインストールされたゲートウェイを介して送信 HTTP トラフィックを誘導します。
前提条件
- ゲートウェイインジェクションを使用してゲートウェイをインストールしている。
手順
次のコマンドを実行して、
curlという名前の namespace を作成します。$ oc create namespace curl使用している更新ストラテジーに応じて、適切なコマンドを実行して namespace でサイドカーインジェクションを有効にします。
InPlace更新ストラテジーを使用している場合は、次のコマンドを実行します。$ oc label namespace curl istio-injection=enabledRevisionBased更新ストラテジーを使用している場合は、次のコマンドを実行します。次のコマンドを実行してリビジョン名を表示します。
$ oc get istiorevisions.sailoperator.io出力例
NAME TYPE READY STATUS IN USE VERSION AGE default Local True Healthy True v1.24.3 3m33s次のコマンドを実行して、namespace にリビジョン名のラベルを付け、サイドカーインジェクションを有効にします。
$ oc label namespace curl istio.io/rev=default
次のコマンドを実行して、
curlアプリケーションをデプロイします。$ oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yamlcurl Pod の名前で初期化された
CURL_POD環境変数をエクスポートします。$ export CURL_POD=$(oc get pod -n curl -l app=curl -o jsonpath='{.items[0].metadata.name}')メッシュから外部サービスにトラフィックを誘導する
http-se.yamlという名前の YAML ファイルを作成します。次の例では、URL のServiceEntryを定義します。設定例
apiVersion: networking.istio.io/v1 kind: ServiceEntry metadata: name: egress-se namespace: curl spec: hosts: - docs.redhat.com ports: - number: 80 name: http-port protocol: HTTP location: MESH_EXTERNAL resolution: DNS以下のコマンドを実行して、YAML ファイルを適用します。
$ oc apply -f http-se.yamlServiceEntry設定が正しく適用されていることを確認します。次のコマンドを実行して、前の手順で指定したホストに HTTP リクエストを送信します。$ oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.comこのコマンドは、接続が機能していることを示す
301(リダイレクト) または200(成功) などの HTTP ステータスコードを返します。Egress
Gatewayを作成し、メッシュからのトラフィックを外部サービスに指定されたホストにルーティングするhttp-gtw.yamlという名前の YAML ファイルを作成します。設定例
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: egress-gw namespace: <gateway_namespace> # Namespace where the egress gateway is deployed spec: selector: istio: <gateway_name> # Selects the egress-gateway instance to handle this traffic servers: - port: number: 80 name: http protocol: HTTP hosts: - docs.redhat.com # External service host, not a full URL. --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: egress-dr namespace: <gateway_namespace> # Namespace where the egress gateway is deployed spec: host: <gateway_name>.<gateway_namespace>.svc.cluster.local subsets: - name: rh-docs以下のコマンドを実行して、YAML ファイルを適用します。
$ oc apply -f http-gtw.yamlアプリケーションサイドカーから Egress ゲートウェイを経由して外部ホストまでのトラフィックのフローを管理する
VirtualServiceをセットアップするhttp-vs.yamlという名前の YAML ファイルを作成します。設定例
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: egress-vs namespace: curl # Namespace where the curl pod is running spec: hosts: - docs.redhat.com # External service host, not a full URL. gateways: - mesh - <gateway_namespace>/egress-gw # Egress gateway name defined in the file that you used in the previous step. http: - match: - gateways: - mesh port: 80 route: - destination: host: <gateway_name>.<gateway_namespace>.svc.cluster.local subset: rh-docs port: number: 80 weight: 100 - match: - gateways: - <gateway_namespace>/egress-gw # Egress gateway name defined in the file that you used in the previous step. port: 80 route: - destination: host: docs.redhat.com port: number: 80 weight: 100以下のコマンドを実行して、YAML ファイルを適用します。
$ oc apply -f http-vs.yamlHTTP リクエストを URL に再送信します。
$ oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.comターミナルには、以下の出力に似た情報が表示されます。
出力例
... HTTP/1.1 301 Moved Permanently ... location: <example_url> ... HTTP/2 200 Content-Type: text/html; charset=utf-8次のコマンドを実行して、リクエストがゲートウェイ経由でルーティングされたことを確認します。
$ oc logs deployment/<gateway_name> -n <gateway_namespace> | tail -1注記この検証手順を実行するには、アクセスロギングを有効にする必要があります。Istio リソースで
spec.values.meshConfig.accessLogFileフィールドを/dev/stdoutに設定することで、標準出力へのアクセスロギングを有効にできます。ターミナルには、以下の出力に似た情報が表示されます。
出力例
[2024-11-07T14:35:52.428Z] "GET / HTTP/2" 301 - via_upstream - "-" 0 0 24 24 "10.128.2.30" "curl/8.11.0" "79551af2-341b-456d-b414-9220b487a03b" "docs.redhat.com" "23.55.176.201:80" outbound|80||docs.redhat.com 10.128.2.29:49766 10.128.2.29:80 10.128.2.30:38296 -