dockerの開発環境は便利なのですが、トラブルの発生時にコンテナごとにエラーを確認することになるので、そこは少し不便です。 なので、そこの利便性を上げるためにFluentdを使います。 今回はnginx + pfhp-fpm + fluentdの構成でログを集約してみます。
ディレクトリ構造とファイルの配置の予定
my-app/ ├─dokcer/ │ ├─nginx/ │ │ ├─nginx.conf │ │ └─default.conf │ ├─php-fpm/ │ │ ├─php-extends.ini │ │ └─log.conf │ └─fluentd/ │ └─fluent.conf └─docker-compose.yml
ファイルの作成
nginx.conf
access_log、error_logを設定。 php-fpmとの接続を安定させるためにfastcgi_connect_timeout、fastcgi_read_timeout、fastcgi_send_timeoutを設定。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
sendfile on;
#tcp_nopush on;
keepalive_timeout 300;
send_timeout 300;
fastcgi_connect_timeout 300;
fastcgi_read_timeout 300;
fastcgi_send_timeout 300;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
default.conf
host.access.log、host.error.logを設定。 9000番にきたらphp-fpmへつなげる。
server {
listen 80;
listen [::]:80;
server_name ~^localhost$;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name ~^localhost$;
access_log /var/log/nginx/host.access.log;
error_log /var/log/nginx/host.error.log;
location / {
root /var/www/html/public;
index index.php index.html index.htm;
# リクエストされたファイルが存在しなければ、
# フロントコントローラーに内部リダイレクト
try_files $uri /index.php?$query_string;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /var/www/html/public;
fastcgi_pass localhost$:9000;
fastcgi_index index.php;
# - 全てのリクエストをフロントコントローラーで実行
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
include fastcgi_params;
}
}
php-extends.ini
phpエラーログとタイムゾーンの設定。
[date] date.timezone = "Asia/Tokyo" [php] log_errors = On error_log = /var/log/php-fpm/php.log
log.conf
php-fpmのエラーログを設定。
; The access log file ; Default: not set access.log = /var/log/php-fpm/access.log ; Error log file ; If it's set to "syslog", log is sent to syslogd instead of being written ; into a local file. ; Note: the default prefix is /usr/local/var ; Default Value: log/php-fpm.log error_log = /var/log/php-fpm/error.log
fluent.conf
fluentdでログを受け取る。
そのまま出力する設定。
docckerコンテナのログのタグと、matchを変更することで受け取るログを判別することもできる。
そのまま出力する設定。
docckerコンテナのログのタグと、matchを変更することで受け取るログを判別することもできる。
<source>
@type forward
port 24224
</source>
<match *>
@type stdout
</match>
docker-compose.ymlの作成
コンテナのloggingでdriverにfluentdを指定することで、fluentdにログを送ることができます。
version: '3'
services:
nginx:
build: nginx:1.23.3
container_name: nginx
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/local.conf:/etc/nginx/conf.d/default.conf
logging:
# ログ出力先にfluentdを指定
driver: "fluentd"
options:
# fluentdサーバー
fluentd-address: "localhost:24224"
# ログに付与するタグ
tag: "nginx"
fluentd-async-connect: "true"
depends_on:
- php-fpm
- fluent
php-fpm:
build: php:8.1.16-fpm
container_name: php-fpm
restart: always
volumes:
- ./php-fpm/php-extends.ini:/usr/local/etc/php/php-extends.ini
- ./php-fpm/log.conf:/usr/local/etc/php-fpm.d/log.conf
- ../:/var/www/html
logging:
# ログ出力先にfluentdを指定
driver: "fluentd"
options:
# fluentdサーバー
fluentd-address: "localhost:24224"
# ログに付与するタグ
tag: "php-fpm"
fluentd-async-connect: "true"
fluent:
image: "fluent/fluentd:v1.15-debian-1"
container_name: fluentd
ports:
- "24224:24224"
- "24224:24224/udp"
environment:
- 'FLUENTD_CONF=fluent.conf'
- 'TZ:Asia/Tokyo'
volumes:
- ./fluentd/config:/fluentd/etc/
コンテナを起動する
fluentdのログには、nginxとphp-fpmのログを取得する準備ができていることを確認できます。
起動したコンテナのログがfluentdで確認できるようになりました。
起動したコンテナのログがfluentdで確認できるようになりました。
2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: init supervisor logger path=nil rotate_age=nil rotate_size=nil 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: parsing config file is succeeded path="/fluentd/etc/fluent.conf" 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: gem 'fluentd' version '1.15.3' 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: using configuration file: <ROOT> 2023-09-25 12:16:25 <source> 2023-09-25 12:16:25 @type forward 2023-09-25 12:16:25 port 24224 2023-09-25 12:16:25 </source> 2023-09-25 12:16:25 <match nginx> 2023-09-25 12:16:25 @type stdout 2023-09-25 12:16:25 </match> 2023-09-25 12:16:25 <match php-fpm> 2023-09-25 12:16:25 @type stdout 2023-09-25 12:16:25 </match> 2023-09-25 12:16:25 </ROOT> 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: starting fluentd-1.15.3 pid=7 ruby="3.1.3" 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: spawn command to main: cmdline=["/usr/local/bin/ruby", "-Eascii-8bit:ascii-8bit", "/usr/local/bundle/bin/fluentd", "--config", "/fluentd/etc/fluent.conf", "--plugin", "/fluentd/plugins", "--under-supervisor"] 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: init supervisor logger path=nil rotate_age=nil rotate_size=nil 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: #0 init worker0 logger path=nil rotate_age=nil rotate_size=nil 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: adding match pattern="nginx" type="stdout" 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: adding match pattern="php-fpm" type="stdout" 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: adding source type="forward" 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: #0 starting fluentd worker pid=16 ppid=7 worker=0 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: #0 listening port port=24224 bind="0.0.0.0" 2023-09-25 12:16:25 2023-09-25 03:16:25 +0000 [info]: #0 fluentd worker is now running worker=0