0
0

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 1 year has passed since last update.

node_exporterをansibleでデプロイする方法

Posted at

exporterをAnsibleで管理したい!

はじめに

prometheusのnode_exporterをAnsibleでインストール&起動,有効化まで行う方法をまとめました。
手作業でも問題はないのですが、後々やったことを忘れるのでAnsibleを使いました。

実行環境

  • centos 7(wsl2)
  • ansible 2.9.27
  • Prometheus node_exporter 1.5.0
    ※ansibleはインストール済みかつsshの疎通も通ってる前提で進めます

ファイル構成

/
├root
 ├playbook.yml
 └node_exporter.servce
└etc/ansible
 └hosts

あくまでデプロイすることが目的なので、構成はかなり適当です。
Ansibleのベストプラクティスについてはここを参考にしてください。

hosts
localhost ansible_host=127.0.0.1 ansible_connection=local
playbook.yml
---
- hosts: all
  become: true
  tasks:
    - name: node-exporterをダウンロードして解凍
      # ansible 2.0からリモートから取得したファイルを直接解凍できるようになったみたいです
      # 参考:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html#examples
      unarchive:
        src: https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
        dest: ~/
        remote_src: yes
    - name: 実行ファイルをコピー
      copy:
        src: ~/node_exporter-1.5.0.linux-amd64/node_exporter
        dest: /sbin/
        owner: root
        group: root
        mode: '0755'
    - name: unit-fileを作成
      copy: 
        src: ~/node_exporter.service
        dest: /etc/systemd/system/node_exporter.service
    - name: daemonを再起動
      systemd:
        daemon_reload: yes
    - name: node_exporterを起動
      systemd:
        state: started
        name: node_exporter.service
    - name: node_exporterを有効化
      systemd:
        name: node_exporter.service
        enabled: true
        masked: no
node_exporter.service
[Unit]
Description=Node Exporter
Requires=node_exporter.socket

[Service]
User=root
ExecStart=/sbin/node_exporter

[Install]
WantedBy=multi-user.target  

実行

cd /root
ansible-playbook playbook.yml

実行結果

# ansible-playbook playbook.yml

PLAY [all] *************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [localhost]

TASK [node-exporterをダウンロードして解凍] ****************************************************************************************
ok: [localhost]

TASK [実行ファイルをコピー] ******************************************************************************************************
ok: [localhost]

TASK [unit-fileを作成] ****************************************************************************************************
ok: [localhost]

TASK [daemonを再起動] ******************************************************************************************************
ok: [localhost]

TASK [node_exporterを起動] ************************************************************************************************
ok: [localhost]

TASK [node_exporterを有効化] ***********************************************************************************************
ok: [localhost]

PLAY RECAP *************************************************************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

# curl localhot:9100/metrics
(レスポンスが帰ってくればOK)

まとめ

node_exporrterをansibleでデプロイする方法をまとめました。
現状の課題として

  • node_exporterしかインストールできていない
  • Prometheus側の制御を全く書いていない
  • Ansibleのベストプラクティスに則っていない

などが挙げられます。今後はこのあたりを中心に改善指定校と思います。

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?