2018年8月29日水曜日

VagrantでMySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(Ubuntu16.04)を構築する

MySQL Server Exporterで、Prometheusの為にMariaDBの監視を行うことができます。

〇MySQL Server ExporterのデータをPrometheusで表示した画面


〇構築方法
以下のVagrantfileを使用して、MySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(Ubuntu16.04)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604mariadbmysqlexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604mariadbmysqlexporter"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
timedatectl set-timezone Asia/Tokyo

# install mariadb
echo "mariadb-server-10.0 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.0 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -e "CREATE USER prometheus@localhost IDENTIFIED BY 'prometheus';"
mysql -uroot -e "GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'localhost';"
mysql -uroot -e "FLUSH PRIVILEGES;"

# install prometheus mysql exporter
groupadd prometheus
useradd -g prometheus -s /bin/bash -d /home/prometheus -m prometheus

mkdir -p /opt/prometheus
cd /opt/prometheus
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz

ln -s /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter /bin/mysqld_exporter

cat << EOF > /etc/systemd/system/mysqld-exporter.service
[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=prometheus:prometheus@(localhost:3306)/
ExecStart=/bin/mysqld_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl enable mysqld-exporter.service
systemctl start mysqld-exporter.service
echo 'port 9104'
SHELL
end

0 件のコメント:

コメントを投稿