Question

Difficulty: MediumDeploying and Managing Google Kubernetes Engine (GKE) Clusters and Workloads

A DevOps engineer creates a new Google Kubernetes Engine (GKE) cluster named `prod-cluster` in the `us-central1` region. From a newly provisioned administrator workstation, the engineer attempts to deploy a application workload using `kubectl apply -f deployment.yaml`. However, the command fails with an error indicating that the connection to server `localhost:8080` was refused. Which action should the engineer take to allow `kubectl` to successfully communicate with the GKE cluster?

  1. Execute `gcloud container clusters get-credentials prod-cluster --region us-central1` to update the local kubeconfig file with cluster entry details and authentication credentials.Answer
  2. B
    Execute `gcloud config set container/cluster prod-cluster` to register the default cluster target in the gcloud configuration settings.
  3. C
    Recreate the cluster as a GKE Autopilot cluster because Standard clusters require starting a local API proxy daemon before running kubectl commands.
  4. D
    Modify the deployment manifest to specify Spot VMs using node selectors so that kubectl can automatically locate available compute nodes.

Answer

Execute `gcloud container clusters get-credentials prod-cluster --region us-central1` to retrieve the cluster kubeconfig credentials.
The option directing the user to run `gcloud container clusters get-credentials prod-cluster --region us-central1` is correct because `kubectl` relies on the local `kubeconfig` file to locate and authenticate against the GKE API server endpoint. Fetching credentials via `gcloud` writes the necessary context and authorization tokens to `kubeconfig`.

Step-by-Step Solution

1
Identify the cause of the `localhost:8080` connection error.
Recognize that `kubectl` defaults to `localhost:8080` when no cluster context or API server endpoint is configured in the workstation's `~/.kube/config` file.
Before `kubectl` can send requests to a remote GKE API server, it requires cluster certificates and endpoint information.
2
Run the `gcloud container clusters get-credentials` command specifying the cluster name and region.
The command queries GKE endpoints, retrieves cluster credentials, and populates an entry in the local `kubeconfig` file.
This configures the active context for `kubectl` to target the GKE cluster control plane.
3
Re-run `kubectl apply -f deployment.yaml`.
The deployment manifest is submitted directly to the GKE control plane and resources are created.
The authenticated request reaches the correct cluster control plane URL.

Key Concept

Configuring kubectl credentials for GKE cluster management using gcloud
Rate this question