Skip to content

Instantly share code, notes, and snippets.

@arvindkgs
Last active March 5, 2021 07:18
Show Gist options
  • Save arvindkgs/de85f3883cb056fa6f1c46e1c9ebd485 to your computer and use it in GitHub Desktop.
Save arvindkgs/de85f3883cb056fa6f1c46e1c9ebd485 to your computer and use it in GitHub Desktop.
[Kubernetes] Kubernetes commands #kubecl

Common kubectl commands


debug

  1. Open shell to debug cluster
    kubectl run ubuntu -it --image=ubuntu --restart=Never --rm

log

  1. Log from current instead of from beginning
    kubectl logs -f --tail 0 pod

format

  1. Get only pod status
    kubectl get pods -o=jsonpath='{.items[*].status.phase}'

Context

  1. To configure access to multiple clusters, you need to have multiple kubeconfig files.

Each cluster is configured to map to a single context
To add a particular context, you

  • Add cluster open corresponding kubeconfig file and copy values: certificate-authority-data, server

    Run command
    kubectl config set-cluster <cluster-name> --server=<server> --certificate-authority-data=<certificate-authority-data

    For example: kubectl config set-cluster cluster-name --server=https://170839116A56374EF061B65D0E47872F.gr7.us-west-2.eks.amazonaws.com --certificate-authority-data=LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS

  • Add user
    open corresponding kubeconfig file and copy values: token

    Run command
    kubectl config set-credentials <user-name> --token=<server>

    For example: kubectl config set-credentials user-name --token=eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdW....

  • Add context Link cluster and user to a context by running command kubectl config set-context <context-name> --cluster <cluster-name> --user=<user-name>

    you can also set optional namespace as ' --namespace=' kubectl config set-context context-name --cluster cluster-name --namespace=myspace --user=user-name

Grep and exec

alias sh_pod='kubectl exec -it $(kubectl get pods | grep $POD | awk '"'"'{print $1}'"'"') -- /bin/bash'

Copy files to pod

kubectl cp local-file pod-name:location-in-pod

Copy files from pod

kubectl cp pod-name:location-in-pod local-file

List pods belonging to specific deployment

kubectl get pods --selector=app.kubernetes.io/instance=[deployment] kubectl get pods --selector=app.kubernetes.io/name=[deployment-name]

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