# kubectl
> Official Kubernetes command-line tool for cluster management

## Summary
Official Kubernetes CLI for deploying applications, managing cluster resources, viewing logs, and performing all operations against the Kubernetes API.

## Capabilities
- Create, update, and delete Kubernetes resources
- View pod logs and exec into running containers
- Apply manifests declaratively from YAML/JSON files
- Port-forward to services and pods for local access
- Manage contexts and switch between clusters and namespaces
- Scale deployments and manage rollouts
- Output resource details in JSON, YAML, or custom formats

## When to Use
- Managing Kubernetes clusters and workloads from the terminal
- Deploying applications defined in YAML manifests
- Debugging pods by viewing logs or exec-ing into containers
- Scripting Kubernetes operations in CI/CD pipelines
- Inspecting cluster state and resource details

## When NOT to Use
- Not using Kubernetes
- Want a visual/interactive experience for cluster management (use K9s or Lens)
- Managing Kubernetes packages with many resources (use Helm)

## Key Flags
- `apply -f`: Apply a configuration from a file or directory
- `get`: Display one or many resources
- `describe`: Show details of a specific resource
- `logs`: Print the logs for a container in a pod
- `exec -it`: Execute a command in a running container
- `port-forward`: Forward local ports to a pod
- `-o json`: Output resource information as JSON

## Usage Patterns
### Apply a manifest file
```
kubectl apply -f deployment.yaml
```
Creates or updates resources defined in the YAML file

### View pod logs
```
kubectl logs -f deployment/my-app
```
Streams logs from a deployment's pods

### Exec into a running pod
```
kubectl exec -it my-pod -- /bin/sh
```
Opens an interactive shell inside the running container

### Get resources with custom output
```
kubectl get pods -o wide --all-namespaces
```
Lists all pods across namespaces with extended info

## Input / Output
- **stdin**: Accepts manifests via stdin (e.g., kubectl apply -f -)
- **stdout**: Resource listings, logs, and command output
- **stderr**: Error messages and warnings

## Installation
- **homebrew**: `brew install kubectl`

## Related Tools
- [k9s](/tools/k9s) (complementary): K9s provides a TUI on top of kubectl for interactive cluster management
- [helm](/tools/helm) (complementary): Helm manages packaged Kubernetes applications, kubectl manages individual resources
- [pulumi](/tools/pulumi) (complementary): Pulumi provisions infrastructure including Kubernetes clusters that kubectl then manages

---
Source: https://littlescripts.sh/tools/kubectl
