Essential Kubernetes Commands

Namespace Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# List all namespaces
kubectl get namespaces

# Create a namespace
kubectl create namespace [namespace_name]
kubectl create ns devops --context myContext

# Delete a namespace
kubectl delete namespace devops

# Set default namespace for current context
kubectl config set-context --current --namespace=devops

Pod Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# List pods
kubectl get pods -A                    # All namespaces
kubectl get po -A                      # Short form
kubectl get pods -n kube-system        # Specific namespace
kubectl get pods -o wide              # Detailed pod information
kubectl get pods --watch              # Watch pod status changes
kubectl get pods -l label_name        # Filter pods by label

# Pod management
kubectl logs -f pod_name              # Follow pod logs
kubectl logs pod_name                 # View pod logs
kubectl exec -it pod_name -- /bin/sh  # Shell into pod
kubectl delete pods pod_name          # Delete pod
kubectl edit pod pod_name             # Edit pod configuration
kubectl cp pod_name:/src/path local_path  # Copy from pod

Deployment & ReplicaSet

1
2
3
4
5
6
# View deployments and replicasets
kubectl get deployment
kubectl get rs

# Scale deployment
kubectl scale deploy deployment_name --replicas=3

Service Operations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# List services
kubectl get services
kubectl get svc -w    # Watch service changes

# Get service details
kubectl get services -n namespace service_name -o yaml

# Access service (Minikube)
minikube service service_name --url
minikube service -n namespace_name service_name --url

Storage Operations

1
2
3
4
5
6
7
8
9
# Persistent Volumes
kubectl apply -f volume.yaml
kubectl get pv
kubectl delete pv pv_name

# Persistent Volume Claims
kubectl apply -f volumeClaim.yaml
kubectl get pvc
kubectl delete pvc pvc_name

Configuration & Secrets

1
2
3
4
5
6
7
8
9
# ConfigMaps
kubectl get configmap
kubectl apply -f configmap.yaml

# Secrets
kubectl apply -f secret.yaml
kubectl get secrets
kubectl get secrets secret_name -o yaml
kubectl get docker-registry -o yaml

Helm Commands

1
2
3
4
5
6
7
8
# Get values from release
helm get values release_name

# Show and save chart values
helm show values repo/chart > values.yaml

# Upgrade release with values
helm upgrade release_name repo/chart --version x.y.z -f values.yaml

RBAC (Role-Based Access Control)

Role Permissions

  • create
  • update
  • delete
  • get
  • list
  • patch
  • watch

Resource Types

1
2
3
4
kind: Role
rules:
- resources: ["pods", "services"]
  verbs: ["get", "list", "watch"]

Container Lifecycle

Probes

1
2
3
4
5
6
readinessProbe:
  exec:
    command:
      - health-check
      - get
      - status

Important Concepts

Resource Management

  • Kubernetes schedules containers based on resource requests, not limits
  • Best practice: Set equal values for requests and limits in production

Deployment Controller

  • Deployments create ReplicaSets
  • ReplicaSets manage pod lifecycle
  • Default policies:
    • imagePullPolicy: Always
    • restartPolicy: Always

Security Features

  • Resource Quotas: Limit resource usage per namespace
  • Pod Security Policies: Restrict pod privileges
  • RBAC: Control access to Kubernetes resources

Additional Tools

Security Scanning

  • Trivy: Container vulnerability scanner
  • Grype: Vulnerability scanner for containers

Observability

  • OpenTelemetry: Observability framework for cloud-native applications

Build Tools

  • Kaniko: Container image builder in Kubernetes

Configuration Location

  • Default config path: ~/.kube/config
  • Windows path: C:\Users\Username.kube