Kubernetes Basics
Essential kubectl commands, pod management, and fundamental Kubernetes concepts.
kubectl Basics
Cluster Information
# Check cluster info
kubectl cluster-info
# Get cluster status
kubectl get componentstatuses
# View cluster nodes
kubectl get nodes
kubectl get nodes -o wide
# Show kubectl version
kubectl version
kubectl version --short
# Get current context
kubectl config current-context
# List all contexts
kubectl config get-contexts
# Switch context
kubectl config use-context context-name
Basic Resource Operations
# Get resources
kubectl get pods
kubectl get services
kubectl get deployments
kubectl get all
# Get resources in all namespaces
kubectl get pods --all-namespaces
kubectl get pods -A
# Get resources with labels
kubectl get pods --show-labels
kubectl get pods -l app=nginx
# Get resources with output formats
kubectl get pods -o wide
kubectl get pods -o yaml
kubectl get pods -o json
Resource Description and Details
# Describe resources
kubectl describe pod pod-name
kubectl describe service service-name
kubectl describe node node-name
# Get resource details
kubectl get pod pod-name -o yaml
kubectl get service service-name -o json
# Show resource events
kubectl get events
kubectl get events --sort-by=.metadata.creationTimestamp
Pod Management
Creating Pods
# Create pod from command line
kubectl run nginx --image=nginx
kubectl run busybox --image=busybox --rm -it -- /bin/sh
# Create pod with specific options
kubectl run nginx --image=nginx --port=80 --labels="app=web,env=prod"
# Create pod with resource limits
kubectl run nginx --image=nginx --requests="cpu=100m,memory=128Mi" --limits="cpu=200m,memory=256Mi"
# Create pod in specific namespace
kubectl run nginx --image=nginx -n my-namespace
# Dry run (generate YAML without creating)
kubectl run nginx --image=nginx --dry-run=client -o yaml
Pod Operations
# List pods
kubectl get pods
kubectl get pods -n namespace-name
kubectl get pods --field-selector=status.phase=Running
# Delete pods
kubectl delete pod pod-name
kubectl delete pod pod-name --grace-period=0 --force
# Execute commands in pods
kubectl exec pod-name -- command
kubectl exec -it pod-name -- /bin/bash
kubectl exec -it pod-name -c container-name -- /bin/sh
# Port forwarding
kubectl port-forward pod-name 8080:80
kubectl port-forward pod-name 8080:80 --address=0.0.0.0
# Copy files to/from pods
kubectl cp local-file pod-name:/path/to/file
kubectl cp pod-name:/path/to/file local-file
kubectl cp pod-name:/path/to/file local-file -c container-name
Pod Logs
# View pod logs
kubectl logs pod-name
kubectl logs pod-name -c container-name
# Follow logs
kubectl logs -f pod-name
kubectl logs -f pod-name -c container-name
# Get logs from previous container instance
kubectl logs pod-name --previous
# Get logs with timestamps
kubectl logs pod-name --timestamps
# Get last N lines
kubectl logs pod-name --tail=50
# Get logs since specific time
kubectl logs pod-name --since=1h
kubectl logs pod-name --since=2023-01-01T00:00:00Z
Namespaces
Namespace Operations
# List namespaces
kubectl get namespaces
kubectl get ns
# Create namespace
kubectl create namespace my-namespace
# Delete namespace
kubectl delete namespace my-namespace
# Set default namespace for context
kubectl config set-context --current --namespace=my-namespace
# Get resources in specific namespace
kubectl get pods -n my-namespace
kubectl get all -n my-namespace
Working with Namespaces
# Create resource in namespace
kubectl run nginx --image=nginx -n my-namespace
# Apply manifest to namespace
kubectl apply -f manifest.yaml -n my-namespace
# Get resources across all namespaces
kubectl get pods --all-namespaces
kubectl get pods -A
# Filter by namespace
kubectl get pods --field-selector metadata.namespace=default
YAML Manifests
Basic Pod Manifest
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
env: production
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80
resources:
requests:
memory: '128Mi'
cpu: '100m'
limits:
memory: '256Mi'
cpu: '200m'
env:
- name: ENV_VAR
value: 'production'
Multi-Container Pod
apiVersion: v1
kind: Pod
metadata:
name: multi-container-pod
spec:
containers:
- name: web
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
- name: sidecar
image: busybox
command:
[
'sh',
'-c',
'while true; do echo "$(date): Hello from sidecar" > /data/index.html; sleep 10; done',
]
volumeMounts:
- name: shared-data
mountPath: /data
volumes:
- name: shared-data
emptyDir: {}
Pod with Volume Mounts
apiVersion: v1
kind: Pod
metadata:
name: pod-with-volume
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: config-volume
mountPath: /etc/config
- name: data-volume
mountPath: /data
volumes:
- name: config-volume
configMap:
name: app-config
- name: data-volume
persistentVolumeClaim:
claimName: data-pvc
Labels and Selectors
Working with Labels
# Show labels
kubectl get pods --show-labels
kubectl get nodes --show-labels
# Add labels
kubectl label pod pod-name app=nginx
kubectl label node node-name disk=ssd
# Remove labels
kubectl label pod pod-name app-
kubectl label node node-name disk-
# Update labels
kubectl label pod pod-name app=apache --overwrite
# Select resources by labels
kubectl get pods -l app=nginx
kubectl get pods -l env=production
kubectl get pods -l 'app in (nginx,apache)'
kubectl get pods -l app=nginx,env=production
kubectl get pods -l app!=nginx
Label Selectors
# Equality-based selectors
kubectl get pods -l environment=production
kubectl get pods -l tier!=frontend
# Set-based selectors
kubectl get pods -l 'environment in (production,qa)'
kubectl get pods -l 'tier notin (frontend,backend)'
kubectl get pods -l partition
kubectl get pods -l '!partition'
# Multiple selectors
kubectl get pods -l app=nginx,env=prod
kubectl get pods -l 'app in (nginx,apache),env=prod'
Resource Management
Apply and Create
# Create resources
kubectl create -f manifest.yaml
kubectl create -f directory/
kubectl create -f https://example.com/manifest.yaml
# Apply resources (create or update)
kubectl apply -f manifest.yaml
kubectl apply -f directory/
kubectl apply -R -f directory/ # Recursive
# Apply with record (for rollback)
kubectl apply -f manifest.yaml --record
# Dry run
kubectl apply -f manifest.yaml --dry-run=client
kubectl apply -f manifest.yaml --dry-run=server
Update and Patch
# Replace resource
kubectl replace -f manifest.yaml
# Patch resources
kubectl patch pod pod-name -p '{"spec":{"containers":[{"name":"nginx","image":"nginx:1.21"}]}}'
kubectl patch pod pod-name --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"nginx:1.21"}]'
# Edit resources
kubectl edit pod pod-name
kubectl edit deployment deployment-name
Delete Resources
# Delete by name
kubectl delete pod pod-name
kubectl delete service service-name
# Delete by file
kubectl delete -f manifest.yaml
# Delete by label
kubectl delete pods -l app=nginx
# Delete all pods
kubectl delete pods --all
# Delete with grace period
kubectl delete pod pod-name --grace-period=30
kubectl delete pod pod-name --grace-period=0 --force
# Delete and wait
kubectl delete pod pod-name --wait=true
Quick Reference
Essential Commands
kubectl get pods- List podskubectl describe pod name- Pod detailskubectl logs pod-name- View logskubectl exec -it pod-name -- /bin/bash- Access pod shellkubectl apply -f file.yaml- Apply manifestkubectl delete pod name- Delete podkubectl get all- List all resources
Resource Shortcuts
po= podssvc= servicesdeploy= deploymentsrs= replicasetscm= configmapssecrets= secretsns= namespacespv= persistentvolumespvc= persistentvolumeclaims
Common Patterns
# Quick pod for testing
kubectl run test --image=busybox --rm -it -- /bin/sh
# Generate YAML template
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
# Watch resources
kubectl get pods -w
kubectl get pods -w -o wide
# Sort by creation time
kubectl get pods --sort-by=.metadata.creationTimestamp
# Custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
Troubleshooting Commands
kubectl describe pod name- Detailed pod informationkubectl logs pod-name- Container logskubectl get events- Cluster eventskubectl top pods- Resource usage (requires metrics-server)kubectl exec -it pod-name -- /bin/bash- Debug inside container