控管私人執行個體的輸出

本節將說明在開發階段和管道執行階段,從私人 Cloud Data Fusion 執行個體建立出口控制的整體架構。

下列系統架構圖顯示,在開發管道時,私人 Cloud Data Fusion 執行個體如何連線至公開網際網路:

私人執行個體架構圖

您可以在管道開發或執行期間,透過將所有傳出流量重新導向至客戶專案,控制與 SaaS 應用程式和第三方公開雲端服務的連線。這項程序會使用下列資源:

  • 自訂 VPC 網路路徑:自訂 VPC 網路會透過匯入的自訂路徑將流量轉送至閘道 VM,並使用 VPC 對等互連匯出至租用戶專案的 VPC。

  • Proxy VM:Proxy VM 會將 Cloud Data Fusion 租用戶專案的傳出流量,透過公用網際網路路由至指定目的地。 Google Cloud 您可以在客戶專案中建立及管理閘道 VM。建議您使用內部負載平衡器 (ILB) 在高可用性 (HA) 設定中設定這些項目。如果您有多個使用相同虛擬私有雲網路的私人 Cloud Data Fusion 執行個體,則可在虛擬私有雲中重複使用相同的 VM。

事前準備

在管道開發期間設定輸出控制

輸出控管可讓您控管或篩選可從網路傳出的內容,這在 VPC Service Controls 環境中相當實用。沒有可用於執行此任務的偏好網路 Proxy。代理伺服器的例子包括 Squid proxyHAProxyEnvoy

本指南的範例說明如何在使用 Debian 映像檔的 VM 執行個體上,設定 HTTP 篩選器的 HTTP Proxy。範例使用 Squid Proxy 伺服器,這是設定 Proxy 伺服器的方式之一。

建立 Proxy VM

在私人 Cloud Data Fusion 執行個體所在的 VPC 中,使用下列啟動指令碼和 IP 轉送功能建立 VM。

這個指令碼會安裝 Squid Proxy,並將其設為攔截 HTTP 流量,並允許 .squid-cache.org.google.com 網域。您可以將這些網域替換為要與 Cloud Data Fusion 執行個體連結的網域。

控制台

  1. 前往「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體頁面

  2. 點選「建立執行個體」

  3. 使用與私人 Cloud Data Fusion 執行個體設定網路對等互連的相同 VPC。如要進一步瞭解此情境中的虛擬私有雲網路對等互連,請參閱事前準備

  4. 為與 Cloud Data Fusion 執行個體位於相同網路的執行個體啟用 IP 轉送

  5. 在「Startup script」(開機指令碼) 欄位中輸入下列指令碼:

    #! /bin/bash
    apt-get -y install squid3
    cat <<EOF > /etc/squid/conf.d/debian.conf
    #
    # Squid configuration settings for Debian
    #
    logformat squid %ts.%03tu %6tr %>a %Ss/%03>Hs %<st %rm %ru %ssl::>sni %Sh/%<a %mt
    logfile_rotate 10
    debug_options rotate=10
    
    # configure intercept port
    http_port 3129 intercept
    
    # allow only certain sites
    acl allowed_domains dstdomain "/etc/squid/allowed_domains.txt"
    http_access allow allowed_domains
    
    # deny all other http requests
    http_access deny all
    EOF
    
    # Create a file with allowed egress domains
    # Replace these example domains with the domains that you want to allow
    # egress from in Data Fusion pipelines
    cat <<EOF > /etc/squid/allowed_domains.txt
    .squid-cache.org
    .google.com
    EOF
    
    /etc/init.d/squid restart
    
    iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3129
    
    echo 1 > /proc/sys/net/ipv4/ip_forward
    echo net.ipv4.ip_forward=1 > /etc/sysctl.d/11-gce-network-security.conf
    iptables -t nat -A POSTROUTING -s 0.0.0.0/0 -p tcp --dport 443 -j MASQUERADE
    

gcloud

export CDF_PROJECT=<cdf-project>
export PROXY_VM=<proxy-vm>
export ZONE=<vm-zone>
export SUBNET=<subnet>
export VPC_NETWORK=<vpc-network>
export COMPUTE_ENGINE_SA=<compute-engine-sa>

gcloud beta compute --project=$CDF_PROJECT instances create $PROXY_VM --zone=$ZONE --machine-type=e2-medium --subnet=$SUBNET --no-address --metadata=startup-script=\#\!\ /bin/bash$'\n'apt-get\ -y\ install\ squid3$'\n'cat\ \<\<EOF\ \>\ /etc/squid/conf.d/debian.conf$'\n'\#$'\n'\#\ Squid\ configuration\ settings\ for\ Debian$'\n'\#$'\n'logformat\ squid\ \%ts.\%03tu\ \%6tr\ \%\>a\ \%Ss/\%03\>Hs\ \%\<st\ \%rm\ \%ru\ \%ssl::\>sni\ \%Sh/\%\<a\ \%mt$'\n'logfile_rotate\ 10$'\n'debug_options\ rotate=10$'\n'$'\n'\#\ configure\ intercept\ port$'\n'http_port\ 3129\ intercept$'\n'$'\n'\#\ allow\ only\ certain\ sites$'\n'acl\ allowed_domains\ dstdomain\ \"/etc/squid/allowed_domains.txt\"$'\n'http_access\ allow\ allowed_domains$'\n'$'\n'\#\ deny\ all\ other\ http\ requests$'\n'http_access\ deny\ all$'\n'EOF$'\n'$'\n'$'\n'\#\ Create\ a\ file\ with\ allowed\ egress\ domains$'\n'\#\ Replace\ these\ example\ domains\ with\ the\ domains\ that\ you\ want\ to\ allow\ $'\n'\#\ egress\ from\ in\ Data\ Fusion\ pipelines$'\n'cat\ \<\<EOF\ \>\ /etc/squid/allowed_domains.txt$'\n'.squid-cache.org$'\n'.google.com$'\n'EOF$'\n'$'\n'/etc/init.d/squid\ restart$'\n'$'\n'iptables\ -t\ nat\ -A\ PREROUTING\ -p\ tcp\ --dport\ 80\ -j\ REDIRECT\ --to-port\ 3129$'\n'echo\ 1\ \>\ /proc/sys/net/ipv4/ip_forward$'\n'echo\ net.ipv4.ip_forward=1\ \>\ /etc/sysctl.d/11-gce-network-security.conf$'\n'iptables\ -t\ nat\ -A\ POSTROUTING\ -s\ 0.0.0.0/0\ -p\ tcp\ --dport\ 443\ -j\ MASQUERADE --can-ip-forward --maintenance-policy=MIGRATE --service-account=$COMPUTE_ENGINE_SA --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append --tags=http-server,https-server --image=debian-10-buster-v20210420 --image-project=debian-cloud --boot-disk-size=10GB --boot-disk-type=pd-balanced --boot-disk-device-name=instance-1 --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --reservation-affinity=any

gcloud compute --project=$CDF_PROJECT firewall-rules create egress-allow-http --direction=INGRESS --priority=1000 --network=$VPC_NETWORK --action=ALLOW --rules=tcp:80 --source-ranges=0.0.0.0/0 --target-tags=https-server

gcloud compute --project=$CDF_PROJECT firewall-rules create egress-allow-https --direction=INGRESS --priority=1000 --network=$VPC_NETWORK --action=ALLOW --rules=tcp:443 --source-ranges=0.0.0.0/0 --target-tags=https-server

建立自訂路徑

建立自訂路徑,連線至您建立的閘道 VM 執行個體

控制台

如要在 Google Cloud 控制台中建立路徑,請參閱「新增靜態路徑」。

設定路線時,請執行下列操作:

  • 將「Priority」設為大於或等於 1001
  • 使用與私人 Cloud Data Fusion 執行個體相同的專案和 VPC。
  • 請確認 VPC 網路對等互連設定允許匯出路徑,以便 Cloud Data Fusion 租用戶專案 VPC 透過 VPC 網路對等互連匯入這條自訂路徑。

gcloud

如要在 gcloud CLI 中建立路徑,請按照下列步驟操作:

gcloud beta compute routes create $ROUTE --project=$CDF_PROJECT \
    --network=$VPC_NETWORK --priority=1001 \
    --destination-range=0.0.0.0/0 \
    --next-hop-instance=$PROXY_VM \
    --next-hop-instance-zone=$ZONE

設定管道執行作業的輸出控管機制

在設計環境中,您可以使用允許的主機名稱在預覽和 Wrangler 中存取公開網際網路,之後即可部署管道。根據預設,部署的 Cloud Data Fusion 管道會在 Dataproc 叢集上執行。

如要確保 Dataproc 叢集中的所有公開網際網路流量都會經過一或多個 Proxy VM,請新增私人 DNS 區域和記錄。這個步驟是必要的,因為 Cloud NAT 不支援篩選。

在 DNS 記錄中加入代理 VM 或 ILB 的 IP 位址。

部署管道

在設計階段驗證管道後,請部署管道。根據預設,部署的管道會在 Dataproc 叢集中執行。

為確保 Dataproc 叢集中的所有公用網際網路流量都會經過一或多個 Proxy VM,請在與 Dataproc VM 相同的 VPC 中,新增含有執行個體標記 proxy 和優先順序 1000 的自訂路由:

建立自訂路徑

修改管道以使用 Dataproc 標記,因為 Cloud NAT 目前不支援任何出口篩選。

後續步驟