Most Important kubernetes Commands
Table of Contents
- 1 Most Important kubernetes Commands
- 1.1 How to create namespace?
- 1.2 How to set context name in local?
- 1.3 How to get the local configuration?
- 1.4 How to use namespace or context name?
- 1.5 How to see current context name?
- 1.6 How to see list of context names?
- 1.7 How to get the all namespaces?
- 1.8 How to get the nodes?
- 1.9 How get the pods?
- 1.10 How get the pods lable details?
- 1.11 How get all pods for all namespaces?
- 1.12 How to describe the pod?
- 1.13 How to see the pod logs?
- 1.14 How to get the services?
- 1.15 How to restart the pod?
- 1.16 How to monitor the restarting the pod?
- 1.17 How to get the ingress details?
- 1.18 How to get the ingress mapped application details?
- 1.19 How to get the container access?
- 1.20 How to get POD YAML file
- 1.21 How to get the Cluster information
- 1.22 References
- 1.23 Related Posts
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
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.