kubectl Tips Flashcards
command to show all commands
kubectl | more
Enable command completion (autocomplete)
Kubectl source
true or false, command completion is always available after you enable it?
False: To enable command completion persistently you must add it to your .bash profile file
Command to add command completion to your bash profile file
echo “source > ~/.bash_profile
How to find CLI shortnames for resources
kubectl api-resources | more
second column will list shortname options.
How to find namespaces for pods?
Kubectl get pods –all-namespaces
How to find namespaces for pods with a specified label?
Kubectl get pods –all-namespaces -L
How to find namespaces for pods with a specified label and return only defined lables?
Kubectl get pods –all-namespaces -l
Explanation:
The -l is a lower case L instead of the uppercase. Lower case L will only return resources with defined labels, where upper case will return all, defined or not.
How to find namespaces for pods with a specified label and return only those with a specific label value
Kubectl get pods –all-namespaces -L -l =
Example:
Kubectl get pods –all-namespaces -L k8s-app -l k8s-app=kube-proxy
How to find namespaces for pods with a specified label and return only those that do NOT match a specified value
Kubectl get pods –all-namespaces -L -l !=
Explanation
!= means “does not equal” and therefore returns all values that do not equal
How to find namespaces for pods with a specified label and return based on multiple values
Join the values with commas.
ex. Kubectl get pods –all-namespaces -L -l !=,
What command allows you to sort kubectl get returns?
- -sort-by-
ex. kubctl get pods -n kube-system –sort-by=’{.metadata.creationTimestamp}’
how can you generate a manifest file template using kubectl?
using the create command with -o yaml –dry-run
explanation
–dry-run will will not create resources but will instead showcase what would be created. When you output as yaml with -o yaml, a manifest file is generated and displayed.
How can you create a manifest from existing resources?
use get and output ( -o ) to yaml.
example:
kubectl get pod -n kube-system jube-proxy-hwjjt -o yaml
When creating a manifest file from existing resources, how can you strip out cluster specific lines that you don’t want present in a generated manifest file?
add the –export option
example:
kubectl get pod -n kube-system jube-proxy-hwjjt -o yaml –export