Key Takeaways
- ✓IT teams spend an average of 34 working days per year resolving Kubernetes issues - good kubectl mastery significantly reduces this time
- ✓'5 essential commands: get, describe, apply, logs, exec'
- ✓kubectl is the single entry point to the Kubernetes API
kubectl is the official command-line interface for interacting with a Kubernetes cluster. Knowing basic kubectl commands is the first step for any professional wishing to administer or develop on Kubernetes. With 82% of container users in production on Kubernetes, this skill becomes essential.
TL;DR: kubectl allows you to create, inspect, modify, and delete all Kubernetes resources from your terminal. This cheatsheet covers fundamental commands:kubectl get,kubectl describe,kubectl apply,kubectl logs, andkubectl exec. Learn the basic syntax and shortcuts to gain productivity immediately.
This skill is at the heart of the Kubernetes Fundamentals training.
Why Master kubectl Beginner Commands?
kubectl represents the single entry point to the Kubernetes API. Every action on your cluster goes through this tool.
Three reasons justify learning kubectl first:
- Total control: deploy, scale, debug without a graphical interface
- Automation: integrate kubectl into your CI/CD scripts
- Certification: CKA and CKAD exams require perfect kubectl mastery
Remember: kubectl communicates directly with the kube-apiserver. Every command you execute translates to a REST API call to the cluster.
To understand the underlying architecture, consult our Kubernetes Training Complete Guide.
kubectl Beginner Commands: Basic Syntax
The structure of every kubectl command follows this pattern:
kubectl [command] [TYPE] [NAME] [flags]
| Element | Description | Example |
|---|---|---|
| command | Action to perform | get, create, delete |
| TYPE | Resource type | pod, deployment, service |
| NAME | Resource name | nginx, my-app |
| flags | Additional options | -n namespace, -o yaml |
Configure your default context before any operation:
# Check active cluster
kubectl config current-context
# List all available contexts
kubectl config get-contexts
# Switch context
kubectl config use-context my-prod-cluster
IT teams spend an average of 34 working days per year resolving Kubernetes issues. Good kubectl mastery considerably reduces this time.
How to List and Inspect Resources with kubectl?
The kubectl get command is the most used. It displays resources of a given type.
# List all pods in current namespace
kubectl get pods
# List pods from all namespaces
kubectl get pods -A
# Display more details
kubectl get pods -o wide
# Full YAML format
kubectl get pod my-pod -o yaml
Inspect a resource in depth with describe:
kubectl describe pod my-pod
kubectl describe deployment my-deployment
kubectl describe node worker-01
Remember:kubectl getgives a summary view.kubectl describeprovides events, conditions, and configuration details.
Shortcuts accelerate your productivity:
| Resource | Full form | Shortcut |
|---|---|---|
| pods | pods | po |
| deployments | deployments | deploy |
| services | services | svc |
| namespaces | namespaces | ns |
| configmaps | configmaps | cm |
Deepen these concepts in Kubernetes Fundamentals for Beginners.
How to Create and Apply Resources?
Two approaches exist for creating Kubernetes resources.
Imperative Approach
Create directly from the command line:
# Create an nginx pod
kubectl run nginx --image=nginx:1.25
# Create a deployment
kubectl create deployment my-app --image=my-image:v1 --replicas=3
# Expose a deployment
kubectl expose deployment my-app --port=80 --type=ClusterIP
Declarative Approach
Apply YAML files for reproducible management:
# Apply a file
kubectl apply -f deployment.yaml
# Apply an entire folder
kubectl apply -f ./manifests/
# Apply from a URL
kubectl apply -f https://example.com/manifest.yaml
Example minimal manifest:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
Remember: prefer the declarative approach in production. It allows versioning, review, and rollback.
To master Kubernetes Deployments, the declarative approach is essential.
How to Debug with kubectl logs and exec?
Debugging represents a critical skill. According to The Enterprisers Project: "Anybody can learn Kubernetes. With abundant documentation and development tools available online, teaching yourself Kubernetes is very much within reach."
View Logs
# Pod logs
kubectl logs my-pod
# Logs from a specific container
kubectl logs my-pod -c my-container
# Follow logs in real time
kubectl logs -f my-pod
# Logs from last 100 lines
kubectl logs --tail=100 my-pod
Execute Commands in a Container
# Open an interactive shell
kubectl exec -it my-pod -- /bin/bash
# Execute a single command
kubectl exec my-pod -- cat /etc/hostname
# In a specific container
kubectl exec -it my-pod -c my-container -- sh
Diagnose startup problems:
# Recent events
kubectl get events --sort-by='.lastTimestamp'
# Detailed state of a failing pod
kubectl describe pod pod-in-error | grep -A 10 Events
These debugging techniques also apply to Kubernetes Services.
Cheatsheet: Essential kubectl Commands
| Command | Description | Example |
|---|---|---|
kubectl get | List resources | kubectl get pods -n production |
kubectl describe | Full details | kubectl describe svc my-service |
kubectl apply | Create/modify | kubectl apply -f app.yaml |
kubectl delete | Delete | kubectl delete pod my-pod |
kubectl logs | View logs | kubectl logs -f deploy/my-app |
kubectl exec | Execute command | kubectl exec -it pod -- bash |
kubectl port-forward | Local tunnel | kubectl port-forward svc/api 8080:80 |
kubectl scale | Adjust replicas | kubectl scale deploy/app --replicas=5 |
kubectl rollout | Manage deployments | kubectl rollout status deploy/app |
kubectl top | CPU/RAM metrics | kubectl top pods |
Useful Flags to Memorize
-n, --namespace # Specify namespace
-A, --all-namespaces # All namespaces
-o yaml/json/wide # Output format
-l, --selector # Filter by label
--dry-run=client # Simulate without applying
With 80% of organizations using Kubernetes in production, these commands constitute the minimum skills foundation.
How to Go Further with kubectl?
Automate your repetitive tasks with aliases:
# In ~/.bashrc or ~/.zshrc
alias k='kubectl'
alias kgp='kubectl get pods'
alias kgd='kubectl get deployments'
alias kgs='kubectl get services'
alias kaf='kubectl apply -f'
Enable autocompletion:
# Bash
source <(kubectl completion bash)
# Zsh
source <(kubectl completion zsh)
The official kubectl tutorial recommends practicing on a local cluster. Install minikube or kind to experiment without risk.
For structured progression toward Kubernetes Fundamentals, combine theory and intensive practice.
Take Action: SFEIR Kubernetes Training
kubectl mastery opens the door to the complete Kubernetes ecosystem. To structure your learning:
- Discover Kubernetes in 1 day with the Kubernetes Fundamentals training: architecture, pods, deployments, services
- Prepare for CKA certification with LFS458 Kubernetes Administration: 4 intensive days
- Target CKAD with LFD459 Kubernetes for Developers: 3 days focused on development
Contact our advisors for a path suited to your level and goals.