Cette page explique comment améliorer la sécurité du réseau et le contrôle du trafic dans votre cluster en configurant des règles de réseau multiréseaux qui s'appliquent spécifiquement à un réseau de pods désigné. Ces règles de réseau multiréseaux contrôlent le trafic à l'aide de règles de pare-feu au niveau du pod et le flux de trafic entre les pods et les services.
Pour comprendre le fonctionnement des règles de réseau multiréseaux, consultez la section dédiée au fonctionnement des règles de réseau avec des réseaux de pods.
Conditions requises
Pour utiliser des règles de réseau multiréseaux, tenez compte des exigences suivantes :
Google Cloud CLI version 459 et ultérieure.
Un cluster GKE doit exécuter l'une des versions suivantes :
Les règles de réseau FQDN et CiliumClusterWide ne sont pas prises en charge : si vous utilisez une règle de réseau FQDN et une règle de réseau CiliumClusterWide sur un pod connecté à plusieurs réseaux, les règles affectent toutes les connexions du pod, y compris celles auxquelles les règles ne sont pas appliquées.
Configurer des règles de réseau multiréseaux
Pour utiliser des règles de réseau multiréseaux, procédez comme suit :
Si vous souhaitez utiliser Google Cloud CLI pour cette tâche, installez puis initialisez gcloud CLI. Si vous avez déjà installé gcloud CLI, assurez-vous de disposer de la dernière version en exécutant la commande gcloud components update.
Créer une règle de réseau
Pour créer une règle de réseau qui applique des règles sur le même réseau de pods que votre charge de travail, référencez le réseau de pods spécifique dans la définition de la règle de réseau.
Pour définir les règles de trafic d'entrée et les pods cibles sélectionnés en fonction de libellés ou d'autres sélecteurs, créez une règle de réseau standard.
Enregistrez l'exemple de fichier manifeste suivant sous le nom sample-ingress-network-policy1.yaml :
apiVersion:networking.k8s.io/v1
kind:NetworkPolicy
metadata:
name:sample-network-policy
namespace:default
annotations:
networking.gke.io/network:blue-pod-network# GKE-specific annotation for network selection
spec:
podSelector:
matchLabels:
app:test-app-2# Selects pods with the label "app: test-app-2"policyTypes:
-Ingress# Specifies the policy applies only to incoming trafficingress:
-from:# Allow incoming traffic only from...-podSelector:
matchLabels:
app:test-app-1# ...pods with the label "app: test-app-1"
Appliquez le fichier manifeste sample-ingress-network-policy1.yaml :
kubectlapply-fsample-ingress-network-policy1.yaml
Pour définir les règles de trafic de sortie et les pods cibles sélectionnés en fonction de libellés ou d'autres sélecteurs, créez une règle de réseau standard.
Enregistrez l'exemple de fichier manifeste suivant sous le nom sample-egress-network-policy2.yaml :
Appliquez le fichier manifeste sample-egress-network-policy2.yaml :
kubectlapply-fsample-egress-network-policy2.yaml
Résoudre les problèmes liés aux règles de réseau multiréseaux
Si vous rencontrez des problèmes avec les règles de réseau, qu'elles soient appliquées ou non à des réseaux de pods spécifiques, vous pouvez diagnostiquer et résoudre le problème en exécutant les commandes suivantes :
kubectl get networkpolicy : répertorie tous les objets de règle de réseau et les informations les concernant.
iptables-save : récupère et répertorie toutes les chaînes de tables d'adresses IP pour un nœud particulier. Vous devez exécuter cette commande sur le nœud en tant qu'utilisateur racine.
cilium bpf policy get <endpoint-id> : récupère et répertorie les adresses IP autorisées à partir du mappage de règles de chaque point de terminaison.
cilium policy selectors : affiche les identités et les règles associées qui les ont sélectionnées.
cilium identity list : affiche les mappages d'identité associés à l'adresse IP.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[],[],null,["# Set up multi-network network policies\n\n[Standard](/kubernetes-engine/docs/concepts/choose-cluster-mode)\n\n*** ** * ** ***\n\n|\n| **Preview**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis page explains how you can enhance network security and traffic control\nwithin your cluster by configuring multi-network network policies that apply\nspecifically to a designated Pod network. These multi-network network policies\ncontrol traffic by using firewall rules at the Pod level, and they control\ntraffic flow between Pods and Services.\n\nTo understand how multi-network network policies work, see [how Network\nPolicies\nwork](/kubernetes-engine/docs/concepts/about-multi-networking-policies#how-it-works)\nwith Pod networks.\n\nRequirements\n------------\n\nTo use multi-network network policies, consider the following requirements:\n\n- Google Cloud CLI version 459 and later.\n- You must have a GKE cluster running one of the following versions:\n - 1.28.5-gke.1293000 or later\n - 1.29.0-gke.1484000 or later\n- Your cluster must use [GKE Dataplane V2](/kubernetes-engine/docs/concepts/dataplane-v2).\n\nLimitations\n-----------\n\n**FQDN network policy and CiliumClusterWide network policy are not supported**:\nIf you use an FQDN network policy and a CiliumClusterWide network policy on a\nPod that's connected to multiple networks, the policies affect all the Pod's\nconnections, including connections where the policies aren't applied.\n\nConfigure multi-network network policies\n----------------------------------------\n\nTo use multi-network network policies, do the following:\n\n1. Create a cluster with [multi-network enabled GKE](/kubernetes-engine/docs/how-to/setup-multinetwork-support-for-pods#create-a-gke-cluster).\n2. Create a [node pool](/kubernetes-engine/docs/how-to/setup-multinetwork-support-for-pods#create-gke-node-pool) and a [Pod network](/kubernetes-engine/docs/how-to/setup-multinetwork-support-for-pods#create-pod-network).\n3. [Reference the Pod network](/kubernetes-engine/docs/how-to/setup-multinetwork-support-for-pods#reference-the-prepared-network).\n4. [Create a network policy](#create-network-policy) to be enforced that references the same Pod network utilized by the workload.\n\nBefore you begin\n----------------\n\nBefore you start, make sure that you have performed the following tasks:\n\n- Enable the Google Kubernetes Engine API.\n[Enable Google Kubernetes Engine API](https://console.cloud.google.com/flows/enableapi?apiid=container.googleapis.com)\n- If you want to use the Google Cloud CLI for this task, [install](/sdk/docs/install) and then [initialize](/sdk/docs/initializing) the gcloud CLI. If you previously installed the gcloud CLI, get the latest version by running `gcloud components update`. **Note:** For existing gcloud CLI installations, make sure to set the `compute/region` [property](/sdk/docs/properties#setting_properties). If you use primarily zonal clusters, set the `compute/zone` instead. By setting a default location, you can avoid errors in the gcloud CLI like the following: `One of [--zone, --region] must be supplied: Please specify location`. You might need to specify the location in certain commands if the location of your cluster differs from the default that you set.\n\nCreate network policy\n---------------------\n\n1. To create a network policy that enforces rules on the same Pod network as\n your workload, reference the specific Pod network in the network policy\n definition.\n\n2. To define the selected ingress traffic rules and target Pods based on labels\n or other selectors, create a standard Network Policy.\n\n Save the following sample manifest as `sample-ingress-network-policy1.yaml`: \n\n apiVersion: networking.k8s.io/v1\n kind: NetworkPolicy\n metadata:\n name: sample-network-policy\n namespace: default\n annotations:\n networking.gke.io/network: blue-pod-network # GKE-specific annotation for network selection\n spec:\n podSelector:\n matchLabels:\n app: test-app-2 # Selects pods with the label \"app: test-app-2\"\n policyTypes:\n - Ingress # Specifies the policy applies only to incoming traffic\n ingress:\n - from: # Allow incoming traffic only from...\n - podSelector:\n matchLabels:\n app: test-app-1 # ...pods with the label \"app: test-app-1\"\n\n3. Apply the `sample-ingress-network-policy1.yaml` manifest:\n\n kubectl apply -f sample-ingress-network-policy1.yaml\n\n4. To define the selected egress traffic rules and target Pods based on labels\n or other selectors, create a standard network policy.\n\n Save the following sample manifest as `sample-egress-network-policy2.yaml`: \n\n apiVersion: networking.k8s.io/v1\n kind: NetworkPolicy\n metadata:\n name: sample-network-policy-2\n namespace: default\n annotations:\n networking.gke.io/network: blue-pod-network # GKE-specific annotation (optional)\n spec:\n podSelector:\n matchLabels:\n app: test-app-2\n policyTypes:\n - Egress # Only applies to outgoing traffic\n egress:\n - to:\n - podSelector:\n matchLabels:\n app: test-app-3\n\n5. Apply the `sample-egress-network-policy2.yaml` manifest:\n\n kubectl apply -f sample-egress-network-policy2.yaml\n\nTroubleshoot multi-network network policies\n-------------------------------------------\n\nIf you experience issues with network policies, whether they are applied to\nspecific Pod networks or not, you can diagnose and troubleshoot the problem by\nrunning the following commands:\n\n1. `kubectl get networkpolicy`: lists all network policy objects and information about them.\n2. `iptables-save`: retrieves and lists all IP address tables chains for a particular node. You must run this command on the node as root.\n3. `cilium bpf policy get \u003cendpoint-id\u003e`: retrieves and lists allowed IP addresses from each endpoint's policy map.\n4. `cilium policy selectors`: prints out the [identities](https://docs.cilium.io/en/latest/gettingstarted/terminology/#identity) and the associated policies that have selected them.\n5. `cilium identity list`: shows mappings from identity to IP address.\n\nWhat's next\n-----------\n\n- Read [About multi-network network policies](/kubernetes-engine/docs/concepts/about-multi-networking-policies)\n- Read [Set up multi-network support for Pods](/kubernetes-engine/docs/how-to/setup-persistent-ip-addresses-on-gke-pods)"]]