Kubernetes Commands

kubectl commands and Kubernetes resource management.

Cluster Info

kubectl cluster-info

Display cluster information

kubectl version

Show client and server version

kubectl get nodes

List all nodes in cluster

kubectl get nodes -o wide

List nodes with additional info

kubectl describe node node-name

Show detailed node information

kubectl top nodes

Show node resource usage

Pods

kubectl get pods

List pods in current namespace

kubectl get pods -A

List pods in all namespaces

kubectl get pods -o wide

List pods with additional details

kubectl describe pod pod-name

Show detailed pod information

kubectl logs pod-name

View pod logs

kubectl logs -f pod-name

Follow pod logs in real-time

kubectl logs pod-name -c container-name

View logs from specific container

kubectl exec -it pod-name -- /bin/bash

Execute shell in pod

kubectl delete pod pod-name

Delete a pod

kubectl top pods

Show pod resource usage

Deployments

kubectl get deployments

List deployments

kubectl describe deployment deployment-name

Show deployment details

kubectl create deployment name --image=nginx

Create deployment

kubectl scale deployment name --replicas=3

Scale deployment to 3 replicas

kubectl set image deployment/name container=image:tag

Update deployment image

kubectl rollout status deployment/name

Check rollout status

kubectl rollout history deployment/name

View rollout history

kubectl rollout undo deployment/name

Rollback to previous version

kubectl delete deployment name

Delete deployment

Services

kubectl get services

List services

kubectl get svc

List services (short)

kubectl describe service service-name

Show service details

kubectl expose deployment name --port=80 --type=LoadBalancer

Expose deployment as service

kubectl delete service service-name

Delete service

kubectl get endpoints

List service endpoints

ConfigMaps & Secrets

kubectl get configmaps

List ConfigMaps

kubectl describe configmap name

Show ConfigMap details

kubectl create configmap name --from-file=file.txt

Create ConfigMap from file

kubectl get secrets

List secrets

kubectl describe secret name

Show secret details

kubectl create secret generic name --from-literal=key=value

Create secret from literal

kubectl delete configmap name

Delete ConfigMap

kubectl delete secret name

Delete secret

Namespaces

kubectl get namespaces

List all namespaces

kubectl get ns

List namespaces (short)

kubectl create namespace name

Create new namespace

kubectl delete namespace name

Delete namespace

kubectl config set-context --current --namespace=name

Set default namespace

kubectl get pods -n namespace-name

List pods in specific namespace

Resources & YAML

kubectl apply -f file.yaml

Apply configuration from file

kubectl delete -f file.yaml

Delete resources from file

kubectl get all

List all resources

kubectl get pod pod-name -o yaml

Export pod as YAML

kubectl get pod pod-name -o json

Export pod as JSON

kubectl explain pods

Show resource documentation

kubectl api-resources

List all resource types

Context & Config

kubectl config view

View kubeconfig

kubectl config get-contexts

List contexts

kubectl config current-context

Show current context

kubectl config use-context context-name

Switch to context

kubectl config set-context name --cluster=cluster --user=user

Create new context

Labels & Selectors

kubectl get pods -l app=nginx

Filter pods by label

kubectl get pods --show-labels

Show pod labels

kubectl label pods pod-name env=production

Add label to pod

kubectl label pods pod-name env-

Remove label from pod

Troubleshooting

kubectl get events

List cluster events

kubectl get events --sort-by=.metadata.creationTimestamp

List events sorted by time

kubectl describe pod pod-name

Debug pod issues

kubectl logs pod-name --previous

View logs from previous pod instance

kubectl port-forward pod-name 8080:80

Forward local port to pod

kubectl cp pod-name:/path/file ./file

Copy file from pod

kubectl debug pod-name -it --image=busybox

Debug pod with ephemeral container