DEV Community

Cover image for KUBERNETES FROM ZERO TO HERO (PART 3) - INSTALLING MINIKUBE, KUBECTL AND KUBECTL COMMANDS
Samuel Ogunmola
Samuel Ogunmola

Posted on

KUBERNETES FROM ZERO TO HERO (PART 3) - INSTALLING MINIKUBE, KUBECTL AND KUBECTL COMMANDS

Image description
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

KUBECTL

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:
kubectl get pods #Lists all the pods in the current namespace.
Enter fullscreen mode Exit fullscreen mode
  • kubectl describe: Used to view detailed information about a resource, such as its status, labels, and events. Example:
kubectl describe pod <pod-name> 
Enter fullscreen mode Exit fullscreen mode
  • kubectl create: Used to create a new resource in the cluster, such as a pod, service, or deployment. Example:
kubectl create -f <config-file>
Enter fullscreen mode Exit fullscreen mode
  • kubectl apply: Used to apply a configuration file to create or update a resource in the cluster. Example:
kubectl apply -f <config-file>
Enter fullscreen mode Exit fullscreen mode
  • kubectl delete: Used to delete a resource from the cluster. Example
kubectl delete pod <pod-name>
Enter fullscreen mode Exit fullscreen mode
  • kubectl exec: Used to execute a command in a running container. Example:
kubectl exec -it <pod-name> -- <command>
Enter fullscreen mode Exit fullscreen mode
  • kubectl logs: Used to view the logs of a container. Example:
kubectl logs <pod-name>
Enter fullscreen mode Exit fullscreen mode
  • kubectl scale: Used to scale the number of replicas of a deployment or replicaset. Example:
kubectl expose deployment <deployment-name> --type=LoadBalancer --port=80
Enter fullscreen mode Exit fullscreen mode
  • 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:
kubectl top pod #Shows resource usage of pods in the cluster.
Enter fullscreen mode Exit fullscreen mode
  • kubectl cp: Used to copy files to and from containers in a pod. Examples:
kubectl cp <local-file-path> <pod-name>:<remote-file-path> #Copies a file from the local machine to a pod.
Enter fullscreen mode Exit fullscreen mode
  • 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:
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.
Enter fullscreen mode Exit fullscreen mode
  • kubectl drain: Used to drain a node in a cluster, evacuating all pods from the node and marking it as unavailable for scheduling. Example:
kubectl drain <node-name>
Enter fullscreen mode Exit fullscreen mode
  • kubectl cordon: Used to mark a node as unschedulable, preventing new pods from being scheduled on the node. Example:
kubectl cordon <node-name>
Enter fullscreen mode Exit fullscreen mode
  • kubectl uncordon: Used to mark a node as schedulable, allowing new pods to be scheduled on the node. Example:
kubectl uncordon <node-name>
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • kubectl expose: Used to expose a deployment, replicaset, or pod as a service, allowing it to be accessed from outside the cluster.
kubectl expose pod <pod-name> --port=80 --name=<service-name>
Enter fullscreen mode Exit fullscreen mode
  • kubectl explain: Used to view the documentation and detailed information about a resource, including its fields and their descriptions. Example:
kubectl explain pod
Enter fullscreen mode Exit fullscreen mode
  • kubectl cluster-health: Used to view the health of the nodes and pods in a cluster. Example
kubectl cluster-health
Enter fullscreen mode Exit fullscreen mode
  • 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:

kubectl port-forward <pod-name> 8080:80 #Forwards the local port 8080 to the port 80 of a specific pod.
Enter fullscreen mode Exit fullscreen mode
  • kubectl set: Used to set or unset various options, such as the image of a container or the environment variables of a pod. Examples:
kubectl set image deployment <deployment-name> <container-name>=<image>:<tag>

kubectl set env deployment <deployment-name> KEY=VALUE
Enter fullscreen mode Exit fullscreen mode
  • kubectl api-resources: Used to list the available API resources in the cluster, including their names, short names, and categories. Examples:
kubectl api-resources
Enter fullscreen mode Exit fullscreen mode
  • kubectl api-versions: Used to list the available API versions in the cluster. Example:
kubectl api-versions
Enter fullscreen mode Exit fullscreen mode
  • kubectl convert: Used to convert a resource from one format to another, such as YAML to JSON or JSON to YAML. Example:
kubectl convert -f <config-file> -o yaml
Enter fullscreen mode Exit fullscreen mode
  • 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 for live classes and FREE tutorial videos.

Top comments (0)