The Wayback Machine - https://web.archive.org/web/20240828080604/https://www.geeksforgeeks.org/kubernetes-kubectl-commands/
Open In App

Kubernetes – Kubectl Commands

Last Updated : 25 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

 

  • Getting  Nodes: The number of available nodes in the cluster will be displayed by “Kubectl get nodes”. 
Getting  Nodes

 

  • Getting Pods: “Kubectl get pod” can be used to find out how many pods are scheduled in the cluster.
Getting Pods

 

  • Getting Services: “Kubectl get service” can be used to list the services that have been created in the cluster.
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 the 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

 

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

 

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

 

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 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 updates making applications highly available and fault t
8 min read
Deploying NGINX Image To AKS Using Azure CLI and Advanced Kubectl Commands
Pre-requisite: AKS Features Azure Kubernetes Service (AKS) is a managed Kubernetes service that enables you to quickly deploy and manage clusters. AKS makes it easy to deploy and manage containerized applications without having to worry about the underlying infrastructure. It is a fully managed service, meaning that Microsoft handles the maintenanc
3 min read
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 Kubernetes Resources such as Creation, Deletion, Modifica
12 min read
Kubectl Rollout Restart for Statefulset - Kubernetes
Stateful applications are managed via Kubernetes StatefulSets, a particular workload API object. They offer guarantees concerning the ordering and uniqueness of pods, in contrast to deployments. Applications like databases and distributed systems, which need trustworthy, unique network IDs and long-term storage, rely upon stateful sets. They guaran
9 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 donated to CNCF (Cloud Native Computing Foundation).
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 the right place. Nowadays, Kubernetes (sometimes s
10 min read
How To kubectl Exec Into Pod?
To run a command within a container that is already executing inside a pod, use the command "kubectl exec into pod." This feature helps with debugging, troubleshooting, and administrative chores in the containerized environment. What Is Kubectl?A command-line interface for working with Kubernetes clusters is called Kubectl. It lets users to do a lo
5 min read
How to Run Shell Commands in Kubernetes Pods or Containers
In Kubernetes, we create pods by adding an extra layer of information on containers. This Kubernetes in short is known as K8s, an open-source container orchestration tool developed by Google. It is used to orchestrate the containers for bringing Agility in software deployment through scaling, and management. Currently, it is being maintained by the
6 min read
Using Commands and Arguments in a Kubernetes Pod
Kubernetes is open-source software that is used for the deployment and automation of software applications. It is a container orchestration platform. It means that we can manage microservices using containers. What is a Kubernetes pod?Kubernetes Pod is the smallest unit in the structure of Kubernetes. It basically consists of one or more containers
7 min read
Kubernetes - Monolithic Architecture of Kubernetes
There is a new way of developing software apps using a microservices architecture. That's when all the buzz around containers and container orchestration has increased but we have been developing and using these large software apps even before most of us were born. So in this article, we will be discussing what is that old software architecture we
7 min read
Kubernetes - Creating Deployment and Services using Helm in Kubernetes
Prerequisite: Kubernetes Helm is used for managing your Kubernetes Deployment. With helm, we can tear down and create a deployment with a single command. we will be creating deployment and services using Helm in Kubernetes. For simplicity, we will be using the nginx image. Deployment of nginx using HelmStep 1: We need to install the Helm. You can i
4 min read
Why Kubernetes? Benefits of using Kubernetes
The popularity of container orchestration technologies specially Kubernetes comes from its use cases and the problems that it solves. Kubernetes is the most popular container orchestration and is widely used by Cloud Native Computing Foundation (CNCF), the foundation to which Kubernetes as a project was donated by Google, estimates that about 92% b
8 min read
Kubernetes Controller VS Kubernetes Operator
Kubernetes Controllers are ideal for managing stateless apps and maintaining the correct number of copies, but Kubernetes Operators are more appropriate for complicated, stateful applications that require human-like decision-making abilities. Kubernetes ControllerKubernetes Controllers are ideal for managing stateless apps and maintaining the corre
4 min read
Microsoft Azure - Azure CLI Commands to Manage Azure VMs
Azure Command-line interface is used to manage or to create Azure resources. By using the simple AZ CLI command we can manage Azure VM quickly and at a Scale. Let's get started by using a few important Azure CLI commands to manage Azure Virtual Machines. 1. Start a Specific Stopped VM in a Select Resource Group Use the below command to start a spec
2 min read
Microsoft Azure - Manage Azure VMs using Azure PowerShell Commands
Here we are using Azure PS Commands to manage azure resources from Azure Cloud Shell or by using Windows PowerShell. By using the simple AZ PS command we can manage Azure VM Quicks without any efforts of navigating in Azure Portal and at Scale. Let's get started by using the major Azure PS commands to manage Azure Virtual Machine. 1. Start a Specif
2 min read
Docker Commands
Docker is an open-source project that automates the deployment of applications as movable, independent containers that can run locally or in the cloud. You can divide your applications from your infrastructure with the help of Docker, allowing for quick software delivery and it also allows you to manage your infrastructure in the same ways that you
7 min read
Basic Ansible Commands
In the increasingly complicated IT systems of today, effective and consistent infrastructure management is essential. Ansible shows up as a potent automation tool that makes it simple to configure, deploy, and manage systems in a variety of scenarios. This book will provide you with a fundamental grasp of Ansible's fundamental commands and useful u
10 min read
Docker Compose - How To Execute Multiple Commands?
Docker Compose is an orchestration tool that comes with Docker and enables users to run and manage multi-container applications using a YAML format file. Docker Compose comes pre-installed with Docker and does not require any additional installation or activation. In this article, we will explore what Docker Compose is. and how we can execute multi
7 min read
Terraform Cheat Sheet | Commands and Examples
Terraform is an extremely effective solution for cloud resource deployment, administration, and provisioning in the quickly expanding field of infrastructure as code (IaC). Clients may describe their infrastructure in code thanks to Terraform's declarative configuration language and rich ecosystem, which promotes automation, consistency, and scalab
11 min read
Use high-level (s3) commands with the AWS CLI
Amazon Simple Storage Service (Amazon S3) is a generally utilized, versatile, and highly dependable object storage service provided by Amazon Web Services (AWS). S3 allows you to store and recover any measure of data whenever, from anyplace on the web, while the AWS Management Console Center offers an easy to understand interface for managing S3, a
6 min read
DynamoDB Dynamo: AWS CLI Commands for NoSQL Database
As we know, there are three main ways to access and manage AWS services, AWS CLI (command line interface), the AWS Management Console, and the AWS SDK(Software (software kit). Here we are working with the first tool, which is AWS CLI (command line interface) which is basically used for interacting with AWS Resources through scripting and automation
7 min read
Ansible - Ad hoc Commands
Ansible ad hoc commands are simple, one-time tasks you can do on other computers. They use a tool called /usr/bin/ansible. These commands are quick and easy, but you can't save them for later use. Why learn about ad hoc commands? They show how easy and powerful Ansible is. What you learn here will help you understand Ansible playbooks later. Ad hoc
9 min read
AWS CLI SQS Commands: Key Operations and Examples
AWS Simple Queue Service (SQS) is a fully managed message queuing service that helps in decoupling and scaling microservices, distributed systems, and serverless applications, it facilitates the building of reliable communication between different parts of a system, which in turn assists in constructing fault-tolerant, distributed systems capable o
5 min read
AWS CLI Commands for S3 Object Storage
Amazon S3 stands for Simple Storage Service, the most widely adopted cloud storage solution provided by Amazon Web Services. It provides a scalable, secure, and highly durable platform for storing and retrieving any volume of data from or to anywhere on the web. This greatly helps you, whether as a developer, data analyst, or IT administrator, to i
5 min read
How Raspberry Pi and Kubernetes Work Together?
Pre-requisite: Kubernetes and Raspberry Pi Kubernetes and Raspberry Pi are two popular technologies that can be used together to create powerful and scalable applications. In this article, we will look at what each technology is, how they work together, and the benefits and challenges of using the two technologies together. KubernetesKubernetes is
3 min read
Google Cloud Platform - Using Config Sync for Managing Kubernetes
In this article, we will look into how we can manage Kubernetes using Config Sync. To do so let's create a problem statement and resolve the same. Problem Statement: Ravi has a new role, Platform Administrator, and he is tasked with ensuring all the infrastructure created by all of his company's teams is in compliance with governance requirements.
3 min read
Kubernetes - Introduction to Container Orchestration
In this article, we will look into Container Orchestration in Kubernetes. But first, let's explore the trends that gave rise to containers, the need for container orchestration, and how that it has created the space for Kubernetes to rise to dominance and growth. The growth of technology into every aspect of our lives and days has created immense d
4 min read
Microsoft Azure - Starting & Stopping a Azure Kubernetes Service Cluster
In this article, we will learn how to stop and start Azure Kubernetes Service(AKS) clusters. You can stop your entire Azure Kubernetes Service cluster to save costs. To follow along, you will need an existing Azure Kubernetes service that is running. To use start and stop for AKS, we need the AKS preview extension for the Azure CLI. It can be insta
2 min read
Microsoft Azure - Introduction to Kubernetes Diagnostics
In this article, we will learn how to use Azure Kubernetes Service Diagnostics. A solution that runs inside a Kubernetes cluster can quickly become complex. Azure Kubernetes diagnostics can help you to investigate, diagnose and resolve issues in your cluster quickly. Here we have an AKS cluster running, and it is running a sample application. Let u
2 min read
Microsoft Azure - Availability Zones For Kubernetes Cluster
In this article, we will learn how to create a highly available Kubernetes cluster with availability zones. When you create an Azure Kubernetes service or AKS cluster in Azure, its nodes and storage all reside in the same datacenter. The cluster is protected from hardware failure and maintenance within the data center because the nodes and storage
2 min read