[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-14 (世界標準時間)。"],[],[],null,["# Create a network load balancer\n\nThis page describes how to set up an\n[L4 load balancer](https://en.wikipedia.org/wiki/Transport_layer)\nwith an AWS Elastic Load Balancer (ELB) or a Network Load Balancer (NLB)\nendpoint.\n\nFor more information on the other types of load balancers that you can use with\nGKE on AWS, see\n[Load balancer overview](/kubernetes-engine/multi-cloud/docs/aws/how-to/load-balancers).\n\nThis page is for Networking specialists who want to install, configure, and\nsupport network equipment. To learn more about common roles and example tasks\nthat we reference in Google Cloud content, see\n[Common GKE user roles and tasks](/kubernetes-engine/enterprise/docs/concepts/roles-tasks).\n\nBefore you begin\n----------------\n\n- [Create a cluster](/kubernetes-engine/multi-cloud/docs/aws/how-to/create-cluster) and configure `kubectl` to connect to it.\n- [Tag your service load balancer subnets](/kubernetes-engine/multi-cloud/docs/aws/how-to/load-balancer-subnets#tag_your_subnets_for_their_intended_use). This is required for subnet auto-discovery.\n- Decide whether you need a [Classic](https://aws.amazon.com/elasticloadbalancing/features/) or a [Network](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/introduction.html) ELB.\n- Decide whether you need an internet-facing or internal load balancer.\n\n### Choose a load balancer type\n\nGKE on AWS creates a Service load balancer as either an AWS Classic\nElastic Load Balancer (Classic ELB) or NLB. By default,\nGKE on AWS creates a Classic ELB. To create an NLB, set the\n`service.beta.kubernetes.io/aws-load-balancer-type` annotation to `nlb`.\nFor more information on the differences between load balancer types, see\n[Load balancer types](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/load-balancer-types.html)\nin the AWS documentation.\n| **Note:** You cannot apply the NLB annotation to an existing Service. To change type, you must delete and recreate the existing LoadBalancer.\n\n### Choose an internet-facing or internal load balancer\n\nService load balancers can be either internet-facing (with a publicly\nresolvable DNS name) or internal (only accessible within your VPC).\n\nBy default, new load balancers are internet-facing. To create an internal\nload balancer, set the `service.beta.kubernetes.io/aws-load-balancer-internal`\nannotation to `\"true\"` in your manifest.\n\nYou cannot apply the `aws-load-balancer-internal` annotation to an existing\nService. To change between internet-facing and internal configurations, you must\ndelete and recreate the existing LoadBalancer.\n\n### Choose your subnets\n\nWhen creating load balancers, AWS needs to know what subnets to place them in.\nBy default, these subnets are automatically discovered from among the subnets in\nthe VPC. This requires that subnets have specific tags. For details of subnet\nauto-discovery and tagging, see\n[Load Balancer Subnets](/kubernetes-engine/multi-cloud/docs/aws/how-to/load-balancer-subnets).\n\nAlternately, you can specify load balancer subnets with an annotation, by adding\nthe `service.beta.kubernetes.io/aws-load-balancer-subnets` annotation to the\nService. The value for this annotation is a comma-separated list of subnet IDs\nor subnet names--- for example\n`subnet-012345678abcdef,subnet-abcdef123456789,subnet-123456789abcdef`.\n\nCreate an example load balancer\n-------------------------------\n\nYou create a Service of type LoadBalancer by creating a deployment and exposing\nthat deployment with a Service. In the following example, you create a sample\nload balancer.\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 kubectl apply -f my-deployment-50001.yaml\n\n3. Verify that three Pods are running:\n\n kubectl get pods --selector=app=products\n\n4. Create a Service of type `LoadBalancer` for your deployment.\n\n5. Decide what type of load balancer you need:\n\n - An internet-facing Classic ELB\n - An internet-facing NLB\n - An internal Classic ELB\n - An internal NLB\n\n Choose the tab that matches your requirements and copy the manifest\n within it to a file named `my-lb-service.yaml`. \n\n ### Internet-facing Classic\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 ### Internet-facing NLB\n\n You create an NLB by setting the annotation\n `service.beta.kubernetes.io/aws-load-balancer-type`\n to `nlb`. The following YAML includes this annotation. \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 ### Internal Classic\n\n You create an internal LoadBalancer by setting the annotation\n `service.beta.kubernetes.io/aws-load-balancer-internal`\n to `\"true\"`. The following YAML includes this annotation. \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 ### Internal NLB\n\n You create an internal 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\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\n6. Create the Service with `kubectl apply`:\n\n kubectl apply -f my-lb-service.yaml\n\n | **Note:** Configuring the load balancer and IP address takes several minutes.\n7. View the Service's address with `kubectl get service`.\n\n kubectl get service my-lb-service\n\n The output will include a column `EXTERNAL-IP` with an address of the\n load balancer (either public or private depending how the load balancer was\n created).\n8. If you have created an internet-facing load balancer you can connect to the\n load balancer with `curl` using the following command:\n\n curl http://\u003cvar translate=\"no\"\u003eEXTERNAL_IP\u003c/var\u003e:60000\n\n Replace \u003cvar translate=\"no\"\u003eEXTERNAL_IP\u003c/var\u003e with the address\n from the EXTERNAL-IP column in the previous step.\n\nThe output resembles the following: \n\n ```none\n Hello, world!\n Version: 2.0.0\n Hostname: my-deployment-50001-84b6dc5555-zmk7q\n ```\n\n### Cleaning up\n\nTo remove the Service and Deployment, use the `kubectl delete` command: \n\n kubectl delete -f my-lb-service.yaml\n kubectl delete -f my-deployment-50001.yaml\n\nNext steps\n----------\n\n- [Set up an HTTP Load Balancer](/kubernetes-engine/multi-cloud/docs/aws/how-to/http-load-balancing).\n\n- Learn more about GKE on Google Cloud documentation on\n [Exposing applications using services](/kubernetes-engine/docs/how-to/exposing-apps).\n\n- See the complete list of\n [supported annotations](https://github.com/kubernetes/cloud-provider-aws/blob/a43d818415c2fbd62a61c2ee613ae4d770426ea5/pkg/providers/v1/aws.go#L101-L243)."]]