Kubernetes - Kubectl Commands
Last Updated :
06 Jan, 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 <podname> --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 <podname> owner=gfg |
Remove label | kubectl label pods <podname> 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 <servicename> -o yaml |
6. Volumes & Volume Claims
Name | Commands |
---|
List storage class | kubectl get storageclass |
Check the mounted volumes | kubectl exec storage<nameofpv> |
Check to persist volume | kubectl describe <nameofpv> |
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 <resource>
- Getting all Resources: The "kubectl get all" command enables us to view all of the cluster's resource
- Getting Nodes: The number of available nodes in the cluster will be displayed by "Kubectl get nodes".
- Getting Pods: "Kubectl get pod" can be used to find out how many pods are scheduled in the cluster.
- Getting Services: "Kubectl get service" can be used to list the services that have been created in the cluster.
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 the 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
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 <resource_type> <resource_name>
- Updating Deployment: The command in the below output opens a Vim-like editor in the terminal to edit the Nginx-deal 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]
Interacting with running 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]
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 <host path pod name:container path>
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 <history> , <undo> , <status>, <restart>
Similar Reads
Kubernetes - Kubectl
Kubectl is a command-line software tool that is used to run commands in command-line mode against the Kubernetes Cluster. Kubectl runs the commands in the command line, Behind the scenes it authenticates with the master node of Kubernetes Cluster with API calls and performs the operations on Kuberne
12 min read
Kubernetes - Kubectl Delete
Kubernetes is an open-source Container Management tool that automates container deployment, container scaling, descaling, and container load balancing (also called a container orchestration tool). It is written in Golang and has a huge community because it was first developed by Google and later don
8 min read
Kubectl Command Cheat Sheet
If you are inspired to become a DevOps (Devlopment+Operations)'s Engineer and start your journey as a beginner, or if you're a professional looking to refresh your DevOps knowledge or transition into DevOps, or even if you're a learning geek seeking to expand your knowledge base, then you landed to
10 min read
What is Kubelet in Kubernetes?
A Kubelet in Kubernetes is a crucial component of the primary node agent that runs on each node. It assists with container management and orchestration within a Kubernetes cluster. Kubelet supports communication between the Kubernetes control plane and individual nodes and it also enables the effici
4 min read
Kubernetes - ConfigMaps
An open-source container orchestration system called Kubernetes is primarily used for automated software deployment, scaling, and management. Another name for Kubernetes is K8s. Originally developed by Google, Kubernetes is now managed by the Cloud Native Computing Foundation. Although it now suppor
10 min read
Kubernetes - Images
Pre-requisite:- Kubernetes A container image is used to represent binary data that is being used to encapsulate an application and all its software dependencies. Container images can be represented as executable software bundles that run standalone and make very defined assumptions about their runti
3 min read
Kubernetes - Kubectl Create and Kubectl Apply
Kubernetes is an open-source Container Orchestrating software platform that is widely used in modern application deployment and management. It comes up with many internal resources to manage the other working nodes Organizing as a Cluster facilitates seamless automatic scaling, and deployment update
8 min read
Kubernetes Cluster
A group of nodes (Computers/Operating systems) working together to communicate with the help of Kubernetes software is known as the Kubernetes cluster. It works in a mechanism in such a way that it performs actions on the worker node with the help of the manager node. Need for a Kubernetes ClusterIn
6 min read
Kubernetes - Jobs
Pre-requisite: Kubernetes In the Kubernetes world, jobs are considered an object to act as a supervisor or controllers of a task. The Kubernetes job will create a pod, monitor the task, and recreate another one if that pod fails for some reason. Upon completion of the task, it will terminate the pod
4 min read
Kubernetes kOps
Kubernetes Kops smoothes out the deployment and management of the Kubernetes cluster, tending to the intricacies related to orchestrating containerized applications. Kubernetes, an open-source container orchestration platform, mechanizes application sending and scaling. Be that as it may, proficient
11 min read