bashkuberneteskubectlcmder

kubectl: how to display pod logs without specyfing the pod name explicitly?


My pods have a dynamically generated ID appended to their names like i.e. my-app-name-7b587cd75b-dscsr which is different on every deployment (next time it could be my-app-name-xcgv83bfsd-4kjsf).

This makes using some commands really cumbersome, because every time I need to see the logs I have to list all pods first and copy-paste the changed name to the logs command: kubectl -n [namespace] logs my-app-name-7b587cd75b-dscsr.

Is there a way I can skip using the pod name or part of the name and do something like kubectl -n [namespace] logs my-pod-name-~ or kubectl -n [namespace] logs service/my-pod-name like in port-forward command?

I tried to inject grep inside the logs command to obtain the pod name and run logs in a single command, but Cmder on Windows, as great as it is, doesn't seem to support $(): kubectl -n [namespace] logs $(kubectl -n my-app-name get pod | grep my-app-name | sed 's/ .*//')


Solution

  • Rather than using POD/$POD_NAME, you can use Deployment/$DEPLOYMENT_NAME to fetch the logs of pods

    kubectl logs deployment/$DEPLOY_NAME

    
      # Return snapshot logs from container nginx-1 of a deployment named nginx
      kubectl logs deployment/nginx -c nginx-1
    
    

    kubectl logs --help will provide more info