Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Injecter des proxys side-car avec Cloud Service Mesh
Ce document explique comment configurer l'injection de proxy side-car avec Cloud Service Mesh afin d'améliorer la sécurité, la fiabilité et l'observabilité du réseau. Ces fonctions sont extraites du conteneur principal de l'application et implémentées dans un proxy commun hors processus (le side-car), fourni sous la forme d'un conteneur séparé dans le même pod. Cela fournit les fonctionnalités de Cloud Service Mesh sans avoir à repenser vos applications de production pour participer à un maillage de services.
L'injection automatique du proxy side-car (injection automatique) se produit lorsque Cloud Service Mesh détecte un libellé d'espace de noms que vous configurez pour le pod de la charge de travail. Le proxy intercepte tout le trafic entrant et sortant vers les charges de travail et communique avec Cloud Service Mesh.
Autorisations requises pour ces tâches
Pour effectuer les tâches décrites sur cette page, vous devez disposer du rôle roles/container.clusterAdmin ou d'un rôle supérieur. Consultez la section Rôles dans Google Kubernetes Engine pour en savoir plus sur les autorisations incluses dans ce rôle.
Activer l'injection side-car automatique
La méthode recommandée pour injecter des proxys side-car est d'utiliser l'injecteur side-car automatique basé sur les webhooks, mais vous pouvez mettre à jour manuellement la configuration Kubernetes de vos pods.
Pour activer l'injection automatique, vous devez ajouter un libellé à vos espaces de noms en utilisant les libellés d'injection par défaut, si le tag par défaut est configuré, ou bien le libellé de révision.
Le libellé que vous ajoutez varie également selon que vous avez déployé Cloud Service Mesh géré (avec l'API Fleet ou avec asmcli) ou que vous avez installé le plan de contrôle intégré au cluster. Le libellé est utilisé par le webhook d'injecteur side-car pour associer les side-cars injectés à une révision spécifique du plan de contrôle.
Pour activer l'injection automatique, procédez comme suit :
Dans le cluster
Exécutez la commande suivante pour localiser le libellé de révision sur istiod :
kubectl -n istio-system get pods -l app=istiod --show-labels
Dans le résultat, sous la colonne LABELS, notez la valeur du libellé de révision istiod, qui suit le préfixe istio.io/rev=. Dans cet exemple, la valeur est asm-11910-9.
Appliquez le libellé de révision aux espaces de noms et supprimez le libellé d'injection Istio (le cas échéant). Dans la commande suivante, NAMESPACE est le nom de l'espace de noms dans lequel vous souhaitez activer l'injection automatique, et REVISION est le libellé de révision noté à l'étape précédente.
Vous pouvez ignorer le message "istio-injection not found" dans le résultat. Cela signifie que l'espace de noms ne portait pas précédemment le libellé istio-injection, auquel on s'attend dans de nouvelles installations de Cloud Service Mesh ou de nouveaux déploiements. Étant donné que le comportement de l'injection automatique n'est pas défini lorsqu'un espace de noms possède à la fois le libellé istio-injection et le libellé de révision, toutes les commandes kubectl label de la documentation Cloud Service Mesh s'assurent explicitement qu'un seul est défini.
Redémarrez les pods concernés en suivant la procédure décrite dans la section suivante.
Maillage de services géré
Exécutez la commande suivante pour localiser les versions disponibles :
kubectl -n istio-system get controlplanerevision
Le résultat ressemble à ce qui suit :
NAME AGE
asm-managed 6d7h
Dans le résultat, la valeur de la colonne NAME est le libellé REVISION qui correspond à la version disponible pour la version de Cloud Service Mesh. Appliquez ce libellé à vos espaces de noms et supprimez le libellé istio-injection (s'il existe).
Dans la commande suivante, remplacez REVISION par le libellé de révision indiqué ci-dessus et remplacez NAMESPACE par le nom de l'espace de noms dans lequel vous souhaitez activer l'injection automatique :
Vous pouvez ignorer le message "istio-injection not found" dans le résultat. Cela signifie que l'espace de noms ne portait pas précédemment le libellé istio-injection, auquel on s'attend dans de nouvelles installations de Cloud Service Mesh ou de nouveaux déploiements. Étant donné que le comportement de l'injection automatique n'est pas défini lorsqu'un espace de noms possède à la fois le libellé istio-injection et le libellé de révision, toutes les commandes kubectl label de la documentation Cloud Service Mesh s'assurent explicitement qu'un seul est défini.
Redémarrez les pods concernés en suivant la procédure décrite dans la section suivante.
Si vous n'avez pas utilisé de déploiement, supprimez les pods. Ils sont automatiquement recréés avec les side-cars :
kubectl delete pod -n YOUR_NAMESPACE --all
Vérifiez que tous les pods de l'espace de noms disposent de side-cars injectés :
kubectl get pod -n YOUR_NAMESPACE
Dans l'exemple de résultat suivant, qui concerne la commande précédente, la colonne READY indique qu'il existe deux conteneurs pour chacune de vos charges de travail : le conteneur principal et le conteneur du proxy side-car.
NAME READY STATUS RESTARTS AGE
YOUR_WORKLOAD 2/2 Running 0 20s
...
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,["# Inject sidecar proxies with Cloud Service Mesh\n==============================================\n\nThis document covers how to configure sidecar proxy injection with Cloud Service Mesh\nto enhance network security, reliability, and observability. These functions are\nabstracted away from the application's primary container and implemented in a\ncommon out-of-process proxy (the sidecar), delivered as a separate container in\nthe same Pod. This provides the\n[Cloud Service Mesh's features](/service-mesh/v1.19/docs/overview) without redesigning your\nproduction applications to participate in a service mesh.\n\nAutomatic sidecar proxy injection (auto-injection) occurs when Cloud Service Mesh\ndetects a namespace label you configure for the workload's Pod. The proxy\nintercepts all inbound and outbound traffic to the workloads and communicates\nwith Cloud Service Mesh.\n\n#### Permissions required for these tasks\n\n\nTo perform the tasks on this page, you must have the\n`roles/container.clusterAdmin` or a higher role. See\n[Google Kubernetes Engine roles](/iam/docs/understanding-roles#container) for\ndetails on the permissions included in this role.\n\nEnabling automatic sidecar injection\n------------------------------------\n\nThe recommended way to inject sidecar proxies is to use the webhooks-based\nautomatic sidecar injector, although you can manually update your Pods'\nKubernetes configuration.\n\nTo enable auto-injection, you label your namespaces with the\n*[default injection labels](https://istio.io/latest/docs/setup/upgrade/canary/#default-tag)*\nif the default tag is set up, or with the\n[revision label](/service-mesh/v1.19/docs/revisions-overview) to your namespace.\nThe label that you add also depends on whether you deployed\nmanaged Cloud Service Mesh (with the\n[fleet API](/service-mesh/v1.19/docs/managed/provision-managed-anthos-service-mesh) or with\n[`asmcli`](/service-mesh/v1.19/docs/managed/provision-managed-anthos-service-mesh-asmcli)), or\ninstalled the in-cluster control plane. The label is used by the sidecar\ninjector webhook to associate injected sidecars with a particular control plane\nrevision.\n\nTo enable auto-injection: \n\n### In-cluster\n\n1. Use the following command to locate the revision label on `istiod`:\n\n kubectl -n istio-system get pods -l app=istiod --show-labels\n\n The output looks similar to the following: \n\n ```bash\n NAME READY STATUS RESTARTS AGE LABELS\n istiod-asm-11910-9-5788d57586-bljj4 1/1 Running 0 23h app=istiod,istio.io/rev=asm-11910-9,istio=istiod,pod-template-hash=5788d57586\n istiod-asm-11910-9-5788d57586-vsklm 1/1 Running 1 23h app=istiod,istio.io/rev=asm-11910-9,istio=istiod,pod-template-hash=5788d57586\n ```\n\n In the output, under the `LABELS` column, note the value of the `istiod`\n revision label, which follows the prefix `istio.io/rev=`. In this\n example, the value is `asm-11910-9`.\n\n\n | **Note:** You can substitute `istio.io/rev` with the `istio-injection=enabled` label if the [default tag](https://istio.io/latest/docs/setup/upgrade/canary/#default-tag) is configured. Verify the default tag exists by running ` istioctl tag list` with the `istioctl` from \u003cvar translate=\"no\"\u003eOUTPUT_DIR\u003c/var\u003e.\n\n \u003cbr /\u003e\n\n2. Apply the revision label to namespaces and remove the istio-injection label\n (if it exists). In the following command, \u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e is\n the name of the namespace where you want to enable auto-injection, and\n \u003cvar translate=\"no\"\u003eREVISION\u003c/var\u003e is the revision label you noted in the\n previous step.\n\n kubectl label namespace \u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e istio-injection- istio.io/rev=\u003cvar translate=\"no\"\u003eREVISION\u003c/var\u003e --overwrite\n\n\n You can ignore the message `\"istio-injection not found\"` in the\n output. That means that the namespace didn't previously have the\n `istio-injection` label, which you should expect in new\n installations of Cloud Service Mesh or new deployments. Because auto-injection\n behavior is undefined when a namespace has both the `istio-injection`\n and the revision label, all `kubectl label` commands in the\n Cloud Service Mesh documentation explicitly ensure that only one is set.\n3. Restart the affected pods, using the steps in the next section.\n\n### Managed service mesh\n\n1. Use the following command to locate the available release channels:\n\n kubectl -n istio-system get controlplanerevision\n\n The output is similar to the following: \n\n NAME AGE\n asm-managed 6d7h\n\n In the output, select the value under the `NAME` column is the\n \u003cvar translate=\"no\"\u003eREVISION\u003c/var\u003e label that corresponds to the available\n [release channel](/service-mesh/v1.19/docs/managed/select-a-release-channel#anthos_service_mesh_versions_per_channel)\n for the Cloud Service Mesh version. Apply this label to your namespaces, and\n remove the `istio-injection` label (if it exists).\n In the following command, replace \u003cvar translate=\"no\"\u003eREVISION\u003c/var\u003e with the\n revision label you noted above, and replace\n \u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e` ` with the name of the namespace where you\n want to enable auto-injection: \n\n kubectl label namespace \u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e istio-injection- istio.io/rev=\u003cvar translate=\"no\"\u003eREVISION\u003c/var\u003e --overwrite\n\n\n You can ignore the message `\"istio-injection not found\"` in the\n output. That means that the namespace didn't previously have the\n `istio-injection` label, which you should expect in new\n installations of Cloud Service Mesh or new deployments. Because auto-injection\n behavior is undefined when a namespace has both the `istio-injection`\n and the revision label, all `kubectl label` commands in the\n Cloud Service Mesh documentation explicitly ensure that only one is set.\n2. Restart the affected pods, using the steps in the next section.\n\n3. If you also deployed the optional\n [Google-managed data plane](/service-mesh/v1.19/docs/managed/provision-managed-anthos-service-mesh#managed-data-plane),\n annotate the `demo` namespace as follows:\n\n kubectl annotate --overwrite namespace \u003cvar translate=\"no\"\u003eYOUR_NAMESPACE\u003c/var\u003e \\\n mesh.cloud.google.com/proxy='{\"managed\":\"true\"}'\n\n### Restart Pods to update sidecar proxies\n\n| **Warning:** Unless you have a load balancer or router setup for [blue-green deployments](https://martinfowler.com/bliki/BlueGreenDeployment.html), make sure you test restarting Pods in a staging environment to verify that your services can handle any potential traffic interruption.\n\nWith automatic sidecar injection, you can update the sidecars for existing Pods\nwith a Pod restart:\n\nHow you restart Pods depends on if they were created as part of a\n[Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/).\n\n1. If you used a Deployment, restart the Deployment, which restarts all Pods\n with sidecars:\n\n ```\n kubectl rollout restart deployment -n YOUR_NAMESPACE\n ```\n\n If you didn't use a Deployment, delete the Pods, and they are automatically\n recreated with sidecars: \n\n ```\n kubectl delete pod -n YOUR_NAMESPACE --all\n ```\n2. Check that all the Pods in the namespace have sidecars injected:\n\n ```\n kubectl get pod -n YOUR_NAMESPACE\n ```\n\n In the following example output from the previous command, notice that the\n `READY` column indicates there are two containers for each of your\n workloads: the primary container and the container for the sidecar proxy. \n\n ```\n NAME READY STATUS RESTARTS AGE\n YOUR_WORKLOAD 2/2 Running 0 20s\n ...\n ```\n\nWhat's next\n-----------\n\nLearn more about:\n\n- [Cloud Service Mesh control plane revisions](/service-mesh/v1.19/docs/revisions-overview)\n- [Deploying workloads](/kubernetes-engine/docs/how-to/deploying-workloads-overview)\n- [Customizing injection](https://istio.io/latest/docs/setup/additional-setup/sidecar-injection/#customizing-injection)"]]