適切な対策を講じないと、システムが新たな脆弱性にさらされる危険性があります。したがって、ファイアウォール ルールを作成して、Google Cloud のインスタンスで送受信されるトラフィックを制御することが重要です。この Codelab では、Google Cloud のファイアウォール ルールと Compute Engine VM インスタンスに適用する方法について説明します。
ファイアウォール ルールとは
Google Cloud のファイアウォール ルールを使用すると、指定した構成に基づいて VM インスタンスとの間のトラフィックを許可または拒否できます。有効になっているファイアウォール ルールは常に適用され、構成やオペレーティング システムに関係なく、インスタンスが再起動されたかどうかに関係なく保護されます。
すべての VPC ネットワークは、分散ファイアウォールとして機能します。ファイアウォール ルールはネットワーク レベルで定義されますが、接続はインスタンスごとに許可または拒否されます。ファイアウォール ルールは、インスタンスと他のネットワークの間だけでなく、同じネットワーク内の個々のインスタンスの間にも存在します。
作成するアプリの概要
- 異なるサブネットワーク内の 2 つの Compute Engine インスタンス
- ネットワーク ping テスト
- インスタンス タグを使用するファイアウォール ルール
- 次のアーキテクチャ:
ラボの内容
- ファイアウォール ルールの特徴
- インスタンス タグを使用してインスタンスにファイアウォール ルールを適用する方法
- ファイアウォール ルールのベスト プラクティス
必要なもの
- Google アカウント
- 2 つの Compute Engine インスタンス
セルフペース型の環境設定
Google アカウントをまだお持ちでない場合は、アカウントを作成する必要があります。Google Cloud Console にログインして新しいプロジェクトを作成します。
プロジェクト ID(すべての Google Cloud プロジェクトにおいて一意の名前)を覚えておいてください。これらの Codelab では PROJECT_ID と呼びます。
次に、Google Cloud リソースを使用するために、Cloud Console で課金を有効にする必要があります。
この Codelab を実施した場合、費用は数ドルを超えることはありませんが、より多くのリソースを使用する場合や、実行を継続する場合は、さらに費用がかかる可能性があります(この Codelab の最後にあるクリーンアップのセクションをご覧ください)。Google Cloud の新規ユーザーは 300 ドル分の無料トライアルをご利用いただけます。
プロジェクト環境を初期化する
[コンピューティング] > [Compute Engine] > [VM インスタンス] の順に移動します。
Compute Engine API を有効にしたら、ほとんどの作業を Cloud Shell で行うことができます。これにより、任意のブラウザでコマンドラインからインフラストラクチャとアプリケーションを管理できます。Debian ベースの VM には、必要なすべての開発ツール(gcloud
コマンドライン ツール、Git
など)と、永続的な 5 GB のホーム ディレクトリが用意されています。画面右上隅のアイコンをクリックして Cloud Shell を開きます。
さまざまなゾーンを選択することもできます。詳しくは、リージョンとゾーンをご覧ください。
[ネットワーキング] > [VPC ネットワーク] > [ファイアウォール ルール] に移動して、既存のファイアウォール ルールを表示します。
デフォルト ネットワークにはファイアウォール ルールのみが作成されます。
custom-network1
の作成時にファイアウォール ルールを指定しなかった場合は、既存の Compute Engine インスタンスに対してファイアウォール ルールを作成して SSH 接続する必要があります。
次に、タグ付けされたインスタンスへの SSH アクセスを許可するファイアウォール ルールを作成します。
ファイアウォール ルールのページから移動しません。[ファイアウォール ルールを作成] をクリックします。
次の仕様を追加します。
- 名前: allow-ssh
- ネットワーク: custom-network1
- トラフィックの方向: 上り(内向き)
- 一致したときのアクション: 許可
- ターゲット: 指定されたターゲットタグ
- ターゲットタグ: iperf-access
- ソース IP 範囲: 0.0.0.0/0
- プロトコルとポート: 指定されたプロトコルとポート: tcp: 22
既存の 2 つのインスタンスにタグを適用します。
[Compute] > [Compute Engine] > [VM インスタンス] に移動して、Compute Engine インスタンスのページに移動します。
[instance-1] をクリックして、VM インスタンスの詳細を表示します。[編集] をクリックします。
[ネットワーク タグ] セクションまで下にスクロールし、iperf-access というタグを付けます。
下部にある [保存] をクリックします。[VM インスタンス] ページに戻り、[編集] をクリックして再度 iperf-access
をインスタンス タグとして追加して、instance-2 を繰り返します。
iPerf
iPerf は一般的に使用されているネットワーク テストツールで、TCP/UDP データ ストリームを作成し、伝送するネットワークのスループットを測定できます。これを使用して、custom-network1
内の異なるサブネットワークにあるインスタンス間でクイックテストを実行します。
iPerf をインストールする
Compute Engine インスタンスのページに移動します。
各 Codelab で作成した SSH ボタンをクリックして、前の Codelab で作成した 2 つの VM に接続します。
2 つの SSH ウィンドウが表示されます。次のコマンドで iPerf を両方にインストールします。
sudo apt-get install iperf
iPerf テストの実行
instance-2
で、instance-2 ssh ターミナルに次のコマンドを入力して、iPerf サーバーであることを指定します。
iperf -s
instance-1 ssh ウィンドウで、instance-2
の内部 IP を含む次のコマンドを入力します。
iperf -c [INSTANCE-2 INTERNAL IP]
instance-1
と instance-2
間の通信を許可する別のファイアウォール ルールを実装していないため、ハングします。Ctrl+Z(Macintosh では Command+Z)を入力して、コマンドを終了します。
今回は、Cloud Shell でファイアウォール ルールを作成します。
インスタンスの SSH セッションではなく、Cloud Shell 内にいることを確認します。次のコマンドを入力してファイアウォール ルールを作成します。
gcloud compute firewall-rules create iperf-access --allow tcp:5001 \ --source-ranges 0.0.0.0/0 \ --network custom-network1 \ --target-tags=iperf-access
次の方法で作成されたことを確認します。
gcloud compute firewall-rules list NAME NETWORK DIRECTION PRIORITY ALLOW DENY DISABLED allow-ssh custom-network1 INGRESS 1000 tcp:22 False default-allow-icmp default INGRESS 65534 icmp False default-allow-internal default INGRESS 65534 tcp:0-65535,udp:0-65535,icmp False default-allow-rdp default INGRESS 65534 tcp:3389 False default-allow-ssh default INGRESS 65534 tcp:22 False iperf-access custom-network1 INGRESS 1000 tcp:5001 False
instance-2
の SSH ウィンドウに戻ります。iperf セッションは引き続き有効です。含まれていない場合は、次のコマンドをもう一度実行してください。
iperf -s
instance-1
ssh ウィンドウで、instance-2
の内部 IP を含む次のコマンドを入力します。
iperf -c [INSTANCE-2 INTERNAL IP]
iPerf が instance-2
への接続を確立し、instance-1
と instance-2
間の内部ネットワーク パフォーマンスの出力を返します。
これで、Google Cloud でファイアウォール ルールを作成するための基本を学びました。
学習した内容
- ファイアウォール ルールによるインスタンスへの影響
- Cloud Console と Cloud Shell を使用してファイアウォール ルールを作成する方法
- ファイアウォール ルールとインスタンスにタグを適用する方法
- インスタンス間で iPerf テストを実行する方法
次のステップ
- ファイアウォール ルールの概要でファイアウォール ルールについて学習する。
Cloud Shell で次のコマンドを使用して、Compute Engine インスタンスを削除します。
gcloud compute instances delete instance-1 --zone us-central1-a The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost. - [instance-1] in [us-central1-a] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/ypc-demo/zones/us-central1-a/instances/instance-1].
gcloud compute instances delete instance-2 --zone europe-west1-d The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost. - [instance-2] in [europe-west1-d] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/ypc-demo/zones/europe-west1-d/instances/instance-2].
次のコマンドを使用してファイアウォール ルールを削除します。
gcloud compute firewall-rules delete allow-ssh The following firewalls will be deleted: - [allow-ssh] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/global/firewalls/allow-ssh].
gcloud compute firewall-rules delete iperf-access The following firewalls will be deleted: - [iperf-access] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/global/firewalls/iperf-access].
次のコマンドを使用してサブネットワークを削除します。
gcloud compute networks subnets delete subnet-us-central-192 --region us-central1 The following subnetworks will be deleted: - [subnet-us-central-192] in [us-central1] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/regions/us-central1/subnetworks/subnet-us-central-192].
gcloud compute networks subnets delete subnet-europe-west-192 --region europe-west1 The following subnetworks will be deleted: - [subnet-europe-west-192] in [europe-west1] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/regions/europe-west1/subnetworks/subnet-europe-west-192].
次のコマンドを使用してカスタム VPC を削除します。
gcloud compute networks delete custom-network1 The following networks will be deleted: - [custom-network1] Do you want to continue (Y/n)? y Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/global/networks/custom-network1].