Il prodotto descritto in questa documentazione, Anthos Clusters on AWS (generazione precedente), è ora in modalità di manutenzione. Tutte le nuove installazioni devono utilizzare l'attuale prodotto di generazione, Cluster Anthos on AWS.
Dalla directory anthos-aws, utilizza
anthos-gke per cambiare contesto e passare al cluster di utenti.
cd anthos-aws
env HTTPS_PROXY=http://localhost:8118 \
anthos-gke aws clusters get-credentials CLUSTER_NAME
Sostituisci CLUSTER_NAME con il nome del cluster di utenti.
Avere installato lo strumento a riga di comando curl o uno strumento simile.
Selezione di un bilanciatore del carico esterno o interno
GKE su AWS crea un bilanciatore del carico esterno (nella sottorete pubblica) o interno (nella sottorete privata) in base a un'annotazione della risorsa LoadBalancer.
Scegli se creare un bilanciatore del carico classico (ELB classico) o un bilanciatore del carico di rete (NLB). Per ulteriori informazioni sulle differenze tra i tipi di bilanciatori del carico, consulta Tipi di bilanciatori del carico nella documentazione di AWS.
Creazione di un bilanciatore del carico
Per creare un bilanciatore del carico, devi creare un deployment e esporlo con un servizio.
Crea il tuo deployment. I container in questo deployment rimangono in ascolto sulla porta 50001.
Salva il seguente codice YAML in un file denominato my-deployment-50001.yaml:
Crea un servizio di tipo LoadBalancer per il tuo deployment. Puoi creare un bilanciatore di carico classico o di rete nella subnet pubblica o privata.
Scegli una delle seguenti opzioni:
Un ELB classico nella sottorete pubblica
Un NLB nella subnet pubblica
Un ELB classico nella subnet privata
Un NLB nella subnet privata
Quindi, copia il seguente manifest in un file denominato my-lb-service.yaml.
Puoi creare un LoadBalancer privato impostando l'annotazione
service.beta.kubernetes.io/aws-load-balancer-internal
su "true". Il seguente file YAML include questa annotazione.
L'output è simile a
elb-id.elb.aws-region.amazonaws.com.
Se hai creato un bilanciatore del carico rivolto all'esterno e hai accesso alla subnet VPC pubblica, puoi connetterti al bilanciatore del carico con curl.
Sostituisci external-ip con l'IP dell'output di kubectl get service del passaggio precedente.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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)."]]