kubectl exec — executing commands in a Pod.
1# Basic exec2kubectl exec pod_name -- ls -la3kubectl exec pod_name -- cat /etc/config.yaml4kubectl exec pod_name -- ps aux56# Interactive shell7kubectl exec -it pod_name -- sh # Alpine8kubectl exec -it pod_name -- bash # Ubuntu/Debian910# Multi-container Pod11kubectl exec -it pod_name -c container_name -- sh1213# Namespace14kubectl exec -it pod_name -n production -- sh1516# Debugging17kubectl exec -it pod_name -- env18kubectl exec -it pod_name -- cat /etc/resolv.conf19kubectl exec -it pod_name -- nslookup kubernetes.default20kubectl exec -it pod_name -- curl localhost:30002122# Copying files23kubectl cp pod_name:/path/to/file ./local_file24kubectl cp ./local_file pod_name:/path/to/file2526# Tunneling27kubectl port-forward pod_name 3000:300028kubectl port-forward svc/service_name 8080:80
Use cases: