# kubectl Commands with Examples

> URL: https://www.atlantic.net/dedicated-server-hosting/kubectl-commands-with-examples/ | Published: 2021-07-05 | Updated: 2023-11-21 | Author: Hitesh Jethva

Kubernetes is a free, open-source, and popular container orchestration tool developed by Google. It is used to manage and scale containerized infrastructure. The kubectl is a command-line utility used to query and manage a Kubernetes cluster. It uses the API interface to make this process more comfortable.

In this post, we will show you how to use the kubectl command to manage a Kubernetes cluster.

## Step 1 – Install Kebectl

By default, kubectl automatically installed during the Kubernetes cluster setup. If you want to manage the Kubernetes cluster from the remote system, then you can install the kubectl using the following commands:

```
apt-get update -y
wget https://dl.k8s.io/release/v1.21.2/bin/linux/amd64/kubectl -O /usr/bin/kubectl
chmod +x /usr/bin/kubectl
```

Next, check your kubectl setup using the following command:

```
kubectl version --client
```

## Step 2 – Create and List Resources

To get a list of all namespaces, run the following command:

```
kubectl get namespaces
```

To generate a list of all daemon sets, run the following command:

```
kubectl get daemonset
```

To list all pods, run the following command:

```
kubectl get pods
```

To get a list of all pods with detailed information, run the following command:

```
kubectl get pods -o wide
```

To list a specific replication controller, run the following command:

```
kubectl get replicationcontroller "replication-controller-name"
```

To list all replication controllers and services, run the following command:

```
kubectl get replicationcontroller,services
```

You can use “kubectl create” command to create a resource such as a service, a deployment, a job, or a namespace.

For example, to create a new namespace, run the following command:

```
kubectl create namespace namespace-name
```

To create a resource from a JSON or YAML file, run the following command:

```
kubectl create -f [filename]
```

## Step 3 – Update a Resource with kubectl

You can use the kubectl apply command to apply or update a resource.

To create a new replication controller or a service with the definition contained in filename.yaml, run the following command:

```
kubectl apply -f filename.yaml
```

To create the object defined in the filename.yaml file located inside any directory, run the following command:

```
kubectl apply -f directory-name
```

You can use the kubectl edit command to edit any resource file.

For example, to edit a service, run the following command:

```
kubectl edit service-name
```

## Step 4 – Check the Status of Resources

You can use the kubectl describe command to check the status of any resources.

To check the status of any node, run the following command:

```
kubectl describe nodes nodename
```

To check the status of any pod, run the following command:

```
kubectl describe pods pod-name
```

To check the status of any pod listed in the pod.json file, run the following command:

```
Kubectl describe -f pod.json
```

To display detailed information of all pods, run the following command:

```
kubectl describe pods
```

## Step 5 – Delete a Resource with kubectl

You can use the kubectl delete command to delete any resources.

To remove a pod listed in the pod.yaml file, run:

```
kubectl delete -f pod.yaml
```

To remove all pods, run:

```
kubectl delete pods --all
```

## Step 6 – Modify kubeconfig Files

To display kubectl’s current configuration, run:

```
kubectl config view
```

To list all available contexts, run:

```
kubectl config get-contexts
```

To get the current context for kubectl, run:

```
kubectl config current-context
```

To set a cluster entry in kubeconfig, run:

```
kubectl config set-cluster [cluster-name] --server=[server-name]
```

You can use the kubectl logs command to print logs from containers in a pod.

```
kubectl logs "pod-name"
```

## Conclusion

In the above guide, you learned how to use the kubectl command to manage the Kubernetes effectively, Refer to the [kubectl official documentation](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands) for more information. Try it today on [dedicated server hosting](https://www.atlantic.net/dedicated-server-hosting/) from Atlantic.Net.
