Skip to content

Instantly share code, notes, and snippets.

@ngrogg
Last active March 27, 2024 09:58
Show Gist options
  • Save ngrogg/db3c0dbc437a625cbde0536c512034d2 to your computer and use it in GitHub Desktop.
Save ngrogg/db3c0dbc437a625cbde0536c512034d2 to your computer and use it in GitHub Desktop.
Some useful kubectl commands

Some useful kubectl commands

Authenticate with cluster if using gcloud CLI,
gcloud container clusters get-credentials CLUSTER_NAME --region=COMPUTE_REGION

Run again with different value to set a new default cluster

List pods in cluster,
kubectl get pods

Search for a specific pod with grep,
kubectl get pods | grep POD

List public IPs, and URLs,
kubectl get ingress

Copy to pod,
kubectl cp /path/to/file POD:/path/to/destination

Copying from pod is same command in reverse,
kubectl cp POD:/path/to/file /path/to/destination

Copy with only partial match on pod name,
kubectl cp /path/to/file $(kubectl get pods | grep POD | awk '{print $1}'):/path/to/destination

Useful for cases where pods are regularly destroyed and re-deployed with extrenuous names.
i.e.
mypod-prod-1987y671-2io87

Would just have to remember mypod-prod.

Connect to pods,
kubectl exec -it POD -- /bin/bash

Can also match with partials like cp command,
kubectl exec -it $(kubectl get pods | grep POD | awk '{print $1}') -- /bin/bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment