部署及驗證 GKE NFO 多網路與高效能介面

程式碼研究室簡介
schedule91 分鐘
subject上次更新時間:2023年8月15日
account_circle作者:Deepak Michael

GCP 長期支援 VM 執行個體層級的多個介面。透過多個介面,VM 最多可以將 7 個介面 (預設 + 7 個介面) 連線至不同的虛擬私有雲。GKE 網路現在會將此行為延伸至在節點上執行的 Pod。在這項功能推出前,GKE 叢集讓所有 NodePool 只有一個介面,因此可對應至單一虛擬私有雲。透過「Pod 上的多網路」功能,使用者現在可以為節點和 GKE 叢集中的 Pod 啟用多個介面。

在此教學課程中,您將會建構全方位的 GKE 多 nic 環境,說明圖 1 中的用途。

  1. 利用 Fastbox 建立 netdevice-l3-pod 以完成下列事項:
  2. 進行 PING 和wget - 在 eth2 中 netdevice-vpc 中的 wget - S 至 netdevice-apache 執行個體
  3. 進行 PING 和在 eth1 中,wget -S 至 l3-apache 執行個體
  4. 利用 TTLG 建立 l3-pod 進行 PING 及wget -S 到 l3-apache 執行個體 over eth1

無論是哪種用途,Pod 的 eth0 介面都會連線至預設網路。

圖 1

9d93019ee608587f.png

課程內容

  • 如何建立 L3 類型子網路
  • 如何建立網路裝置類型子網路
  • 如何建立多 nic GKE 節點集區
  • 如何建立具備 NetDevice 和 L3 功能的 Pod
  • 如何建立具備第 3 級能力的 Pod
  • 如何建立及驗證 GKE 物件網路
  • 如何使用 PING、wget 和防火牆記錄檔,驗證遠端 Apache 伺服器的連線能力

軟硬體需求

  • Google Cloud 專案

2. 術語與概念

主要虛擬私有雲:主要的VPC是指預先設定的虛擬私有雲,並有一組預設設定和資源。GKE 叢集會建立於這個虛擬私有雲。

子網路:在 Google Cloud 中,子網路是在虛擬私有雲中建立無網路遮罩的無類別跨網域路由 (CIDR) 方法。子網路有一個指派給節點的主要 IP 位址範圍,且可擁有多個屬於 Pod 和 Service 的次要範圍。

節點網路:節點網路是指虛擬私有雲與子網路組合的專屬組合,在這個節點網路中,系統會從主要 IP 位址範圍,為屬於節點集區的節點分配 IP 位址。

次要範圍:Google Cloud 次要範圍是隸屬於虛擬私有雲中特定區域的 CIDR 和網路遮罩。GKE 會做為第 3 層 Pod 網路使用。一個虛擬私有雲可以有多個次要範圍,而一個 Pod 可連線至多個 Pod 網路。

網路 (L3 或裝置):做為 Pod 連線點的網路物件。在本教學課程中,網路為 l3 網路和 netdevice 網路,其中裝置可以是 netdevice 或 dpdk。預設網路是強制性網路,在叢集建立時,依據 default-nodepool 子網路來建立。

第 3 層網路會對應到子網路的次要範圍,代表:

虛擬私有雲 ->子網路名稱 ->次要範圍名稱

裝置網路對應於虛擬私有雲中的子網路,代表:

虛擬私有雲 ->子網路名稱

預設 Pod 網路:Google Cloud 會在叢集建立期間建立預設的 Pod 網路。預設的 Pod 網路會使用主要虛擬私有雲做為節點網路。根據預設,預設的 Pod 網路適用於所有叢集節點和 Pod。

具有多個介面的 Pod:在 GKE 中擁有多個介面的 Pod 無法連線到相同的 Pod 網路,因為 Pod 的每個介面都必須連線至不重複的網路。

更新專案以支援程式碼研究室

本程式碼研究室會使用 $variables,協助在 Cloud Shell 中實作 gcloud 設定。

在 Cloud Shell 中執行以下操作:

gcloud config list project
gcloud config
set project [YOUR-PROJECT-NAME]
projectid
=YOUR-PROJECT-NAME
echo $projectid

3. 主要虛擬私有雲設定

建立主要虛擬私有雲

在 Cloud Shell 中執行以下操作:

gcloud compute networks create primary-vpc --project=$projectid --subnet-mode=custom

建立節點和次要子網路

在 Cloud Shell 中執行以下操作:

gcloud compute networks subnets create primary-node-subnet --project=$projectid --range=192.168.0.0/24 --network=primary-vpc --region=us-central1 --enable-private-ip-google-access --secondary-range=sec-range-primay-vpc=10.0.0.0/21

4. 建立 GKE 叢集

建立指定 primary-vpc 子網路的私人 GKE 叢集,建立含有必要旗標 (enable-multi-networking 和 –enable-dataplane-v2) 的預設節點集區,以支援多 nic 節點集區。

在 Cloud Shell 中建立 GKE 叢集:

gcloud container clusters create multinic-gke \
   
--zone "us-central1-a" \
   
--enable-dataplane-v2 \
   
--enable-ip-alias \
   
--enable-multi-networking \
   
--network "primary-vpc" --subnetwork "primary-node-subnet" \
   
--num-nodes=2 \
   
--max-pods-per-node=32 \
   
--cluster-secondary-range-name=sec-range-primay-vpc \
   
--no-enable-master-authorized-networks \
   
--release-channel "regular" \
   
--enable-private-nodes --master-ipv4-cidr "100.100.10.0/28" \
   
--enable-ip-alias

驗證 multinic-gke 叢集

在 Cloud Shell 中透過叢集進行驗證:

gcloud container clusters get-credentials multinic-gke --zone us-central1-a --project $projectid

在 Cloud Shell 中,確認兩個節點從 default-pool 產生:

kubectl get nodes

範例:

user@$ kubectl get nodes
NAME                                          STATUS   ROLES    AGE    VERSION
gke
-multinic-gke-default-pool-3d419e48-1k2p   Ready    <none>   2m4s   v1.27.3-gke.100
gke
-multinic-gke-default-pool-3d419e48-xckb   Ready    <none>   2m4s   v1.27.3-gke.100

5. netdevice-vpc 設定

建立 netdevice-vpc 網路

在 Cloud Shell 中執行以下操作:

gcloud compute networks create netdevice-vpc --project=$projectid --subnet-mode=custom

建立 netdevice-vpc 子網路

在 Cloud Shell 中,建立用於多 net 網路裝置網路的子網路:

gcloud compute networks subnets create netdevice-subnet --project=$projectid --range=192.168.10.0/24 --network=netdevice-vpc --region=us-central1 --enable-private-ip-google-access

在 Cloud Shell 中,為 netdevice-apache 執行個體建立子網路:

gcloud compute networks subnets create netdevice-apache --project=$projectid --range=172.16.10.0/28 --network=netdevice-vpc --region=us-central1 --enable-private-ip-google-access

Cloud Router 和 NAT 設定

由於 VM 執行個體沒有外部 IP 位址,因此教學課程會使用 Cloud NAT,進行軟體套件安裝作業。

在 Cloud Shell 建立 Cloud Router。

gcloud compute routers create netdevice-cr --network netdevice-vpc --region us-central1

在 Cloud Shell 中建立 NAT 閘道。

gcloud compute routers nats create cloud-nat-netdevice --router=netdevice-cr --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --region us-central1

建立 netdevice-apache 執行個體

在下一節中,您將建立 netdevice-apache 執行個體。

在 Cloud Shell 中建立執行個體:

gcloud compute instances create netdevice-apache \
   
--project=$projectid \
   
--machine-type=e2-micro \
   
--image-family debian-11 \
   
--no-address \
   
--image-project debian-cloud \
   
--zone us-central1-a \
   
--subnet=netdevice-apache \
   
--metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install apache2 -y
      sudo service apache2 restart
      echo 'Welcome to the netdevice-apache instance !!' | tee /var/www/html/index.html
      EOF"

6. l3-vpc 設定

建立 l3-vpc 網路

在 Cloud Shell 中執行以下操作:

gcloud compute networks create l3-vpc --project=$projectid --subnet-mode=custom

建立 l3-vpc 子網路

在 Cloud Shell 中建立主要和次要範圍子網路。這個次要範圍(sec-range-l3-subnet) 用於多 nic l3 網路:

gcloud compute networks subnets create l3-subnet --project=$projectid --range=192.168.20.0/24 --network=l3-vpc --region=us-central1 --enable-private-ip-google-access --secondary-range=sec-range-l3-subnet=10.0.8.0/21

在 Cloud Shell 中,為 l3-apache 執行個體建立子網路:

gcloud compute networks subnets create l3-apache --project=$projectid --range=172.16.20.0/28 --network=l3-vpc --region=us-central1 --enable-private-ip-google-access

Cloud Router 和 NAT 設定

由於 VM 執行個體沒有外部 IP 位址,因此教學課程會使用 Cloud NAT,進行軟體套件安裝作業。

在 Cloud Shell 建立 Cloud Router。

gcloud compute routers create l3-cr --network l3-vpc --region us-central1

在 Cloud Shell 中建立 NAT 閘道。

gcloud compute routers nats create cloud-nat-l3 --router=l3-cr --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --region us-central1

建立 l3-apache 執行個體

在下一節中,您將建立 l3-apache 執行個體。

在 Cloud Shell 中建立執行個體:

gcloud compute instances create l3-apache \
   
--project=$projectid \
   
--machine-type=e2-micro \
   
--image-family debian-11 \
   
--no-address \
   
--image-project debian-cloud \
   
--zone us-central1-a \
   
--subnet=l3-apache \
   
--metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install apache2 -y
      sudo service apache2 restart
      echo 'Welcome to the l3-apache instance !!' | tee /var/www/html/index.html
      EOF"

7. 建立多式節點集區

在下一節中,您將建立由下列旗標組成的多群組節點集區:

–additional-node-network (對裝置類型介面來說為必要)

範例:

--additional-node-network network=netdevice-vpc,subnetwork=netdevice-subnet

–其他節點網路與–additional-pod-network ( 適用於 L3 類型介面的必要項目)

範例:

--additional-node-network network=l3-vpc,subnetwork=l3-subnet --additional-pod-network subnetwork=l3-subnet,pod-ipv4-range=sec-range-l3-subnet,max-pods-per-node=8

機器類型:部署節點集區時,請考慮機器類型依附元件。例如「e2-standard-4」機器類型搭載 4 個 vCPU 最多可支援 4 個虛擬私有雲。舉例來說,netdevice-l3-pod 總共有 3 個介面 (預設、網路裝置和 l3),因此教學課程中使用的機器類型為 e2-standard-4。

在 Cloud Shell 中,建立由類型裝置和 L3 組成的節點集區:

gcloud container --project "$projectid" node-pools create "multinic-node-pool" --cluster "multinic-gke" --zone "us-central1-a" --additional-node-network network=netdevice-vpc,subnetwork=netdevice-subnet --additional-node-network network=l3-vpc,subnetwork=l3-subnet --additional-pod-network subnetwork=l3-subnet,pod-ipv4-range=sec-range-l3-subnet,max-pods-per-node=8 --machine-type "e2-standard-4"

8. 驗證多個節點集區

在 Cloud Shell 中,確認透過多 NIC 節點集區產生三個節點:

kubectl get nodes

範例:

user@$ kubectl get nodes
NAME                                                STATUS   ROLES    AGE     VERSION
gke
-multinic-gke-default-pool-3d419e48-1k2p         Ready    <none>   15m     v1.27.3-gke.100
gke
-multinic-gke-default-pool-3d419e48-xckb         Ready    <none>   15m     v1.27.3-gke.100
gke
-multinic-gke-multinic-node-pool-135699a1-0tfx   Ready    <none>   3m51s   v1.27.3-gke.100
gke
-multinic-gke-multinic-node-pool-135699a1-86gz   Ready    <none>   3m51s   v1.27.3-gke.100
gke
-multinic-gke-multinic-node-pool-135699a1-t66p   Ready    <none>   3m51s   v1.27.3-gke.100

9. 建立 netdevice-網路

在下列步驟中,您將產生網路與 GKENetworkParamSet Kubernetes 物件,以建立在後續步驟中用來與 Pod 建立關聯的 netdevice-network。

建立 netdevice-network 物件

在 Cloud Shell 中,使用 VI 編輯器或 nano 編輯器建立網路物件 YAML netdevice-network.yaml,請留意路徑是 netdevice-vpc 中的子網路 172.16.10.0/28 (netdevice-apache)。

apiVersion: networking.gke.io/v1
kind
: Network
metadata
:
    name
: netdevice-network
spec
:
    type
: "Device"
    parametersRef
:
     
group: networking.gke.io
      kind
: GKENetworkParamSet
      name
: "netdevice"
    routes
:
   
- to: "172.16.10.0/28"

在 Cloud Shell 中套用 netdevice-network.yaml:

kubectl apply -f netdevice-network.yaml 

在 Cloud Shell 中,確認 netdevice-網路狀態類型已就緒。

kubectl describe networks netdevice-network

範例:

user@$ kubectl describe networks netdevice-network
Name:         netdevice-network
Namespace:    
Labels:       <none>
Annotations:  networking.gke.io/in-use: false
API
Version:  networking.gke.io/v1
Kind:         Network
Metadata:
 
Creation Timestamp:  2023-07-30T22:37:38Z
 
Generation:          1
 
Resource Version:    1578594
  UID
:                 46d75374-9fcc-42be-baeb-48e074747052
Spec:
 
Parameters Ref:
   
Group:  networking.gke.io
   
Kind:   GKENetworkParamSet
   
Name:   netdevice
 
Routes:
   
To:  172.16.10.0/28
 
Type:  Device
Status:
 
Conditions:
   
Last Transition Time:  2023-07-30T22:37:38Z
   
Message:               GKENetworkParamSet resource was deleted: netdevice
   
Reason:                GNPDeleted
   
Status:                False
   
Type:                  ParamsReady
   
Last Transition Time:  2023-07-30T22:37:38Z
   
Message:               Resource referenced by params is not ready
   
Reason:                ParamsNotReady
   
Status:                False
   
Type:                  Ready
Events:                    <none>

建立 GKENetworkParamSet

在 Cloud Shell 中,使用 VI 編輯器或 nano 編輯器建立網路物件 YAML netdevice-network-parm.yaml,規格會對應至 netdevice-vpc 子網路部署作業。

apiVersion: networking.gke.io/v1
kind
: GKENetworkParamSet
metadata
:
    name
: "netdevice"
spec
:
    vpc
: "netdevice-vpc"
    vpcSubnet
: "netdevice-subnet"
    deviceMode
: "NetDevice"

在 Cloud Shell 中套用 netdevice-network-parm.yaml

kubectl apply -f netdevice-network-parm.yaml 

在 Cloud Shell 中,驗證 netdevice-網路狀態的原因 GNPParmsReady 和 NetworkReady:

kubectl describe networks netdevice-network

範例:

user@$ kubectl describe networks netdevice-network
Name:         netdevice-network
Namespace:    
Labels:       <none>
Annotations:  networking.gke.io/in-use: false
API
Version:  networking.gke.io/v1
Kind:         Network
Metadata:
 
Creation Timestamp:  2023-07-30T22:37:38Z
 
Generation:          1
 
Resource Version:    1579791
  UID
:                 46d75374-9fcc-42be-baeb-48e074747052
Spec:
 
Parameters Ref:
   
Group:  networking.gke.io
   
Kind:   GKENetworkParamSet
   
Name:   netdevice
 
Routes:
   
To:  172.16.10.0/28
 
Type:  Device
Status:
 
Conditions:
   
Last Transition Time:  2023-07-30T22:39:44Z
   
Message:              
   
Reason:                GNPParamsReady
   
Status:                True
   
Type:                  ParamsReady
   
Last Transition Time:  2023-07-30T22:39:44Z
   
Message:              
   
Reason:                NetworkReady
   
Status:                True
   
Type:                  Ready
Events:                    <none>

在 Cloud Shell 中,在後續步驟中驗證用於 Pod 介面的 gkenetworkparamset CIDR 區塊 192.168.10.0/24。

kubectl describe gkenetworkparamsets.networking.gke.io netdevice

範例:

user@$ kubectl describe gkenetworkparamsets.networking.gke.io netdevice
Name:         netdevice
Namespace:    
Labels:       <none>
Annotations:  <none>
API
Version:  networking.gke.io/v1
Kind:         GKENetworkParamSet
Metadata:
 
Creation Timestamp:  2023-07-30T22:39:43Z
 
Finalizers:
    networking
.gke.io/gnp-controller
    networking
.gke.io/high-perf-finalizer
 
Generation:        1
 
Resource Version:  1579919
  UID
:               6fe36b0c-0091-4b6a-9d28-67596cbce845
Spec:
 
Device Mode:  NetDevice
 
Vpc:          netdevice-vpc
 
Vpc Subnet:   netdevice-subnet
Status:
 
Conditions:
   
Last Transition Time:  2023-07-30T22:39:43Z
   
Message:              
   
Reason:                GNPReady
   
Status:                True
   
Type:                  Ready
 
Network Name:            netdevice-network
 
Pod CID Rs:
   
Cidr Blocks:
     
192.168.10.0/24
Events:  <none>

10. 建立 L3 網路

在下列步驟中,您將產生網路與 GKENetworkParamSet kubernetes 物件,以建立在後續步驟中用來與 Pod 建立關聯的 L3 網路。

建立 l3 網路物件

在 Cloud Shell 中,使用 VI 編輯器或 nano 編輯器建立網路物件 YAML l3-network.yaml。請留意路徑是 l3-vpc 中的子網路 172.16.20.0/28 (l3-apache)。

apiVersion: networking.gke.io/v1
kind
: Network
metadata
:
  name
: l3-network
spec
:
  type
: "L3"
  parametersRef
:
   
group: networking.gke.io
    kind
: GKENetworkParamSet
    name
: "l3-network"
  routes
:
 
- to: "172.16.20.0/28"

在 Cloud Shell 中套用 l3-network.yaml:

kubectl apply -f l3-network.yaml 

在 Cloud Shell 中,確認 l3 網路狀態類型已就緒。

kubectl describe networks l3-network

範例:

user@$ kubectl describe networks l3-network
Name:         l3-network
Namespace:    
Labels:       <none>
Annotations:  networking.gke.io/in-use: false
API
Version:  networking.gke.io/v1
Kind:         Network
Metadata:
 
Creation Timestamp:  2023-07-30T22:43:54Z
 
Generation:          1
 
Resource Version:    1582307
  UID
:                 426804be-35c9-4cc5-bd26-00b94be2ef9a
Spec:
 
Parameters Ref:
   
Group:  networking.gke.io
   
Kind:   GKENetworkParamSet
   
Name:   l3-network
 
Routes:
  to
:  172.16.20.0/28
 
Type:  L3
Status:
 
Conditions:
   
Last Transition Time:  2023-07-30T22:43:54Z
   
Message:               GKENetworkParamSet resource was deleted: l3-network
   
Reason:                GNPDeleted
   
Status:                False
   
Type:                  ParamsReady
   
Last Transition Time:  2023-07-30T22:43:54Z
   
Message:               Resource referenced by params is not ready
   
Reason:                ParamsNotReady
   
Status:                False
   
Type:                  Ready
Events:                    <none>

建立 GKENetworkParamSet

在 Cloud Shell 中,使用 VI 編輯器或 nano 編輯器建立網路物件 YAML l3-network-parm.yaml。請注意,規格會對應至 l3-vpc 子網路部署。

apiVersion: networking.gke.io/v1
kind
: GKENetworkParamSet
metadata
:
  name
: "l3-network"
spec
:
  vpc
: "l3-vpc"
  vpcSubnet
: "l3-subnet"
  podIPv4Ranges
:
    rangeNames
:
   
- "sec-range-l3-subnet"

在 Cloud Shell 中套用 l3-network-parm.yaml

kubectl apply -f l3-network-parm.yaml 

在 Cloud Shell 中,驗證 l3 網路的狀態原因為 GNPParmsReady 和 NetworkReady:

kubectl describe networks l3-network

範例:

user@$ kubectl describe networks l3-network
Name:         l3-network
Namespace:    
Labels:       <none>
Annotations:  networking.gke.io/in-use: false
API
Version:  networking.gke.io/v1
Kind:         Network
Metadata:
 
Creation Timestamp:  2023-07-30T22:43:54Z
 
Generation:          1
 
Resource Version:    1583647
  UID
:                 426804be-35c9-4cc5-bd26-00b94be2ef9a
Spec:
 
Parameters Ref:
   
Group:  networking.gke.io
   
Kind:   GKENetworkParamSet
   
Name:   l3-network
 
Routes:
   
To:  172.16.20.0/28
 
Type:  L3
Status:
 
Conditions:
   
Last Transition Time:  2023-07-30T22:46:14Z
   
Message:              
   
Reason:                GNPParamsReady
   
Status:                True
   
Type:                  ParamsReady
   
Last Transition Time:  2023-07-30T22:46:14Z
   
Message:              
   
Reason:                NetworkReady
   
Status:                True
   
Type:                  Ready
Events:                    <none>

在 Cloud Shell 中,驗證用來建立 Pod 介面的 gkenetworkparamset l3-network CIDR 10.0.8.0/21。

kubectl describe gkenetworkparamsets.networking.gke.io l3-network

範例:

user@$ kubectl describe gkenetworkparamsets.networking.gke.io l3-network
Name:         l3-network
Namespace:    
Labels:       <none>
Annotations:  <none>
API
Version:  networking.gke.io/v1
Kind:         GKENetworkParamSet
Metadata:
 
Creation Timestamp:  2023-07-30T22:46:14Z
 
Finalizers:
    networking
.gke.io/gnp-controller
 
Generation:        1
 
Resource Version:  1583656
  UID
:               4c1f521b-0088-4005-b000-626ca5205326
Spec:
  podIPv4Ranges
:
   
Range Names:
      sec
-range-l3-subnet
 
Vpc:         l3-vpc
 
Vpc Subnet:  l3-subnet
Status:
 
Conditions:
   
Last Transition Time:  2023-07-30T22:46:14Z
   
Message:              
   
Reason:                GNPReady
   
Status:                True
   
Type:                  Ready
 
Network Name:            l3-network
 
Pod CID Rs:
   
Cidr Blocks:
     
10.0.8.0/21
Events:  <none>

11. 建立 netdevice-l3-pod

在下一節中,您將建立 netdevice-l3-pod 執行 繁忙 box,稱為「瑞士軍刀」,這些指令可支援 300 多種常用指令。Pod 已設為使用 eth1 和 netdevice-vpc 透過 eth2 與 l3-vpc 通訊。

在 Cloud Shell 中,使用 VI 編輯器或 nano 編輯器,建立名為 netdevice-l3-pod.yaml 的忙碌方塊容器。

apiVersion: v1
kind
: Pod
metadata
:
  name
: netdevice-l3-pod
  annotations
:
    networking
.gke.io/default-interface: 'eth0'
    networking
.gke.io/interfaces: |
     
[
     
{"interfaceName":"eth0","network":"default"},
     
{"interfaceName":"eth1","network":"l3-network"},
     
{"interfaceName":"eth2","network":"netdevice-network"}
     
]
spec
:
  containers
:
 
- name: netdevice-l3-pod
    image
: busybox
    command
: ["sleep", "10m"]
    ports
:
   
- containerPort: 80
  restartPolicy
: Always

在 Cloud Shell 中套用 netdevice-l3-pod.yaml

kubectl apply -f netdevice-l3-pod.yaml

驗證 netdevice-l3-pod 建立作業

在 Cloud Shell 中,確認 netdevice-l3-pod 正在執行:

kubectl get pods netdevice-l3-pod

範例:

user@$ kubectl get pods netdevice-l3-pod 
NAME               READY   STATUS    RESTARTS   AGE
netdevice
-l3-pod   1/1     Running   0          74s

在 Cloud Shell 中,驗證指派給 Pod 介面的 IP 位址。

kubectl get pods netdevice-l3-pod -o yaml

在提供的範例中,networking.gke.io/pod-ips 欄位包含與 l3 網路和 netdevice-network 的 Pod 介面相關聯的 IP 位址。PodIP 下方有預設網路 IP 位址 10.0.1.22 的詳細資訊:

user@$ kubectl get pods netdevice-l3-pod -o yaml
apiVersion
: v1
kind
: Pod
metadata
:
  annotations
:
    kubectl
.kubernetes.io/last-applied-configuration: |
     
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{"networking.gke.io/default-interface":"eth0","networking.gke.io/interfaces":"[\n{\"interfaceName\":\"eth0\",\"network\":\"default\"},\n{\"interfaceName\":\"eth1\",\"network\":\"l3-network\"},\n{\"interfaceName\":\"eth2\",\"network\":\"netdevice-network\"}\n]\n"},"name":"netdevice-l3-pod","namespace":"default"},"spec":{"containers":[{"command":["sleep","10m"],"image":"busybox","name":"netdevice-l3-pod","ports":[{"containerPort":80}]}],"restartPolicy":"Always"}}
    networking
.gke.io/default-interface: eth0
    networking
.gke.io/interfaces: |
     
[
     
{"interfaceName":"eth0","network":"default"},
     
{"interfaceName":"eth1","network":"l3-network"},
     
{"interfaceName":"eth2","network":"netdevice-network"}
     
]
    networking
.gke.io/pod-ips: '[{"networkName":"l3-network","ip":"10.0.8.4"},{"networkName":"netdevice-network","ip":"192.168.10.2"}]'
  creationTimestamp
: "2023-07-30T22:49:27Z"
  name
: netdevice-l3-pod
 
namespace: default
  resourceVersion
: "1585567"
  uid
: d9e43c75-e0d1-4f31-91b0-129bc53bbf64
spec
:
  containers
:
 
- command:
   
- sleep
   
- 10m
    image
: busybox
    imagePullPolicy
: Always
    name
: netdevice-l3-pod
    ports
:
   
- containerPort: 80
      protocol
: TCP
    resources
:
      limits
:
        networking
.gke.io.networks/l3-network.IP: "1"
        networking
.gke.io.networks/netdevice-network: "1"
        networking
.gke.io.networks/netdevice-network.IP: "1"
      requests
:
        networking
.gke.io.networks/l3-network.IP: "1"
        networking
.gke.io.networks/netdevice-network: "1"
        networking
.gke.io.networks/netdevice-network.IP: "1"
    terminationMessagePath
: /dev/termination-log
    terminationMessagePolicy
: File
    volumeMounts
:
   
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name
: kube-api-access-f2wpb
      readOnly
: true
  dnsPolicy
: ClusterFirst
  enableServiceLinks
: true
  nodeName
: gke-multinic-gke-multinic-node-pool-135699a1-86gz
  preemptionPolicy
: PreemptLowerPriority
  priority
: 0
  restartPolicy
: Always
  schedulerName
: default-scheduler
  securityContext
: {}
  serviceAccount
: default
  serviceAccountName
: default
  terminationGracePeriodSeconds
: 30
  tolerations
:
 
- effect: NoExecute
    key
: node.kubernetes.io/not-ready
   
operator: Exists
    tolerationSeconds
: 300
 
- effect: NoExecute
    key
: node.kubernetes.io/unreachable
   
operator: Exists
    tolerationSeconds
: 300
 
- effect: NoSchedule
    key
: networking.gke.io.networks/l3-network.IP
   
operator: Exists
 
- effect: NoSchedule
    key
: networking.gke.io.networks/netdevice-network
   
operator: Exists
 
- effect: NoSchedule
    key
: networking.gke.io.networks/netdevice-network.IP
   
operator: Exists
  volumes
:
 
- name: kube-api-access-f2wpb
    projected
:
      defaultMode
: 420
      sources
:
     
- serviceAccountToken:
          expirationSeconds
: 3607
          path
: token
     
- configMap:
          items
:
         
- key: ca.crt
            path
: ca.crt
          name
: kube-root-ca.crt
     
- downwardAPI:
          items
:
         
- fieldRef:
              apiVersion
: v1
              fieldPath
: metadata.namespace
            path
: namespace
status
:
  conditions
:
 
- lastProbeTime: null
    lastTransitionTime
: "2023-07-30T22:49:28Z"
    status
: "True"
    type
: Initialized
 
- lastProbeTime: null
    lastTransitionTime
: "2023-07-30T22:49:33Z"
    status
: "True"
    type
: Ready
 
- lastProbeTime: null
    lastTransitionTime
: "2023-07-30T22:49:33Z"
    status
: "True"
    type
: ContainersReady
 
- lastProbeTime: null
    lastTransitionTime
: "2023-07-30T22:49:28Z"
    status
: "True"
    type
: PodScheduled
  containerStatuses
:
 
- containerID: containerd://dcd9ead2f69824ccc37c109a47b1f3f5eb7b3e60ce3865e317dd729685b66a5c
    image
: docker.io/library/busybox:latest
    imageID
: docker.io/library/busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79
    lastState
: {}
    name
: netdevice-l3-pod
    ready
: true
    restartCount
: 0
    started
: true
    state
:
      running
:
        startedAt
: "2023-07-30T22:49:32Z"
  hostIP
: 192.168.0.4
  phase
: Running
  podIP
: 10.0.1.22
  podIPs
:
 
- ip: 10.0.1.22
  qosClass
: BestEffort
  startTime
: "2023-07-30T22:49:28Z"

驗證 netdevice-l3-pod 路徑

在 Cloud Shell 中,驗證從 netdevice-l3-pod 連至 netdevice-vpc 和 l3-vpc 的路徑:

kubectl exec --stdin --tty netdevice-l3-pod   -- /bin/sh

組成執行個體,驗證 Pod 介面:

ifconfig

在此範例中,eth0 連線至預設網路,eth1 連線至 l3 網路,eth2 則連結至 netdevice-網路。

/ # ifconfig
eth0      
Link encap:Ethernet  HWaddr 26:E3:1B:14:6E:0C  
          inet addr
:10.0.1.22  Bcast:10.0.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU
:1460  Metric:1
          RX packets
:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets
:7 errors:0 dropped:0 overruns:0 carrier:0
          collisions
:0 txqueuelen:0
          RX bytes
:446 (446.0 B)  TX bytes:558 (558.0 B)

eth1      
Link encap:Ethernet  HWaddr 92:78:4E:CB:F2:D4  
          inet addr
:10.0.8.4  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU
:1460  Metric:1
          RX packets
:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets
:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions
:0 txqueuelen:1000
          RX bytes
:446 (446.0 B)  TX bytes:516 (516.0 B)

eth2      
Link encap:Ethernet  HWaddr 42:01:C0:A8:0A:02  
          inet addr
:192.168.10.2  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU
:1460  Metric:1
          RX packets
:73 errors:0 dropped:0 overruns:0 frame:0
          TX packets
:50581 errors:0 dropped:0 overruns:0 carrier:0
          collisions
:0 txqueuelen:1000
          RX bytes
:26169 (25.5 KiB)  TX bytes:2148170 (2.0 MiB)

lo        
Link encap:Local Loopback  
          inet addr
:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU
:65536  Metric:1
          RX packets
:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets
:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions
:0 txqueuelen:1000
          RX bytes
:0 (0.0 B)  TX bytes:0 (0.0 B)

透過 netdevice-l3-pod 驗證通往 netdevice-vpc (172.16.10.0/28) 和 l3-vpc (172.16.20.0/28) 的路徑。

建立執行個體、驗證 Pod 路徑:

ip route

範例:

/ # ip route
default via 10.0.1.1 dev eth0 #primary-vpc
10.0.1.0/
24 via 10.0.1.1 dev eth0  src 10.0.1.22
10.0.1.1 dev eth0 scope link  src 10.0.1.22
10.0.8.0/21 via 10.0.8.1 dev eth1 #l3-vpc (sec-range-l3-subnet)
10.0.8.1 dev eth1 scope link
172.16.10.0/28 via 192.168.10.1 dev eth2 #netdevice-vpc (netdevice-apache subnet)
172.16.20.0/28 via 10.0.8.1 dev eth1 #l3-vpc (l3-apache subnet)
192.168.10.0/24 via 192.168.10.1 dev eth2 #pod interface subnet
192.168.10.1 dev eth2 scope link

如要返回 Cloud Shell,請從執行個體中退出該 Pod。

exit

12. 建立 l3-pod

在下一節中,您將建立執行 TTLbox 的 l3 Pod,也稱為「瑞士軍刀」。,這些指令可支援 300 多種常用指令。Pod 已設為只使用 eth1 與 l3-vpc 通訊。

在 Cloud Shell 中,使用 VI 編輯器或 nano 編輯器建立名為 l3-pod.yaml 的忙碌方塊容器。

apiVersion: v1
kind
: Pod
metadata
:
  name
: l3-pod
  annotations
:
    networking
.gke.io/default-interface: 'eth0'
    networking
.gke.io/interfaces: |
     
[
     
{"interfaceName":"eth0","network":"default"},
     
{"interfaceName":"eth1","network":"l3-network"}
     
]
spec
:
  containers
:
 
- name: l3-pod
    image
: busybox
    command
: ["sleep", "10m"]
    ports
:
   
- containerPort: 80
  restartPolicy
: Always

在 Cloud Shell 中套用 l3-pod.yaml

kubectl apply -f l3-pod.yaml

驗證 l3-pod 建立作業

在 Cloud Shell 中,確認 netdevice-l3-pod 正在執行:

kubectl get pods l3-pod

範例:

user@$ kubectl get pods l3-pod
NAME     READY   STATUS    RESTARTS   AGE
l3
-pod   1/1     Running   0          52s

在 Cloud Shell 中,驗證指派給 Pod 介面的 IP 位址。

kubectl get pods l3-pod -o yaml

在提供的範例中,networking.gke.io/pod-ips 欄位包含與 l3 網路中的 Pod 介面相關聯的 IP 位址。PodIP 下方有預設網路 IP 位址 10.0.2.12 的詳細資訊:

user@$ kubectl get pods l3-pod -o yaml
apiVersion
: v1
kind
: Pod
metadata
:
  annotations
:
    kubectl
.kubernetes.io/last-applied-configuration: |
     
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{"networking.gke.io/default-interface":"eth0","networking.gke.io/interfaces":"[\n{\"interfaceName\":\"eth0\",\"network\":\"default\"},\n{\"interfaceName\":\"eth1\",\"network\":\"l3-network\"}\n]\n"},"name":"l3-pod","namespace":"default"},"spec":{"containers":[{"command":["sleep","10m"],"image":"busybox","name":"l3-pod","ports":[{"containerPort":80}]}],"restartPolicy":"Always"}}
    networking
.gke.io/default-interface: eth0
    networking
.gke.io/interfaces: |
     
[
     
{"interfaceName":"eth0","network":"default"},
     
{"interfaceName":"eth1","network":"l3-network"}
     
]
    networking
.gke.io/pod-ips: '[{"networkName":"l3-network","ip":"10.0.8.22"}]'
  creationTimestamp
: "2023-07-30T23:22:29Z"
  name
: l3-pod
 
namespace: default
  resourceVersion
: "1604447"
  uid
: 79a86afd-2a50-433d-9d48-367acb82c1d0
spec
:
  containers
:
 
- command:
   
- sleep
   
- 10m
    image
: busybox
    imagePullPolicy
: Always
    name
: l3-pod
    ports
:
   
- containerPort: 80
      protocol
: TCP
    resources
:
      limits
:
        networking
.gke.io.networks/l3-network.IP: "1"
      requests
:
        networking
.gke.io.networks/l3-network.IP: "1"
    terminationMessagePath
: /dev/termination-log
    terminationMessagePolicy
: File
    volumeMounts
:
   
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name
: kube-api-access-w9d24
      readOnly
: true
  dnsPolicy
: ClusterFirst
  enableServiceLinks
: true
  nodeName
: gke-multinic-gke-multinic-node-pool-135699a1-t66p
  preemptionPolicy
: PreemptLowerPriority
  priority
: 0
  restartPolicy
: Always
  schedulerName
: default-scheduler
  securityContext
: {}
  serviceAccount
: default
  serviceAccountName
: default
  terminationGracePeriodSeconds
: 30
  tolerations
:
 
- effect: NoExecute
    key
: node.kubernetes.io/not-ready
   
operator: Exists
    tolerationSeconds
: 300
 
- effect: NoExecute
    key
: node.kubernetes.io/unreachable
   
operator: Exists
    tolerationSeconds
: 300
 
- effect: NoSchedule
    key
: networking.gke.io.networks/l3-network.IP
   
operator: Exists
  volumes
:
 
- name: kube-api-access-w9d24
    projected
:
      defaultMode
: 420
      sources
:
     
- serviceAccountToken:
          expirationSeconds
: 3607
          path
: token
     
- configMap:
          items
:
         
- key: ca.crt
            path
: ca.crt
          name
: kube-root-ca.crt
     
- downwardAPI:
          items
:
         
- fieldRef:
              apiVersion
: v1
              fieldPath
: metadata.namespace
            path
: namespace
status
:
  conditions
:
 
- lastProbeTime: null
    lastTransitionTime
: "2023-07-30T23:22:29Z"
    status
: "True"
    type
: Initialized
 
- lastProbeTime: null
    lastTransitionTime
: "2023-07-30T23:22:35Z"
    status
: "True"
    type
: Ready
 
- lastProbeTime: null
    lastTransitionTime
: "2023-07-30T23:22:35Z"
    status
: "True"
    type
: ContainersReady
 
- lastProbeTime: null
    lastTransitionTime
: "2023-07-30T23:22:29Z"
    status
: "True"
    type
: PodScheduled
  containerStatuses
:
 
- containerID: containerd://1d5fe2854bba0a0d955c157a58bcfd4e34cecf8837edfd7df2760134f869e966
    image
: docker.io/library/busybox:latest
    imageID
: docker.io/library/busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79
    lastState
: {}
    name
: l3-pod
    ready
: true
    restartCount
: 0
    started
: true
    state
:
      running
:
        startedAt
: "2023-07-30T23:22:35Z"
  hostIP
: 192.168.0.5
  phase
: Running
  podIP
: 10.0.2.12
  podIPs
:
 
- ip: 10.0.2.12
  qosClass
: BestEffort
  startTime
: "2023-07-30T23:22:29Z"

驗證 l3-pod 路徑

在 Cloud Shell 中,驗證從 netdevice-l3-pod 通往 l3-vpc 的路徑:

kubectl exec --stdin --tty l3-pod   -- /bin/sh

組成執行個體,驗證 Pod 介面:

ifconfig

在此範例中,eth0 連線至預設網路,eth1 則連線至 l3 網路。

/ # ifconfig
eth0      
Link encap:Ethernet  HWaddr 22:29:30:09:6B:58  
          inet addr
:10.0.2.12  Bcast:10.0.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU
:1460  Metric:1
          RX packets
:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets
:7 errors:0 dropped:0 overruns:0 carrier:0
          collisions
:0 txqueuelen:0
          RX bytes
:446 (446.0 B)  TX bytes:558 (558.0 B)

eth1      
Link encap:Ethernet  HWaddr 6E:6D:FC:C3:FF:AF  
          inet addr
:10.0.8.22  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU
:1460  Metric:1
          RX packets
:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets
:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions
:0 txqueuelen:1000
          RX bytes
:446 (446.0 B)  TX bytes:516 (516.0 B)

lo        
Link encap:Local Loopback  
          inet addr
:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU
:65536  Metric:1
          RX packets
:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets
:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions
:0 txqueuelen:1000
          RX bytes
:0 (0.0 B)  TX bytes:0 (0.0 B)

透過 l3-pod 驗證連至 l3-vpc (172.16.20.0/28) 的路徑。

建立執行個體、驗證 Pod 路徑:

ip route

範例:

/ # ip route
default via 10.0.2.1 dev eth0 #primary-vpc
10.0.2.0/
24 via 10.0.2.1 dev eth0  src 10.0.2.12
10.0.2.1 dev eth0 scope link  src 10.0.2.12
10.0.8.0/21 via 10.0.8.17 dev eth1 #l3-vpc (sec-range-l3-subnet)
10.0.8.17 dev eth1 scope link #pod interface subnet
172.16.20.0/28 via 10.0.8.17 dev eth1 #l3-vpc (l3-apache subnet)

如要返回 Cloud Shell,請從執行個體中退出該 Pod。

exit

13. 防火牆更新

必須提供從 GKE 多 cnic-pool 連至 netdevice-vpc 和 l3-vpc 輸入防火牆規則的連線,您將建立防火牆規則,將來源範圍指定為 Pod 網路子網路,例如netdevice-subnet、sec-range-l3-subnet

舉例來說,最近建立的容器「l3-pod, eth2 介面 10.0.8.22」(從 sec-range-l3-subnet 分配) 是連線至 l3-vpc 中的 l3-apache 執行個體時的來源 IP 位址。

netdevice-vpc:允許從 netdevice-subnet 到 netdevice-apache

在 Cloud Shell 中,在 netdevice-vpc 中建立防火牆規則,允許 netdevice-subnet 存取 netdevice-apache 執行個體。

gcloud compute --project=$projectid firewall-rules create allow-ingress-from-netdevice-network-to-all-vpc-instances --direction=INGRESS --priority=1000 --network=netdevice-vpc --action=ALLOW --rules=all --source-ranges=192.168.10.0/24 --enable-logging

l3-vpc:允許從 sec-range-l3-subnet 到 l3-apache

在 Cloud Shell 中,在 l3-vpc 中建立防火牆規則,允許 sec-range-l3-subnet 存取 l3-apache 執行個體。

gcloud compute --project=$projectid firewall-rules create allow-ingress-from-l3-network-to-all-vpc-instances --direction=INGRESS --priority=1000 --network=l3-vpc --action=ALLOW --rules=all --source-ranges=10.0.8.0/21 --enable-logging

14. 驗證 Pod 連線

在下一節中,您將登入 Pod 並執行 wget -S,驗證軟體伺服器首頁的下載作業,藉此驗證從 netdevice-l3-pod 和 l3-pod 與 Apache 執行個體之間的連線。由於 netdevice-l3-pod 的設定是採用 netdevice-vpc 和 l3 網路的介面,因此可以連線至 netdevice-vpc 和 l3-vpc 中的 Apache 伺服器。

相反地,透過 l3-pod 執行 wget -S 時,無法連線到 netdevice-vpc 中的 Apache 伺服器,因為 l3 Pod 只能使用 l3 網路的介面設定。

取得 Apache Server IP 位址

在 Cloud 控制台中前往 Compute Engine → VM 執行個體,取得 Apache 伺服器的 IP 位址

fee492b4fd303859.png

netdevice-l3-pod 到 netdevice-apache 連線能力測試

在 Cloud Shell 中登入 netdevice-l3-pod:

kubectl exec --stdin --tty netdevice-l3-pod   -- /bin/sh

在容器中,根據前一個步驟取得的 IP 位址,對 netdevice-apache 執行個體執行連線偵測 (ping)。

ping <insert-your-ip> -c 4

範例:

/ #  ping 172.16.10.2 -c 4
PING 172.16.10.2 (172.16.10.2): 56 data bytes
64 bytes from 172.16.10.2: seq=0 ttl=64 time=1.952 ms
64 bytes from 172.16.10.2: seq=1 ttl=64 time=0.471 ms
64 bytes from 172.16.10.2: seq=2 ttl=64 time=0.446 ms
64 bytes from 172.16.10.2: seq=3 ttl=64 time=0.505 ms

--- 172.16.10.2 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/
avg/max = 0.446/0.843/1.952 ms
/ #

在 Cloud Shell 中,根據上一步取得的 IP 位址執行 wget -S 至 netdevice-apache 執行個體,200 OK 表示網頁下載成功。

wget -S <insert-your-ip>

範例:

/ # wget -S 172.16.10.2
Connecting to 172.16.10.2 (172.16.10.2:80)
  HTTP/
1.1 200 OK
 
Date: Mon, 31 Jul 2023 03:12:58 GMT
 
Server: Apache/2.4.56 (Debian)
 
Last-Modified: Sat, 29 Jul 2023 00:32:44 GMT
 
ETag: "2c-6019555f54266"
 
Accept-Ranges: bytes
 
Content-Length: 44
 
Connection: close
 
Content-Type: text/html
 
saving to
'index.html'
index
.html           100% |********************************|    44  0:00:00 ETA
'index.html' saved
/ #

netdevice-l3-pod 至 l3-apache 連線能力測試

在 Cloud Shell 中,根據上一步取得的 IP 位址,對 l3-apache 執行個體執行連線偵測 (ping)。

ping <insert-your-ip> -c 4

範例:

/ # ping 172.16.20.3 -c 4
PING 172.16.20.3 (172.16.20.3): 56 data bytes
64 bytes from 172.16.20.3: seq=0 ttl=63 time=2.059 ms
64 bytes from 172.16.20.3: seq=1 ttl=63 time=0.533 ms
64 bytes from 172.16.20.3: seq=2 ttl=63 time=0.485 ms
64 bytes from 172.16.20.3: seq=3 ttl=63 time=0.462 ms

--- 172.16.20.3 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/
avg/max = 0.462/0.884/2.059 ms
/ #

在 Cloud Shell 中,刪除先前的 index.html 檔案,然後根據上一步取得的 IP 位址執行 wget -S 到 l3-apache 執行個體,200 OK 代表網頁下載成功。

rm index.html 
wget
-S <insert-your-ip>

範例:

/ # rm index.html 
/
# wget -S 172.16.20.3
Connecting to 172.16.20.3 (172.16.20.3:80)
  HTTP
/1.1 200 OK
 
Date: Mon, 31 Jul 2023 03:41:32 GMT
 
Server: Apache/2.4.56 (Debian)
 
Last-Modified: Mon, 31 Jul 2023 03:24:21 GMT
 
ETag: "25-601bff76f04b7"
 
Accept-Ranges: bytes
 
Content-Length: 37
 
Connection: close
 
Content-Type: text/html
 
saving to
'index.html'
index
.html           100% |*******************************************************************************************************|    37  0:00:00 ETA
'index.html' saved

如要返回 Cloud Shell,請從執行個體中退出該 Pod。

exit

l3-pod 到 netdevice-apache 連線能力測試

在 Cloud Shell 中登入 l3-pod:

kubectl exec --stdin --tty l3-pod   -- /bin/sh

在容器中,根據前一個步驟取得的 IP 位址,對 netdevice-apache 執行個體執行連線偵測 (ping)。因為 l3-pod 沒有與 net-device- 網路相關聯的介面,因此連線偵測 (ping) 就會失敗。

ping <insert-your-ip> -c 4

範例:

/ # ping 172.16.10.2 -c 4
PING
172.16.10.2 (172.16.10.2): 56 data bytes


--- 172.16.10.2 ping statistics ---
4 packets transmitted, 0 packets received, 100% packet loss

選用:在 Cloud Shell 中,根據上一個步驟取得會逾時的 IP 位址,執行 wget -S 到 netdevice-apache 執行個體。

wget -S <insert-your-ip>

範例:

/ # wget -S 172.16.10.2
Connecting to 172.16.10.2 (172.16.10.2:80)
wget
: can't connect to remote host (172.16.10.2): Connection timed out

l3-pod 到 l3-apache 連線能力測試

在 Cloud Shell 中,根據上一步取得的 IP 位址,對 l3-apache 執行個體執行連線偵測 (ping)。

ping <insert-your-ip> -c 4

範例:

/ # ping 172.16.20.3 -c 4
PING 172.16.20.3 (172.16.20.3): 56 data bytes
64 bytes from 172.16.20.3: seq=0 ttl=63 time=1.824 ms
64 bytes from 172.16.20.3: seq=1 ttl=63 time=0.513 ms
64 bytes from 172.16.20.3: seq=2 ttl=63 time=0.482 ms
64 bytes from 172.16.20.3: seq=3 ttl=63 time=0.532 ms

--- 172.16.20.3 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/
avg/max = 0.482/0.837/1.824 ms
/ #

在 Cloud Shell 中,根據上一步取得的 IP 位址執行 wget -S 至 l3-apache 執行個體,200 OK 表示網頁下載成功。

wget -S <insert-your-ip>

範例:

/ # wget -S 172.16.20.3
Connecting to 172.16.20.3 (172.16.20.3:80)
  HTTP/
1.1 200 OK
 
Date: Mon, 31 Jul 2023 03:52:08 GMT
 
Server: Apache/2.4.56 (Debian)
 
Last-Modified: Mon, 31 Jul 2023 03:24:21 GMT
 
ETag: "25-601bff76f04b7"
 
Accept-Ranges: bytes
 
Content-Length: 37
 
Connection: close
 
Content-Type: text/html
 
saving to
'index.html'
index
.html           100% |*******************************************************************************************************|    37  0:00:00 ETA
'index.html' saved
/ #

15. 防火牆記錄檔

防火牆規則記錄功能可讓您稽核、驗證及分析防火牆規則的成效。舉例來說,您可以確認用於拒絕流量的防火牆規則是否正常運作。當您需要查明特定防火牆規則影響的連線數量時,防火牆規則記錄也能派上用場。

在本教學課程中,您在建立輸入防火牆規則時啟用防火牆記錄功能。讓我們看看從記錄中取得的資訊。

從 Cloud 控制台前往「Logging」→「記錄檔探索工具」

根據螢幕截圖插入下方查詢,然後選取「執行查詢 jsonPayload.rule_details.reference:("network:l3-vpc/firewall:allow-ingress-from-l3-network-to-all-vpc-instances」)

280d00f2c5ce6109.png

仔細檢視擷取,可為資安管理員提供資訊元素。內容包括來源和目的地 IP 位址、通訊埠、通訊協定和節點集區名稱。

ae4638ed9b718ac6.png

如要探索更多防火牆記錄檔,請前往「虛擬私有雲網路」→「防火牆」→「allow-ingress-from-netdevice-network-to-all-vpc-instances」,然後在記錄檔探索工具中選取。

16. 清除所用資源

透過 Cloud Shell 刪除教學課程元件。

gcloud compute instances delete l3-apache netdevice-apache --zone=us-central1-a --quiet

gcloud compute routers
delete l3-cr netdevice-cr --region=us-central1 --quiet

gcloud container clusters
delete multinic-gke --zone=us-central1-a --quiet

gcloud compute firewall
-rules delete allow-ingress-from-l3-network-to-all-vpc-instances allow-ingress-from-netdevice-network-to-all-vpc-instances --quiet

gcloud compute networks subnets
delete l3-apache l3-subnet netdevice-apache netdevice-subnet primary-node-subnet --region=us-central1 --quiet

gcloud compute networks
delete l3-vpc netdevice-vpc primary-vpc --quiet

17. 恭喜

恭喜!您已成功設定及驗證建立多 NIC 節點集區,並建立執行 消除 box 的 Pod,藉此使用 PING 和 wget 驗證與 Apache 伺服器的 L3 和裝置類型連線能力。

此外,您也學到瞭如何運用防火牆記錄檔來檢查 Pod 容器和 Apache 伺服器之間的來源和目的地封包。

Cosmopup 認為教學課程非常精彩!

e6d3675ca7c6911f.jpeg

其他資訊與影片

參考文件