1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

syslogサーバをdockerで立てる

1
Posted at

syslogサーバ を docker-compose で起こしてみました

様々なサービスをdockerであげることにしましたが、そのログをsyslogにまとめて管理したくなってきました。

ディレクトリ構成

syslogsv/
├── docker-compose.yml
├── Dockerfile
├── rsyslog.conf
└── logs/          (実行後に自動生成されるログ保存先)

Dockerfile

rsyslog をインストールした軽量なイメージ

FROM alpine:latest

# rsyslogのインストール
RUN apk add --no-log rsyslog

# 設定ファイルをコピー
COPY rsyslog.conf /etc/rsyslog.conf

# 514ポートで待ち受けるために、フォアグラウンドで実行
CMD ["rsyslogd", "-n"]

rsyslog.conf

UDP/TCP での受信を有効にし、ログが /var/log/remote/[ホスト名]/[プログラム名].log のように整理されて保存される設定

# モジュールのロード
module(load="imudp")
input(type="imudp" port="514")

module(load="imtcp")
input(type="imtcp" port="514")

# ログの保存テンプレート定義
# リモートからのログをホスト名ごとに分けて保存する設定
template(name="RemoteLogs" type="string" string="/var/log/remote/%hostname%/%programname%.log")

# 全てのログに対してテンプレートを適用
*.* ?RemoteLogs

# 処理したログはここで停止(ローカルのsyslogに二重書き込みしないため)
& stop

docker-compose.yml

services:
  syslogsv:
    build: .
    container_name: syslogsv
    ports:
      - "514:514/udp"
      - "514:514/tcp"
    volumes:
      - ./.docker/logs:/var/log/remote
    restart: always

これで設定は完了です

補足

テストメッセージ

logger --tcp --server 127.0.0.1 -P 514 -p local0.info "Testing log tcp"
logger --udp --server 127.0.0.1 -P 514 -p local0.info "Testing log udp"

各docker-compose.yml

    logging:
      driver: "syslog"
      options:
        syslog-address: "udp://192.168.x.x:514"
        tag: "testserver_{{.Name}}"

ログのバックアップとローテート

vi /etc/logrotate.d/syslog-container
/home/shinichi/docker/syslogsv/.docker/logs/**/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 0644 root root
}

説明:上から

  • 毎日実行
  • 7日分残して古いものは削除
  • gzipで圧縮する
  • 直近のログは圧縮せず、次のローテーション時に圧縮(書き込み中のエラー防止)
  • ログファイルがなくてもエラーにしない
  • 空のファイルはローテーションしない
  • 新しいファイルを作成する権限
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?