k8s awesome (Kubernetes Practical Cheat Sheet)
DevOps

k8s awesome (Kubernetes Practical Cheat Sheet)

A small practical guide for everyday Kubernetes usage: copying files into containers, entering pods, and handling common real-world pitfalls.

Enter a container shell

  1. Verify context
    kubectl config get-contexts
    kubectl config use-context my_CTX
  2. List pods
    kubectl get pods -n my_NS
  3. Enter pod shell (single container)
    kubectl exec -it my_POD -n my_NS -- sh

    or, if multiple containers:

    kubectl exec -it my_POD -n my_NS -c container-name -- sh

    ALL in ONE command:

    
    kubectl config use-context my_CTX && kubectl exec -it $(kubectl get pods -n my_NS --sort-by=.metadata.creationTimestamp -o name | grep api | tail -1) -n my_NS -- sh
    

 

Copy file from local machine to a container

  1. Show all Kubernetes contexts and see which one is active (*)
    kubectl config get-contexts
  2. Switch to the required context
    kubectl config use-context my_CTX
  3. List namespaces (usually your application name)
    kubectl get namespaces
  4. List pods in the namespace
    kubectl get pods -n my_NS
  5. Check container names inside the pod
    kubectl get pod my_POD -n my_NS -o jsonpath='{.spec.containers[*].name}'
  6. Copy a file into the pod (single container)
    kubectl cp /path/to/local/file.txt my_NS/my_POD:/path/in/container
  7. Copy a file into a specific container (multiple containers)
    kubectl cp /path/to/local/file.txt my_NS/my_POD:/path/in/container -c container-name

Fix file permissions inside container (IMPORTANT)

⚠️ Important: After copying files, do not forget to apply correct ownership and permissions. Files copied with kubectl cp are often owned by root.

Example:

chown app-data:app-data /var/www/app/var/*.csv

Pro tips

  1. Debug containers are safer than touching app containers

    If your cluster supports it, prefer ephemeral debug containers:

    kubectl debug -it my_POD -n my_NS --image=busybox

 

 

Quote of the day:

Смертный, скользи по жизни, но не напирай на нее.
By den On February 10, 2026
145

Leave a reply