[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-03。"],[],[],null,["# Configure cluster access for kubectl\n\nThis page explains how to configure cluster access for the kubectl command-line\ntool for GKE on AWS.\n\nThis page is for IT administrators and Operators who want to set up,\nmonitor, and manage cloud infrastructure. To learn more about common roles and\nexample tasks that we reference in Google Cloud content, see\n[Common GKE user roles and tasks](/kubernetes-engine/enterprise/docs/concepts/roles-tasks).\n\nOverview\n--------\n\nTo access your GKE on AWS, you need to configure `kubectl`. You can\nset a default cluster for `kubectl` by setting the current context in\nKubernetes' `kubeconfig` file. Additionally, you can run `kubectl` commands\nagainst a specific cluster using the `--cluster` flag.\n\nFor more information about `kubeconfig` and cluster endpoints, see\n[About Kubeconfig](/kubernetes-engine/multi-cloud/docs/aws/concepts/about-kubeconfig).\n\nBefore you begin\n----------------\n\nIf you use the [Connect gateway](/anthos/multicluster-management/gateway) for\n`kubectl` access, you must grant admin users who aren't project owners the\nIAM roles described in\n[Use kubectl with identity from the gcloud CLI](/kubernetes-engine/multi-cloud/docs/aws/how-to/connect-and-authenticate-to-your-cluster#use-kubectl-with-identity-from-the-gcloud-cli).\n\nCheck current kubeconfig\n------------------------\n\nThis section explains how to check your current `kubeconfig`.\n\n### View kubeconfig contexts\n\nTo view your environment's `kubeconfig` and all of the contexts it contains,\nrun the following command: \n\n kubectl config view\n\nThe command returns a list of all clusters for which `kubeconfig` entries have\nbeen generated. If a GKE on AWS cluster is listed, you can run `kubectl`\ncommands against it in your current environment. Otherwise, you need to\n[generate a `kubeconfig` entry](#generate_kubeconfig_entry) for the cluster.\n\n### View the current context for kubectl\n\nTo view the current context that the kubectl command-line tool uses, run the\nfollowing command: \n\n kubectl config current-context\n\n`kubectl` outputs the name of the current context.\n\nGenerate a kubeconfig entry\n---------------------------\n\nTo run `kubectl` commands against GKE clusters, you need to generate a\n`kubeconfig` entry. How you generate a `kubeconfig` file depends on how you\nwant to connect to your cluster. We recommend that you use the\n[Connect gateway](/anthos/multicluster-management/gateway). When you use the\nConnect gateway, the `gke-connect-agent` Deployment sends requests through\nGoogle to the cluster control plane.\n\nTo check which `kubectl` commands are supported by the Connect gateway, see\n[Limitations](/kubernetes-engine/enterprise/multicluster-management/gateway/using#limitations).\n\nIf you want to connect through a\n[private endpoint](/kubernetes-engine/multi-cloud/docs/aws/concepts/about-kubeconfig#cluster_endpoint), you\nneed to have a connection and routing to the control plane endpoint in your\nAWS Virtual Private Cloud. The cluster's\n[kube-apiserver](https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/)\nis part of the control plane. In order to send requests directly to the private\nendpoint, you might need to widen firewall rules. For more information on\nconnecting to your control plane, see\n[AWS security groups](/kubernetes-engine/multi-cloud/docs/aws/reference/security-groups). \n\n### Connect gateway\n\n gcloud container aws clusters get-credentials \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e \\\n --location \u003cvar translate=\"no\"\u003eGOOGLE_CLOUD_LOCATION\u003c/var\u003e\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e: the name of your cluster\n- \u003cvar translate=\"no\"\u003eGOOGLE_CLOUD_LOCATION\u003c/var\u003e: the Google Cloud region or zone where your GKE on AWS are managed\n\n### Private endpoint\n\nTo connect to the private endpoint, you need connectivity to the\ncluster's control plane. See\n[Connect to your cluster's control plane](/kubernetes-engine/multi-cloud/docs/aws/how-to/connect-and-authenticate-to-your-cluster#connect-to-vpn). \n\n gcloud container aws clusters get-credentials \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e \\\n --location \u003cvar translate=\"no\"\u003eGOOGLE_CLOUD_LOCATION\u003c/var\u003e \\\n --private-endpoint\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e: the name of your cluster\n- \u003cvar translate=\"no\"\u003eGOOGLE_CLOUD_LOCATION\u003c/var\u003e: the Google Cloud region or zone where your GKE on AWS are managed\n\nWork with multiple clusters\n---------------------------\n\nThis section describes how to use `kubectl` with multiple GKE clusters.\n\n### Set a default cluster for kubectl commands\n\nYou can switch the current context for `kubectl` to that cluster by running: \n\n gcloud container aws clusters get-credentials \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e \\\n --location \u003cvar translate=\"no\"\u003eGOOGLE_CLOUD_LOCATION\u003c/var\u003e \\\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eCLUSTER_NAME\u003c/var\u003e: the name of your cluster\n- \u003cvar translate=\"no\"\u003eGOOGLE_CLOUD_LOCATION\u003c/var\u003e: the Google Cloud region or zone where your GKE on AWS are managed\n\nFor example, consider a project with two clusters, `cluster-1` and `cluster-2`.\nThe current context is `cluster-2`. To switch the current context from\n`cluster-2` to `cluster-1`, run the following command: \n\n gcloud container aws clusters get-credentials cluster-1 \\\n --location \u003cvar translate=\"no\"\u003eGOOGLE_CLOUD_LOCATION\u003c/var\u003e\n\n### Run individual kubectl commands against a specific cluster\n\nYou can run individual `kubectl` commands against a specific cluster by passing\nin the name of the cluster\n[as it appears in `kubeconfig`](#view_kubeconfig) as the argument to the\n`--cluster` flag.\n\nFor example, consider an environment with two clusters, `cluster-1` and\n`cluster-2`, in which the current context is `cluster-1`. You don't want to\nchange the current context. To get a list of Pod objects on `cluster-2` without\nchanging the current context, run the following command: \n\n kubectl get pod --cluster cluster-2"]]