Skip to content
Related Articles
Open in App
Not now

Related Articles

Kubernetes – Kubectl Commands

Improve Article
Save Article
  • Last Updated : 26 Nov, 2022
Improve Article
Save Article

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 all Resources

 

Getting  Nodes: This command shows only the available nodes.

Getting  Nodes

 

Getting Pods: This command shows only the available pods.

Getting Pods

 

Getting Services: This command shows only the available services.

Getting 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 Deployment

 

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

Creating service

 

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 Deployment

 

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

Updating Service

 

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 Deployment

 

Deleting Service: The command deletes a service named Nginx.

Deleting Service

 

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]

Applying a Config file

 

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]

Deleting using Config file

 

Debugging Pods:

Viewing Logs of a Pod: The command shows the logs of the pod mentioned once the pod started.

$ kubectl logs [pod-name]

Viewing Logs of a Pod

 

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 an Interactive terminal for a pod

 

Get info about a Resource: The command gives details about the nginx deployment

$ kubectl describe <resource_type> [resource-name]

Get info about a Resource

 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!