Useful Kubectl commands in Kubernetes
In this article, I have clubbed important commands which are used in Kubernetes on a daily basis.
Kubectl cluster-info
kubectl get nodes
Specifying alias for Kubernetes
alias k =kubectl
Below is the sample output
Setup AutoComplete in Kubectl
Install bash-completion package on your master node. (Ubuntu)
apt-get install bash-completion
The above commands create /usr/share/bash-completion/bash_completion, which is the main script of bash-completion
apt-get update
Check the bash-completion install directory
ls /usr/share/bash-completion/
Add the following to your ~/.bashrc file:
source /usr/share/bash-completion/bash_completion
Setup autocomplete for Kubectl.
complete -F __start_kubectl k
Start typing `g` and then press TAB which will then autocomplete `get`.
kubectl g TAB
kubectl get
kubectl get de TAB
kubectl get deployment
Creating objects such as PODs, deployments etc. from multiple files
kubectl apply -f ./firstfile.yaml -f ./secondfile.yaml
Get all pods in namespaces
Below example shows details of the pod using the format
To get all pods in all name spaces
kubectl get pods --all-namespaces
Below command is used to get details of the pod
kubectl describe pods <pod-name> -ns <name space in which pod exists>
Get POD YAML file
kubectl get pod <pod name> -o yaml
Get documentation of POD manifests
kubectl explain pods
List all services in the namespace
kubectl get services
List a particular deployment
kubectl get deployment <deployment name>
Running command in a POD
Create the Pod as an example:
kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml
Verify that the Container is running:
kubectl get pod shell-demo
Get a shell to the running Container:
kubectl exec -it shell-demo -- /bin/bash
Note: The double dash symbol “–” is used to separate the arguments you want to pass to the command from the kubectl arguments. In your shell, list the root directory:
root@shell-demo:/#
Merging multiple YAML manifests files into one.
This is just a YAML containing multiple Kubernetes Resources, separated by the three dashes —--. In the below example, three yaml files are combined to one using ---
Two YAML files is for PODs and one is for replica set
Command to check API Versions and API resources in Kubernetes
kubectl api-versions
kubectl api-resources -o wide
Api-versions for few of the k8s objects
Below objects are used with core V1 apiVersions
bindings
componentstatuses
configmaps
endpoints
events
limitranges
namespaces
nodes
persistentvolumeclaims
persistentvolumes
pods
podtemplates
replicationcontrollers
resourcequotas
secrets
serviceaccounts
services
Below objects are used with apps/V1
controllerrevisions
daemonsets
deployments
replicasets
statefulsets
Below objects are used with networking.k8s.io/v1
ingressclasses
ingresses
networkpolicies
Below objects are used with rbac.authorization.k8s.io/v1
clusterrolebindings
clusterroles
rolebindings
roles
Helping build a better Internet | Tech Sales & Solution Innovator | Cybersecurity Advocate | Partner Ecosystem builder | IIT Palakkad
4 年Nice One Atul..
Data & Analytics Leader | Architect | Procter & Gamble
4 年Great work Atul Sharma
Sr. Release Engineer Salesforce
4 年It's a nice quick reference. Works more than juz a cheatsheet.. Nice work Atul