Command Line Tool (Kubectl)
Command Line Tool (Kubectl)
Syntax
Use the following syntax to run kubectl commands from your terminal
window:
Caution: Flags that you specify from the command line override
default values and any corresponding environment variables.
If you need help, run kubectl help from the terminal window.
If:
Operations
Resource types
Output options
Use the following sections for information about how you can format
or sort the output of certain commands. For details about which
commands support the various output options, see
the kubectl reference documentation.
Formatting output
The default output format for all kubectl commands is the human
readable plain-text format. To output details to your terminal
window in a specific format, you can add either the -o or --
output flags to a supported kubectl command.
Syntax
Example
Custom columns
To define custom columns and output only the details that you want
into a table, you can use the custom-columns option. You can choose
to define the custom columns inline or use a template file: -o
custom-columns=<spec> or -o custom-columns-file=<filename>.
Examples
Inline:
NAME RSRC
metadata.name metadata.resourceVersion
The result of running either command is similar to:
NAME RSRC
submit-queue 610995
Server-side columns
Examples
NAME AGE
pod-name 1m
Syntax
Example
# Create the objects that are defined in any .yaml, .yml, or .json
file within the <directory> directory.
kubectl apply -f <directory>
kubectl get - List one or more resources.
# Display the details of all the pods that are managed by the
replication controller named <rc-name>.
# Remember: Any pods that are created by the replication controller
get prefixed with the name of the replication controller.
kubectl describe pods <rc-name>
# Delete a pod using the type and name specified in the pod.yaml
file.
kubectl delete -f pod.yaml
# Delete all the pods and services that have the label '<label-
key>=<label-value>'.
kubectl delete pods,services -l <label-key>=<label-value>
/usr/local/bin/kubectl-hello
/usr/local/bin/kubectl-foo
/usr/local/bin/kubectl-bar
kubectl plugin list also warns you about plugins that are not
executable, or that are shadowed by other plugins; for example:
/usr/local/bin/kubectl-hello
/usr/local/bin/kubectl-foo
- warning: /usr/local/bin/kubectl-foo identified as a plugin, but
it is not executable
/usr/local/bin/kubectl-bar
cat ./kubectl-whoami
The next few examples assume that you already made kubectl-
whoami have the following contents:
#!/bin/bash
# this plugin makes use of the `kubectl config` command in order to
output
# information about the current user, based on the currently
selected context
kubectl config view --template='{{ range .contexts }}{{ if eq .name
"'$(kubectl config current-context)'" }}Current user: {{ printf "%s\
n" .context.user }}{{ end }}{{ end }}'
Running the above command gives you an output containing the user
for the current context in your KUBECONFIG file:
kubectl whoami
Current user: plugins-user
What's next