Top 15 Must-Know Kubectl commands for Kubernetes API (2024)

In this article, we are going to learn about the top 15 most important Kubectl Imperative commands used in the Kubernetes system which manages containers used to build applications. Kubectl commands are helpful in carrying out the HTTP requests to the Kubernetes API.

Before learning the Kubectl commands, you should know about the basics of Kubectl, where it is used, and how it works.If you want to try out the Kubernetes platform practically for passing the Kubernetes Certification Exam, then first start learning these Kubectl commands.

Kubernetes is one of the most popular open-source platforms working in a container orchestration system. Kubernetes are helpful in auto-scaling the applications, enabling operational codes, automating the deployment of software applications and management.Kubernetes can run on any popular cloud service providers such as Amazon AWS, Microsoft Azure, and Google cloud. Also, it can be installed on any operating system like Linux, Windows, and macOS.

Table of Contents

What is Kubectl?

Kubectl stands for “Kubernetes Command-line interface”. It is a command-line tool for the Kubernetes platform to perform API calls. Kubectl is the main interface that allows users to create (and manage) individual objects or groups of objects inside a Kubernetes cluster.

As a user, Kubectl helps you to control Kubernetes as a co*ckpit. Every operation in Kubernetes can be controlled by a user with the help of kubectl commands. In technical, the same kubectl acts as a client for Kubernetes API.

Here’s how Kubectl sends HTTP requests to the Kubernetes API

Top 15 Must-Know Kubectl commands for Kubernetes API (1)

It provides users access to all the different operations such as creation, updation, deletion of objects. It can be used to do these operations in two ways – Imperative & Declarative commands. From the perspective of the Kubernetes certification exam, understanding declarative and imperative commands are very important to pass the certification exam.

Let’s see what they are.

Declarative

Declarative Commands are used for creating resources from manifest files using the ‘kubectl apply’ or ‘kubectl create’ command. This is a method commonly used in CICD pipelines where YAMLs usually present.

Imperative

Imperative commands are used to create, update and delete objects on Kubernetes clusters without having to specify any manifest files beforehand. They are a godsend for Kubernetes application developers and administrators because they are very easy to remember and let you get things done more efficiently!

Kubectl cheat sheet to execute Imperative commands

This Kubectl cheat sheet goes through several commonly used important imperative commands. Do a lot of practice with these Kubectl commands as a part of preparing for Kubernetes certifications

  1. Creation of pods-
    kubectl run <pod-name> --image=<image-name>e.g. kubectl run nginx --image=nginx
  2. Creation of config maps-
    kubectl create cm <configmap-name> --from-literal=<key>=<value>kubeclt create cm <configmap-name> --from-file=<file-name>e.g.kubectl create cm website --from-literal=name=whizlabskubectl create cm exams --from-literal=whizlabs.txt

    3. Creation of secrets-

    kubectl create secret generic <secret-name> --from-literal=<key>=<value>kubectl create secret generic <secret-name> --from-file=<file-name>e.g.kubectl create secret website --from-literal=name=whizlabskubectl create secret exams --from-literal=whizlabs.txt'

    4. Creation of deployments-

    kubectl create deployment <deployment-name> --image=<image-name>kubectl create deployment <deployment-name> --image=<image-name> --replicas=<replica-count>e.g. kubectl create deployment nginx --image=nginxkubectl create deployment ubuntu --image=ubuntu --replicas=3

    5. Modify properties of an existing deployment-

    kubectl scale deployment <deployment-name> --replicas=<new-replica-count>kubectl set image deployment <deployment-name> <name-of-container>=<new-image-name>e.g. kubectl scale deployment nginx --replicas=3kubectl set image deployment ubuntu ubuntu=ubuntu:16

    6. Performing rollout actions on existing deployment-

    kubectl rollout status deployment <deployment-name>kubectl rollout history deployment <deployment-name>kubectl rollout undo deployment <deployment-name> --to-revision=<revision-number>e.g. kubectl rollout status deployment nginxkubectl rollout history deployment nginxkubectl rollout undo deployment nginx --to-revision=2

    7. Creating services for exposing deployments-

    kubectl expose deployment <deployment-name> --type=<svc-type> --port=<port-number> --target-port=<target-port-number> --name=<svc-name>e.g.kubectl expose deployment nginx --type=ClusterIP --port=8080 --target-port=80 --name=nginx-clusterip-svckubectl expose deployment ubuntu --type=NodePort --port=8080 --target-port=80 --name=nginx-nodeport-svc

    8. Creating pods with additional properties-

    kubectl run <pod-name> --image=<image-name> --requests=cpu=<cpu-requests>,memory=<memory-requests> --limits=cpu=<cpu-limits>,memory=<memory-limits>e.g.kubectl run alpine --image=alpine --requests=cpu=200m,memory=256Mi --limits=cpu=400m,memory=512Mi

    9. Creating deployment objects with additional properties

    kubectl create deployment <deployment-name> --image=<image-name> --replicas=<replica-count> -- <command> <arguments>kubectl create deployment <deployment-name> --image=<image-name> --replicas=<replica-count> --port=<port-to-expose>e.g.kubectl create deployment ubuntu --image=ubuntu --replicas=2 -- sleep 500kubectl create deployment nginx --image=nginx --replicas=3 --port=80

    10. Creating jobs and crons-

    kubectl create job <job-name> --image=<image-name> -- <command> <arguments>kubectl create cronjob <job-name> --image=<image-name> --schedule="<cron-schedule>" -- <command> <arguments>e.g.kubectl create job whizlab --image=ubuntu -- sleep 200kubectl create cronjob whizlabs --image=ubuntu --schedule="* * * * *" -- sleep 300

    11. Performing action related to annotations

    To add:kubectl annotate pods <podname> <key>=<value>e.g.kubectl annotate pods nginx prepareon='whizlabs'To remove:kubectl annotate pods <podname> <key>-e.g.kubectl annotate pods nginx prepareon-To overwrite:kubectl annotate pods <podname> <key>=<new-value> --overwritee.g.kubectl annotate pods nginx prepareon='Whizlabz' --overwrite

    12. Performing actions related to labeling

    To add:kubectl label pods <podname> <key>=<value>e.g.kubectl label pods nginx prepareon='whizlabs'To remove:kubectl label pods <podname> <key>-e.g.kubectl label pods nginx prepareon-To overwrite:kubectl label pods <podname> <key>=<new-value> --overwritee.g.kubectl label pods nginx prepareon='Whizlabz' --overwrite

    13. Create a service account

    kubectl create sa <serviceaccount-name>e.g. kubectl create sa whizlabs

    14. Create roles

    kubectl create role <role-name> --verb=<list-of-verbs> --resources=<list-of-resource>kubectl create clusterrole <clusterrole-name> --verb=<list-of-verbs> --resources=<list-of-resource>e.g.kubectl create role role123 --verb=get,list --resources=pods,serviceskubectl create clusterrole clusterrole123 --verb=get,list --resources=pods,services

    15. Create role bindings

    kubectl create rolebinding <rolebinding-name> --role=<role-name> --serviceaccount=<namespace>:<serviceaccount-name>kubectl create clusterrolebinding <clusterrolebinding-name> --clusterrole=<role-name> --serviceaccount=<namespace>:<serviceaccount-name>e.g.kubectl create rolebinding rb123 --role=role123 --serviceaccount=default:whizlabskubectl create clusterrolebinding rb123 --role=clusterrole123 --serviceaccount=default:whizlabs

    Final words

    There were the most commonly used imperative commands. Additionally, these commands are often used along “dry-run” and “output” flags to generate full-fledged YAML manifests of the Kubernetes objects. It greatly reduces the manual work for Kubernetes application developers & administrators!

    e.g.kubectl create deployment nginx --dry-run=client -o yamlkubectl expose deployment nginx --type=ClusterIP --port-80 --name=nginx-svc --dry-run=client -o yamlkubectl create sa whizlabs --dry-run=client -o yamlkubectl run alpine --image=alpine --requests=cpu=200m,memory=256Mi --limits=cpu=400m,memory=512Mi --dry-run=client -o yamlkubectl create role role123 --verb=get,list --resources=pods,services --dry-run=client -o yamlkubectl create rolebinding rb123 --role=role123 --serviceaccount=default:whizlabs --dry-run=client -o yaml

References

Summary

That’s all. Hope you got the list of important imperative Kubectl commands to communicate in HTTP REST API which is the default user interface of the Kubernetes platform. The best way to learn all these Kubectl commands is to try using them in the Kubernetes platform and find out the results driven by it. If you are a beginner at the Kubernetes platform, by using these commands, you can prepare for the Kubernetes certification exams practically. Keep learning!

  • About the Author
  • More from Author

Top 15 Must-Know Kubectl commands for Kubernetes API (2)

About Shishir Khandelwal

Shishir has the passion and zeal to master his field of cloud & containers. He is a strong advocate of finding smart solutions to complex problems by leveraging the power of cloud & container technology such as Kubernetes and a strong believer in learning by doing because of which he does a lot of POCs and personal projects. He is a certified expert in AWS & Kubernetes.

  • NGINX Tutorial for Beginners (NEW) – Learn for FREE ! - February 21, 2022
  • The Beginner’s Guide to Helm Charts - February 16, 2022
  • The Complete Guide to Kubernetes Logging - January 14, 2022
  • Explained: Kubernetes Architecture - January 12, 2022
  • Top 15 Important Kubectl Imperative Commands in Kubernetes [CHEAT SHEET] - January 12, 2022
  • What is Prometheus Grafana Stack ? - December 28, 2021
  • How to prepare for the Certified Kubernetes Application Developer (CKAD) Exam? - December 6, 2021
Top 15 Must-Know Kubectl commands for Kubernetes API (2024)

FAQs

Top 15 Must-Know Kubectl commands for Kubernetes API? ›

Ways of Accessing the API

Via kubectl: kubectl is the official command-line tool for Kubernetes and provides a convenient way to interact with the Kubernetes API. It simplifies tasks like creating, updating, and deleting resources using intuitive commands.

Which command is used to make Kubernetes API calls? ›

Ways of Accessing the API

Via kubectl: kubectl is the official command-line tool for Kubernetes and provides a convenient way to interact with the Kubernetes API. It simplifies tasks like creating, updating, and deleting resources using intuitive commands.

Which common command is used in Kubernetes? ›

The kubectl command makes HTTP requests to these URLs to access the Kubernetes objects that reside at these paths. The most basic command for viewing Kubernetes objects via kubectl is get . If you run kubectl get <resource-name> you will get a listing of all resources in the current namespace.

What is the command to list out the services of Kubernetes? ›

5. Service
NameCommands
List all serviceskubectl get services
List service endpointskubectl get endpoints
Get service detailkubectl get service <servicename> -o yaml
Apr 25, 2023

Which kubectl command can be used to list all available API groups? ›

Using kubectl api-resources -o wide shows all the resources, verbs and associated API-group.

What is API used in Kubernetes? ›

The Kubernetes API is a resource-based (RESTful) programmatic interface provided via HTTP. It supports retrieving, creating, updating, and deleting primary resources via the standard HTTP verbs (POST, PUT, PATCH, DELETE, GET).

How do I deploy API in Kubernetes? ›

How to Deploy a REST API in Kubernetes
  1. Set Up Local Kubernetes. There's a couple options for running Kubernetes locally, with the most popular ones including minikube, k3s, kind, microk8s. ...
  2. Create a Simple API. We will create a simple API using Express. ...
  3. Deploy. Now, we deploy the image to our local Kubernetes cluster.

What is the most basic Kubernetes object? ›

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

How do I check container logs in Kubernetes? ›

You can see the logs of a particular container by running the command kubectl logs <container name> . Here's an example for Nginx logs generated in a container. If you want to access logs of a crashed instance, you can use –previous . This method works for clusters with a small number of containers and instances.

How do I permanently delete a pod in Kubernetes? ›

Pods can be deleted simply using the kubectl delete pod command. However, the challenge is usually to maintain application uptime and avoid service disruption. To do this, you can use the kubectl drain command to gracefully bring pods up on another node before they are deleted.

How do you practice Kubernetes commands? ›

Expose Pod with a Service
  1. port: Port on which node port service listens in Kubernetes cluster internally.
  2. targetPort: We define container port here on which our application is running.
  3. NodePort: Worker Node port on which we can access our application.
Mar 20, 2023

How do I delete a deployment in Kubernetes? ›

Open a terminal or command prompt and connect to your K8S cluster. View a list of deployments with the kubectl get deployment command. Use the -n <namespace> flag to specify the namespace of the deployment. Delete the deployment with the kubectl delete deployment <deployment name> -n <namespace name> .

How do I check my Kubernetes pod configuration? ›

To view the entire configuration of the pod, just run kubectl describe pod nginx in your terminal. The terminal will now display the YAML for the pod, starting with the name nginx, its location, the Minikube node, start time, and current status.

How do I get all API resources in Kubernetes? ›

Understanding the kubectl api-resources Command. kubectl api-resources –verbs=list –namespaced -o name retrieves all namespaced API resource types that support the list API verb. Then it outputs their names. Those names are then redirected to xargs as standard input.

What are the subgroups of Kubernetes API? ›

Kubernetes divides the endpoints into 2 subgroups: core API group and other API group.
  • The core (also called legacy) group is found at the REST path /api/v1.
  • The named groups are found at the REST path /apis/$GROUP_NAME/$VERSION.
Jan 17, 2022

How do I list API groups in Kubernetes? ›

You can also view these on your Kubernetes cluster. Access your Kube API server at port 6443, without any path and it will list you the available API groups. And then within the named API groups it returns all supported groups.

How do I call Kubernetes rest API? ›

Directly accessing the REST API
  1. Run kubectl in proxy mode (recommended). This method is recommended, since it uses the stored API server location and verifies the identity of the API server using a self-signed certificate. ...
  2. Alternatively, you can provide the location and credentials directly to the http client.
Dec 6, 2023

How do you call an API function? ›

How to Use the Fetch API for GET Requests
  1. We defined the API URL that we want to call.
  2. We used the fetch function to make a GET request to the API URL. ...
  3. The .then() method handles the asynchronous response from the server.
  4. The response.ok property is checked to ensure the response is valid.
Nov 3, 2023

How do I connect to Kubernetes cluster API? ›

Once the proxy is running, you can access the Kubernetes API server by making requests to localhost or 127.0. 0.1 on the designated port. Running kubectl in proxy mode provides a secure and convenient way to access the Kubernetes API server, allowing you to interact with and manage your cluster from your local machine.

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5851

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.