2018年8月14日火曜日

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

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

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


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

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604mysqlexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604mysqlexporter"
     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
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 mysql
apt-get -y install libaio1 libmecab2
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-common_5.7.22-1ubuntu16.04_amd64.deb
dpkg -i mysql-common_5.7.22-1ubuntu16.04_amd64.deb
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client_5.7.22-1ubuntu16.04_amd64.deb
dpkg -i mysql-community-client_5.7.22-1ubuntu16.04_amd64.deb
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-client_5.7.22-1ubuntu16.04_amd64.deb
dpkg -i mysql-client_5.7.22-1ubuntu16.04_amd64.deb
export DEBIAN_FRONTEND=noninteractive
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server_5.7.22-1ubuntu16.04_amd64.deb
echo "mysql-server-5.7 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mysql-server-5.7 mysql-server/root_password_again password root" | sudo debconf-set-selections
dpkg -i mysql-community-server_5.7.22-1ubuntu16.04_amd64.deb
apt-get -y install -f
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 件のコメント:

コメントを投稿