Working with clusters (kubectl)
Once a cluster exists and kubectl has credentials, you drive it entirely
through kubectl. Know how the kubeconfig file and contexts work, the everyday
inspect / deploy / introspect commands, and the difference between imperative
kubectl create and declarative kubectl apply -f.
The kubeconfig file and contexts
gcloud container clusters get-credentials writes a kubeconfig file (default
~/.kube/config) holding the endpoint + auth details for a cluster, then sets it as the
active context. kubectl runs every command against whichever cluster the
current-context points to.
- A single kubeconfig (
~/.kube/config) can hold many clusters/contexts;current-contextmarks the one active cluster. - A GKE context name is
gke_PROJECT_LOCATION_NAME- thegkeprefix, project ID, location, and cluster display name, joined by underscores. kubectl config viewprints the file with certificate data replaced byDATA+OMITTED.
Clusters you created in the same context (same user, same environment) already have
their kubeconfig entry populated at creation time - no get-credentials needed. You
do run it to connect to a cluster created by another user or environment, or as
an easy way to switch the active context.
Inspecting a cluster
kubectl top returns "metrics not available yet" until the metrics-server has data -
just re-run it. Likewise a just-created pod may report "server is currently unable to
handle the request" until the deployment finishes and becomes Ready. On Autopilot,
a new pod can also show a transient FailedScheduling / TriggeredScaleUp event
while the cluster autoscales a node in to fit it - this is normal, not an error.
Deploying pods
A Pod groups one or more containers scheduled together as a unit. Deploy one imperatively, or declaratively from a manifest.
Imperative - kubectl create
With no registry in the image name, the image is pulled from Docker Hub (the default public registry).
Declarative - kubectl apply -f
The preferred way is a manifest (a YAML "config file"), which captures complex options far more readably than a long command line. YAML is more concise than JSON but gives the same hierarchical structure.
kubectl create is imperative and fine for a quick pod; kubectl apply -f with a
manifest is declarative - the desired state is documented in a file you can version,
review, and re-apply. This mirrors the declarative-first rule: reach for
manifests, use imperative commands for quick fixes.
Serving content and exposing a pod
You can copy a file straight into a running container, then expose the pod so external clients can reach it.
A LoadBalancer service shows <pending> in the EXTERNAL-IP column until GCP
provisions the load balancer - re-run kubectl get services a few times until the IP
appears. A pod needs a Service to be reachable from outside the cluster at all.
Introspecting a live pod
Use these for troubleshooting or experimenting only - changes made this way are not in the pod's source image, so they won't appear in replicas.
Editing files or installing tools inside a running container (via kubectl exec) changes
only that one live container, not the image. Any new replica starts from the original
image without your changes. Bake changes into the image (or a manifest) to make them
durable.
kubectl port-forward blocks the terminal while forwarding, so you need a second Cloud
Shell session to curl the pod. It also skips the need for a Service - handy for
testing a single pod directly without exposing it.