https://cloud.google.com/vpc/docs/using-packet-mirroring?hl=ja
https://qiita.com/hssh2_bin/items/722176ca3f813d339a41
Packet Mirroring を有効にするには、パケット ミラーリング コレクタとして機能する内部 TCP / UDP ロードバランサが必要です
バックエンド サービスで構成したヘルスチェックに応答するようにコレクタ インスタンスが設定されていない場合、
ヘルスチェックが失敗することがあります。その場合も、パケットのミラーリングは可能です。
vm01 -> ミラー元
mig01 -> ミラー先 インスタンスグループ コレクタ
-- 1. 前作業
gcloud init
gcloud auth list
gcloud --version
gcloud projects create project01-9999999 \
--name="project01"
gcloud projects list
gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet
gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111
gcloud services enable compute.googleapis.com --project project01-9999999
gcloud components update
-- 2. VPC作成
gcloud compute networks create vpc01 \
--subnet-mode=custom \
--mtu=1460 \
--bgp-routing-mode=regional
gcloud compute networks list
-- 3. サブネット作成
gcloud compute networks subnets create subnet01 \
--range=10.0.1.0/24 \
--stack-type=IPV4_ONLY \
--network=vpc01 \
--region=asia-northeast1
gcloud compute networks subnets create subnet02 \
--range=10.0.2.0/24 \
--stack-type=IPV4_ONLY \
--network=vpc01 \
--region=asia-northeast1
gcloud compute networks subnets list
gcloud compute instances create vm01 \
--machine-type=e2-micro \
--image-project=centos-cloud \
--image=centos-7-v20221004 \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--network vpc01 \
--subnet subnet01 \
--zone asia-northeast1-a
gcloud compute instances list
-- 5. インスタンステンプレート作成
gcloud compute instance-templates create template01 \
--machine-type e2-micro \
--image-project=centos-cloud \
--image=centos-7-v20221004 \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--network vpc01 \
--subnet subnet02
gcloud compute instance-templates list
gcloud compute instance-templates describe template01
-- 6. インスタンスグループ(ミラー先) 作成
gcloud compute instance-groups managed create mig01 \
--size 1 \
--template template01 \
--zone asia-northeast1-a \
--base-instance-name mig01
gcloud compute instance-groups list
-- 7. Packet Mirroring 用の内部ロードバランサを作成する
-- 7.1 HTTP ヘルスチェックを作成
gcloud compute health-checks create http hc01 \
--port 80 \
--region asia-northeast1
gcloud compute health-checks list
-- 7.2 バックエンド サービスを作成
gcloud compute backend-services create backend01 \
--region=asia-northeast1 \
--health-checks-region=asia-northeast1 \
--load-balancing-scheme=internal \
--protocol=tcp \
--health-checks=hc01
gcloud compute backend-services list
gcloud compute backend-services describe backend01
-- 7.3 バックエンド サービスにインスタンス グループを追加
gcloud compute backend-services add-backend backend01 \
--region=asia-northeast1 \
--instance-group=mig01 \
--instance-group-zone=asia-northeast1-a
gcloud compute backend-services describe backend01
-- 7.4 バックエンド サービスの転送ルールを作成
gcloud compute forwarding-rules create rule01 \
--region=asia-northeast1 \
--network=vpc01 \
--subnet=subnet01 \
--backend-service=backend01 \
--load-balancing-scheme=internal \
--ip-protocol=TCP \
--ports=all \
--is-mirroring-collector
gcloud compute forwarding-rules list
-- 8. ファイアウォールルール作成
gcloud compute firewall-rules create fw01 \
--network vpc01 \
--allow tcp:22,icmp
gcloud compute firewall-rules list
-- 9. パケット ミラーリング ポリシーを作成
gcloud compute packet-mirrorings create policy01 \
--network=vpc01 \
--collector-ilb=rule01 \
--mirrored-subnets=subnet01 \
--mirrored-instances=projects/project01-9999999/zones/asia-northeast1-a/instances/vm01 \
--filter-protocols=icmp \
--filter-direction=both
gcloud compute packet-mirrorings list
gcloud compute packet-mirrorings describe policy01
-- 10. 動作確認
作業端末からミラー元(vm01)にping送信し、ミラー先(mig01)へミラーされたパケットをミラー先で確認
gcloud compute instances list
gcloud compute ssh vm01
gcloud compute ssh mig01-xxxx
sudo tcpdump icmp
ping 192.0.2.1
-- 11. クリーンアップ
gcloud projects list
gcloud projects delete project01-9999999