🔧 Cluster & Namespace Basics
kubectl get nodes ==> Shows number of Nodes of cluster kubectl cluster-info ==> Gives the cluster info kubectl config get-contexts ==> Shows config files for remote access kubectl get namespace /ns ==> Lists namespaces kubectl create ns batch39 ==> Creates namespace 'batch39' kubectl create -f phatan.yaml ==> Creates namespace using YAML kubectl apply -f pathan.yaml ==> Creates or updates using YAML
📦 Pod & ReplicaSet Management
kubectl get nodes / no kubectl get pods / pods kubectl get pods -o wide ==> Shows running pods in wide format kubectl get pods -l app=nginx ==> Filters pods with label app=nginx kubectl get pods -n default kubectl get pods -n my-nginx-podsapce kubectl get pods --all-namespaces | grep nginx kubectl get replicaset / rs
🌐 Service & Deployment
kubectl get service / svc kubectl get svc -n default ==> Services in default namespace kubectl edit svc nginx -n==> Edit service type kubectl describe (no, po, rs, svc) kubectl delete (no, po, rs, svc)
🚀 Run & Expose
kubectl run nginx --image=nginx:1.6 ==> Deploy nginx v1.6 kubectl run nginx --image=nginx:latest ==> Deploy latest nginx kubectl expose pod nginx --port=80 --target-port=80 --type=LoadBalancer -n my-nginx-podspace kubectl expose deployment nginx --port=80 --target-port=80 --type=LoadBalancer -n my-nginx-podspace
Note: -n
flag specifies the namespace.
📊 Summary of Kubernetes Commands
🔖 Category | 💻 Command | 📝 Description |
---|---|---|
Cluster Info | kubectl get nodes |
Shows number of nodes in the cluster |
Cluster Info | kubectl cluster-info |
Displays cluster endpoint details |
Cluster Info | kubectl config get-contexts |
Lists available kubeconfig contexts |
Namespace | kubectl get ns |
Lists all namespaces |
Namespace | kubectl create ns batch39 |
Creates a new namespace |
Namespace | kubectl apply -f pathan.yaml |
Creates or updates resources from YAML |
Pods | kubectl get pods -o wide |
Shows detailed pod info |
Pods | kubectl get pods -l app=nginx |
Filters pods by label |
Pods | kubectl get pods --all-namespaces | grep nginx |
Searches for nginx pods across namespaces |
ReplicaSet | kubectl get rs |
Lists ReplicaSets |
Service | kubectl get svc -n default |
Lists services in default namespace |
Service | kubectl edit svc nginx -n <namespace> |
Edits service configuration |
Run | kubectl run nginx --image=nginx:latest |
Deploys nginx container |
Expose | kubectl expose pod nginx --port=80 --type=LoadBalancer |
Exposes pod as a service |
Expose | kubectl expose deployment nginx --port=80 --type=LoadBalancer |
Exposes deployment as a service |