Docker
Fluentd
Fluentd ロギング・ドライバを使ってDockerのアプリログをFluentdに流せるので試してみた。
今回はnginxのログを収集する。
fluent.confは以下のようにする。
<source>
@type forward
</source>
<match docker.**>
@type stdout
</match>
docker-compose.ymlは以下のようにする。
Dockerfileは前回のエントリーと同じであるため省略。
version: '3'
services:
fluentd:
build:
context: .
dockerfile: docker/fluentd/Dockerfile
environment:
- TZ=Asia/Tokyo
ports:
- 24224:24224
volumes:
- ./docker/fluentd/etc:/fluentd/etc
Fluentdを起動する。
stdoutにしているためバックグラウンド起動としていない。
docker-compose up
次にnginxもDockerで起動する。
ターミナルはFluentdとは別で起動する。
docker run -it -p 8080:80 \
--log-driver=fluentd \
--log-opt fluentd-address=localhost:24224 \
--log-opt tag=docker.{{.Name}} \
nginx:latest
起動したら、http://localhost:8080 にアクセスする。
さきほど起動したFluentd側のログが以下のようになれば、nginxのログが流れていることが確認できる。
fluentd_1 | 2020-07-26 05:52:31.000000000 +0000 docker.loving_lamarr: {"log":"/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration\r","container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout"}
fluentd_1 | 2020-07-26 05:52:31.000000000 +0000 docker.loving_lamarr: {"container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout","log":"/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/\r"}
fluentd_1 | 2020-07-26 05:52:31.000000000 +0000 docker.loving_lamarr: {"container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout","log":"/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh\r"}
fluentd_1 | 2020-07-26 05:52:31.000000000 +0000 docker.loving_lamarr: {"container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout","log":"10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf\r"}
fluentd_1 | 2020-07-26 05:52:31.000000000 +0000 docker.loving_lamarr: {"source":"stdout","log":"10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf\r","container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr"}
fluentd_1 | 2020-07-26 05:52:31.000000000 +0000 docker.loving_lamarr: {"container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout","log":"/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh\r"}
fluentd_1 | 2020-07-26 05:52:31.000000000 +0000 docker.loving_lamarr: {"container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout","log":"/docker-entrypoint.sh: Configuration complete; ready for start up\r"}
fluentd_1 | 2020-07-26 05:53:24.000000000 +0000 docker.loving_lamarr: {"container_name":"/loving_lamarr","source":"stdout","log":"172.17.0.1 - - [26/Jul/2020:05:53:24 +0000] \"GET / HTTP/1.1\" 304 0 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36\" \"-\"\r","container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775"}
fluentd_1 | 2020-07-26 05:53:27.000000000 +0000 docker.loving_lamarr: {"log":"172.17.0.1 - - [26/Jul/2020:05:53:27 +0000] \"GET / HTTP/1.1\" 304 0 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36\" \"-\"\r","container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout"}
fluentd_1 | 2020-07-26 05:53:28.000000000 +0000 docker.loving_lamarr: {"source":"stdout","log":"172.17.0.1 - - [26/Jul/2020:05:53:28 +0000] \"GET / HTTP/1.1\" 304 0 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36\" \"-\"\r","container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr"}
fluentd_1 | 2020-07-26 05:53:29.000000000 +0000 docker.loving_lamarr: {"log":"172.17.0.1 - - [26/Jul/2020:05:53:29 +0000] \"GET / HTTP/1.1\" 304 0 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36\" \"-\"\r","container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout"}
fluentd_1 | 2020-07-26 05:53:30.000000000 +0000 docker.loving_lamarr: {"log":"172.17.0.1 - - [26/Jul/2020:05:53:30 +0000] \"GET / HTTP/1.1\" 304 0 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36\" \"-\"\r","container_id":"19df98bb6889da6762b0643e9c827904334500da6b1cc6fc02627189cf0cf775","container_name":"/loving_lamarr","source":"stdout"}
Fluentdのportを公開することでnginxからFluentdへログを転送することが確認できた。
ただ、今回の設定だといろいろなログが混ざっているので、S3へのログアーカイブなどを行うにはfilterなどひと手間加える必要がありそう。
設定周りも今後調査していく。