# k8s: Essential commands

In this article, I'll show essential commands that you use every day to work with k8s cluster.

## Commands

### Object creation, updating and deleting commands

`kubectl run app --image=nginx` - Create and start a new pod that uses the `nginx` image

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699811108008/3aee1559-d294-479c-a455-019abc88fa09.png align="center")

`kubectl create deployment nginx-server --image=nginx` - Create and start a new deployment named `nginx-server` that uses `nginx:latest` image

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699811132803/0f527ba7-24e0-41f0-bbfd-0259af3a319f.png align="center")

`kubectl create -f pod-definition.yml` - Create resources that defined in the `pod-definition.yml` file

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699811876098/c6f6a906-cc7b-4562-bded-3af0c5f841bf.png align="center")

`kubectl delete -f pod-definition.yml` - Delete resources that defined in the `pod-definition.yml` file

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699811946018/c2cf41ad-31fd-42a4-866a-bdf0a18f3ae1.png align="center")

### Information gathering commands

`kubectl get pod` / `kubectl get po` - Get the list of all pods in the current namespace

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699811920356/f51eb55d-693d-4c15-b9ae-4b65e40d24e8.png align="center")

`kubectl get pod -o wide` - Get the list of all pods in the current namespace (with extra information)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699811993022/8c2c3cc3-4b3d-44b8-87c5-837ac729a89f.png align="center")

`kubectl get deployment` - Get the list of deployments in the current namespace

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812060815/e4aaf47a-0abb-48fa-ac93-9064c54e92ab.png align="center")

`kubectl get nodes` / `kubectl get no` - Get the list of all nodes

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812076898/002566c0-adda-4fab-a298-0eabdc452da5.png align="center")

`kubectl get all` - Get the list of main resource types (pod, deployment, replica set, service, horizontal pod autoscaler, ...) in the current namespace

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812112004/972fda23-faf9-43d6-a048-c23c6f9b3677.png align="center")

`kubectl get all -n app` - Get the list of main resource types (pod, deployment, replica set, service, horizontal pod autoscaler, ...) in the `app` namespace

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812214842/64dc3930-a9d1-447e-9938-0063d9ad2165.png align="center")

`kubectl get all -A` - Get the list of main resource types (pod, deployment, replica set, service, horizontal pod autoscaler, ...) in all namespaces

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812294560/d0ebb441-1209-4027-83b8-ccdf67c99978.png align="center")

`kubectl describe pod <POD_NAME>` - Show detailed information about pod

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812334966/d67ee3aa-7cb1-41f9-85d6-4a2991af65ef.png align="center")

`kubectl describe deployment <DEPLOYMENT_NAME>` - Show detailed information about deployment

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812381329/3ccdc3ea-29a2-41e9-b688-df80e8be5d34.png align="center")

`kubectl logs <OBJECT_NAME>` - Display logs of the pod (retrieves logs of the primary container if the pod has [multiple containers](https://mosh.hashnode.dev/k8s-multi-container-pods))

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812410866/5a8376e2-3648-4c1f-bc45-6055cbde15fa.png align="center")

`kubectl logs -f <OBJECT_NAME>` - Continuously retrieve logs of object. For multiple containers it retrieves logs from random pod (retrieves logs of the primary container if pod have [multiple containers](https://mosh.hashnode.dev/k8s-multi-container-pods))

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699812453607/65b2901a-aaeb-4929-9974-726f27c4f39d.png align="center")

---

## Example

Create a pod definition file (`backend-pod.yml`):

```yaml
# backend-pod.yml

apiVersion: v1
kind: Pod

metadata:
  name: nginx-from-file

spec:
  containers:
    - name: webserver
      image: nginx:latest
```

Then run `kubectl create -f ./backend-pod.yml` to create pod defined in `backend-pod.yml` file

Then you can see it in the list of pods by running `kubectl get pods` command

To get logs of the pod you can run `kubectl logs <POD_NAME>`

To get details of the pod you can run `kubectl describe pod <POD_NAME>`

To delete the pod you can use:

* By pod name - `kubectl delete pod <POD_NAME>`
    
* By file definition - `kubectl delete -f ./backend-pod.yml`
