Most Important kubernetes Commands

Most Important kubernetes Commands

Here I am sharing most import and frequently using kubernetes commands in every day while working with kubernetes. Cloud providers(Google, AWS, Azure, Oracle etc..) are providing kubernetes services.

Here we can use name space either in short name or full name like follows. And namespace no need to mention if we set the local context name.

Name space short name : -n

Name space full name: –namespace

In the following examples, I am going to using short name -n

 

How to create namespace?

Syntax
kubectl create namespace <namespace-name>

Example
kubectl create namespace  narayanatutorial

How to set context name in local?

<local-namespace-name>: localnarayanatutorial  – This is the local name to identify the particular namespace

–namespace : narayanatutorial — this is the remote actual namespace

–cluster: narayanatutorialCluster  — this is the cluster name

–user : clusterUser_narayanatutorialClusterUser — this is the cluster user name and get it this one by executing the kubectl config view

Syntax
kubectl config set-context <local-namespace-name> --namespace=<name-space> --cluster=<cluster-name> --user=<cluster-user>

Example
kubectl config set-context localnarayanatutorial --namespace=narayanatutorial --cluster=narayanatutorialCluster --user=clusterUser_narayanatutorialClusterUser

How to get the local configuration?

Example
kubectl config view

How to use namespace or context name?

Once you setup local context name for the namespace then no need to mention the namespace for each command.

Syntax
kubectl config use-context <context-name or namespace>

Example
kubectl config use-context localnarayanatutorial

How to see current context name?

Example
kubectl config current-context

How to see list of context names?

Example
kubectl config get-contexts

How to get the all namespaces?

syntax
kubectl get namespaces

Example
kubectl get namespaces

How to get the nodes?

syntax
kubectl get nodes

Example
kubectl get nodes

How get the pods?

Syntax
kubectl get pods -n <name-space> 

Example
kubectl get pods -n narayanatutorial

How get the pods lable details?

Syntax
kubectl get pods --show-labels -n <name-space> 

Example
kubectl get pods --show-labels -n narayanatutorial

 

How get all pods for all namespaces?

Syntax
kubectl get pods --all-namespaces 

Example
kubectl get pods  --all-namespaces

 

How to describe the pod?

Here WebApp1 is a pod name.

Syntax
kubectl describe pods <pod-name> -n <name-space> 

Example
kubectl describe pods WebApp1 -n narayanatutorial

How to see the pod logs?

Syntax
kubectl logs <pod-name> -n <name-space> 

Example
kubectl logs WebApp1 -n narayanatutorial

How to get the services?

Syntax
kubectl get services -n <name-space> 

Example
kubectl  get services -n narayanatutorial

How to restart the pod?

Syntax
kubectl rollout restart deployment/<deployment-name> -n <name-space> 

Example
kubectl rollout restart deployment/NTDeploymentName -n narayanatutorial

Or

Syntax 
kubectl delete <pod-name> -n <name-space>

Example 
kubectl delete  WebApp1  -n narayanatutorial

How to monitor the restarting the pod?

Syntax
kubectl rollout status -w deployment/<deployment-name> -n <name-space> 

Example
kubectl rollout status -w deployment/NTDeploymentName -n narayanatutorial

 

How to get the ingress details?

Syntax
kubectl get ingress -n <name-space> 

Example
kubectl get ingress -n narayanatutorial

How to get the ingress mapped application details?

Syntax
kubectl describe ingress <Mapped-App-Name> -n <name-space> 

Example
kubectl describe ingress NTApp  -n narayanatutorial

How to get the container access?

Syntax
kubectl exec <pod-name> -it bash  -n <name-space> 

Example
kubectl exec NTPod -it bash  -n narayanatutorial

How to get POD YAML file

Syntax
kubectl get pod <pod-name>--output=yaml -n <name-space> 

Example
kubectl get pod NTPod --output=yaml -n narayanatutorial

How to get the Cluster information

Example
kubectl cluster-info

References

kubectl Cheat Sheet

 

 

Leave a Reply