Le produit décrit dans cette documentation, Anthos Clusters on AWS (génération précédente), est désormais en mode de maintenance. Toutes les nouvelles installations doivent utiliser le produit de génération actuelle, Anthos Clusters on AWS.
À partir de votre répertoire anthos-aws, utilisez anthos-gke pour basculer vers le contexte de votre cluster d'utilisateur.
cd anthos-aws
env HTTPS_PROXY=http://localhost:8118 \
anthos-gke aws clusters get-credentials CLUSTER_NAME
Remplacez CLUSTER_NAME par le nom de votre cluster d'utilisateur.
Installez l'outil de ligne de commande curl ou un outil similaire.
Sélectionner un équilibreur de charge externe ou interne
GKE sur AWS crée un équilibreur de charge externe (dans votre sous-réseau public) ou interne (dans votre sous-réseau privé) en fonction d'une annotation de la ressource LoadBalancer.
Choisissez entre créer un équilibreur de charge classique (ELB classique) ou un équilibreur de charge réseau (NLB). Pour découvrir les différences entre les types d'équilibreurs de charge, consultez la section Types d'équilibreurs de charge de la documentation AWS.
Créer un équilibreur de charge
Pour créer un équilibreur de charge, vous devez créer un déploiement et l'exposer avec un service.
Créez votre déploiement. Les conteneurs de ce déploiement écoutent le port 50001.
Enregistrez le fichier YAML suivant dans un fichier nommé my-deployment-50001.yaml :
Créez un service de type LoadBalancer pour votre déploiement. Vous pouvez créer un ELB classique ou réseau sur votre sous-réseau public ou privé.
Choisissez l'une des options suivantes :
Un ELB classique sur le sous-réseau public
Un NLB sur le sous-réseau public
Un ELB classique sur le sous-réseau privé
Un NLB sur le sous-réseau privé
Copiez ensuite le fichier manifeste suivant dans un fichier nommé my-lb-service.yaml.
Pour créer une ressource LoadBalancer privée, définissez l'annotation service.beta.kubernetes.io/aws-load-balancer-internal sur "true". Le code YAML suivant inclut cette annotation.
Le résultat ressemble au suivant : elb-id.elb.aws-region.amazonaws.com.
Si vous avez créé un équilibreur de charge externe et que vous avez accès au sous-réseau VPC public, vous pouvez vous connecter à l'équilibreur de charge à l'aide de curl.
Remplacez external-ip par l'adresse IP obtenue dans le résultat de la commande kubectl get service de l'étape précédente.
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/07/31 (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/07/31 (UTC)."],[],[],null,["# Creating a load balancer\n\nThis topic shows you how to set up an AWS Elastic Load Balancer (ELB) with\nGKE on AWS.\n\nWhen you create a Service of type `LoadBalancer`, a GKE on AWS\ncontroller configures a\n[Classic](https://aws.amazon.com/elasticloadbalancing/features/) or\n[Network](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/introduction.html)\nELB on AWS.\n\nYou can also follow the [Quickstart](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/quickstart) to create an\nexternally facing Classic ELB from the Google Cloud console or\n[Create an AWS Application Load Balancer (ALB)](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/how-to/loadbalancer-alb).\n\nBefore you begin\n----------------\n\n\nBefore you start using GKE on AWS, make sure you have performed the following tasks:\n\n- Complete the [Prerequisites](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/how-to/prerequisites).\n\n\u003c!-- --\u003e\n\n- Install a [management service](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/how-to/installing-management).\n- Create a [user cluster](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/how-to/creating-user-cluster).\n- From your `anthos-aws` directory, use `anthos-gke` to switch context to your user cluster. \n\n ```sh\n cd anthos-aws\n env HTTPS_PROXY=http://localhost:8118 \\\n anthos-gke aws clusters get-credentials CLUSTER_NAME\n ```\n Replace \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e with your user cluster name.\n- Have the `curl` command line tool or a similar tool installed.\n\n### Selecting an external or internal load balancer\n\nGKE on AWS creates an external (in your public subnet) or\ninternal (in your private subnet) load balancer depending on\nan annotation to the LoadBalancer resource.\n\nIf you select an external load balancer, it is accessible by the IP addresses\nallowed in the node pool's\n[security groups](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/reference/security-groups)\nand the subnet's\n[network access control lists (ACLs)](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html).\n\n### Choosing a load balancer type\n\nChoose if you want to create a Classic Load Balancer (Classic ELB) or a\nNetwork Load Balancer (NLB). For more information on the differences between\nload balancer types, see\n[Load balancer types](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/load-balancer-types.html)\nin the AWS documentation.\n\nCreating a LoadBalancer\n-----------------------\n\nYou create a load balancer by creating a deployment and exposing that deployment\nwith a service.\n\n1. Create your deployment. Containers in this Deployment listen on port 50001.\n Save the following YAML to a file named `my-deployment-50001.yaml`:\n\n apiVersion: apps/v1\n kind: Deployment\n metadata:\n name: my-deployment-50001\n spec:\n selector:\n matchLabels:\n app: products\n department: sales\n replicas: 3\n template:\n metadata:\n labels:\n app: products\n department: sales\n spec:\n containers:\n - name: hello\n image: \"gcr.io/google-samples/hello-app:2.0\"\n env:\n - name: \"PORT\"\n value: \"50001\"\n\n2. Create the Deployment with `kubectl apply`:\n\n env HTTPS_PROXY=http://localhost:8118 \\\n kubectl apply -f my-deployment-50001.yaml\n\n3. Verify that three Pods are running:\n\n env HTTPS_PROXY=http://localhost:8118 \\\n kubectl get pods --selector=app=products\n\n4. Create a Service of type `LoadBalancer` for your deployment. You can create\n a Classic or Network ELB on either your public or private subnet.\n Choose from one of the following options:\n\n - A Classic ELB on the public subnet\n - An NLB on the public subnet\n - A Classic ELB on the private subnet\n - An NLB on the private subnet\n\n Then, copy the following manifest to a file named `my-lb-service.yaml`. \n\n ### Classic Public\n\n apiVersion: v1\n kind: Service\n metadata:\n name: my-lb-service\n spec:\n type: LoadBalancer\n selector:\n app: products\n department: sales\n ports:\n - protocol: TCP\n port: 60000\n targetPort: 50001\n\n ### NLB Public\n\n You create a NLB by setting the annotation\n `service.beta.kubernetes.io/aws-load-balancer-type`\n to `nlb`. The following YAML includes this annotation.\n **Note:** You cannot apply the NLB annotation to an existing Service. To create an NLB, you must delete the existing external LoadBalancer and re-create it. \n\n apiVersion: v1\n kind: Service\n metadata:\n name: my-lb-service\n annotations:\n service.beta.kubernetes.io/aws-load-balancer-type: nlb\n spec:\n type: LoadBalancer\n selector:\n app: products\n department: sales\n ports:\n - protocol: TCP\n port: 60000\n targetPort: 50001\n\n ### Classic Private\n\n You create a private LoadBalancer by setting the annotation\n `service.beta.kubernetes.io/aws-load-balancer-internal`\n to `\"true\"`. The following YAML includes this annotation.\n **Note:** You cannot apply the internal load balancer annotation to an existing Service. To create an internal load balancer, you must delete the existing external LoadBalancer and re-create it. \n\n apiVersion: v1\n kind: Service\n metadata:\n name: my-lb-service\n annotations:\n service.beta.kubernetes.io/aws-load-balancer-internal: \"true\"\n spec:\n type: LoadBalancer\n selector:\n app: products\n department: sales\n ports:\n - protocol: TCP\n port: 60000\n targetPort: 50001\n\n ### NLB Private\n\n You create a private NLB by setting the annotations:\n - `service.beta.kubernetes.io/aws-load-balancer-internal` to `\"true\"`\n - `service.beta.kubernetes.io/aws-load-balancer-type` to `nlb`\n\n The following YAML includes both annotations.\n **Note:** You cannot apply these annotations to an existing Service. To create an internal NLB, you must delete the existing external LoadBalancer and re-create it. \n\n apiVersion: v1\n kind: Service\n metadata:\n name: my-lb-service\n annotations:\n service.beta.kubernetes.io/aws-load-balancer-internal: \"true\"\n service.beta.kubernetes.io/aws-load-balancer-type: nlb\n spec:\n type: LoadBalancer\n selector:\n app: products\n department: sales\n ports:\n - protocol: TCP\n port: 60000\n targetPort: 50001\n\n5. Create the Service with `kubectl apply`:\n\n env HTTPS_PROXY=http://localhost:8118 \\\n kubectl apply -f my-lb-service.yaml\n\n | **Note:** Configuring the load balancer and IP address takes several minutes.\n6. View the Service's hostname with `kubectl get service`.\n\n env HTTPS_PROXY=http://localhost:8118 \\\n kubectl get service my-lb-service \\\n --output jsonpath=\"{.status.loadBalancer.ingress..hostname}{'\\n'}\"\n\n The output resembles\n \u003cvar translate=\"no\"\u003eelb-id\u003c/var\u003e`.elb.`\u003cvar translate=\"no\"\u003eaws-region\u003c/var\u003e`.amazonaws.com`.\n7. If you have created an externally-facing load balancer and you have access\n to the public VPC subnet, you can connect to the load balancer with `curl`.\n Replace \u003cvar translate=\"no\"\u003eexternal-ip\u003c/var\u003e with the IP from the output of\n `kubectl get service` from the previous step.\n\n curl \u003cvar translate=\"no\"\u003eexternal-ip\u003c/var\u003e:60000\n\n The output resembles the following: \n\n Hello, world!\n Version: 2.0.0\n Hostname: my-deployment-50001-84b6dc5555-zmk7q\n\nCleaning up\n-----------\n\nTo remove the Service and Deployment, use `kubectl delete`. \n\n env HTTPS_PROXY=http://localhost:8118 \\\n kubectl delete -f my-lb-service.yaml\n\n env HTTPS_PROXY=http://localhost:8118 \\\n kubectl delete -f my-deployment-50001.yaml\n\nTroubleshooting\n---------------\n\nIf you cannot access a load balancer endpoint, try\n[tagging your subnets](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/troubleshooting#tagging_subnets).\n\nWhat's Next\n-----------\n\n- Follow the [Quickstart](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/quickstart) to create an\n externally facing Classic ELB from the Google Cloud console.\n\n- [Create an AWS Application Load Balancer (ALB)](/kubernetes-engine/multi-cloud/docs/aws/previous-generation/how-to/loadbalancer-alb).\n\n- Read the GKE on Google Cloud documentation on\n [Exposing applications using services](/kubernetes-engine/docs/how-to/exposing-apps)."]]