Question

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

A software engineer on your team is attempting to deploy a new stateless API service to an existing Google Kubernetes Engine (GKE) cluster named `api-cluster` located in region `us-central1`. When executing `kubectl apply -f deployment.yaml` on a newly provisioned workstation, `kubectl` fails with an error indicating that it cannot connect to a cluster API server at `localhost:8080`. Which action should the engineer take to resolve this issue and successfully deploy the workload?

  1. Execute `gcloud container clusters get-credentials api-cluster --region us-central1` to update the local kubeconfig file with the cluster credentials and endpoint details.Answer
  2. B
    Execute `gcloud config set container/cluster api-cluster` to set the active cluster destination in the global gcloud CLI properties.
  3. C
    SSH directly into the managed cluster control plane master node to execute `kubectl apply -f deployment.yaml` from within the control plane environment.
  4. D
    Add a new Spot VM node pool to the cluster and assign the workload service account to it to bypass cluster API authentication.

Answer

Execute `gcloud container clusters get-credentials api-cluster --region us-central1` to populate the local kubeconfig file with the correct GKE API server endpoint and credentials.
The correct option executes `gcloud container clusters get-credentials api-cluster --region us-central1`. This command retrieves authentication details and API endpoint information for the specified GKE cluster and configures the local `kubeconfig` file so that `kubectl` can target the cluster.

Step-by-Step Solution

1
Identify the cause of the `kubectl` connection error
Recognize that `kubectl` defaults to `localhost:8080` when no valid context or kubeconfig entry exists for the target cluster.
Before `kubectl` can interact with a GKE cluster, the local environment must have an active context pointing to the cluster's API server.
2
Generate cluster kubeconfig credentials using gcloud
Run `gcloud container clusters get-credentials api-cluster --region us-central1`.
This command securely retrieves the cluster's endpoint and generates authentication tokens, writing them into `~/.kube/config`.
3
Deploy the workload manifest
Execute `kubectl apply -f deployment.yaml` successfully.
With valid cluster credentials and context active, `kubectl` sends the API requests directly to the GKE control plane.

Key Concept

Fetching GKE Cluster Kubeconfig Credentials
Rate this question