Basic kubectl Commands: Your First Step to Mastering Kubernetes

If you want to work with Kubernetes, learning kubectl is non-negotiable.

kubectl is the command-line tool that lets you manage and inspect every part of your Kubernetes cluster — from Pods to Nodes, Deployments to Services.

In this post, we’ll walk through the most important kubectl commands you need to know as a beginner, along with practical examples and real use cases.

Table of Contents


1. kubectl Syntax Overview

The basic format of a kubectl command is:

1
2

kubectl \[command] \[resource] \[name] \[flags]
PartDescription
commandAction to perform (get, describe, apply)
resourceType of object (pod, svc, deployment)
nameName of specific object (optional)
flagsExtra options (e.g. -n, -o, --watch)

Example

1
2
3
kubectl get pods
kubectl describe node my-node
kubectl delete pod nginx-pod

2. Viewing Cluster State

1) Check Node Status

1
kubectl get nodes

Returns a list of nodes in your cluster and their health:

1
2
3
NAME        STATUS   ROLES           AGE   VERSION
master      Ready    control-plane   10d   v1.29.0
worker-01   Ready    <none>          10d   v1.29.0

1
2
kubectl get pods
kubectl get pods -A  # All namespaces

1
kubectl get svc

Shows Service names, IPs, types (ClusterIP, NodePort, etc.)


1
kubectl get deployments

Check how many replicas are running and whether updates are complete.


3. Get Detailed Information

1) Describe Resource

1
2
kubectl describe pod my-pod
kubectl describe node my-node

This shows detailed info like labels, events, environment variables, and logs.


1
kubectl get pod my-pod -o yaml

See the full manifest as Kubernetes understands it — great for debugging.


4. Creating Resources

1) Apply from YAML File

1
kubectl apply -f deployment.yaml

Most real-world deployments are done this way — declarative, repeatable.


1
kubectl run nginx --image=nginx

Quick way to create a single Pod — good for testing.


5. Editing Resources

1) Edit Live Resource

1
kubectl edit deployment my-deploy

This opens the resource’s YAML in your default editor (e.g. Vim).


1
kubectl apply -f updated-deployment.yaml

Use this to update Deployments, Services, or other resources declaratively.


6. Deleting Resources

1
2
kubectl delete pod my-pod
kubectl delete -f deployment.yaml

Delete by name or from a YAML file.


7. Real-Time Monitoring

1) Watch Resource Changes

1
kubectl get pods --watch

Live updates as Pods start/stop.


1
kubectl logs my-pod

If your Pod has multiple containers:

1
kubectl logs my-pod -c my-container

8. Working with Namespaces

Get resources in a namespace

1
kubectl get pods -n my-namespace

Create a namespace

1
kubectl create namespace dev

Set default namespace

1
kubectl config set-context --current --namespace=dev

9. Enable Autocomplete (Optional)

1
source <(kubectl completion bash)

You can also add this to .bashrc or .zshrc.


10. kubectl Cheatsheet (Summary)

TaskCommand
Check nodeskubectl get nodes
List podskubectl get pods
View servicekubectl get svc
View YAMLkubectl get pod nginx -o yaml
Describe resourceskubectl describe pod nginx
Apply YAMLkubectl apply -f file.yaml
Edit live resourcekubectl edit deployment my-app
Delete resourcekubectl delete pod nginx
Watch for changeskubectl get pods --watch
View logskubectl logs my-pod
Use namespacekubectl get pods -n dev
Set default namespacekubectl config set-context --current --namespace=dev

Final Thoughts

kubectl is the gateway to mastering Kubernetes. Whether you’re deploying apps, debugging Pods, or scaling services — it all happens through kubectl.

By learning these basic commands, you’re laying the groundwork for more advanced Kubernetes workflows.