2018年8月21日火曜日

AnsibleでMariaDBをインストールしたサーバにMySQL Server Exporter(Prometheus)をインストールする(CentOS7用)

MySQL Server Exporter(Prometheus)でMySQLの情報の収集を行います。

〇インストール方法
1.下準備
~/.ansible.cfgに以下の内容を設定します
[ssh_connection]
pipelining=True
[defaults]
host_key_checking = False

2. インストール対象ホストのユーザやパスワードを環境に合わせてinventoryファイルに記入します

inventory例
[mysql-exporter]
192.168.1.106

[all:vars]
ansible_ssh_port=22
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible_sudo_pass=vagrant

3.inventory mysql-exporter-deb.ymlを準備し、以下のコマンドを実行します。
ansible-playbook -i inventory mysql-exporter.yml

mysql-exporter.yml
- hosts: mysql-exporter
  vars:
    - password: prometheus
    - dbuser: prometheus
    - dbpassword: prometheus
    - dbhost: localhost
    - dbport: 3306
  tasks:
    - name: create prometheus group
      group:
        name: prometheus
      become: true
    - name: create prometheus user
      user:
        name: prometheus
        group: prometheus
        password: "{{ password | password_hash('sha512') }}"
      become: true
    - name: create prometheus directory
      file:
        path: /opt/prometheus
        state: directory
        owner: prometheus
      become: true
    - name: download mysql server exporter
      get_url:
        url: https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
        dest: /opt/prometheus/
      become: true
      become_user: prometheus
    - name: extract prometheus node exporter
      unarchive:
        remote_src: yes
        src: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64.tar.gz
        dest: /opt/prometheus/
      become: true
      become_user: prometheus
    - name: symlink
      file:
        path: /bin/mysqld_exporter
        state: link
        src: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter
      become: true
    - name: clean up
      file:
        state: absent
        path: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64.tar.gz
      become: yes
      become_user: prometheus
    - name: setup systemd
      blockinfile:
        dest: /etc/systemd/system/mysqld-exporter.service
        create: yes
        block: |
          [Unit]
          Description=Prometheus MySQL Server Exporter
          Requires=network.target
          [Service]
          Restart=always
          WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
          Environment=DATA_SOURCE_NAME={{ dbuser }}:{{ dbpassword }}@({{ dbhost }}:{{ dbport }})/
          ExecStart=/bin/mysqld_exporter
          ExecReload=/bin/kill -HUP $MAINPID
          [Install]
          WantedBy=multi-user.target
      become: true
    - name: install epel-release
      yum:
        name: epel-release
        state: present
      become: yes
    - name: Install required software
      yum: name={{ item }} state=present enablerepo=epel
      with_items:
        - mariadb-devel
        - python-devel
        - python36-devel
        - python-pip
      become: true
    - name: install MySQL-python using pip
      pip:
        name: "{{ item }}"
        state: forcereinstall
      with_items:
        - MySQL-python
      become: true
    - name: create and grant a database user
      mysql_user:
        name={{ dbuser }}
        password={{ dbpassword }}
        priv="*.*:PROCESS,REPLICATION CLIENT,SELECT"
        state=present
      become: true
    - name: enable and start mysqld-exporter
      systemd:
        daemon_reload: yes
        enabled: yes
        state: started
        name: mysqld-exporter.service
      become: true

0 件のコメント:

コメントを投稿