0
0
GCPcloud~5 mins

Kubectl configuration for GKE in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create a Kubernetes cluster on Google Cloud, you need to connect your computer to it. Kubectl configuration helps your computer talk to the cluster so you can manage it easily.
When you want to deploy an app to your Google Kubernetes Engine cluster from your laptop.
When you need to check the status of your pods and services running in GKE.
When you want to update or delete resources in your GKE cluster using your local terminal.
When you want to switch between multiple Kubernetes clusters on Google Cloud.
When you want to run commands like scaling or rolling updates on your GKE workloads.
Commands
This command fetches the access credentials for your GKE cluster and updates your local kubectl configuration so you can control the cluster.
Terminal
gcloud container clusters get-credentials example-cluster --zone us-central1-a --project example-project
Expected OutputExpected
Fetching cluster endpoint and auth data. kubeconfig entry generated for example-cluster.
--zone - Specifies the zone where your GKE cluster is located.
--project - Specifies the Google Cloud project containing the cluster.
This command lists all the worker machines (nodes) in your GKE cluster to confirm your connection is working.
Terminal
kubectl get nodes
Expected OutputExpected
NAME STATUS ROLES AGE VERSION example-cluster-node-1 Ready <none> 10m v1.26.3-gke.1000
This command shows which Kubernetes cluster your kubectl is currently set to control.
Terminal
kubectl config current-context
Expected OutputExpected
gke_example-project_us-central1-a_example-cluster
Key Concept

If you remember nothing else from this pattern, remember: use 'gcloud container clusters get-credentials' to set up kubectl access to your GKE cluster.

Common Mistakes
Running kubectl commands before fetching credentials with gcloud.
kubectl won't know which cluster to talk to and will give errors.
Always run 'gcloud container clusters get-credentials' first to configure kubectl.
Using wrong zone or project values in the get-credentials command.
The command will fail or configure kubectl for a non-existent cluster.
Double-check your cluster's zone and project ID before running the command.
Summary
Use 'gcloud container clusters get-credentials' to configure kubectl for your GKE cluster.
Verify connection by listing nodes with 'kubectl get nodes'.
Check current cluster context with 'kubectl config current-context'.