Halaman ini menunjukkan cara mengekspos port acak di Pod yang berjalan di cluster Google Kubernetes Engine (GKE) Autopilot.
Dengan mengekspos port di Pod, Pod dapat menerima koneksi masuk secara langsung, tanpa load balancer. GKE memilih port acak untuk dibuka dari rentang yang Anda tentukan dalam spesifikasi Pod. Jenis eksposur port acak ini berguna saat workload Anda memerlukan koneksi masuk langsung, tetapi tidak bergantung pada jumlah port persisten. Contoh jenis workload ini adalah resource Agones GameServer dengan kebijakan port Dynamic default.
Saat Anda meminta port acak, GKE akan mengganti nilai dalam kolom hostPort di spesifikasi container ke port yang dipilih oleh GKE untuk Anda.
Sebelum memulai
Sebelum memulai, pastikan Anda telah melakukan tugas berikut:
Jika ingin menggunakan Google Cloud CLI untuk tugas ini,
instal lalu
lakukan inisialisasi
gcloud CLI. Jika sebelumnya Anda telah menginstal gcloud CLI, dapatkan versi terbaru dengan menjalankan gcloud components update.
Pastikan Anda memiliki cluster GKE Autopilot yang menjalankan versi 1.24.7-gke.1200 dan yang lebih baru atau 1.25.3-gke.1100 dan yang lebih baru.
Batasan
Anda hanya dapat menetapkan hostPort acak untuk Pod statis atau untuk Pod yang dikelola oleh pengontrol khusus, seperti Agones. Fungsi ini tidak didukung di pengontrol terkelola Kubernetes seperti Deployment.
Meminta port acak
Simpan manifes berikut sebagai host-port-pod.yaml:
MIN_PORT: nomor port minimum untuk rentang tempat GKE memilih port acak.
MAX_PORT: nomor port maksimum untuk rentang tempat GKE memilih port acak.
HOST_PORT1, HOST_PORT2: semua nomor port yang valid. Saat Pod dijadwalkan, GKE akan memperbarui kolom ini dengan port yang ditetapkan secara acak. Jika Anda memiliki beberapa container, gunakan jumlah port yang berbeda untuk setiap container.
Rentang port (perbedaan antara MAX_PORT dan MIN_PORT) minimal harus 1.000 port.
Terapkan manifes:
kubectlapply-fhost-port-pod.yaml
Saat Anda menerapkan manifes, GKE akan memilih port acak dari rentang Anda dan menetapkan port tersebut ke container Anda. Jika GKE menetapkan nilai port yang sama ke dua Pod, GKE akan otomatis menempatkan Pod di node terpisah untuk menghindari konflik port.
Memeriksa port yang ditetapkan
Untuk menemukan jumlah port yang ditetapkan GKE untuk container Anda, periksa Pod:
metadata.annotations.autopilot.gke.io/host-port-assignment: penetapan port, yang menunjukkan nilai asli untuk hostPort yang Anda tetapkan untuk setiap container dan nilai yang diperbarui untuk hostPort yang ditetapkan GKE. Kolom ini berguna jika Anda meminta beberapa port di spesifikasi Pod.
spec.containers.ports.hostPort: port terbuka yang ditetapkan GKE ke setiap container.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-01 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)."]]