Kubectl Imperative Commands CHEAT-SHEET

Top 15 Important Kubectl Imperative Commands in Kubernetes [CHEAT SHEET]

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.

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

kubectl kubernetes api
Source: www.dockerlabs.collabnix.com

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=whizlabs
    kubectl 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=whizlabs
    kubectl 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=nginx 
    kubectl 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=3
    kubectl 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 nginx
    kubectl rollout history deployment nginx
    kubectl 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-svc
    kubectl 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 500
    kubectl 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 200
    kubectl 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> --overwrite
    e.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> --overwrite
    e.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,services
    kubectl 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:whizlabs
    kubectl 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 yaml
    kubectl expose deployment nginx --type=ClusterIP --port-80 --name=nginx-svc --dry-run=client -o yaml
    kubectl create sa whizlabs --dry-run=client -o yaml
    kubectl run alpine --image=alpine --requests=cpu=200m,memory=256Mi --limits=cpu=400m,memory=512Mi --dry-run=client -o yaml
    kubectl create role role123 --verb=get,list --resources=pods,services --dry-run=client -o yaml
    kubectl 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 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.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top