kubectl Commands
The most popular kubectl commands and flags are listed below.
1. Common Commands
Name |
Commands |
---|---|
Run a two-replica nginx deployment |
kubectl run my-nginx –image=nginx –replicas=5 –port=80 |
Run and expose the Nginx pod |
kubectl run my-nginx –restart=Never –image=nginx –port=80 –expose |
Run nginx deployment and expose it |
kubectl run my-nginx –image=nginx –port=80 –expose |
List of nodes and pods |
kubectl get pod -o wide |
List all of them. |
kubectl get all –all-namespaces |
Get every service |
kubectl get service –all-namespaces |
Show labeled nodes |
kubectl get nodes –show-labels |
Using a dry run, verify the yaml file |
kubectl create –dry-run –validate -f pod-GFG.yaml |
2. Check Performance
Name | Command |
---|---|
learn about node resource use | kubectl top node |
Obtain pod resource use. | kubectl top pod |
Get the resource utilization for the specified pod. | kubectl top <podname> –containers |
List each container’s resource usage. | kubectl top pod –all-namespaces –containers=true |
3. Label & Annontation
Name | Commands |
---|---|
By label, sort the pods | kubectl get pods -l owner=gfg |
Add a label by hand to a pod. | kubectl label pods <podname> owner=gfg |
Remove label | kubectl label pods <podname> owner- GFG |
4. Secrets
Name | Commands |
---|---|
List secrets | kubectl get secrets –all-namespaces |
Obtain a certain hidden field of sceret. | kubectl get secret GFG-cluster-kubeconfig |
5. Service
Name | Commands |
---|---|
List all services | kubectl get services |
List service endpoints | kubectl get endpoints |
Get service detail | kubectl get service <servicename> -o yaml |
6. Volumes & Volume Claims
Name | Commands |
---|---|
List storage class | kubectl get storageclass |
Check the mounted volumes | kubectl exec storage<nameofpv> |
Check to persist volume | kubectl describe <nameofpv> |
Kubernetes – Kubectl Commands
Pre-requisites: Kubernetes
The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. Some basic Kubectl commands in a Kubernetes cluster are as follows: