tailing sidecar container image is a an image which can be used to manually extend Pods by streaming sidecar containers.
To understand benefits of using tailing sidecar see example below.
Assuming that container writes logs to two different files and Pod has this specification:
apiVersion: v1
kind: Pod
metadata:
name: example-with-tailling-sidecars
spec:
containers:
- name: count
image: busybox
args:
- /bin/sh
- -c
- >
i=0;
while true;
do
echo "example1: $i $(date)" >> /var/log/example1.log;
echo "example2: $i $(date)" >> /var/log/example2.log;
i=$((i+1));
sleep 1;
done
volumeMounts:
- name: varlog
mountPath: /var/log
volumes:
- name: varlog
emptyDir: {}
Pod can be extended by adding tailing sidecar containers for easier log access:
apiVersion: v1
kind: Pod
metadata:
name: example-with-tailling-sidecars
spec:
containers:
- name: count
image: busybox
args:
- /bin/sh
- -c
- >
i=0;
while true;
do
echo "example1: $i $(date)" >> /var/log/example1.log;
echo "example2: $i $(date)" >> /var/log/example2.log;
i=$((i+1));
sleep 1;
done
volumeMounts:
- name: varlog
mountPath: /var/log
- name: sidecar1
image: ghcr.io/sumologic/tailing-sidecar:latest
env:
- name: PATH_TO_TAIL
value: /var/log/example1.log
- name: LOG_LEVEL
value: warning
volumeMounts:
- name: varlog
mountPath: /var/log
- name: volume-sidecar1
mountPath: /tailing-sidecar/var
- name: sidecar2
image: ghcr.io/sumologic/tailing-sidecar:latest
env:
- name: PATH_TO_TAIL
value: /var/log/example2.log
- name: LOG_LEVEL
value: warning
volumeMounts:
- name: varlog
mountPath: /var/log
- name: volume-sidecar2
mountPath: /tailing-sidecar/var
volumes:
- name: varlog
emptyDir: {}
- name: volume-sidecar1
hostPath:
path: /var/log/sidecar1
type: DirectoryOrCreate
- name: volume-sidecar2
hostPath:
path: /var/log/sidecar2
type: DirectoryOrCreate
Notice that tailing sidecar containers are configured through two environmental variables:
PATH_TO_TAIL
- pattern specifying a log file or multiple ones through the use of common wildcards, multiple patterns separated by commas are also allowedLOG_LEVEL
- verbosity level, by default 'warning' is set, allowed values: error, warning, info, debug, trace
Try it!
kubectl apply -f sidecar/examples/pod_with_tailing_sidecars.yaml
And check logs:
$ kubectl logs example-with-tailling-sidecars sidecar1
example1: 0 Wed Jan 27 11:59:28 UTC 2021
example1: 1 Wed Jan 27 11:59:29 UTC 2021
example1: 2 Wed Jan 27 11:59:30 UTC 2021
example1: 3 Wed Jan 27 11:59:31 UTC 2021
...
$ kubectl logs example-with-tailling-sidecars sidecar2
example2: 0 Wed Jan 27 11:59:28 UTC 2021
example2: 1 Wed Jan 27 11:59:29 UTC 2021
example2: 2 Wed Jan 27 11:59:30 UTC 2021
example2: 3 Wed Jan 27 11:59:31 UTC 2021
...
To run tailing sidecar in Docker container define following variables:
DIR_TO_TAIL
- path to directory with files to readFLUENT_BIT_DB_DIR
- path to directory where Fluent Bit database will be created, Fluent Bit uses database to track monitored files and offsets, for details please see tail plugin documentationFILES_PATTERN
- pattern to match files in directory specified asDIR_TO_TAIL
TAILING_SIDECAR_IMAGE
- tailing sidecar Docker imageLOG_LEVEL
- verbosity level, by default 'warning' is set, allowed values: error, warning, info, debug, trace
e.g.
export DIR_TO_TAIL="$PWD/examples"
export FLUENT_BIT_DB_DIR="$PWD/var"
export FILES_PATTERN="*.log"
export TAILING_SIDECAR_IMAGE="ghcr.io/sumologic/tailing-sidecar:latest"
export LOG_LEVEL="warning"
And run tailing sidecar in Docker container:
docker run --rm -it \
-v ${DIR_TO_TAIL}:/tmp/host \
-v ${FLUENT_BIT_DB_DIR}:/tailing-sidecar/var \
--env "PATH_TO_TAIL=/tmp/host/${FILES_PATTERN}" \
--env "LOG_LEVEL=${LOG_LEVEL}" ${TAILING_SIDECAR_IMAGE}
To build and run Docker container with tailing sidecar to tail files in $PWD/examples
which match pattern *.log
and save Fluent Bit database in $PWD/var
:
make run \
TAG=sidecar:dev \
DIR_TO_TAIL="$PWD/examples" \
FILES_PATTERN="*.log" \
FLUENT_BIT_DB_DIR="$PWD/var" \
LOG_LEVEL="warning"
To build Docker image with tailing sidecar:
make build TAG=<DOCKER_IMAGE_TAG>
e.g.
make build TAG=localhost:32000/sumologic/tailing-sidecar:latest
To push Docker image to container registry:
make push TAG=<DOCKER_IMAGE_TAG>
e.g.
make push TAG=localhost:32000/sumologic/tailing-sidecar:latest
Please install the following:
- VirtualBox
- Vagrant
- vagrant-disksize plugin
Start and provision the Vagrant environment:
vagrant up
Connect to virtual machine:
vagrant ssh
Build and push docker image to local container registry:
/tailing-sidecar/sidecar/Makefile
Run example Pod:
kubectl apply -f /tailing-sidecar/sidecar/examples/pod_with_tailing_sidecars.yaml