Kubernetes Kubectl Commands (original) (raw)

Last Updated : 23 Jul, 2025

Pre-requisites: Kubernetes

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:

**kubectl Commands

The most popular kubectl commands and flags are listed below.

1. Common Commands

Name Commands
Run a two-replica nginx deployment kubectl run my-nginx --image=nginx --replicas=5 --port=80
Run and expose the Nginx pod kubectl run my-nginx --restart=Never --image=nginx --port=80 --expose
Run nginx deployment and expose it kubectl run my-nginx --image=nginx --port=80 --expose
List of nodes and pods kubectl get pod -o wide
List all of them. kubectl get all --all-namespaces
Get every service kubectl get service --all-namespaces
Show labeled nodes kubectl get nodes --show-labels
Using a dry run, verify the yaml file kubectl create --dry-run --validate -f pod-GFG.yaml

2. Check Performance

Name Command
learn about node resource use kubectl top node
Obtain pod resource use. kubectl top pod
Get the resource utilization for the specified pod. kubectl top --containers
List each container's resource usage. kubectl top pod --all-namespaces --containers=true

3. Label & Annontation

Name Commands
By label, sort the pods kubectl get pods -l owner=gfg
Add a label by hand to a pod. kubectl label pods owner=gfg
Remove label kubectl label pods owner- GFG

4. Secrets

Name Commands
List secrets kubectl get secrets --all-namespaces
Obtain a certain hidden field of sceret. kubectl get secret GFG-cluster-kubeconfig

5. Service

Name Commands
List all services kubectl get services
List service endpoints kubectl get endpoints
Get service detail kubectl get service -o yaml

6. Volumes & Volume Claims

Name Commands
List storage class kubectl get storageclass
Check the mounted volumes kubectl exec storage
Check to persist volume kubectl describe

**Kubectl apply

We can update or apply the configuration to a cluster with the aid of ****"kubectl apply"**. With the help of the apply command, Kubernetes resources can be modified and created using a configuration file or a collection of configurations from a directory.

**Creating objects: The basic syntax of ****"kubectl apply"**

kubectl apply -f <filename.yaml>

Viewing and Finding 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

Getting all Resources

Getting  Nodes

Getting Pods

Getting Services

**Creating Resources

These commands are used to create resources like deployments, services, secrets, config maps, etc.

$ kubectl create OPTIONS

Creating Deployment

Creating service

**Editing 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

Updating Deployment

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

Deleting Deployment

Deleting Service

**Using Configuration File for CRUD

These commands are used to use YAML configuration files for CRUD operations in a cluster.

$ kubectl apply -f [file-name]

Applying a Config file

$ kubectl delete -f [file-name]

Deleting using Config file

**Interacting with running Pods

$ kubectl logs [pod-name]

Viewing Logs of a Pod

$ kubectl exec -it [pod-name] -- bin/bash

Get an Interactive terminal for a pod

$ kubectl describe [resource-name]

Get info about a Resource

Copying Files and Directories to and from Containers

With the help of the ****"kubectl cp"** command, we can copy files from host to container and vice versa.

The basic syntax of ****"kubectl cp"**

kubectl cp

This command will only function if the "tar" command in the container images is present or the command failed to run.

Updating Resources

The ****"kubectl get rs"** command allows us to check the list of deployments, To update them, look up the revision history of prior deployments, and roll back to a specific revision if necessary. Using the following commands.

kubectl rollout , , ,