# Kubectl Cheatsheet

# **What is Kubectl?**

Before we jump into what commands we use, first we need to know what Kubectl is exactly.

> ***Kubectl is a command-line tool used to interact with Kubernetes clusters. It provides a wide range of commands to manage various aspects of the cluster, such as deploying applications, scaling resources, inspecting cluster state, and more.***

In layman's terms, Kubectl is a command-line tool used to manage Kubernetes clusters.

---

# Commands

Since we now know what Kubectl is, let's see what commands are used in managing Kubernetes clusters.

## 1\. kubectl get

Used to retrieve information about various Kubernetes resources.

Examples:

* `kubectl get pods`: Lists all the pods in the cluster.
    
* `kubectl get deployments`: Lists all the deployments in the cluster.
    

## 2\. kubectl describe

Describes a specific resource in detail.

Examples:

* `kubectl describe pod <pod_name>`: Displays detailed information about a specific pod.
    
* `kubectl describe service <service_name>`: Shows Detailed information about a specific service in the cluster.
    

## 3\. kubectl create

Creates a resource from a YAML or JSON file.

Example:

* `kubectl create -f deployment.yaml`: Creates a deployment using the configuration specified in the deployment.yaml file.
    

## 4\. kubectl apply

Applies changes to a resource or creates a new resource from a YAML or JSON file.

Example:

* `kubectl apply -f service.yaml`: Applies changes or creates a deployment based on the configuration in the service.yaml file.
    

## 5\. kubectl delete

Deletes a Resource \[Crazy I know🤯\]

Examples:

* `kubectl delete pod <pod_name>`: Deletes a specific pod.
    
* `kubectl delete deployment <deployment_name>`: Deletes a deployment.
    

## 6\. kubectl scale

Changes the number of replicas for a deployment, replicaset, or statefulset.

Example:

* `kubectl scale deployment <deployment_name> --replicas=3`: Scales a deployment to have three replicas.
    
* `kubectl scale deployment <deployment_name> --replicas=5`: Scales a deployment to have 5 replicas.
    

## 7\. kubectl logs

Prints all the logs of a specific container within a pod.

Example:

* `kubectl logs <pod_name>`: Displays the logs of the first container in a pod.
    
* `kubectl logs nginx`: Return snapshot logs from pod nginx with only one container.
    

## 8\. kubectl exec

Executes a command within a pod.

Example:

* `kubectl exec <pod_name> -- <command>`: Executes the specified command within a container of the given pod.
    
* `kubectl exec 123456-7890 -c ruby-container date`: Get output from running 'date' in ruby-container from pod [**123456-7890**](tel:1234567890).
    

## 9\. kubectl port-forward

Forward one or more local ports to a pod.

**Example:**

* `kubectl port-forward <pod_name> <local_port>:<pod_port>`: Forwards a local port to the specified pod and port.
    
* `kubectl port-forward mypod 8888:5000`: Listen on port 8888 locally, forwarding to 5000 in the pod.
    

## 10\. kubectl run

Used to run a particular image on the cluster.

**Example:**

* `kubectl run nginx --image=nginx`: Start a single instance of nginx.
    
* `kubectl run hazelcast --image=hazelcast --port=5701`: Start a single instance of hazelcast and let the container expose port 5701.
    

Happy Learning:)

Mohammed muqafamuddin
