Kubernetes – Kubectl Commands
The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. Some basic Kubectl commands in a Kubernetes cluster are as follows:
Getting Resources:
These commands are used to show all the resources. It works like a READ operation. It is used to view resources like nodes, deployments, services, config maps, etc.
$ kubectl get <resource>
Getting all Resources: This command is showing all available resources.

Getting Nodes: This command shows only the available nodes.

Getting Pods: This command shows only the available pods.

Getting Services: This command shows only the available services.

Creating Resources:
These commands are used to create resources like deployments, services, secrets, config maps, etc.
$ kubectl create <resource_type> <resource_name> OPTIONS
Creating Deployment: The command creates a deployment named nginx-depl using image Nginx.

Creating service: The command in the creates a service of type node port named nginx and it is exposed on port 80 of the local machine

Updating Resources:
After running the command below you can update the resource configuration file and if there are any changes the resource will be updated according to it.
kubectl edit <resource_type> <resource_name>
Updating Deployment: The command in the below output opens a vim-like editor in the terminal to edit the Nginx-depl deployment config file.

Updating Service: The command opens a vim-like editor in the terminal to edit the Nginx service config file.

Deleting Resources:
These commands are used to delete resources like deployments, services, config maps, pods, secrets, etc. You can choose a particular resource name of a resource type or you can also delete all resources of a resource type by specifying –all flag
kubectl delete <resource_type> <resource_name> | –all
Deleting Deployment: The command deletes a deployment named nginx-depl.

Deleting Service: The command deletes a service named Nginx.

Using Configuration File for CRUD:
These commands are used to use YAML configuration files for CRUD operations in a cluster.
Applying a Config file: The command creates the resource if it does not exist or updates it if it already exists according to the configuration in the YAML file mentioned after the -f flag.
$ kubectl apply -f [file-name]

Deleting using Config file: The command deletes the resource that was created using the YAML config file mentioned after the -f flag/
$ kubectl delete -f [file-name]

Debugging Pods:
Viewing Logs of a Pod: The command shows the logs of the pod mentioned once the pod started.
$ kubectl logs [pod-name]

Get an Interactive terminal for a pod: The command starts an interactive terminal of the Nginx pod so that we can control the pod directly through its terminal.
$ kubectl exec -it [pod-name] — bin/bash

Get info about a Resource: The command gives details about the nginx deployment
$ kubectl describe <resource_type> [resource-name]

Please Login to comment...