## Introduction to Kubectl Command Line

> Since viewing cluster credentials allows direct login to the cluster, the operation of viewing cluster credentials has been categorized as an "Add Permission" in user role permissions. To view cluster credentials, ensure that the role you are in has enabled UK8S Add Permission.

kubectl is a command-line tool for operating Kubernetes clusters. This article will briefly introduce the syntax of kubectl and provide some common command examples. If you want to understand kubectl usage in depth, please refer to the official documentation [kubectl overview](https://kubernetes.io/docs/reference/kubectl/overview/), or use kubectl
help command for detailed help. For installing kubectl, please check [Installation and Configuration of kubectl](/docs/uk8s/cluster/manageviakubectl/connectviakubectl).

### kubectl Syntax

The syntax example for kubectl is as follows:

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

**command:** Command refers to the operation you want to perform on some resources. Regular commands include create, get, describe, delete, etc.

**TYPE:** TYPE defines the resource type that command needs to operate on. TYPE is case-insensitive, insensitive to singular and plural, and supports abbreviations. The following commands are all feasible and equivalent:

```bash
kubectl get pod 
kubectl get pods
kubectl get po
kubectl get POD
```

**NAME:** NAME refers to the name of the resource and NAME is case sensitive. If you don't specify the NAME of a resource, all resources will be displayed. A command like **kubectl get pods** will display all the pods under the Default namespace.

You can also get the details of multiple resources at the same time, such as getting the details of one or multiple types of resources:

```bash
kubectl get pods pod1 pod2
```

```bash
kubectl get pod/example-pod1 replicationcontroller/example-rc1
```

**flags：**
Optional parameter. For example, you can use all-namespaces to get resource objects under all namespaces. For usage of flags in each command, please refer to [kubectl command](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands)

Note: Flags specified on the command line will override default values and any corresponding environment variables.

For more details about kubectl Syntax, you can use kubectl help.

### Common Commands

**kubectl create** - Create resources using a file or standard input.

```bash
# Create a "service" object using the exampe-service.yaml file
$ kubectl create -f example-service.yaml

# Create a "replication" object using the example-controller.yaml file
$ kubectl create -f example-controller.yaml
```

**kubectl describe** - Get detailed status of resources, including those being initialized.

```bash
# View the details of the node named <node-name>
$ kubectl describe nodes <node-name>

# View the details of the pod named <pod-name>, including the creation log of the pod
$ kubectl describe pods/<pod-name>

# View all pods managed by replication named <rc-name>.
# Note: Any pod created by the replication controller, the prefix of its name is the replication name.
$ kubectl describe pods <rc-name>

# View all pods, but not including uninitialized pods
$ kubectl describe pods --include-uninitialized=false
```

**kubectl logs** - Get logs of a pod

```bash
# Get a snapshot of a pod's logs
$ kubectl logs <pod-name>

# Get a real-time log stream of a pod, similar to the 'tail -f' in linux
$ kubectl logs -f <pod-name>
```

**kubectl exec** - Execute commands in a pod container

```bash
# Get output of "date" command from a pod, and by default, it is from the first container in the pod.
$ kubectl exec <pod-name> date

# Get output of "date" command from a specified container in the pod
$ kubectl exec <pod-name> -c <container-name> date

# Get an interactive TTY (control terminal) from a pod, and execute /bin/bash
$ kubectl exec -ti <pod-name> /bin/bash
```
