Writing to stdout/stderr in Docker / Kubernetes
April 23, 2025
note-to-self
k8s
docker
Recently I was testing some log aggregation, and I needed to write data to stdout/stderr so that the logging agent could send them to the aggregator. Here's a snippet of code to run that for awhile so you can test out various scenarios.
for i in $(seq 1 51); do
D=$(php -r "echo date('c');");
sleep 2 \
&& kubectl exec $(kubectl get pods -l app=app-name -o json | jq -r '.items[0].metadata.name') -- sh -c "echo '$D $F' >> /proc/1/fd/2" \
&& kubectl exec $(kubectl get pods -l app=app-name -o json | jq -r '.items[0].metadata.name') -- sh -c "echo '$G' >> /proc/1/fd/2" ;
done
Why? /dev/stdout and /dev/stderr seem to write back to the terminal or get lost altogether, but /proc/1/fd/1 is the output for 1 PID, which is the default process for the container.
Note: I set $F and $G with log data I was testing; basically just a log line starting with a date and then a stack trace.
F=$(< ~/f)
G=$(< ~/g)
aws ec2 describe-network-interfaces --filters Name=vpc-id,Values=vpc-47e4ee25 <<< check vpc being used or not
These posts are for my own understanding. Reader beware. Info may be wrong but it reflects my current understanding.