Container Log on file

Logs on files

apiVersion: v1
kind: Pod
metadata:
  name: counter
spec:
  containers:
    - name: count
      image: busybox
      args:
        - /bin/sh
        - -c
        - >
          i=0;
          while true;
          do
            echo "$i: $(date)" >> /var/log/1.log;
            echo "$(date) INFO $i" >> /var/log/2.log;
            i=$((i+1));
            sleep 1;
          done
      volumeMounts:
        - name: varlog
          mountPath: /var/log
  volumes:
    - name: varlog
      hostPath:
        path: /tmp/k3d
kubectl apply -f counter-log.yaml

If you are running on k3d cluster,

docker ps
CONTAINER ID   IMAGE                      COMMAND                  CREATED       STATUS       PORTS                                                               NAMES
0a2156ea6ba3   rancher/k3d-proxy:v4.3.0   "/bin/sh -c nginx-pr…"   3 hours ago   Up 3 hours   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:50998->6443/tcp   k3d-dev-serverlb
2f44a924576c   rancher/k3s:latest         "/bin/k3s agent"         3 hours ago   Up 3 hours                                                                       k3d-dev-agent-1
67695f6fba1b   rancher/k3s:latest         "/bin/k3s agent"         3 hours ago   Up 3 hours                                                                       k3d-dev-agent-0
056e19f5b579   rancher/k3s:latest         "/bin/k3s server --t…"   3 hours ago   Up 3 hours   0.0.0.0:30000-30010->30000-30010/tcp                                k3d-dev-server-0
docker exec -it 2f44a924576c sh     
# cat /tmp/k3d/1.log 
0: Tue Apr 13 05:08:21 UTC 2021
1: Tue Apr 13 05:08:22 UTC 2021
2: Tue Apr 13 05:08:23 UTC 2021
3: Tue Apr 13 05:08:24 UTC 2021
4: Tue Apr 13 05:08:25 UTC 2021
5: Tue Apr 13 05:08:26 UTC 2021
6: Tue Apr 13 05:08:27 UTC 2021
7: Tue Apr 13 05:08:28 UTC 2021
8: Tue Apr 13 05:08:29 UTC 2021
9: Tue Apr 13 05:08:30 UTC 2021
10: Tue Apr 13 05:08:31 UTC 2021

Logs output

A sample pod that generates log output every second:

apiVersion: v1
kind: Pod
metadata:
  name: counter
spec:
  containers:
    - name: count
      image: busybox
      args:
        [
          /bin/sh,
          -c,
          'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done',
        ]
kubectl logs counter
kubectl logs counter -c count

0: Tue Apr 13 05:29:54 UTC 2021
1: Tue Apr 13 05:29:55 UTC 2021
2: Tue Apr 13 05:29:56 UTC 2021
3: Tue Apr 13 05:29:57 UTC 2021

Leave a Reply

Your email address will not be published.

ANOTE.DEV