Kubernetes

Essential Kubernetes commands and configurations for container orchestration and cluster management.

cli
kubernetesk8scontainersdevopsorchestration

Cluster Information

# View cluster info
kubectl cluster-info

# Get all nodes
kubectl get nodes

# Get node details
kubectl describe node <node-name>

# View kubectl configuration
kubectl config view

# List available contexts
kubectl config get-contexts

# Switch context
kubectl config use-context <context-name>

Pods

# List all pods
kubectl get pods

# List pods in all namespaces
kubectl get pods --all-namespaces

# Get pod details
kubectl describe pod <pod-name>

# Create pod from YAML
kubectl apply -f pod.yaml

# Delete a pod
kubectl delete pod <pod-name>

# Get pod logs
kubectl logs <pod-name>

# Follow pod logs
kubectl logs -f <pod-name>

# Get logs from specific container
kubectl logs <pod-name> -c <container-name>

# Execute command in pod
kubectl exec -it <pod-name> -- /bin/bash

Deployments

# List deployments
kubectl get deployments

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

# Apply deployment from file
kubectl apply -f deployment.yaml

# Scale deployment
kubectl scale deployment <name> --replicas=3

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

# Rollout status
kubectl rollout status deployment/<name>

# Rollout history
kubectl rollout history deployment/<name>

# Rollback deployment
kubectl rollout undo deployment/<name>

# Delete deployment
kubectl delete deployment <name>

Services

# List services
kubectl get services

# Expose deployment as service
kubectl expose deployment <name> --port=80 --type=LoadBalancer

# Get service details
kubectl describe service <name>

# Delete service
kubectl delete service <name>

Namespaces

# List namespaces
kubectl get namespaces

# Create namespace
kubectl create namespace <name>

# Set default namespace
kubectl config set-context --current --namespace=<name>

# Delete namespace
kubectl delete namespace <name>

# Get resources in namespace
kubectl get all -n <namespace>

ConfigMaps & Secrets

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

# Create ConfigMap from file
kubectl create configmap <name> --from-file=<path>

# Get ConfigMaps
kubectl get configmaps

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

# Get Secrets
kubectl get secrets

# Decode Secret
kubectl get secret <name> -o jsonpath='{.data.key}' | base64 --decode

Resource Management

# Get all resources
kubectl get all

# Get resources with labels
kubectl get pods -l app=myapp

# Add label to resource
kubectl label pod <pod-name> env=prod

# Remove label
kubectl label pod <pod-name> env-

# Get resource YAML
kubectl get pod <pod-name> -o yaml

# Edit resource
kubectl edit deployment <name>

# Delete resources by label
kubectl delete pods -l app=myapp

Debugging

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

# Describe resource for troubleshooting
kubectl describe pod <pod-name>

# Port forward to pod
kubectl port-forward <pod-name> 8080:80

# Run temporary debug pod
kubectl run debug --image=busybox -it --rm -- /bin/sh

# Copy files to/from pod
kubectl cp <pod-name>:/path/to/file ./local/path
kubectl cp ./local/path <pod-name>:/path/to/file

# Get resource usage
kubectl top nodes
kubectl top pods

YAML Templates

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