コンテンツにスキップ

gRPC

gRPCは、クライアントアプリケーションがリモートサーバー上のメソッドを、まるで同じローカルマシン上で実行しているかのように呼び出すことを可能にするリモートプロシージャコール(RPC)フレームワークです。gRPCサーバーとクライアントをCloudflareのグローバルネットワークに接続することで、異なるデータセンターや環境にまたがるサービスを使用するアプリケーションの構築が容易になります。

Cloudflare Tunnel supports gRPC traffic via private subnet routing. Public hostname deployments are not currently supported.



この例では、cloudflared daemonを使用してgRPCサーバーをCloudflareに接続し、Gatewayポリシーでサーバーを保護し、Cloudflare WARPクライアントを使用してサーバーへのgRPCチャネルを開きます。

1. gRPCサーバーのセットアップ

  1. gRPC Pythonアプリケーションをセットアップするには、このクイックスタートガイドに従ってください。

  2. サーバーを起動します:

Terminal window
~/grpc/examples/python/helloworld $ python3 greeter_server.py
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1721770418.373806 3677 config.cc:230] gRPC experiments enabled: call_status_override_on_cancellation, event_engine_dns, event_engine_listener, http2_stats_fix, monitoring_experiment, pick_first_new, trace_record_callops, work_serializer_clears_time_cache
Server started, listening on 50051

2. サーバーをCloudflareに接続

Cloudflareへの安全なアウトバウンド接続を確立するには:

  1. ダッシュボードセットアップガイドに従って、サーバー用のCloudflareトンネルを作成します。アプリケーションを接続するステップをスキップして、ネットワークの接続に直接進むことができます。

  2. トンネルのプライベートネットワークタブで、サーバーのプライベートIPアドレス(またはサーバーIPを含む範囲)を入力します。

3. WARPを介してプライベートネットワークIPをルーティング

By default, WARP excludes traffic bound for RFC 1918 space, which are IP addresses typically used in private networks and not reachable from the Internet. In order for WARP to send traffic to your private network, you must configure Split Tunnels so that the IP/CIDR of your private network routes through WARP.

  1. First, check whether your Split Tunnels mode is set to Exclude or Include mode.

  2. If you are using Include mode, add your network’s IP/CIDR range to the list. Your list should also include the domains necessary for Cloudflare Zero Trust functionality.

  3. If you are using Exclude mode:

    1. Delete your network’s IP/CIDR range from the list. For example, if your network uses the default AWS range of 172.31.0.0/16, delete 172.16.0.0/12.
    2. Re-add IP/CDIR ranges that are not explicitly used by your private network. For the AWS example above, you would add new entries for 172.16.0.0/13, 172.24.0.0/14, 172.28.0.0/15, and 172.30.0.0/16. This ensures that only traffic to 172.31.0.0/16 routes through WARP.

By tightening the private IP range included in WARP, you reduce the risk of breaking a user’s access to local resources.

4. (推奨)Gatewayポリシーを作成

gRPCサーバーへのアクセスをブロックまたは許可するために、Gatewayネットワークポリシーを構成できます。以下の例は、2つのポリシーで構成されています。最初のポリシーは、デバイスポスチャーチェックを通過したデバイスからのgRPC接続を許可し、2つ目は他のすべてのトラフィックをブロックします。Allowポリシーがより高い優先度を持っていることを確認してください。

1. セキュアなデバイスを許可

SelectorOperatorValueLogicAction
Destination Portis50051AndAllow
Destination IPis172.31.0.133And
Passed Device Posture ChecksismacOS firewall (Firewall)And
Passed Device Posture ChecksismacOS disk encryption (Disk encryption)

2. その他すべてをブロック

SelectorOperatorValueAction
Destination IPin172.31.0.0/16Block

Gatewayプロキシの設定に関する詳細は、Gatewayでネットワークトラフィックをフィルタリングするを参照してください。

5. クライアントのセットアップ

gRPCクライアントは、デバイスにCloudflare WARPをインストールし、Zero Trust組織に登録することでサーバーに接続できます。クライアントがCloudflareトンネルを介して公開されたプライベートIPにリクエストを送信すると、WARPは接続をCloudflareのネットワークを通じて対応するトンネルにルーティングします。

gRPCクライアントをセットアップするには:

  1. GatewayのWARPモードでデバイスにWARPクライアントをデプロイします。
  2. デバイス登録ルールを作成して、どのデバイスがZero Trust組織に登録できるかを決定します。
  3. このクイックスタートガイドに従って、デバイスにgRPCをインストールします。
  4. greeter.pyを修正して、gRPCサーバーのプライベートIPを指すようにします。これは、Cloudflareトンネルルートで設定したのと同じプライベートIPです。例えば、
def run():
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
# used in circumstances in which the with statement does not fit the needs
# of the code.
print("Will try to greet world ...")
with grpc.insecure_channel("172.31.0.133:50051") as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name="you"))
print("Greeter client received: " + response.message)

6. 接続のテスト

  1. クライアントデバイスで、WARPがConnectedであることを確認します。
  2. gRPCクライアントアプリケーションを実行します:
Terminal window
~/grpc/examples/python/helloworld $ python3 greeter_client.py
Will try to greet world ...
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1721771484.489711 4414247 config.cc:230] gRPC experiments enabled: call_status_override_on_cancellation, event_engine_dns, event_engine_listener, http2_stats_fix, monitoring_experiment, pick_first_new, trace_record_callops, work_serializer_clears_time_cache
Greeter client received: Hello, you!

トンネルにリクエストが到達し、gRPCサーバーに意図した通りに到達していることを確認するには、トンネルログを表示できます。