KUBERNETES FROM ZERO TO HERO (PART 3) - INSTALLING MINIKUBE, KUBECTL AND KUBECTL COMMANDS
Tags: beginners, devops, kubernetes, tutorial
Published: 2022-12-23 12:01:30


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft80xfcx4wa98nj9ghq2.jpeg)
Minikube is a tool that allows you to run a single-node Kubernetes cluster locally, on your own machine. It is designed to be used for testing, development, and learning purposes, as it allows you to run a full-featured Kubernetes cluster on your laptop or desktop.

Minikube uses the popular open-source hypervisor VirtualBox to create a virtual machine (VM) on which it runs the Kubernetes control plane and other components. It also uses the kubectl command-line interface (CLI) to manage the cluster and deploy applications.

To install Minikube, you need to follow these steps:

Install a hypervisor, such as VirtualBox and Hyper-V for Windows or Hyper-kit if on MacOS, on your machine.

Download and install the minikube CLI on your machine. This can be done using a package manager, such as apt, yum, or brew, depending on your operating system.

Run the minikube start command to create and start the Minikube cluster. By default, this will create a cluster with one node running in a VM on your machine.

Run the kubectl get nodes command to verify that the cluster is running and the node is ready.

Use the kubectl CLI to deploy applications to the cluster and manage the resources in the cluster.

---

There are several ways to communicate with kubernetes control plane or master node and one of them is using a tool called **kubectl**

## <u>KUBECTL</u>

kubectl is the command-line interface (CLI) for managing Kubernetes clusters. It is a powerful tool that allows you to control and manage the resources of a cluster, such as pods, services, and deployments.

kubectl communicates with the Kubernetes API server to perform various operations, such as creating, updating, and deleting resources in the cluster. It can be used to view the status of the cluster, debug issues, and perform other tasks related to the management of the cluster.

Here are the most useful kubectl commands:

- **kubectl get**: Used to list the resources in a cluster, such as pods, services, and deployments.
Example:
```bash
kubectl get pods #Lists all the pods in the current namespace.
```
- **kubectl describe**: Used to view detailed information about a resource, such as its status, labels, and events.
Example:

```bash
kubectl describe pod <pod-name> 
```
- **kubectl create**: Used to create a new resource in the cluster, such as a pod, service, or deployment.
Example: 

```bash
kubectl create -f <config-file>
```
- **kubectl apply:** Used to apply a configuration file to create or update a resource in the cluster.
Example: 

```bash
kubectl apply -f <config-file>
```
- **kubectl delete**: Used to delete a resource from the cluster.
Example
```bash
kubectl delete pod <pod-name>
```
- **kubectl exec**: Used to execute a command in a running container.
Example:

```bash
kubectl exec -it <pod-name> -- <command>
```
- **kubectl logs**: Used to view the logs of a container.
Example: 

```bash
kubectl logs <pod-name>
```
- kubectl scale: Used to scale the number of replicas of a deployment or replicaset.
Example: 

```bash
kubectl expose deployment <deployment-name> --type=LoadBalancer --port=80
```
- **kubectl rollout**: Used to manage the rollout of a deployment, including pausing, resuming, and rolling back a rollout.
- **kubectl edit**: Used to edit a resource in-place, allowing you to make changes to the resource directly from the command line.
- **kubectl port-forward**: Used to forward a local port to a port on a pod, allowing you to access the pod from your local machine.
- **kubectl top**: Used to view resource usage of pods or nodes in the cluster.
Example: 

```bash
kubectl top pod #Shows resource usage of pods in the cluster.
```
- **kubectl cp**: Used to copy files to and from containers in a pod.
Examples: 

```bash
kubectl cp <local-file-path> <pod-name>:<remote-file-path> #Copies a file from the local machine to a pod.
```
- **kubectl cluster-info**: Used to view information about the cluster, such as the version of Kubernetes and the list of nodes.
- **kubectl config**: Used to manage the kubectl configuration, including setting the current context and namespace.
- **kubectl patch**: Used to apply a patch to a resource, allowing you to make changes to the resource without replacing it.
Example:

```bash
kubectl patch deployment <deployment-name> -p '{"spec":{"template":{"spec":{"containers":[{"name":"<container-name>","image":"<image>:<tag>"}]}}}}' #Applies a patch to a deployment to change the image of a specific container.
```
- **kubectl drain**: Used to drain a node in a cluster, evacuating all pods from the node and marking it as unavailable for scheduling.
Example: 

```bash
kubectl drain <node-name>
```
- **kubectl cordon**: Used to mark a node as unschedulable, preventing new pods from being scheduled on the node.
Example:

```bash
kubectl cordon <node-name>
```
- **kubectl uncordon**: Used to mark a node as schedulable, allowing new pods to be scheduled on the node.
Example:

```
kubectl uncordon <node-name>
```
- **kubectl run**: Used to create a new deployment or replicaset by running a single container in a pod.
Example: 

```
kubectl run <deployment-name> --image=<image>:<tag> --replicas=2
```
- **kubectl expose**: Used to expose a deployment, replicaset, or pod as a service, allowing it to be accessed from outside the cluster.

```bash
kubectl expose pod <pod-name> --port=80 --name=<service-name>
```
- **kubectl explain**: Used to view the documentation and detailed information about a resource, including its fields and their descriptions.
Example:

```bash
kubectl explain pod
```
- **kubectl cluster-health**: Used to view the health of the nodes and pods in a cluster.
Example
```bash
kubectl cluster-health
```
- **kubectl taint**: Used to add, modify, or remove a taint on a node, allowing you to control which pods can be scheduled on the node.
- **kubectl label**: Used to add, modify, or remove labels on a resource, allowing you to classify and organize resources in the cluster.

- **Kubectl Port-forward**: Forwards the local port to a specific pod port.
Example:
```bash
kubectl port-forward <pod-name> 8080:80 #Forwards the local port 8080 to the port 80 of a specific pod.
```
- **kubectl set**: Used to set or unset various options, such as the image of a container or the environment variables of a pod.
Examples: 

```bash
kubectl set image deployment <deployment-name> <container-name>=<image>:<tag>

kubectl set env deployment <deployment-name> KEY=VALUE
```
- **kubectl api-resources**: Used to list the available API resources in the cluster, including their names, short names, and categories.
Examples:

```bash
kubectl api-resources
```
- **kubectl api-versions**: Used to list the available API versions in the cluster.
Example:

```bash
kubectl api-versions
```
- **kubectl convert**: Used to convert a resource from one format to another, such as YAML to JSON or JSON to YAML.
Example:

```bash
kubectl convert -f <config-file> -o yaml
```
- **kubectl certificate**: Used to manage the certificate authorities (CAs) and certificates used by the cluster.

Overall, kubectl is a powerful and flexible tool for managing Kubernetes clusters. It can be used to perform a wide range of tasks, from listing and describing resources to creating, updating, and deleting them. It is an essential tool for anyone working with Kubernetes.

🌟 🔥 If you want to switch your career into tech and you are considering DevOps, you can join our online community [here](https://chat.whatsapp.com/LARpsS1xhYi75TdJYh8TMa) for live classes and FREE tutorial videos.