⎈ k8s knowledge compiler

Command line tool (kubectl) [page]deterministic

reference

[definition:kubectl]

This tool is named `kubectl`.

For configuration, `kubectl` looks for a file named `config` in the `$HOME/.kube` directory. You can specify other [kubeconfig](/docs/concepts/configuration/organize-cluster-access-kubeconfig/) files by setting the `KUBECONFIG` environment variable or by setting the [`--kubeconfig`](/docs/concepts/configuration/organize-cluster-access-kubeconfig/) flag.

This overview covers `kubectl` syntax, describes the command operations, and provides common examples. For details about each command, including all the supported flags and subcommands, see the [kubectl](/docs/reference/kubectl/generated/kubectl/) reference documentation.

For an overview, see [The kubectl command-line tool](/docs/concepts/overview/kubectl/). For installation instructions, see [Installing kubectl](/docs/tasks/tools/#kubectl); for a quick guide, see the [cheat sheet](/docs/reference/kubectl/quick-reference/). If you're used to using the `docker` command-line tool, [`kubectl` for Docker Users](/docs/reference/kubectl/docker-cli-to-kubectl/) explains some equivalent commands for Kubernetes.

## Syntax

Use the following syntax to run `kubectl` commands from your terminal window:

```shell kubectl [command] [TYPE] [NAME] [flags] ```

where `command`, `TYPE`, `NAME`, and `flags` are:

* `command`: Specifies the operation that you want to perform on one or more resources, for example `create`, `get`, `describe`, `delete`.

* `TYPE`: Specifies the [resource type](#resource-types). Resource types are case-insensitive and you can specify the singular, plural, or abbreviated forms. For example, the following commands produce the same output:

```shell kubectl get pod pod1 kubectl get pods pod1 kubectl get po pod1 ```

* `NAME`: Specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed, for example `kubectl get pods`.

When performing an operation on multiple resources, you can specify each resource by type and name or specify one or more files:

* To specify resources by type and name:

* To group resources if they are all the same type: `TYPE1 name1 name2 name<#>`.<br/> Example: `kubectl get pod example-pod1 example-pod2`

* To specify multiple resource types individually: `TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE<#>/name<#>`.<br/> Example: `kubectl get pod/example-pod1 replicationcontroller/example-rc1`

* To specify resources with one or more files: `-f file1 -f file2 -f file<#>`

* Use YAML rather than JSON since YAML tends to be more user-friendly, especially for configuration files.<br/> Example: `kubectl get -f ./pod.yaml`

* `flags`: Specifies optional flags. For example, you can use the `-s` or `--server` flags to specify the address and port of the Kubernetes API server.<br/>

> 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.

## In-cluster authentication and namespace overrides

By default `kubectl` will first determine if it is running within a pod, and thus in a cluster. It starts by checking for the `KUBERNETES_SERVICE_HOST` and `KUBERNETES_SERVICE_PORT` environment variables and the existence of a service account token file at `/var/run/secrets/kubernetes.io/serviceaccount/token`. If all three are found in-cluster authentication is assumed.

To maintain backwards compatibility, if the `POD_NAMESPACE` environment variable is set during in-cluster authentication it will override the default namespace from the service account token. Any manifests or tools relying on namespace defaulting will be affected by this.

`POD_NAMESPACE` environment variable

If the `POD_NAMESPACE` environment variable is set, cli operations on namespaced resources will default to the variable value. For example, if the variable is set to `seattle`,

…(trimmed)

Sources

reference/kubectl/_index.md · docCommand line tool (kubectl)

Related (11)

defines Kubectl conf=1
part_of Syntaxdescribes conf=1
part_of Operationsdescribes conf=1
part_of Resource typesdescribes conf=1
part_of Output optionsdescribes conf=1
part_of Examples: Common operationsdescribes conf=1
part_of Examples: Creating and using pluginsdescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of Formatting outputdescribes conf=1
part_of Sorting list objectsdescribes conf=1

← all Docs