Useful kubectl commands
A set of commands that will make you more efficient at working with Kubernetes.
There is a set of bash aliases that help to provide some quick kubectl commands:
alias kubecontext='kubectl config get-contexts'
alias kubepods='kubectl get pods'
alias kubelogs='kubectl logs -f'
alias kubesvcs='kubectl get svc'
alias kubedpls='kubectl get deployments'
alias kube-set-context='kubectl config use-context'
alias kube-clear-context='kubectl config unset current-context'We recommend adding those to your source. We will be using these aliases in the remainder of this section.
If you are accessing multiple Kubernetes clusters (like our staging and production), you need to repeat the Connect to the cluster section for each cluster to configure your
kubectlto access both clusters - but to switch between both while they are already configured, you should use these commands:
# This will show you your currently configured clusters, and the one with a star (*) next to it is the one that is currently active and used by kubectl
kubecontext
# output should be similar to this:
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* arn:aws:eks:eu-central-1:359905442998:cluster/crowd-kube-staging arn:aws:eks:eu-central-1:359905442998:cluster/crowd-kube-staging arn:aws:eks:eu-central-1:359905442998:cluster/crowd-kube-staging
arn:aws:eks:eu-central-1:359905442998:cluster/crowd-kube-production arn:aws:eks:eu-central-1:359905442998:cluster/crowd-kube-production arn:aws:eks:eu-central-1:359905442998:cluster/crowd-kube-production
# To switch between contexts, use this command: (this one will switch to the crowd-kube-staging cluster)
kube-set-context arn:aws:eks:eu-central-1:359905442998:cluster/crowd-kube-staging
# from this point on all kubectl command will be accessing this clusterInstead of doing
kubectl get podsjust dokubepodsand it will print the same thing - a list of service instances running inside KubernetesTo get logs from a pod, you first should get the name of the pod with
kubepodsor if you havekubectlautocomplete setup. You can also dokubelogs job-generatorand press tab for autocomplete to fill you in with the rest of the pod name - the final command should look like this:kubelogs job-generator-dpl-64df888968-z4klrTo get shell access within a running pod, you can execute this:
kubectl exec --stdin --tty job-generator-dpl-64df888968-z4klr -- /bin/shreplacing the pod name with the one you want (in this case, it wasjob-generator-dpl-64df888968-z4klr, also depending on the docker image that was used to start the pod, you will have to use/bin/shinstead of/bin/bashfor a shell sincealpineimages donβt havebashshell included./bin/shworks just as well, although it doesnβt have all the tools thatbashoffers you, plus some commands are different - for example, sourcing a file with bash is justsource filewithshyou have to do. file
Last updated
Was this helpful?