|
| 1 | +# Tailing sidecar container image |
| 2 | + |
| 3 | +**tailing sidecar container image** is a an image which can be used to manually extend Pods by [streaming sidecar containers](https://kubernetes.io/docs/concepts/cluster-administration/logging/#streaming-sidecar-container). |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +To understand benefits of using tailing sidecar see example below. |
| 8 | + |
| 9 | +### Extend Pod by adding tailing sidecars |
| 10 | + |
| 11 | +Assuming that container writes logs to two different files and Pod has this specification: |
| 12 | + |
| 13 | +```yaml |
| 14 | +apiVersion: v1 |
| 15 | +kind: Pod |
| 16 | +metadata: |
| 17 | + name: example-with-tailling-sidecars |
| 18 | +spec: |
| 19 | + containers: |
| 20 | + - name: count |
| 21 | + image: busybox |
| 22 | + args: |
| 23 | + - /bin/sh |
| 24 | + - -c |
| 25 | + - > |
| 26 | + i=0; |
| 27 | + while true; |
| 28 | + do |
| 29 | + echo "example1: $i $(date)" >> /var/log/example1.log; |
| 30 | + echo "example2: $i $(date)" >> /var/log/example2.log; |
| 31 | + i=$((i+1)); |
| 32 | + sleep 1; |
| 33 | + done |
| 34 | + volumeMounts: |
| 35 | + - name: varlog |
| 36 | + mountPath: /var/log |
| 37 | + volumes: |
| 38 | + - name: varlog |
| 39 | + emptyDir: {} |
| 40 | +``` |
| 41 | +
|
| 42 | +Pod can be extended by adding tailing sidecar containers for easier log access: |
| 43 | +
|
| 44 | +```yaml |
| 45 | +apiVersion: v1 |
| 46 | +kind: Pod |
| 47 | +metadata: |
| 48 | + name: example-with-tailling-sidecars |
| 49 | +spec: |
| 50 | + containers: |
| 51 | + - name: count |
| 52 | + image: busybox |
| 53 | + args: |
| 54 | + - /bin/sh |
| 55 | + - -c |
| 56 | + - > |
| 57 | + i=0; |
| 58 | + while true; |
| 59 | + do |
| 60 | + echo "example1: $i $(date)" >> /var/log/example1.log; |
| 61 | + echo "example2: $i $(date)" >> /var/log/example2.log; |
| 62 | + i=$((i+1)); |
| 63 | + sleep 1; |
| 64 | + done |
| 65 | + volumeMounts: |
| 66 | + - name: varlog |
| 67 | + mountPath: /var/log |
| 68 | + - name: sidecar1 |
| 69 | + image: ghcr.io/sumologic/tailing-sidecar:latest |
| 70 | + env: |
| 71 | + - name: PATH_TO_TAIL |
| 72 | + value: /var/log/example1.log |
| 73 | + - name: LOG_LEVEL |
| 74 | + value: warning |
| 75 | + volumeMounts: |
| 76 | + - name: varlog |
| 77 | + mountPath: /var/log |
| 78 | + - name: volume-sidecar-1 |
| 79 | + mountPath: /tailing-sidecar/var |
| 80 | + - name: sidecar2 |
| 81 | + image: ghcr.io/sumologic/tailing-sidecar:latest |
| 82 | + env: |
| 83 | + - name: PATH_TO_TAIL |
| 84 | + value: /var/log/example2.log |
| 85 | + - name: LOG_LEVEL |
| 86 | + value: warning |
| 87 | + volumeMounts: |
| 88 | + - name: varlog |
| 89 | + mountPath: /var/log |
| 90 | + - name: volume-sidecar-2 |
| 91 | + mountPath: /tailing-sidecar/var |
| 92 | + volumes: |
| 93 | + - name: varlog |
| 94 | + emptyDir: {} |
| 95 | + - name: volume-sidecar-1 |
| 96 | + hostPath: |
| 97 | + path: /var/log/sidecar1 |
| 98 | + type: DirectoryOrCreate |
| 99 | + - name: volume-sidecar-2 |
| 100 | + hostPath: |
| 101 | + path: /var/log/sidecar2 |
| 102 | + type: DirectoryOrCreate |
| 103 | +``` |
| 104 | +
|
| 105 | +Notice that tailing sidecar containers are configured through two environmental variables: |
| 106 | +
|
| 107 | +- `PATH_TO_TAIL` - pattern specifying a log file or multiple ones through the use of common wildcards, |
| 108 | + multiple patterns separated by commas are also allowed |
| 109 | +- `LOG_LEVEL` - verbosity level, by default 'warning' is set, |
| 110 | + allowed values: error, warning, info, debug, trace |
| 111 | +- `OTEL_FILE_STORAGE_PATH` - path to directory where filelog reciever stores data, |
| 112 | +- `SIDECAR_OTEL_LOG_PATH` - dir path for otel collector own logs. Logs will be in otel.log file inside this directory |
| 113 | + |
| 114 | + |
| 115 | +Try it! |
| 116 | + |
| 117 | +```bash |
| 118 | +kubectl apply -f sidecar/examples/pod_with_tailing_sidecars.yaml |
| 119 | +``` |
| 120 | + |
| 121 | +And check logs: |
| 122 | + |
| 123 | +```bash |
| 124 | +$ kubectl logs example-with-tailling-sidecars sidecar1 |
| 125 | +example1: 0 Wed Jan 27 11:59:28 UTC 2021 |
| 126 | +example1: 1 Wed Jan 27 11:59:29 UTC 2021 |
| 127 | +example1: 2 Wed Jan 27 11:59:30 UTC 2021 |
| 128 | +example1: 3 Wed Jan 27 11:59:31 UTC 2021 |
| 129 | +... |
| 130 | +``` |
| 131 | + |
| 132 | +```bash |
| 133 | +$ kubectl logs example-with-tailling-sidecars sidecar2 |
| 134 | +example2: 0 Wed Jan 27 11:59:28 UTC 2021 |
| 135 | +example2: 1 Wed Jan 27 11:59:29 UTC 2021 |
| 136 | +example2: 2 Wed Jan 27 11:59:30 UTC 2021 |
| 137 | +example2: 3 Wed Jan 27 11:59:31 UTC 2021 |
| 138 | +... |
| 139 | +``` |
| 140 | + |
| 141 | +## Run tailing sidecar in Docker container |
| 142 | + |
| 143 | +To run tailing sidecar in Docker container define following variables: |
| 144 | + |
| 145 | +- `DIR_TO_TAIL` - path to directory with files to read |
| 146 | +- `OTEL_FILE_STORAGE_PATH` - path to directory where filelog reciever stores data, |
| 147 | +- `FILES_PATTERN` - pattern to match files in directory specified as `DIR_TO_TAIL` |
| 148 | +- `TAILING_SIDECAR_IMAGE` - tailing sidecar Docker image |
| 149 | +- `SIDECAR_OTEL_LOG_PATH` - dir path for otel own logs. Logs will be in otel.log file inside this directory |
| 150 | +- `LOG_LEVEL` - verbosity level, by default 'warning' is set, |
| 151 | + allowed values: error, warning, info, debug, trace |
| 152 | + |
| 153 | +e.g. |
| 154 | + |
| 155 | +```bash |
| 156 | +export DIR_TO_TAIL="$PWD/examples" |
| 157 | +export OTEL_FILE_STORAGE_PATH="$PWD/var" |
| 158 | +export FILES_PATTERN="*.log" |
| 159 | +export TAILING_SIDECAR_IMAGE="ghcr.io/sumologic/tailing-sidecar:latest" |
| 160 | +export LOG_LEVEL="warning" |
| 161 | +export SIDECAR_OTEL_LOG_PATH="$PWD/var" |
| 162 | +``` |
| 163 | + |
| 164 | +And run tailing sidecar in Docker container: |
| 165 | + |
| 166 | +```bash |
| 167 | +docker run --rm -it \ |
| 168 | + -v ${DIR_TO_TAIL}:/tmp/host \ |
| 169 | + -v ${$SIDECAR_OTEL_LOG_PATH}:/tailing-sidecar/var/log \ |
| 170 | + -v ${OTEL_FILE_STORAGE_PATH}:/tailing-sidecar/var \ |
| 171 | + --env "PATH_TO_TAIL=/tmp/host/${FILES_PATTERN}" \ |
| 172 | + --env "SIDECAR_OTEL_LOG_PATH=$SIDECAR_OTEL_LOG_PATH" \ |
| 173 | + --env "OTEL_FILE_STORAGE_PATH=$OTEL_FILE_STORAGE_PATH" \ |
| 174 | + --env "LOG_LEVEL=${LOG_LEVEL}" ${TAILING_SIDECAR_IMAGE} |
| 175 | +``` |
| 176 | + |
| 177 | +## Build and run tailing sidecar in Docker container |
| 178 | + |
| 179 | +To build and run Docker container with tailing sidecar to tail files in `$PWD/examples` |
| 180 | +which match pattern `*.log` and save Fluent Bit database in `$PWD/var`: |
| 181 | + |
| 182 | +```bash |
| 183 | +make run \ |
| 184 | + TAG=sidecar:dev \ |
| 185 | + DIR_TO_TAIL="$PWD/examples" \ |
| 186 | + FILES_PATTERN="*.log" \ |
| 187 | + OTEL_FILE_STORAGE_PATH="$PWD/var" \ |
| 188 | + SIDECAR_OTEL_LOG_PATH="$PWD/var" \ |
| 189 | + LOG_LEVEL="warning" |
| 190 | +``` |
| 191 | + |
| 192 | +## Build and push tailing sidecar to Docker registry |
| 193 | + |
| 194 | +To build Docker image with tailing sidecar: |
| 195 | + |
| 196 | +```bash |
| 197 | +make build TAG=<DOCKER_IMAGE_TAG> |
| 198 | +``` |
| 199 | + |
| 200 | +e.g. |
| 201 | + |
| 202 | +```bash |
| 203 | +make build TAG=localhost:32000/sumologic/tailing-sidecar:latest |
| 204 | +``` |
| 205 | + |
| 206 | +To push Docker image to container registry: |
| 207 | + |
| 208 | +```bash |
| 209 | +make push TAG=<DOCKER_IMAGE_TAG> |
| 210 | +``` |
| 211 | + |
| 212 | +e.g. |
| 213 | + |
| 214 | +```bash |
| 215 | +make push TAG=localhost:32000/sumologic/tailing-sidecar:latest |
| 216 | +``` |
| 217 | + |
| 218 | +## Testing in Vagrant environment |
| 219 | + |
| 220 | +### Prerequisites |
| 221 | + |
| 222 | +Please install the following: |
| 223 | + |
| 224 | +- [VirtualBox](https://www.virtualbox.org/) |
| 225 | +- [Vagrant](https://www.vagrantup.com/) |
| 226 | +- [vagrant-disksize](https://github.com/sprotheroe/vagrant-disksize) plugin |
| 227 | + |
| 228 | +### Setting up |
| 229 | + |
| 230 | +Start and provision the Vagrant environment: |
| 231 | + |
| 232 | +```bash |
| 233 | +vagrant up |
| 234 | +``` |
| 235 | + |
| 236 | +Connect to virtual machine: |
| 237 | + |
| 238 | +```bash |
| 239 | +vagrant ssh |
| 240 | +``` |
| 241 | + |
| 242 | +### Build and run tailing sidecar |
| 243 | + |
| 244 | +Build and push docker image to local container registry: |
| 245 | + |
| 246 | +```bash |
| 247 | +/tailing-sidecar/sidecar/otelcol/Makefile |
| 248 | +``` |
| 249 | + |
| 250 | +Run example Pod: |
| 251 | + |
| 252 | +```bash |
| 253 | +kubectl apply -f /tailing-sidecar/sidecar/otel/examples/pod_with_tailing_sidecars.yaml |
| 254 | +``` |
0 commit comments