kubectl Advanced Usage: Your Super Remote Control ๐ฎ
Imagine you have a giant robot factory with hundreds of robots working together. Youโre the factory manager sitting in your office, but you need to see what each robot is doing, talk to them, and sometimes even fix them from your desk. Thatโs exactly what kubectl advanced commands let you do with Kubernetes!
The Story: The Robot Factory Manager
Meet Maya, a factory manager. Her factory has thousands of robots (containers) working in different departments (namespaces). She has a magic remote control called kubectl. Letโs learn how she uses it!
1. kubectl logs โ Peeking at Robot Diaries ๐
What it does: Every robot keeps a diary of what itโs doing. kubectl logs lets you read that diary!
Simple Example:
# Read what my-robot is writing
kubectl logs my-robot
Real Life Story:
Maya notices one robot acting strange. She reads its diary:
# See the last 100 lines
kubectl logs my-robot --tail=100
# Watch the diary in real-time (like a live video)
kubectl logs my-robot -f
# Read diary from a specific container
kubectl logs my-robot -c helper-bot
# See what happened before robot restarted
kubectl logs my-robot --previous
Why it matters: When something goes wrong, the logs tell you exactly what happenedโlike a detective reading clues!
2. kubectl exec โ Talking Directly to Robots ๐ฃ๏ธ
What it does: Sometimes you need to go INSIDE a robot and see things from its point of view. kubectl exec is like teleporting into the robot!
Simple Example:
# Open a door to talk to my-robot
kubectl exec -it my-robot -- /bin/bash
Real Life Story:
Maya needs to check if a robot can see the internet:
# Run one command inside the robot
kubectl exec my-robot -- ls /app
# Open interactive shell (like going inside)
kubectl exec -it my-robot -- /bin/sh
# Talk to specific container in a multi-robot pod
kubectl exec -it my-robot -c main-bot -- bash
The -it flags:
-i= Interactive (you can type)-t= Terminal (pretty display)
3. kubectl port-forward & proxy โ Building Secret Tunnels ๐
What it does: Imagine your robot factory is in a secret location. Port-forward builds a magic tunnel from your desk straight to one specific robot!
port-forward Example:
# Build tunnel: your desk port 8080 โ robot port 80
kubectl port-forward my-robot 8080:80
# Now visit localhost:8080 to see robot's work!
proxy Example:
# Open a door to the ENTIRE factory API
kubectl proxy --port=8001
# Visit: localhost:8001/api/v1/namespaces
The Difference:
| port-forward | proxy |
|---|---|
| Tunnel to ONE robot | Door to entire factory |
| Direct connection | Goes through security |
| For apps/services | For Kubernetes API |
4. kubectl config & contexts โ Multiple Factories! ๐ญ๐ญ๐ญ
What it does: What if Maya manages THREE different robot factories? Contexts let her switch between them instantly!
Simple Example:
# See which factory I'm controlling now
kubectl config current-context
# List all my factories
kubectl config get-contexts
# Switch to different factory
kubectl config use-context production-factory
Real Life Story:
Maya has three factories:
dev-factory(for testing new robots)staging-factory(for final checks)prod-factory(the real thing!)
# Create a new factory shortcut
kubectl config set-context my-shortcut \
--cluster=big-cluster \
--user=maya \
--namespace=special-area
5. Kubeconfig File Structure โ The Master Address Book ๐
What it does: All your factory addresses, keys, and shortcuts live in ONE special file: ~/.kube/config
The File Has Three Main Parts:
graph TD A[kubeconfig file] --> B[clusters] A --> C[users] A --> D[contexts] B --> B1[Factory addresses] C --> C1[Your ID badges] D --> D1[cluster + user + namespace<br/>Combined shortcuts]
What It Looks Like:
apiVersion: v1
kind: Config
current-context: dev-factory
clusters:
- name: dev-factory
cluster:
server: https://dev.example.com
certificate-authority: /path/to/ca.crt
users:
- name: maya
user:
client-certificate: /path/to/cert
client-key: /path/to/key
contexts:
- name: dev-factory
context:
cluster: dev-factory
user: maya
namespace: default
Key Points:
- clusters = Factory addresses (where to go)
- users = Your ID cards (who you are)
- contexts = Combinations (which factory + which ID + which area)
6. Kubectl Plugins & Krew โ Adding Super Powers! โก
What it does: Imagine your remote control can get NEW BUTTONS! Krew is a store where you download extra powers for kubectl!
Installing Krew (The Plugin Store):
# First, install the store itself
# Visit: https://krew.sigs.k8s.io/docs/user-guide/setup/install/
Using Krew:
# Search for plugins
kubectl krew search
# Install a new super power
kubectl krew install ctx
kubectl krew install ns
# Now use it!
kubectl ctx # Quick context switcher
kubectl ns # Quick namespace switcher
Popular Plugins:
| Plugin | What It Does |
|---|---|
ctx |
Switch factories FAST |
ns |
Switch areas FAST |
neat |
Clean up messy output |
tree |
See resources as a tree |
7. Kubectl Output Formatting โ Choose Your View! ๐
What it does: Sometimes you want a simple list. Sometimes you need ALL the details. Output formatting lets you choose!
The Main Formats:
# Normal table (default)
kubectl get pods
# YAML format (all details)
kubectl get pod my-robot -o yaml
# JSON format (for computers)
kubectl get pod my-robot -o json
# Wide table (more columns)
kubectl get pods -o wide
# Just the names
kubectl get pods -o name
# Custom columns (you pick!)
kubectl get pods -o custom-columns=\
NAME:.metadata.name,\
STATUS:.status.phase
JSONPath โ Grab Exactly What You Need:
# Get just the IP address
kubectl get pod my-robot \
-o jsonpath='{.status.podIP}'
# Get all container names
kubectl get pod my-robot \
-o jsonpath='{.spec.containers[*].name}'
Visual Summary:
graph TD A[Output Formats] --> B[Human Readable] A --> C[Machine Readable] A --> D[Custom] B --> B1[default table] B --> B2[-o wide] C --> C1[-o yaml] C --> C2[-o json] D --> D1[-o jsonpath] D --> D2[-o custom-columns]
Putting It All Together: Mayaโs Day ๐
Morning: Maya checks her kubeconfig to see sheโs connected to the right factory.
kubectl config current-context
Mid-morning: A robot is acting strange. She reads its logs:
kubectl logs troubled-robot --tail=50
Lunch: She needs to debug inside the robot:
kubectl exec -it troubled-robot -- /bin/sh
Afternoon: Testing a new app locally via tunnel:
kubectl port-forward svc/my-app 3000:80
Evening: Quick report with custom formatting:
kubectl get pods -o custom-columns=\
NAME:.metadata.name,\
READY:.status.containerStatuses[0].ready
Quick Reference Card ๐
| Command | What It Does |
|---|---|
kubectl logs <pod> |
Read robot diary |
kubectl logs -f <pod> |
Watch diary live |
kubectl exec -it <pod> -- sh |
Go inside robot |
kubectl port-forward <pod> 8080:80 |
Build tunnel |
kubectl proxy |
Open API door |
kubectl config get-contexts |
List factories |
kubectl config use-context <name> |
Switch factory |
kubectl krew install <plugin> |
Add super power |
kubectl get pods -o yaml |
See ALL details |
kubectl get pods -o jsonpath='{...}' |
Get specific data |
You Did It! ๐
You now know how to:
- โ Read what your containers are doing (logs)
- โ Jump inside containers (exec)
- โ Build tunnels to access apps (port-forward)
- โ Switch between clusters (contexts)
- โ Understand your kubeconfig file
- โ Add super powers with plugins (krew)
- โ Get output in any format you need
Remember: kubectl is your magic remote control. These advanced commands make you a Kubernetes SUPERHERO! ๐ฆธ