Labs ICT
โญ Pro Login

kubectl Command Line

The essential tool for interacting with Kubernetes clusters

kubectl โ€” The Command Line Tool

kubectl is the command-line tool for interacting with Kubernetes clusters. It sends commands to the API server and displays the results.

If you learn nothing else, learn kubectl. It is your primary interface with Kubernetes.

Essential Commands


# Cluster information
kubectl cluster-info
kubectl get nodes

# Work with resources
kubectl get pods                    # List pods
kubectl get pods -o wide            # List pods with extra info
kubectl get pods -n kube-system     # List pods in a namespace
kubectl get all                     # List all resources

# Detailed information
kubectl describe pod my-pod
kubectl describe node my-node

# Create/Apply resources
kubectl apply -f manifest.yaml      # Apply a manifest (create or update)
kubectl create -f manifest.yaml     # Create only (fails if exists)

# Delete resources
kubectl delete pod my-pod
kubectl delete -f manifest.yaml
kubectl delete pods --all           # Delete all pods

Debugging and Inspection


# View logs
kubectl logs my-pod
kubectl logs my-pod -c my-container  # Specific container in a multi-container pod
kubectl logs -f my-pod               # Follow/stream logs
kubectl logs --previous my-pod       # Logs from crashed pod

# Execute commands
kubectl exec -it my-pod -- /bin/bash       # Interactive shell
kubectl exec my-pod -- ls /app             # Run a command

# Port forwarding
kubectl port-forward my-pod 8080:80        # Forward local 8080 to pod's 80

# Copy files
kubectl cp my-pod:/app/logs.txt ./logs.txt

Output Formats


# JSON output
kubectl get pod my-pod -o json

# YAML output
kubectl get pod my-pod -o yaml

# Custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase

# JSONPath
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

# Watch for changes
kubectl get pods -w

Useful Aliases


# Add these to your shell profile (~/.bashrc or ~/.zshrc)
alias k=kubectl
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kgn='kubectl get nodes'
alias kd='kubectl describe'
alias kaf='kubectl apply -f'
alias kdf='kubectl delete -f'

# Auto-complete
source <(kubectl completion bash)

๐Ÿงช Quick Quiz

What is kubectl?