이 페이지에서는 Google Kubernetes Engine(GKE) Autopilot 클러스터에서 실행되는 포드에서 임의의 포트를 노출하는 방법을 보여줍니다.
포드에 포트를 노출하면 포드가 부하 분산기 없이 직접 수신 연결을 수락합니다. GKE는 포드 사양에 지정한 범위에서 열 임의의 포트를 선택합니다. 이러한 유형의 임의의 포트 노출은 워크로드에 직접 수신 연결이 필요하지만 영구 포트 번호를 사용하지 않는 경우에 유용합니다. 이 유형의 워크로드 예시로는 기본 Dynamic 포트 정책이 적용된 Agones GameServer 리소스가 있습니다.
임의의 포트를 요청하면 GKE는 컨테이너 사양의 hostPort 필드 값을 GKE에서 자동으로 선택한 포트로 재정의합니다.
metadata.annotations.autopilot.gke.io/host-port-assignment: 각 컨테이너에 설정한 hostPort의 원래 값과 GKE가 할당한 hostPort의 업데이트된 값을 표시하는 포트 할당. 이 필드는 포드 사양에서 포트 여러 개를 요청한 경우에 유용합니다.
spec.containers.ports.hostPort: GKE가 각 컨테이너에 할당한 열린 포트
[[["이해하기 쉬움","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-06-18(UTC)"],[],[],null,["# Allow direct connections to Autopilot Pods using hostPort\n\n[Autopilot](/kubernetes-engine/docs/concepts/autopilot-overview)\n\n*** ** * ** ***\n\nThis page shows you how to expose a random port in a Pod running in a\nGoogle Kubernetes Engine (GKE) Autopilot cluster.\n\nExposing a port in a Pod lets the Pod accept incoming connections directly,\nwithout a load balancer. GKE chooses a random port to open\nfrom a range that you specify in the Pod specification. This type of random\nport exposure is useful when your workload requires direct incoming connections\nbut doesn't rely on a persistent port number. An example of this type of workload is an [Agones GameServer resource](https://agones.dev/site/docs/reference/gameserver/) with the\ndefault `Dynamic` port policy.\n\nWhen you request a random port, GKE overrides the value in the\n`hostPort` field in the container specification to the port that\nGKE selected for you.\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\n\u003c!-- --\u003e\n\n- Ensure that you have a [GKE Autopilot cluster](/kubernetes-engine/docs/how-to/creating-an-autopilot-cluster) running version 1.24.7-gke.1200 and later or 1.25.3-gke.1100 and later.\n\n### Limitations\n\nYou can only assign random hostPorts for static Pods or for Pods that are\nmanaged by a custom controller, such as Agones. This functionality isn't\nsupported on\n[Kubernetes managed controllers](https://kubernetes.io/docs/concepts/workloads/controllers/)\nsuch as Deployments.\n\nRequest a random port\n---------------------\n\n1. Save the following manifest as `host-port-pod.yaml`:\n\n apiVersion: v1\n kind: Pod\n metadata:\n name: game-pod\n annotations:\n autopilot.gke.io/host-port-assignment: '{\"min\":\u003cvar translate=\"no\"\u003eMIN_PORT\u003c/var\u003e,\"max\":\u003cvar translate=\"no\"\u003eMAX_PORT\u003c/var\u003e}'\n spec:\n containers:\n - name: local-chat\n image: ubuntu\n ports:\n - containerPort: 80\n hostPort: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eHOST_PORT1\u003c/span\u003e\u003c/var\u003e\n protocol: tcp\n - name: game-server\n image: ubuntu\n ports:\n - containerPort: 80\n hostPort: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eHOST_PORT2\u003c/span\u003e\u003c/var\u003e\n protocol: udp\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eMIN_PORT\u003c/var\u003e: the minimum port number for the range from which GKE chooses a random port.\n - \u003cvar translate=\"no\"\u003eMAX_PORT\u003c/var\u003e: the maximum port number for the range from which GKE chooses a random port.\n - \u003cvar translate=\"no\"\u003eHOST_PORT1\u003c/var\u003e`, `\u003cvar translate=\"no\"\u003eHOST_PORT2\u003c/var\u003e: any valid port number. When the Pod is scheduled, GKE updates this field with the randomly assigned port. If you have multiple containers, use different port numbers for each container.\n\n The port range (the difference between \u003cvar translate=\"no\"\u003eMAX_PORT\u003c/var\u003e\n and \u003cvar translate=\"no\"\u003eMIN_PORT\u003c/var\u003e) must be at least 1000 ports.\n2. Apply the manifest:\n\n kubectl apply -f host-port-pod.yaml\n\nWhen you apply the manifest, GKE selects a random port from your\nrange and assigns the port to your container. If GKE assigns the\nsame port value to two Pods, GKE automatically places the Pods\non separate nodes to avoid port conflict.\n\nCheck the assigned port\n-----------------------\n\nTo find the port number that GKE assigned to your containers,\ninspect the Pod: \n\n kubectl get pod game-pod --output=yaml\n\nThe output is similar to the following: \n\n apiVersion: v1\n kind: Pod\n metadata:\n annotations:\n autopilot.gke.io/host-port-assignment: '{\"min\":\u003cvar scope=\"MIN_PORT\" translate=\"no\"\u003eMIN_PORT\u003c/var\u003e,\"max\":\u003cvar scope=\"MAX_PORT\" translate=\"no\"\u003eMAX_PORT\u003c/var\u003e,\"portsAssigned\":{\"\u003cvar scope=\"HOST_PORT1\" translate=\"no\"\u003eHOST_PORT1\u003c/var\u003e\":7300,\"\u003cvar scope=\"HOST_PORT2\" translate=\"no\"\u003eHOST_PORT2\u003c/var\u003e\":7450}}'\n name: game-pod\n namespace: default\n spec:\n containers:\n - name: local-chat\n image: ubuntu\n imagePullPolicy: IfNotPresent\n ports:\n - containerPort: 80\n hostPort: 7300\n protocol: TCP\n - name: game-server\n image: ubuntu\n imagePullPolicy: IfNotPresent\n ports:\n - containerPort: 80\n hostPort: 7450\n protocol: UDP\n\nIn this output:\n\n- `metadata.annotations.autopilot.gke.io/host-port-assignment`: the port assignments, showing the original value for `hostPort` that you set for each container and the updated value for `hostPort` that GKE assigned. This field is useful if you requested multiple ports in your Pod specification.\n- `spec.containers.ports.hostPort`: the opened port that GKE assigned to each container.\n\nWhat's next\n-----------\n\n[Track Agones support on Autopilot on GitHub](https://github.com/googleforgames/agones/issues/2777)."]]