Link copied to clipboard
Kubernetes

Kubernetes Cheatsheet: kubectl Commands

Essential kubectl commands for managing Kubernetes clusters, pods, deployments, and services. Quick reference for DevOps.

·11 min read·
Kubernetes Cheatsheet: kubectl Commands

Kubernetes Cheatsheet

Essential kubectl commands for managing Kubernetes clusters, pods, deployments, and services.

Useful Kubernetes Resources


Cluster Information

kubectl cluster-info

Display cluster information

kubectl get nodes

List all nodes in the cluster

kubectl describe node <name>

Show detailed node information

kubectl get namespaces

List all namespaces

kubectl config view

Show kubectl configuration

kubectl config current-context

Show current context

kubectl config use-context <name>

Switch to a different context


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 more details

kubectl describe pod <name>

Show detailed pod information

kubectl logs <pod>

View pod logs

kubectl logs -f <pod>

Follow pod logs in real-time

kubectl logs <pod> -c <container>

View logs of specific container

kubectl exec -it <pod> -- /bin/bash

Execute shell inside pod

kubectl port-forward <pod> 8080:80

Forward local port to pod

kubectl delete pod <name>

Delete a pod

kubectl run nginx --image=nginx

Create a pod from an image


Deployments

kubectl get deployments

List all deployments

kubectl describe deployment <name>

Show deployment details

kubectl create deployment <name> --image=<image>

Create a deployment

kubectl scale deployment <name> --replicas=3

Scale deployment replicas

kubectl set image deployment/<name> <container>=<image>

Update container 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 revision

kubectl rollout restart deployment/<name>

Restart a deployment

kubectl delete deployment <name>

Delete a deployment


Services

kubectl get services

List all services

kubectl describe service <name>

Show service details

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

Expose deployment as a service

kubectl delete service <name>

Delete a service


ConfigMaps & Secrets

kubectl get configmaps

List all ConfigMaps

kubectl create configmap <name> --from-literal=key=value

Create ConfigMap from literal

kubectl create configmap <name> --from-file=<path>

Create ConfigMap from file

kubectl get secrets

List all secrets

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

Create a secret

kubectl describe secret <name>

Show secret details (not values)

kubectl get secret <name> -o jsonpath='{.data.key}' | base64 -d

Decode secret value


Apply & Create

kubectl apply -f <file.yaml>

Create/update resources from file

kubectl apply -f <directory>

Apply all files in directory

kubectl create -f <file.yaml>

Create resources from file

kubectl delete -f <file.yaml>

Delete resources from file

kubectl diff -f <file.yaml>

Show diff before applying


Labels & Selectors

kubectl get pods -l app=nginx

Get pods with specific label

kubectl get pods --selector=app=nginx

Same as above (full syntax)

kubectl label pod <name> env=prod

Add label to a pod

kubectl label pod <name> env-

Remove label from a pod

kubectl get pods --show-labels

Show all labels on pods


Resource Management

kubectl top nodes

Show node resource usage

kubectl top pods

Show pod resource usage

kubectl get events

List cluster events

kubectl get events --sort-by='.lastTimestamp'

List events sorted by time

kubectl api-resources

List all available resource types

kubectl explain pod

Show documentation for a resource


Debugging

kubectl describe pod <name>

Detailed pod information (events, status)

kubectl logs <pod> --previous

View logs from previous container

kubectl get pod <name> -o yaml

Get pod YAML definition

kubectl run debug --image=busybox -it --rm -- sh

Run temporary debug pod

kubectl cp <pod>:/path/file ./local-file

Copy file from pod to local

kubectl attach <pod> -it

Attach to running container


Namespaces

kubectl create namespace <name>

Create a namespace

kubectl get pods -n <namespace>

Get pods in specific namespace

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

Set default namespace for context

kubectl delete namespace <name>

Delete a namespace


YAML Templates

# Pod
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
# Service
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
  type: LoadBalancer

More articles