Vagrant で仮想マシンを構築する(2) 仮想マシンへのプロビジョニング(shell script)
プロビジョニングとは、開発環境生成プロセスの一部として、仮想マシンへ自動的にソフトウェアのインストールや設定を行うことです。
Vagrant では、shell script、chef、puppet によるプロビジョニングをサポートしています。
今回は、shell script を使用したプロビジョニングを行います。
プロビジョニングはいつ実行されるか?
プロビジョニングは以下のタイミングで実行されます。
プロビジョニングで Nginx をセットアップする
プロビジョニングに shell script を使って、nginx のインストール、ドキュメントルートの設定を行います。
1. 仮想マシン初期設定
$ mkdir -p vagrant_example $ cd vagrant_example $ vagrant init ubuntu/trusty64
2. Vagrantfile 設定
- HTTP 通信のポートフォワード
- 共有フォルダの設定
- プロビジョニングの設定
$ vi Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64" config.vm.network "forwarded_port", guest: 80, host: 8888 config.vm.synced_folder "./data", "/home/vagrant/data" config.vm.provision "shell", path: "./script/provision.sh" end
3. プロビジョニング用 shell script 作成
$ mkdir -p script $ cd script $ vi provision.sh #!/bin/bash echo "Installing nginx and setting it up..." aptitude update > /dev/null 2>&1 aptitude install -y nginx > /dev/null 2>&1 rm -rf /usr/share/nginx/html ln -fs /home/vagrant/data /usr/share/nginx/html
4. 仮想マシン起動
$ vagrant up
起動した仮想マシンで nginx がインストールされていることを確認します。
仮想マシン起動時に default: stdin: is not a tty エラーになりますが、ubuntu の問題で動作には問題ないようです。
shell script によるプロビジョニング(補足)
- インラインスクリプト
プロビジョニング内容が単純な場合は、Vagrantfile に直接書くこともできます。
config.vm.provision "shell", inline: "aptitude install -y nginx"
- 1度だけ実行したいプロビジョニング
1度だけ実行したいプロビジョニングは、初回にフラグファイルを作成して、フラグファイルがある場合はプロビジョニングしないようにします。
if [ -f "/var/tmp/provision" ]; then exit 0 fi (処理) touch /var/tmp/provision
Provisioning - Vagrant Documentation
Shell Scripts - Provisioning - Vagrant Documentation
Vagrant で仮想マシンを構築する(1) Vagrant インストールから Vagrant Share での仮想マシン公開
Vagrant 1.5 以降に対応したインストール、設定メモです。
Vagrant 1.5 から Vagrant Cloud や Vagrant Share が使えるようになったので、そのあたりも書いていこうと思います。
ホストマシンは Mac OS X です。
環境構築(VirtualBox, Vagrant のインストール)
1. VirtualBox のインストール
-
- VirtualBox から最新バージョンをダウンロードして、インストールします
2. Vagrant のインストール
-
- Vagrant から最新バージョンをダウンロードして、インストールします
仮想マシンの起動と SSH ログイン
1. 作業用ディレクトリを作成して、vagrant init と vagrant up を実行します。今回は Ubuntu 14.04 LTS な仮想マシンを起動します。
$ mkdir -p vagrant_example $ cd vagrant_example $ vagrant init ubuntu/trusty64 $ vagrant up
$ vagrant ssh
仮想マシンの操作コマンド
- 仮想マシンのステータス確認
$ vagrant status
$ vagrant ssh
- 仮想マシンの停止/起動
$ vagrant halt $ vagrant up
$ vagrant suspend
$ vagrant up
- 仮想マシンの再起動
$ vagrant reload
- 仮想マシンの削除
$ vagrant destroy
仮想マシンのネットワークと共有フォルダ設定
以下の設定を追加します。
$ mkdir data $ vi Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64" config.vm.network "forwarded_port", guest: 80, host: 8888 <- 追記 config.vm.synced_folder "./data", "/home/vagrant/data" <- 追記 end $ vagrant reload
Web サーバの起動
仮想マシンで Web サーバを起動します。
@local host $ cd vagrant_example/data $ vi index.html Hello, Vagrant! @virtual host $ cd /home/vagrant/data $ $ sudo python -m SimpleHTTPServer 80
ホストのブラウザで、http://localhost:8888/index.html へアクセスします。
Vagrant Share で Web サーバを公開
Vagrant 1.5 から使えるようになった Vagrant Share で Web サーバを公開します。
Vagrant Share とは
Vagrant Share allows you to share your Vagrant environment with anyone in the world
インターネットから仮想マシンへアクセスできる機能です。
Vagrant Share を使ってみる
Vagrant Share の使用には Vagrant Cloud へのアカウント作成が必要です。
1. Vagrant Cloud アカウント作成
- Atlas by HashiCorp で SIGN UP します。
2. vagrant login
$ vagrant login In a moment we will ask for your username and password to HashiCorp's Atlas. After authenticating, we will store an access token locally on disk. Your login details will be transmitted over a secure connection, and are never stored on disk locally. If you do not have an Atlas account, sign up at https://atlas.hashicorp.com. Atlas Username: <- Vagrant Cloud に登録したユーザ名 Password (will be hidden): <- Vagrant Cloud に登録したパスワード You are now logged in.
3. vagrant share
$ vagrant share ..... ==> default: Your Vagrant Share is running! Name: gigantic-blizzard-5997 ==> default: URL: http://gigantic-blizzard-5997.vagrantshare.com ==> default: ==> default: You're sharing your Vagrant machine in "restricted" mode. This ==> default: means that only the ports listed above will be accessible by ==> default: other users (either via the web URL or using `vagrant connect`).
vagrant share コマンド実行時に最後の方に表示される URL(上記の場合だと、http://gigantic-blizzard-5997.vagrantshare.com) にアクセスすると、仮想マシンで起動した Web サーバにアクセスできます。
URL は、vagrant share 実行毎に変わります。
(Ubuntu 12.04 LTS::Precise Pangolin) aptitude update 時にエラーが出た時の対応
W: gzip:/var/lib/apt/lists/partial/jp.archive.ubuntu.com_ubuntu_dists_precise_main_source_Sources の取得に失敗しました ハッシュサムが適合しません
E: Some index files failed to download. They have been ignored, or old ones used instead.
aptitude update 時に上記のエラーが出た時の対応メモです。
$ sudo rm -rf /var/lib/apt/lists/* $ sudo mkdir /var/lib/apt/lists/partial $ sudo aptitude clean $ sudo aptitude update $ sudo aptitude safe-upgrade
MySQL便利ツール(percona-toolkit, innotop, dstat)のインストールメモ
モニタツール、便利ツールをインストールしました。
- percona-toolkit
以前はMaatkitと呼ばれていた便利ツール集です。
$ sudo aptitude install libtime-hires-perl libdbi-perl libdbd-mysql-perl $ wget http://www.percona.com/redir/downloads/percona-toolkit/2.0.3/percona-toolkit-2.0.3.tar.gz $ gzip -dc percona-toolkit-2.0.3.tar.gz | tar xvf - $ cd percona-toolkit-2.0.3 $ perl Makefile.PL $ make $ make test $ sudo make install
ホームディレクトリの.my.cnfファイルにMySQL接続ユーザとパスワードを記述しておきます。
$ vi ~/.my.cnf [client] user = root password = pass $ chmod 600 ~/.my.cnf
/usr/local/bin以下にpt-*なコマンド類がインストールされます。
- innotop
$ sudo aptitude install libterm-readkey-perl $ wget http://innotop.googlecode.com/files/innotop-1.8.1.tar.gz $ gzip -dc innotop-1.8.1.tar.gz | tar xvf - $ cd innotop-1.8.1 $ perl Makefile.PL $ sudo make install
- dstat
こちらの記事を読んで、MySQLのモニタもできることを知ったのでインストールしました。
MySQLのソケットファイルが/var/run/mysqld/mysqld.sockにある必要があるようです。
また、InnoDBをモニタするにはmysqlコマンドが/usr/bin/mysqlにある必要があります。
$ sudo aptitude install python-mysqldb $ sudo aptitude install dstat
環境変数を設定します。
$ vi ~/.bashrc export DSTAT_MYSQL_USER=root export DSTAT_MYSQL_PWD=pass export DSTAT_MYSQL_HOST='127.0.0.1' export DSTAT_MYSQL='-uroot -ppass -h127.0.0.1' $ . ~/.bashrc
以下のような感じで実行します。
$ dstat -T --mysql5-cmds --mysql5-io --mysql5-keys $ dstat -T --innodb-io --innodb-buffer --innodb-ops
- (おまけ)Couldn't find MySQL server (/usr/bin/mysqld_safe)エラーでMySQLが起動しなくなったら
mysql-commonがインストールされて、/etc/mysql/my.cnfが作成されている可能性があります。
/etc/mysqlディレクトリをリネームすれば起動するようになります。
Percona Toolkit - Percona Software
innotop -
The best top clone for MySQL, with special attention paid to InnoDB - Google Project Hosting
dstatの万能感がハンパない - (ひ)メモ
(ubuntu 11.10)MySQL5.5のインストールメモ
そろそろMySQL5.5系でも触っておくかと思い、ubuntu 11.10にインストールしました。
- 前準備
ビルドに必要そうなパッケージ類をインストールします。
MySQL5.5からcmakeに変わっているのでインストールしておきます。
$ sudo aptitude install build-essential cmake libncurses5-dev
- MySQL5.5のインストール
起動用ユーザーとグループを作成します。
$ sudo groupadd mysql $ sudo useradd -g mysql -d /usr/local/mysql -s /bin/false -M mysql
cmakeになったので、オプション名が変わっています。
$ wget ftp://ftp.iij.ad.jp/pub/db/mysql/Downloads/MySQL-5.5/mysql-5.5.21.tar.gz $ gzip -dc mysql-5.5.21.tar.gz | tar xvf - $ cd mysql-5.5.21 $ cmake . -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_INNOBASE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=true -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock $ make $ sudo make install
パーミッション変更、MySQLデータの初期化、my.cnfの準備、自動起動設定、環境変数設定を行います。
$ sudo chown -R mysql:mysql /usr/local/mysql $ cd /usr/local/mysql $ sudo scripts/mysql_install_db --user=mysql $ sudo cp support-files/my-large.cnf /etc/my.cnf $ sudo cp support-files/mysql.server /etc/init.d/mysqld $ sudo chmod +x /etc/init.d/mysqld $ sudo update-rc.d mysqld defaults $ vi ~/.bashrc export PATH=$PATH:/usr/local/mysql/bin $ . ~/.bashrc
起動します。
$ sudo /etc/init.d/mysqld start
最後に初期設定スクリプト(mysql_secure_installation)を実行するのですが、testデータベース削除時にエラーとなります。
これはtestディレクトリに、.emptyファイルが存在するためです。*1
事前に削除してから、初期設定スクリプトを実行します。
$ sudo rm /usr/local/mysql/data/test/.empty $ sudo /usr/local/mysql/bin/mysql_secure_installation
いろいろと新機能が追加されているようなので試していこうと思います。
漢(オトコ)のコンピュータ道: MySQL 5.5新機能徹底解説
*1:何のためにあるのか不明です。今後のリリースで修正されるのでしょうか?