My Note

自己理解のためのブログ

Stackdriver Loggingにnginxとjournalログを転送する

やったこと

google-fluentdのインストールとjournalログの転送は以下のブログで実施しました。

yhidetoshi.hatenablog.com

NginxのAccess/Errorログを転送する。

Ansibleで設定を行う。環境はUbuntu16.04で行った。

roles/nginx/
├── handlers
│   └── main.yml
├── tasks
│   └── main.yml
└── templates
    └── td-agent.conf.j2

■ tasks/main.yml

---
- name: add apt repo
  apt_repository:
    repo: "{{ item }}"
  with_items:
    - ppa:nginx/stable
  become: true


- name: install nginx
  apt:
    name: nginx
  become: true


- name: Copy fluentd server configuration
  template:
    src: td-agent.conf.j2
    dest: /etc/google-fluentd/config.d/nginx.conf
  notify: "Restart google-fluentd"
  become: yes


- name: Create systemd nginx snipet directory
  file:
    path: /etc/systemd/system/nginx.service.d
    state: directory
  become: true


- name: ensure nginx start
  systemd:
    state: started
    name: nginx.service
    daemon_reload: yes
  become: true


- name: Change mode log files of nginx
  file:
    path: "{{ item }}"
    state: file
    mode: 0644
  with_items:
    - /var/log/nginx/access.log
    - /var/log/nginx/error.log
  become: yes


- name: symlink nginx conf
  file:
    src: /etc/nginx/sites-available/default
    dest: /etc/nginx/sites-enabled/default
    state: link
  become: true
  notify: "restart nginx"

■ handlers//main.yml

---
- name: "restart nginx"
  block:
  - name: check config
    command: nginx -t
    check_mode: no
    changed_when: false
  - name: restart nginx
    systemd:
      name: nginx
      state: restarted
      daemon_reload: yes
    become: true


- name: "Restart google-fluentd"
  systemd:
    name: google-fluentd
    state: restarted
    daemon_reload: yes
  become: yes

■ templates/td-agent.conf.j2

<source>
  @type tail
  tag nginx.access
  path /var/log/nginx/access.log
  pos_file /var/lib/google-fluentd/pos/access.log.pos
  format none
</source>

<source>
  @type tail
  format none
  path /var/log/nginx/error.log
  pos_file /var/lib/google-fluentd/pos/nginx-error.pos
  read_from_head true
  tag nginx.error
</source>

google-fluentdのconf構造

/etc/google-fluentd$ tree .
.
├── config.d
│   ├── nginx.conf
│   └── syslog.conf
├── google-fluentd.conf
└── plugin

Stackdriver Loggingで転送ログを確認する

f:id:yhidetoshi:20190618230813p:plain

まとめ

AnsibleでNginxのインストールとgoogle-fluentdの設定を行い、Stackdriver-loggingでログ転送を確認した。