kubernetes Commands

Resource Management

kubectl get

Display one or many resources.

kubectl get

Examples:
List all pods
kubectl get pods
List pods in specific namespace
kubectl get pods -n default
List pods with wide output
kubectl get pods -o wide
kubectl apply

Apply a configuration to a resource by filename or stdin.

kubectl apply

Examples:
Apply from file
kubectl apply -f deployment.yaml
Apply from directory
kubectl apply -f ./k8s/
Dry run
kubectl apply -f deployment.yaml --dry-run=client
kubectl create

Create a resource by filename or stdin.

kubectl create

Examples:
Create deployment
kubectl create deployment nginx --image=nginx
Create namespace
kubectl create namespace my-namespace
kubectl delete

Delete resources by filenames, stdin, resources and names, or by resources and label selector.

kubectl delete

Examples:
Delete by file
kubectl delete -f deployment.yaml
Delete pod by name
kubectl delete pod my-pod
Delete all pods
kubectl delete pods --all
kubectl describe

Show details of a specific resource or group of resources.

kubectl describe

Examples:
Describe pod
kubectl describe pod my-pod
Describe service
kubectl describe service my-service
Describe node
kubectl describe node my-node

Monitoring

kubectl logs

Print the logs for a container in a pod.

kubectl logs

Examples:
View pod logs
kubectl logs my-pod
Follow logs
kubectl logs -f my-pod
Show last 100 lines
kubectl logs --tail=100 my-pod
kubectl top

Show resource usage (CPU/memory/storage) for nodes or pods.

kubectl top

Examples:
Show node resource usage
kubectl top nodes
Show pod resource usage
kubectl top pods

Debugging

kubectl exec

Execute a command in a container.

kubectl exec

Examples:
Execute command in pod
kubectl exec my-pod -- ls /app
Open shell in pod
kubectl exec -it my-pod -- /bin/bash

Networking

kubectl port-forward

Forward one or more local ports to a pod.

kubectl port-forward

Examples:
Forward port
kubectl port-forward my-pod 8080:80
Forward multiple ports
kubectl port-forward my-pod 8080:80 8443:443

Scaling

kubectl scale

Set a new size for a deployment, replica set, or replication controller.

kubectl scale

Examples:
Scale deployment
kubectl scale deployment my-deployment --replicas=3
Scale replica set
kubectl scale replicaset my-replicaset --replicas=5

Deployment

kubectl rollout

Manage the rollout of a deployment.

kubectl rollout

Examples:
Check rollout status
kubectl rollout status deployment/my-deployment
Rollback deployment
kubectl rollout undo deployment/my-deployment
Pause rollout
kubectl rollout pause deployment/my-deployment

Configuration

kubectl config

Modify kubeconfig files.

kubectl config

Examples:
View current context
kubectl config current-context
List contexts
kubectl config get-contexts
Switch context
kubectl config use-context my-context

File Management

kubectl cp

Copy files and directories to and from containers.

kubectl cp

Examples:
Copy file to pod
kubectl cp local-file.txt my-pod:/app/
Copy file from pod
kubectl cp my-pod:/app/log.txt ./local-log.txt