GCP
GKE
Kubernetes
Cloud Computing
Command Execution

execute command on GCP GKE node

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A GKE node is a Compute Engine VM, so the classic way to execute a command on it is to SSH into the underlying instance or use gcloud compute ssh --command. The right method depends on your cluster mode, though, because direct node access is not always available or desirable.

For standard GKE clusters you can usually reach the node VM. For Autopilot clusters, node-level access is intentionally restricted, so debugging has to happen through Kubernetes-native tools instead.

Find The Node You Want

Start by listing the Kubernetes nodes:

bash
kubectl get nodes -o wide

That gives you the node names and internal IP addresses. In GKE standard mode, the node name usually matches the backing Compute Engine instance name closely enough for SSH-based workflows.

If you need the exact VM details, you can also list instances with gcloud compute instances list in the relevant project and zone.

Run A Command Through gcloud compute ssh

For a standard GKE node, this is the simplest non-interactive approach:

bash
gcloud compute ssh gke-cluster-node-name \
  --zone us-central1-b \
  --command "uname -a"

That opens an SSH session behind the scenes, runs the command, prints the output, and exits.

You need the right IAM permissions for SSH or OS Login, and the node must be reachable according to your project settings.

Interactive SSH Session

If you need a full shell rather than a one-off command, omit --command:

bash
gcloud compute ssh gke-cluster-node-name --zone us-central1-b

Once connected, you can inspect logs, disk usage, container runtime state, or system processes. Be careful with manual changes, because nodes in managed node pools can be recreated at any time.

Kubernetes-Native Debugging Alternatives

Often you do not actually need SSH. If the goal is troubleshooting, Kubernetes-native debugging is safer and more reproducible. One option is kubectl debug on a node.

bash
kubectl debug node/gke-cluster-node-name -it --image=ubuntu

That launches a privileged debug pod associated with the node so you can inspect the host environment without managing direct SSH access yourself.

This approach is especially useful in environments where security policy discourages node login.

Autopilot Caveat

In GKE Autopilot, direct node management is intentionally abstracted away. You generally cannot rely on SSH access because the platform does not expose nodes as customer-managed VMs in the same operational sense.

If you are on Autopilot, prefer:

  • 'kubectl debug'
  • logs and metrics
  • container-level troubleshooting inside pods

Trying to force node-level workflows in Autopilot usually means fighting the platform design.

Example Operational Checks

Once you have access, these are common diagnostic commands:

bash
gcloud compute ssh gke-cluster-node-name \
  --zone us-central1-b \
  --command "df -h && free -m && sudo journalctl -u kubelet -n 50"

That combines disk usage, memory usage, and recent kubelet logs into one inspection pass.

Security And Operational Risks

Direct node access is powerful, but it can bypass the normal declarative Kubernetes model. If you manually change a package, config file, or runtime setting on a node, that change may disappear when the node is replaced or upgraded.

Treat node commands as inspection or emergency repair, not as a long-term configuration method. Permanent configuration belongs in node pool settings, DaemonSets, or infrastructure automation.

Common Pitfalls

  • Trying to SSH into an Autopilot cluster node as if it were a standard GKE node.
  • Forgetting the correct zone when using gcloud compute ssh.
  • Making manual host changes that are lost when the node is recreated.
  • Using node access when a pod-level or kubectl debug workflow would be safer.
  • Missing IAM or OS Login permissions and assuming the node itself is broken.

Summary

  • On standard GKE, use gcloud compute ssh --command for one-off commands on a node.
  • Use a full SSH session only when you need deeper host inspection.
  • Prefer Kubernetes-native debugging tools when possible.
  • On Autopilot, assume direct node access is restricted and use platform-supported debugging paths.
  • Treat node changes as temporary unless they are codified in your infrastructure.

Course illustration
Course illustration

All Rights Reserved.