Kubernetes – Imperative commands – part 1 – simplifying concepts

Kubernetes - Cluster - starting minikube
Kubernetes - Cluster - starting minikube

Hi All,

Greetings for the day!!!

Lets continue discussing Kubernetes – Today Kubernetes Imperative CMDLETs

Take away from this article

  • What are Kubernetes Imperative CMDLEts
  • What prerequisites required to use Kubernetes imperative CMDLETs
  • Imperative CMDLETs for Kubernetes cluster
  • Imperative CMDLETs Kubernetes objects
  • Imperative CMDLETs Kubernetes Nodes

What are Kubernetes Imperative commands

  • Imperative commands are the CMDLETs used to create , update, delete Kubernetes objects on the Kubernetes cluster built into the KUBECTL command line tool
  • To deploy application on the Kubernetes cluster

Prerequisites

Local development cluster

  • Here I have setup my local Kubernetes cluster using minikube
  • Start the minikube

minikube start

Kubernetes - Cluster - starting minikube
fig : Kubernetes – Cluster – starting minikube
  • Know the details of Kubernetes commands – using -h option
    • Syntax : kubectl <command-name> -h
    • Example :

#get the help on kubectl get command / parameter
kubectl get -h

Kubernetes - Getting help on Kubernetes CMDLETs
fig : Kubernetes – Getting help on Kubernetes CMDLETs
Kubernetes - Getting help on Kubernetes CMDLETs
fig : Kubernetes – Getting help on Kubernetes CMDLETs

Imperative CMDLET for verifying KubeCtl installation

  • We can verify if kubectl command line utility is successfully installed or not by using “version” attribute to kubectl CMDLET as

kubectl version

Kubernetes - Cluster - verifying if kubectl command line utility is installed or not - kubectl version
fig : Kubernetes – Cluster – verifying if kubectl command line utility is installed or not – kubectl version

Imperative CMDLETs for Kubernetes cluster

  • To get cluster information
    • Shows the address of control plane

kubectl cluster-info

Kubernetes - Displaying Cluster Information - kubectl cluster-info
fig : Kubernetes – Displaying Cluster Information – kubectl cluster-info
  • To get more detailed information about cluster using dump parameter
    • This option is used for debugging purpose and to analyse the cluster problems
    • By default it prints all information on console
    • We can specify directory path using –output parameter, in this case Kubernetes put all files in specified directory path

kubectl cluster-info dump

Kubernetes - Displaying very detailed cluster Information - kubectl cluster-info dump
fig : Kubernetes – Displaying very detailed cluster Information – kubectl cluster-info dump
  • To have dump details in files and in directory

kubectl cluster-info dump --output-directory "C:\\Prasham\\Articles\\Kubernetes\\Imperative CMDLETS\\dump"

Kubernetes - dumping Kubernetes all details in folders within files in JSON format
fig : Kubernetes – dumping Kubernetes all details in folders within files in JSON format
Kubernetes - dumping Kubernetes all details in folders within files in JSON format - folder structure
fig : Kubernetes – dumping Kubernetes all details in folders within files in JSON format – folder structure
Kubernetes - dumping Kubernetes all details in folders within files in JSON format - folder structure
fig : Kubernetes – dumping Kubernetes all details in folders within files in JSON format – folder structure
  • To know the Api Versions on the server

kubectl api-versions

Kubernetes - Know the API Versions - kubectl api-versions
fig : Kubernetes – Know the API Versions – kubectl api-versions

General Kubernetes objects CMDLETs

  • What are Kubernetes objects – for details please have a look our article – Kubernetes – Objects – simplifying concepts
  • Creating Kubernetes object with specification in Yaml / JSON file
    • Syntax – kubectl create -f <kubernetes object yaml specification file path>
    • Examples:

kubectl create -f "C:\Prasham\Articles\Kubernetes\Imperative CMDLETS\YAML files\knoweldgejunction_pod.yaml"

Kubernetes - POD YAML file
fig : Kubernetes – POD YAML file
Kubernetes - POD creation
fig : Kubernetes – POD creation
  • Delete kubernetes object – using delete object
    • Syntax – kubectl delete <Kubernetes object>
    • Examples:

kubectl delete deployment/nginx

  • Get one or more object / resources using get parameter to kubectl CMDLET- print basic information about object
    • Syntax – kubectl get [(-o|--output=)json|yaml|object name | resource name
    • Examples:

#list all pods in cluster
kubectl get pods

Kubernetes - Fetching list of Pods in default namespace
fig : Kubernetes – Fetching list of Pods in default namespace
#list all pods with details in cluster
kubectl get pods -o wide

Kubernetes - Fetching list of Pods  with details in default namespace
fig : Kubernetes – Fetching list of Pods with details in default namespace
# List deployments in JSON output format, in the "v1" version of the "apps" API group
kubectl get -f pod.yaml -o json

Kubernetes get pod details in JSON format from YAML file
fig : Kubernetes get pod details in JSON format from YAML file
#To get the multiple objects at time - separated by ","
kubectl get rc,services

Get very detailed information about one or more object / resources using describe parameter to kubectl CMDLET

kubectl describe pod <pod-name>

Kubernetes - get detailed information of Kubernetes object using - describe parameter
fig : Kubernetes – get detailed information of Kubernetes object using – describe parameter

To get the yaml specification of any Kubernetes object

kubectl get <object-name> <object-value> -o yaml

Here,
<object-name> like - pod , deployment, service, ingress 

Example - 
kubectl get pod nginx -o yaml

Kubernetes - get yaml specification of object using - "-o yamal" parameter
fig : Kubernetes – get yaml specification of object using – “-o yamal” parameter

CMDLETs for Nodes

  • To get all labels of node
kubectl get nodes <node name> --show-labels

Example:
kubectl get nodes node01 --show-labels

Kubernetes - get node labels
fig : Kubernetes – get node labels
  • Apply a label to nodes

kubectl label nodes <node name> <labelname>=<labelvalue>

Example:
kubectl label nodes KnowledgeJunction-Mode01 color=blue

Thanks for reading the article !!! Please feel free to discuss in case any issues / suggestions / thoughts / questions !!!

HAVE A GREAT TIME AHEAD !!!

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.