23
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker-composeのログを雑にCloudWatchに投げつける

Last updated at Posted at 2017-07-20

docker-composeでオーケストレーションしているコンテナたちのログを、CloudWatch Logsに集約したいので、
loggingのawslogs driverを使ってみました。

Compose.png

経緯

  • docker-composeでオーケストレーションしているコンテナたちのログを集約したい
  • cloudwatch logsに投げつけたい おりゃ!(イラスト作りたかった)

環境

  • amazon linux: cloudwatch logsのroleをつける
  • docker: 17.03
  • docker-compose: 1.9.0

docker-compose.yml

  • driverに awslogsを指定する
  • optionsにregion,group名,stream名を指定する
logging:
  driver: "awslogs"
  options:
    awslogs-region: "ap-northeast-1"
    awslogs-group: "${RAILS_ENV}"
    awslogs-stream: "nginx"

Dockerfile

  • 必要ならばログを出したいファイルにリンクを貼るとログが出力される
RUN ln -sf  /dev/stdout /var/log/HOGOHOGE/app-access.log
RUN ln -sf  /dev/stderr /var/log/HOGEHOGE/app-error.log

cloud watch

  • 空のgreoupを作成しておかないと、以下のエラーが出てしまいます
ERROR: for redis  Cannot start service redis: Failed to initialize logging driver: ResourceNotFoundException: The specified log group does not exist.
	status code: 400, request id: xxxxxx-xxxxxxxxx-xxxxxxxx-xxxxx

ERROR: for db  Cannot start service db: Failed to initialize logging driver: ResourceNotFoundException: The specified log group does not exist.
	status code: 400, request id: xxxxxx-xxxxxxxxx-xxxxxxxx-xxxxx
スクリーンショット 2017-07-20 16.47.48.png

動かしてみる

$ docker-compose up

ログ集約できた

  • いちいちdocker execしなくてもよくなった
スクリーンショット 2017-07-20 16.48.03.png スクリーンショット 2017-07-20 16.47.36.png

docker-compose.yml全体(dbとredisに意味はない)

docker-compose.yml
version: '2'
services:
  web:
    image: nginx
    depends_on:
      - db
      - redis
    ports:
      - "80:80"
    logging:
      driver: "awslogs"
      options:
        awslogs-region: "ap-northeast-1"
        awslogs-group: "develop"
        awslogs-stream: "nginx"
  redis:
    image: redis
    logging:
      driver: "awslogs"
      options:
        awslogs-region: "ap-northeast-1"
        awslogs-group: "develop"
        awslogs-stream: "redis"
  db:
    image: postgres
    logging:
      driver: "awslogs"
      options:
        awslogs-region: "ap-northeast-1"
        awslogs-group: "develop"
        awslogs-stream: "posgre"

まとめ

  • とてもかんたんですね!!
  • これでログとしては十分かと!
    あとの解析はまた、cloudwatchから何かに投げつければいいかなーと!!
23
27
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
23
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?