0

I would like to see the logs for a microservice with the latest version and send its content to a file according to the date.

user@MacBook-Pro ~ % kubectl -n bci-api get pods | grep ms-example-microservices                                             
ms-example-microservices-neg-re-v1-7-56bfd9f6c7-kjb24            1/1     Running            1 (6h39m ago)     6h47m
ms-example-microservices-neg-re-v2-0-66d88b48fb-9ttcf            1/1     Running            0                 5h14m
ms-example-microservices-neg-re-v2-1-6d8749dfb8-d42jk            1/1     Running            0                 6h26m
ms-example-microservices-neg-re-v2-2-849c97f6c-dnp45             1/1     Running            0                 4h53m
ms-example-microservices-neg-re-v2-3-db6dc776c-x45jl             1/1     Running            0                 5h50m
user@MacBook-Pro ~ % kubectl logs -f -n bci-api ms-example-microservices-neg-re-v2-3-db6dc776c-x45jl > pf_v2-3.2022-07-14.log

I would like to select the latest version (for this example, v2-3), and later create a file with the date.  Is it possible to do it in a single line?

user@MacBook-Pro ~ % kubectl -n bci-api get pods | grep ms-example-microservices | tail -n 1 | awk '{print $1}'    
ms-example-microservices-neg-re-v2-3-db6dc776c-x45jl
user@MacBook-Pro ~ % 

Now creating the date-time for name:

user@MacBook-Pro ~ % $(date +"%Y%m%d_%H%M%S")
zsh: command not found: 20220714_172238
user@MacBook-Pro ~ % 

I was trying the nested command with:

kubectl logs -f -n bci-api $(kubectl -n bci-api get pods | grep ms-example-microservices | tail -n 1 | awk '{print $1}') > "pf_$(date +"%Y%m%d_%H%M%S").log"

Unfortunately I don't know how to select the version v2-3 for the name.

0

2 Answers 2

0

Since you are calling awk, the whole line:

kubectl -n bci-api get pods | grep ms-example-microservices | tail -n 1 | awk '{print $1}'

Could be replaced by

kubectl -n bci-api get pods |
awk '/ms-example-microservices/{last=$1}END{print last}'

Now, to select the version I am assuming that the first string will have the same number of - delimited parts and I will select the part number 6 and number 7:

kubectl -n bci-api get pods |
awk -F'[ -]*' '/ms-example-microservices/{last=$6"-"$7}END{print last}'

And to add the date (with the simplified %F_%T format:

$ printf '%s-%s\n' "$(
         kubectl -n bci-api get pods | 
         awk -F"[ -]" '/ms-example-microservices/{last=$6"-"$7}END{print last}'
       )"   "$(date +'%F-%T')"

v2-3-2022-07-15-04:40:09
0

Kindly try with Below command it works fine

varse=$(kubectl -n bci-api get pods|awk -F "-" '/ms-example-microservices/{x=$0}END{print $6"-"$7}')
kubectl -n bci-api get pods|awk '/ms-example-microservices/{x=$0}END{print }'  >pf_"$varse"_$(date +%Y-%m-$d).log
1
  • No, add a last line like this-line-is-incorrect-and-should-not-be-printed and try again. The idea is to print the last line, yes, but the last line that matched ms-example-microservices. Jul 15, 2022 at 10:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .