VurtualBoxでDRBDを構築検証! - 検証環境構築編
インフラをやっていると、サーバーの冗長化を行う事が多々ありますよね?
イメージサーバーやDBサーバーの冗長化を行う際に、
データの同期方法で悩みまして、DRDBなるものがある事を知りました!
今回は、概要把握と簡単なセットアップをvirtualboxに仮想サーバーを2つ作成し、
それぞれに同じ環境のcentosをインストールして検証を開始したいと思います。
参考にしたサイト
インフラ構築手順書 HeartBeat2+DRBD構築 【DRBD構築手順】
Heartbeat v3 + Pacemaker 再び - とあるサーバ管理者 & PG & SEのメモ帳
fdiskの操作方法
DRBDとは?
DRBD (Distributed Replicated Block Device) は、Linuxプラットフォームの分散ストレージシステムである。
カーネルモジュール、管理アプリケーション、シェルスクリプトで構成され、高可用 (HA) クラスタで使うのが一般的である。 DRBD は RAID 1 に似ているが、ネットワーク上で動作する点が異なる。
んー。。よく分からないw
ミラーリングツール「DRBD」によるデータ保護って記事がありましたので、こっちを確認するといいかもしれません!
Heartbeatでかんたんクラスタリング(4):ミラーリングツール「DRBD」によるデータ保護 (1/3) - @IT
仮想マシンを2つ用意する。
VirturlBoxに検証用マシンを2台用意します。
環境は以下の通りです。
- drdb01 -- 名前:drdb01 -- タイプ:Linux -- バージョン:Redhat(64bit) -- メモリ:1024MB -- 仮想ハードドライブを作成する。 -- VDI(可変サイズ/30GB) - drdb02 -- 名前:drdb02 -- タイプ:Linux -- バージョン:Redhat(64bit) -- メモリ:1024MB -- 仮想ハードドライブを作成する。 -- VDI(可変サイズ/30GB)
作成が完了したら、次はCentOSをインストールします。
今回は、CentOS 64bitのDVDでインストールします。
http://ftp.riken.jp/Linux/centos/6.5/isos/x86_64/CentOS-6.5-x86_64-bin-DVD1.iso
[設定] → [ストレージ] → [CDマーク] → [仮想ドライブを選択]の順でクリックしていき、
ダウンロードしたDVDファイルを選択しましょう!
その後起動すると、自動的にインストール画面が表示されるはずです。
次に、インストールですが、以下のように設定しました。
基本的には、かなり前に最小構成でインストールしたのと
ほとんど変わりません。
ですが、パーティションの設定部分が若干違うので、そこについて記載しておきたいと思います。
# drdb01 ・ホスト名:drdb01.jp ・カスタムレイアウトを使用する。 ・/boot:ext4:200:固定容量:基本パーティションにする ・:swap:2048:固定容量:基本パーティションにする ・/drdb:ext4:10000:固定容量:基本パーティションにする ・/:ext4:200:最大許容量まで使用:基本パーティションにする # drdb01 ・ホスト名:drdb02.jp ・カスタムレイアウトを使用する。 ・/boot:ext4:200:固定容量:基本パーティションにする ・:swap:2048:固定容量:基本パーティションにする ・:ext4:10000:固定容量:基本パーティションにする ・/:ext4:200:最大許容量まで使用:基本パーティションにする
ネットワークの設定
ネットワークの設定をしておきたいと思います。
以前も設定内容を記載したことがあるので、今回は、コマンドと修正内容のみとします。
基本的には、2台とも同じ設定を行いますが、
DRBD01「IPADDR=192.168.33.17」でDRBD02「IPADDR=192.168.33.18」としました!
[root@drdb01] vi /etc/resolv.conf options single-request-reopen nameserver 10.0.2.3 nameserver 8.8.8.8 [root@drdb01] vi /etc/networks default 0.0.0.0 loopback 127.0.0.0 link-local 169.254.0.0 [root@drdb01] vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=drdb01.jp [root@drdb01] vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" BOOTPROTO="dhcp" IPV6INIT="yes" NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Ethernet" UUID="56f45b3e-1d42-4cf8-a78e-4a041835f3a1" [root@drdb01] vi /etc/sysconfig/network-scripts/ifcfg-eth1 NM_CONTROLLED=no BOOTPROTO=none ONBOOT=yes IPADDR=192.168.33.17 NETMASK=255.255.255.0 DEVICE=eth1 PEERDNS=no [root@drdb01] vi /etc/sysconfig/network-scripts/ifcfg-lo DEVICE=lo IPADDR=127.0.0.1 NETMASK=255.0.0.0 NETWORK=127.0.0.0 BROADCAST=127.255.255.255 ONBOOT=yes NAME=loopback [root@drdb01] getenforce [root@drdb01] setenforce 0 [root@drdb01] vi /etc/sysconfig/selinux SELINUX=disabled [root@drdb01] service iptables stop [root@drdb01] chkconfig iptables off [root@drdb01] /etc/init.d/network restart [root@drdb01] yum update
それでは、疎通確認をしておきたいと思います!
[root@drdb01] ssh 192.168.33.18 root@192.168.33.18 password: [root@drdb02]
ゲスト間でも疎通が出来ている事を確認しました!
やっと環境が用意できたので、DEDBの検証に入っていこうと思います。
作るのも結構めんどいので、スナップショットを忘れずに!w
DRBDをインストールする
さて、環境構築が完了したので、DRBDをインストールしていきたいと思います。
DRBDは標準パッケージではインストールできない為、「elrepo」レポジトリを入れてから行います。
以下の処理は2台のマシンとも行います。
[root@drdb01 ~]# rpm -Uvh http://elrepo.org/linux/elrepo/el6/x86_64/RPMS/elrepo-release-6-5.el6.elrepo.noarch.rpm [root@drdb01 ~]# vi /etc/yum.repos.d/elrepo.repo vi 8)enabled=0 [root@drdb01 ~]# yum --enablerepo=elrepo install drbd84-utils.x86_64 [root@drdb01 ~]# yum --enablerepo=elrepo install kmod-drbd84
インストールは以上で完了となります。
DRBDの設定を行う。
インストールが完了したので、設定を行います。
[root@drdb01 ~]# cp -apr /etc/drbd.conf /etc/drbd.conf.org [root@drdb01 ~]# vi /etc/drbd.conf # You can find an example in /usr/share/doc/drbd.../drbd.conf.example #include "drbd.d/global_common.conf"; #include "drbd.d/*.res"; global { usage-count no; } common { syncer { rate 200M; } } resource db { protocol C; startup { wfc-timeout 120; degr-wfc-timeout 120; } net { cram-hmac-alg "sha1"; shared-secret "password"; after-sb-0pri disconnect; after-sb-1pri disconnect; after-sb-2pri disconnect; } disk { on-io-error pass_on; } on drdb01.jp { # uname -n device /dev/drbd0; disk /dev/sda2; address 192.168.33.17:7788; meta-disk internal; } on drdb02.jp { # uname -n device /dev/drbd0; disk /dev/sda2; address 192.168.33.18:7788; meta-disk internal; } }
次は、DRBDのメタファイルを作成するというところなんですが、
ちょっと検証までの道のりが長くなってきてしまうので、
急遽2回に分けて書いていこうと思います。
使用環境
- ホストOS: Windows7 x64
- Cygwin : Cygwin64
- VirtualBox: 4.3.10
僕なりの開発環境まとめ - PHP編
これまで様々な視点で開発環境を作成してきました。
Chefを用いて、色んな環境を試してきたので、そろそろ、その記事をまとめておきたいと思います。
単独で開発する場合は、こんな感じです。
開発の環境に関しては、仮想マシンをVagrant+VatrualBoxで構成。
フロントサイドはSass+Compass+Coffee+Gruntで構成し、フロント開発を簡単に行えるように環境をChefで構築しました。
- ChefでSass+Compass+Coffee+Gruntの実行環境を作る(1) - Program LOG
- ChefでSass+Compass+Coffee+Gruntの実行環境を作る(2) - Program LOG
- Grunt.js開発の実行手順まとめ - Program LOG
サーバーサイドに関しては、rubyでって思ったんですが、使い慣れたPHPで環境を構築しました。
その内容は以下の記事にまとまっています。
- windows+CygwinでChef - 2.基本的な操作とapacheのインストール - Program LOG
- windows+CygwinでChef - 3.ChefでPHPインストール編 - Program LOG
- windows+CygwinでChef - 4.ChefでMysqlインストール編 - Program LOG
またその後、作成したcookbooksのファイルを元に、改良を重ね、もっと開発しやすい環境を構築しました。
- Chefでオレオレ証明書を作成して簡易的にhttpsのアクセスを有効にする - Program LOG
- ChefでphpMyAdminのインストールと、VirtualHostを設定する - Program LOG
- 検証環境でメールを外に飛ばさないで確認する方法 Chef版 - Program LOG
- 開発環境構築 - 検証環境でメールを外に飛ばさないで確認する方法 - Program LOG
また、ファイルのバージョン管理に関しては、SVNから今後はGitに管理を移行していきたいと考えています。
その為、gitをGUIでも触れるようにしたいので、共有ディレクトリを修正しました。
また、ネットワークやGithubで使用する鍵認証についても、正確に手順を作成してみました。
おまけって感じでw
今後は複数人で開発する上で必要な記事をまとめていきたいと思っています。
例えば、Jenkinsやcapistaranoなどのデプロイツールや、自動デバッグなど。
vagrantで簡単実行環境作り!
$ mkdir develop $ cd ./develop $ vagrant init centos $ vi ./Vagrantfile vi) config.vm.network "private_network", ip: "192.168.33.14" $ vagrant up #鍵認証 $ vagrant ssh-config --host develop >> ~/.ssh/config #接続テスト $ ssh develop Welcome to your Vagrant-built virtual machine. [vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed. #リポジトリの作成と仮想マシンにchefを用意 $ knife solo init chef-lepo $ cd chef-lepo $ knife solo prepare develop
使用環境
- ホストOS: Windows7 x64
- Cygwin : Cygwin64
- VirtualBox: 4.3.10
- Vagrant: 1.5.2
忙しすぎて、全く更新できません・・・。
8月に入ってから、ネットワーク障害や、サーバー障害などが、連発しちゃったので、
全く更新できませんでした・・・。
勉強も全く進んでないんですが、今DRBD+Heartbeatのクラスタリングを勉強中です!
近日公開できたらいいなぁーって思っています。
後、会社でgithubを使った運用を行おうって事になってるんですが、
今どうやったらいいのか検討中。。。こいつもまとめたいなっ!って思っています。
後何を勉強すっかなー!
Githubを勉強したら、Capistranoで自動デプロイとか?
自動デプロイなら「Jenkins」をちゃんと勉強してみるってのもいいかもしれないですね。。。
ひとまず、ドットインストールでも見てみようかなぁって思ってます。
取り合えず、今使っている開発環境をまとめた記事をアップして、
それ以降についてはこんな感じで勉強してみようかと!
鍵認証について
先日友人に鍵認証のやり方を教えてくれって言われました。
口頭で説明したら「???」って感じでw
ブログにしておくって言ってしまったので、書きたいと思いますw
僕もやり方は知っていましたが、色々調べながらやった事無かったので、
丁度いい機会でした!
公開鍵認証って何?
CentOS に SSH でログインする方法には、パスワード認証方式と公開鍵認証方式があります。
パスワード認証方式は、現在のインターネット環境では、サーバーの認証として脆弱であると言っても過言ではないでしょう。
特に、SSH で不正にログインされるとサーバーを乗っ取られる可能性があるため、これからの Web サーバ-の認証は公開鍵認証方式を採用すべきです。
この記事では、公開鍵認証による SSH のログイン設定方法をご紹介します。
なるほど!そうゆう事ねw
キーペアの作成
まずは、サーバーでキーペアを作成します。
前回の記事で作成したGitサーバーを使用したいと思います。
(vagrantで作成した仮想マシンですw)
まずは、ログインして、ユーザーを作成します。
$ cd ./gitserver $ vagrant up $ vagrant ssh [vagrant@localhost ~]$ sudo su [root@localhost vagrant]# useradd takumi [root@localhost vagrant]# passwd takumi Changing password for user takumi. New password: takumi BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple Retype new password: takumi passwd: all authentication tokens updated successfully
次に、キーペアを作成します。
passphraseを入力すると、鍵認証時毎回聞かれるのがうざいので、スキップしちゃいますw
[root@localhost vagrant]# su takumi [takumi@localhost vagrant]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/takumi/.ssh/id_rsa): エンターキー Created directory '/home/takumi/.ssh'. Enter passphrase (empty for no passphrase): エンターキー Enter same passphrase again: エンターキー Your identification has been saved in /home/takumi/.ssh/id_rsa. Your public key has been saved in /home/takumi/.ssh/id_rsa.pub. The key fingerprint is: 54:41:c5:50:b0:1e:ac:93:bf:ce:e3:37:76:ac:a4:3f takumi@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ | .*Bo | | o .. | | . + | | . + . | | S . | | o | | . .. | | ..+E o | | o*=o= | +-----------------+ [takumi@localhost vagrant]$ cd /home/takumi/.ssh/ [takumi@localhost .ssh]$ ll total 8 -rw------- 1 takumi takumi 1743 Jul 25 08:52 id_rsa -rw-r--r-- 1 takumi takumi 410 Jul 25 08:52 id_rsa.pub
こんな感じですw
んで、下記コマンドを実行して、公開鍵を公開鍵認証で使用できるようにします。
[takumi@localhost vagrant]$ cp -apr ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys [takumi@localhost vagrant]$ chmod 600 ~/.ssh/authorized_keys
公開鍵認証のログイン設定
鍵認証が行えるように設定しましょう!
[takumi@localhost vagrant]$ exit [root@localhost vagrant]# vi /etc/ssh/sshd_config vi) PubkeyAuthentication yes ←コメントを外す [root@localhost vagrant]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ]
まずは、秘密鍵をダウンロードしましょう!
本来は、ここで、WinSCPなどを用いて秘密鍵をもってくるんですが、
Vagrantなので、共有ディレクトリにコピーして使おうと思います。
[root@localhost vagrant]# cp /home/takumi/.ssh/id_rsa /vagrant/ [root@localhost vagrant]# ll /vagrant/ total 7 -rwxrwxrwx 1 vagrant vagrant 1743 Jul 25 08:58 id_rsa -rwxrwxrwx 1 vagrant vagrant 4733 Jul 25 02:52 Vagrantfile
次にTera Termを設定します。
ユーザー名を入力し、パスフレーズに、秘密鍵のパスフレーズを入力し、
秘密鍵を選択し、接続してみてください!
これで、秘密鍵認証は完了となります。
~/.ssh/configを設定しておこう!
WindowsユーザーでもCygwinを使っている場合、キーフレーズで認証する事が出来ます。
設定しておこうと思います。
$ vim ~/.ssh/config Host gitserver HostName 192.168.33.19 User takumi PasswordAuthentication no IdentityFile C:/Users/takumi-main/Desktop/dev/gitserver/id_rsa IdentitiesOnly yes LogLevel FATAL $ ssh gitserver Last login: Fri Jul 25 09:00:44 2014 from 192.168.33.1 Welcome to your Vagrant-built virtual machine. [takumi@localhost ~]$
ざっと、こんな感じです!
友達には、sudo suのユーザー権限の設定方法も聞かれています。。。w
次回にでも書いておこうと思います。
使用環境
- ホストOS: Windows7 x64
- Cygwin : Cygwin64
- VirtualBox: 4.3.10
- Vagrant: 1.5.2
Git入門
仕事が忙しすぎて、全然更新できませんでした。。。
ようやく、落ち着いてきたので、
今回はGitについて書いてみようと思います。
昔からSVNに慣れてしまっていて、中々Gitに移行する事が出来ずにいます。(現在もw)
ですが、近年出来るプログラマーは、こぞってGitを勧めてきます。
そしてGitを使い始めるようになってきました。
今回は入門編として、概要と、インストール方法、基本的なコマンドを調べてみましたので、まとめてみます。
Gitとは?
Git(ギット またはジット [要出典])は、プログラムのソースコードなどの変更履歴を記録・追跡するための分散型バージョン管理システムである。 もとはLinuxカーネルのソースコード管理に用いるためにリーナス・トーバルズによって開発され、それ以降ほかの多くのプロジェクトで採用されている。 Linuxカーネルのような巨大プロジェクトにも対応できるように、動作速度に重点が置かれている。 現在のメンテナンスは濱野純 (Junio C Hamano) が担当している。
ふーんって感じですねw
まぁ、分散型のファイルバージョン管理ツールって所ですよね?
SVN vs Git
結局SVNとGitって何が違うんだろうってところですね!
SVNは小規模で開発する時は、何の問題も無く運用できるんですが、
フェーズを分けた開発や、グループで開発するのは、Gitのがいいって良く聞きます。
大きな点で、違うところは、ローカルとグローバルの2種類にファイルが管理されているって所だと思います。
ローカルにコミットしたファイル群をプッシュという形でグローバルにアップするという事です。
確かに、開発中のファイル郡ってSVNだと扱いにくい場合がありますよね?
でも、プッシュさえしなければ、コミットしておく事が出来るので、非常に便利だと思います。
まぁ、それ以外にもGithubなどのサイトとの連携が非常に効率的に行える点も高評価な点だと思います。
検証環境を用意する。
さて、それでは早速Gitのインストールを行いたいと思いますが、
バイナリでのインストールも可能なのですし、Cygwinで行いたい場合も、
Cygwinをインストールする際に入れていると思います。
また、インストールしていない場合は、apt-cyg install gitでインストールできると思いますので、
Windows版に関しては、インストール手順をスキップしておきたいと思います。
それでは早速vagrant上に検証環境を用意したいと思います。
- ファイル管理用
-- gitdev
- vagrant用ディレクトリ
-- gitserver
$ mkdir gitdev $ mkdir gitserver $ cd ./gitserver $ vagrant init centos $ vim Vagrantfile vim 26) config.vm.network "private_network", ip: "192.168.33.19" $ vagrant up $ vagrant ssh [vagrant@localhost ~]# sudo su [root@localhost vagrant]# service iptables stop [root@localhost vagrant]# chkconfig iptables off
リモートリポジトリを用意する。
それでは早速gitサーバーを用意していこうと思います。
gitをインストールすれば、すぐにでもリモートリポジトリを作成する事が可能なようです。
[root@localhost vagrant]# yum install -y git [root@localhost vagrant]# git --version git version 1.7.1 [root@localhost vagrant]# cd /usr/local/ [root@localhost local]# mkdir git [root@localhost local]# cd ./git [root@localhost local]# mkdir TestProject.git [root@localhost git]# cd TestProject.git [root@localhost git]# git --bare init --shared Initialized empty shared Git repository in /usr/local/git/TestProject.git/ [root@localhost git]# chown vagrant:vagrant -R /usr/local/git
クライアント側を設定する。
次に、クライアント側を設定していきましょう!
今回はvagrantで構築しているので、sshのキーフレーズに関しては以下のようにして設定してしまいます。
$ vagrant ssh-config --host gitdev >> ~/.ssh/config
お次は、クライアント側でローカルリポジトリを作成してみましょう!
先ほど作成した、gitdevというディレクトリを使用します。
$ cd ../gitdev $ git init Initialized empty Git repository in /cygdrive/c/Users/takumi-main/Desktop/dev/gitdev/.git/
ローカルリポジトリが出来たので、適当にファイルを作成していきます。
そして、コミットしておきます。
$ echo test > test.txt $ ll total 9 drwx------+ 1 takumi-main None 0 Jul 25 17:09 . drwx------+ 1 takumi-main None 0 Jul 25 11:52 .. drwxr-xr-x+ 1 takumi-main None 0 Jul 25 17:08 .git -rw-r--r--+ 1 takumi-main None 5 Jul 25 17:09 test.txt $ git add . $ git status $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: test.txt $ git commit -m "first commit" [master (root-commit) 38634eb] first commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 test.txt
そして、お次は、リモートリポジトリを追加します。
$ git remote add origin ssh://gitdev/usr/local/git/TestProject.git $ git remote -v origin ssh://gitdev/usr/local/git/TestProject.git (fetch) origin ssh://gitdev/usr/local/git/TestProject.git (push)
最後に、リモートへプッシュします。
$ git push origin master Counting objects: 3, done. Writing objects: 100% (3/3), 219 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To ssh://gitdev/usr/local/git/TestProject.git * [new branch] master -> master
いけたっぽいです!
リモートリポジトリからCloneしてくる。
お次は、リモートからプッシュした内容をクローンしてきます。
$ cd ../ $ git clone ssh://gitdev/usr/local/git/TestProject.git gitdev2 $ ll gitdev2 total 9 drwxr-xr-x+ 1 takumi-main None 0 Jul 25 17:20 . drwxr-xr-x+ 1 takumi-main None 0 Jul 25 17:18 .. drwxr-xr-x+ 1 takumi-main None 0 Jul 25 17:20 .git -rw-r--r-- 1 takumi-main None 5 Jul 25 17:20 test.txt
無事にファイルをダウンロードできました!
仮想マシンでの操作は対して難しくありませんね!
後は、どのように使うかって所で、オススメなコマンドというのを
見つけましたので、どんなことが出来るかってくらいは把握しておこうと思います。
Gitの簡単なコマンド集
- git init
- リポジトリを作成する
- git clone
- 既存のリポジトリの複製を作る
- git fsck
- リポジトリの正当性チェックを行う
- git gc
- リポジトリ内の不要なオブジェクトを削除し、最適化を行う
- git status
- 変更が加えられたファイルを表示する
- git diff
- ファイルに加えられた変更点をdiff形式で表示する
- git add
- コミットするファイルを指定する
- git commit
- 変更点をコミットする
- git log
- コミットログを閲覧する
- git reset
- 直前のコミットを取り消す
- git revert
- 作業ツリーを指定したコミット時点の状態にまで戻す
- git branch
- ブランチ情報の表示およびブランチの作成
- git checkout
- ブランチの切り替え
- git show-branch
- ブランチの作成/変更/マージ履歴を表示
- git merge
- ローカルブランチのマージを行う
- git tag
- コミットにタグを付ける
- git stash
- 現在の作業ツリーの状態を一時的に保管する
- git rebase
- ブランチの派生元(上流)を変更する
- git pull
- git push
詳しい内容は下記サイトにものすごく詳しい内容が記載されているので、
そちらを確認してもらえればと思います!
Gitを使いこなすための20のコマンド | SourceForge.JP Magazine
ひとまず、リモートリポジトリとローカルリポジトリを同期して、
ファイルを管理する事が出来るようになりました!
これを機会にGitをもっと触ってみようかと思います。。。!
Grunt.js開発の実行手順まとめ
開発環境について色々記事にしてきましたが、
実際Gruntで開発をする想定で、環境を用意する場合の事を考えてみました。
Gruntのインストールに関しては、別の記事が何個かありますので、
以下を確認してください!
CygwinでGruntjsをインストール - Program LOG
CygwinでGruntjsを使ってみる - Program LOG
ChefでSass+Compass+Coffee+Gruntの実行環境を作る(1) - Program LOG
ChefでSass+Compass+Coffee+Gruntの実行環境を作る(2) - Program LOG
ちなみに今回は、ChefでGruntの実行環境がある前提での記事になりますので、ご注意を!
前提
まず、gruntの環境を用意する上で、package.jsonを生成しますが、
それをどこに作り、どこをルートに開発をしたいかって考えました。
仮想サーバー内部に環境を用意してしまうと、GitなどでGUIを使えないことになります。
GUI大好きな僕からすると、すごい微妙・・・w
やっぱり、Vagrantの共有フォルダをルートにして開発したいと思います。
共有フォルダで開発しよう!
共有フォルダにchefで「root」ってディレクトリを作成して、
その中に「docroot」ってディレクトリをさらに作成。
そして、「/var/www/html/」にシンボリックリンクで、
「/vagrant/root/docroot/」を見るよう設定しました。
レシピは以下の通りです。
#ディレクトリの作成 directory "/vagrant/root/docroot/" do owner "vagrant" group "vagrant" recursive true mode 0755 action :create not_if { ::File.exists?("/vagrant/root/docroot/")} end #元のディレクトリを削除しておく。 directory "/var/www/html" do action :delete not_if { File.symlink?('/var/www/html') } end #シンボリックリンク link "/var/www/html" do to "/vagrant/root/docroot" end
実行結果は以下の通り
$ knife solo cook develop ・・・中略・・・ * directory[/vagrant/root/docroot/] action create - create new directory /vagrant/root/docroot/ - change mode from '' to '0755' - change owner from '' to 'vagrant' - change group from '' to 'vagrant' * directory[/var/www/html] action delete - delete existing directory /var/www/html * link[/var/www/html] action create - create symlink at /var/www/html to /vagrant/root/docroot
package.jsonを生成する。
これは、chefにやらせなくてもいいかと思いますね。
仮に、毎回使いたい物が一緒だったとしても、自動的に実行させなくてもいいかと。。。
手順さえしっかりしていればの話しですが・・・w
これは、以前も実践していますが、おさらいとして記載しておきたいと思います。
[vagrant@localhost root]$ npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sane defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (root) version: (0.0.0) description: entry point: (index.js) test command: git repository: keywords: author: license: (BSD-2-Clause) About to write to /vagrant/root/package.json: { "name": "root", "version": "0.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "BSD-2-Clause" } Is this ok? (yes) [vagrant@localhost root]$ ll total 1 drwxrwxrwx 1 vagrant vagrant 0 Jul 13 07:04 docroot -rwxrwxrwx 1 vagrant vagrant 209 Jul 13 07:08 package.json
前回Cygwinでは出来なかったので、ちょっと感動・・・w
対話しましたよー!w(まぁ、すべてエンターなんですが・・・w)
続いて、Grunt本体のモジュールをインストールしたいと思います。
[vagrant@localhost root]$ npm install grunt --save-dev npm WARN package.json root@0.0.0 No description npm WARN package.json root@0.0.0 No repository field. npm WARN package.json root@0.0.0 No README data ・・・中略・・・ npm ERR! error rolling back Error: UNKNOWN, unlink '/vagrant/root/node_modules/grunt/node_modules/lodash/lodash.js' npm ERR! error rolling back grunt@0.4.5 { [Error: UNKNOWN, unlink '/vagrant/root/node_modules/grunt/node_modules/lodash/lodash.js'] npm ERR! error rolling back errno: -1, npm ERR! error rolling back code: 'UNKNOWN', npm ERR! error rolling back path: '/vagrant/root/node_modules/grunt/node_modules/lodash/lodash.js' } npm ERR! Error: UNKNOWN, symlink '../which/bin/which' npm ERR! If you need help, you may report this log at:
ぬ?・・・・エラー?wなんで?w
エラーを調べてみたけど、よく分からんwけど、対処方法が見つかり「--no-bin-links」をつけて再度実行
[vagrant@localhost root]$ npm install grunt --save-dev --no-bin-links npm WARN package.json root@0.0.0 No description npm WARN package.json root@0.0.0 No repository field. npm WARN package.json root@0.0.0 No README data grunt@0.4.5 node_modules/grunt ├── which@1.0.5 ├── dateformat@1.0.2-1.2.3 ├── eventemitter2@0.4.14 ├── getobject@0.1.0 ├── rimraf@2.2.8 ├── colors@0.6.2 ├── hooker@0.2.3 ├── async@0.1.22 ├── grunt-legacy-util@0.2.0 ├── nopt@1.0.10 (abbrev@1.0.5) ├── exit@0.1.2 ├── minimatch@0.2.14 (sigmund@1.0.0, lru-cache@2.5.0) ├── lodash@0.9.2 ├── coffee-script@1.3.3 ├── glob@3.1.21 (inherits@1.0.0, graceful-fs@1.2.3) ├── underscore.string@2.2.1 ├── iconv-lite@0.2.11 ├── findup-sync@0.1.3 (glob@3.2.11, lodash@2.4.1) ├── grunt-legacy-log@0.1.1 (underscore.string@2.3.3, lodash@2.4.1) └── js-yaml@2.0.5 (esprima@1.0.4, argparse@0.1.15)
いけました!多分大丈夫っぽいw
原因の特定はできないけど、vagrant環境だとあるみたいです。
プラグインを入れよう!
続いて、代表的なプラグインを入れていこうと思います。
今回入れるのは、compass+coffee+watchの3種です。
まずは、ディレクトリの作成しておきたいと思います。
以下のような構成です。
/vagrant/root/docroot/
├── css (sassのコンパイル先)
├── sass (Sassファイル入れる場所)
├── js (coffeeのコンパイル先)
└── coffee (coffeeの入れる場所)
[vagrant@localhost root]$ cd ./docroot/ [vagrant@localhost docroot]$ mkdir css [vagrant@localhost docroot]$ mkdir sass [vagrant@localhost docroot]$ mkdir js [vagrant@localhost docroot]$ mkdir coffee
続いて、テスト用のcoffeeファイルとsassファイルを作成しておきます。
[vagrant@localhost docroot]$ vim ./sass/style.scss #navbar { width: 80%; height: 23px; ul { list-style-type: none; } li { float: left; a { font-weight: bold; } } } [vagrant@localhost docroot]$ vim ./coffee/sample.coffee (function() { alert("hello world!"); }).call(this);
ひとまず、この2つのファイルを自動的にコンパイルしてくれるように作成していきます。
それでは、各種プラグインのインストールから行います。
[vagrant@localhost root]$ npm install grunt-contrib-coffee --save-dev --no-bin-links ・・・中略・・・ grunt-contrib-coffee@0.10.1 node_modules/grunt-contrib-coffee ├── chalk@0.4.0 (has-color@0.1.7, ansi-styles@1.0.0, strip-ansi@0.1.1) ├── lodash@2.4.1 └── coffee-script@1.7.1 (mkdirp@0.3.5) [vagrant@localhost root]$ npm install grunt-contrib-compass --save-dev --no-bin-links ・・・中略・・・ grunt-contrib-compass@0.9.0 node_modules/grunt-contrib-compass ├── dargs@0.1.0 ├── async@0.8.0 ├── tmp@0.0.23 └── bin-version-check@0.1.0 (minimist@0.1.0, semver@2.3.1, bin-version@0.1.0) [vagrant@localhost root]$ npm install grunt-contrib-watch --save-dev --no-bin-links ・・・中略・・・ grunt-contrib-watch@0.6.1 node_modules/grunt-contrib-watch ├── async@0.2.10 ├── lodash@2.4.1 ├── tiny-lr-fork@0.0.5 (debug@0.7.4, faye-websocket@0.4.4, qs@0.5.6, noptify@0.0.3) └── gaze@0.5.1 (globule@0.1.0)
3種類とも無事にインストールする事が出来ました!
あ!compassのプロジェクトを作るのを忘れていました・・・orz
compassのプロジェクトを作成します。
[vagrant@localhost root]$ compass create docroot [vagrant@localhost root]$ mv docroot/config.rb ./ [vagrant@localhost root]$ vim confing.rb http_path = "docroot/" css_dir = "docroot/css" sass_dir = "docroot/sass" images_dir = "docroot/images" javascripts_dir = "docroot/js"
こんな感じですかね?
一回テストで流しておきます。
[vagrant@localhost root]$ compass compile create docroot/css/ie.css create docroot/css/print.css create docroot/css/screen.css create docroot/css/style.css
正常にいったようです。
Gruntfile.js作成
sassとcoffeeをコンパイルし、ためのGruntfile.jsを記述しましょう。
module.exports = function(grunt) { grunt.initConfig({ coffee: { compile: { files:[{ expand: true, cwd: 'docroot/coffee', src: ['**/*.coffee'], dest: 'docroot/js/', ext: '.js', }] } }, compass: { dist: { options: { config: 'config.rb' } } }, watch: { coffee:{ tasks: 'coffee', files:['docroot/**/*.coffee'] }, compass:{ tasks: 'compass', files:['docroot/**/*.scss'] } } }); grunt.loadNpmTasks('grunt-contrib-coffee'); grunt.loadNpmTasks('grunt-contrib-compass'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('default', ['coffee','compass','watch']); };
$ gruntを実行してみよう!
それでは早速gruntを実行したいと思います。
[vagrant@localhost root]$ grunt Running "coffee:compile" (coffee) task Running "compass:dist" (compass) task unchanged docroot/sass/ie.scss unchanged docroot/sass/print.scss unchanged docroot/sass/screen.scss unchanged docroot/sass/style.scss Compilation took 0.042s Running "watch" task Waiting...
正常に各コンパイルしてから、waitingし始めました!
Gruntをバックグランドで実行しておこう!
最後に、gruntコマンドをバックグラウンドで実行しておけば、
変更があったときだけ、実施内容が表示されます。
waitingするのであれば、オススメです!
[vagrant@localhost root]$ nohup grunt & [1] 3830 [vagrant@localhost root]$ nohup: ignoring input and appending output to `nohup.out' [vagrant@localhost root]$
nohup grunt &
は、ログアウトしても、そのまま実行してくれますので、
オススメです!例えば、デバッグなんかをしたくて、終了させて、インラインで実行したい場合は
以下の手順となります。
ただ、実行した内容が、nohup.outというファイルに書き出されますので、そちらを確認するのもいいと思います。
(ファイルがかなり蓄積されるので、ファイルを作成しないというのもいいかもしれません。)
[vagrant@localhost ~]$ ps aux | grep 'grunt' vagrant 3830 2.6 9.6 717572 45492 ? Sl 03:27 0:12 grunt vagrant 3933 0.0 0.1 103236 856 pts/0 S+ 03:34 0:00 grep grunt [vagrant@localhost ~]$ kill 3830 [vagrant@localhost ~]$ cd /vagrant/root/ [vagrant@localhost ~]$ grunt
という感じです。
nohup.outを作成しないで実行する場合は以下の通りです。
[vagrant@localhost root]$ nohup grunt > /dev/null 2>&1 & [1] 3990
いかがでしたでしょうか?
ひとまず、gruntで簡単にコンパイル作業を自動化し、監視もバッグラウンドで行うといった
感じで、構成してみました!
今後は、Gruntで色んなタスクを自動化したいと思います。。。
手順コマンドまとめ
$ cd /vagrant/root/ $ npm init > 対話は全てエンター $ npm install grunt --save-dev --no-bin-links $ compass create docroot $ npm install grunt-contrib-coffee --save-dev --no-bin-links $ npm install grunt-contrib-compass --save-dev --no-bin-links $ npm install grunt-contrib-watch --save-dev --no-bin-links $ grunt & #もしくは $ nohup grunt > /dev/null 2>&1 &
ChefでphpMyAdminのインストールと、VirtualHostを設定する
フロントサイド用の環境構築がひと段落したので、
今度は、サーバーサイドの環境構築をもっと使いやすくしていきたいと思います。
参考にしたサイト
サードパーティ製chefレシピ使ってたの忘れてた - わすれっぽいきみえ
Chefを使ってみる(3) CookbookとRecipe - sparkgeneのブログ
Chefのrecipeの書き方 - ぺーぺーSEの日記
phpMyAdminをインストール
phpMyAdminをインストールするには、remiレポジトリを入れなければなりません。
Berkshelfを用いてインストールした後、phpMyAdminのインストールをしたいと思います。
前回作成し、利用したdevelop cookbooksを再利用したいとおもいます。
まずは、「site-cookbooks」の中にある「metadata.rb」に以下のコードを追記します。
name 'develop' maintainer 'YOUR_COMPANY_NAME' maintainer_email 'YOUR_EMAIL' license 'All rights reserved' description 'Installs/Configures develop' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version '0.1.0' #以下を追記 depends 'yum'
次に、Berksfileに以下を追記します。
site :opscode cookbook 'yum'
recipeのdefault.rbに追記していきます。
yum_repository 'epel' do description 'Extra Packages for Enterprise Linux' mirrorlist 'http://mirrors.fedoraproject.org/mirrorlist?repo=epel-6&arch=$basearch' fastestmirror_enabled true gpgkey 'http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6' action :create end # add the Remi repo yum_repository 'remi' do description 'Les RPM de Remi - Repository' baseurl 'http://rpms.famillecollet.com/enterprise/6/remi/x86_64/' gpgkey 'http://rpms.famillecollet.com/RPM-GPG-KEY-remi' fastestmirror_enabled true action :create end
これでepelとremiが使えるようになります。
次に、phpやmysqlなどをremiで入れていきます。
default.rbに追記していきます。
%w[php php-devel php-mbstring gd-devel php-gd].each do |pkg| yum_package pkg do action :install options "--enablerepo=remi" end end #mysqlのインストール %w[mysql-server mysql-devel phpMyAdmin php-mysql].each do |pkg| package pkg do action :install options "--enablerepo=remi" end end
それでは実行したいと思います。
$ knife solo cook develop ・・・中略・・・ * yum_repository[epel] action createRecipe:* template[/etc/yum.repos.d/epel.repo] action create - create new file /etc/yum.repos.d/epel.repo - update content in file /etc/yum.repos.d/epel.repo from none to 4dd1d3 --- /etc/yum.repos.d/epel.repo 2014-06-28 08:14:16.262168291 +0000 +++ /tmp/chef-rendered-template20140628-2604-yvjhmx 2014-06-28 08:14 :16.266170291 +0000 @@ -1 +1,12 @@ +# This file was generated by Chef +# Do NOT modify this file by hand. + +[epel] +name=Extra Packages for Enterprise Linux +enabled=1 +fastestmirror_enabled=true +gpgcheck=1 +gpgkey=http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6 +mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-6&arch =$basearch +sslverify=1 - change mode from '' to '0644' * execute[yum-makecache-epel] action run - execute yum -q makecache --disablerepo=* --enablerepo=epel * ruby_block[yum-cache-reload-epel] action create - execute the ruby block yum-cache-reload-epel * execute[yum-makecache-epel] action nothing (skipped due to action :nothing) * ruby_block[yum-cache-reload-epel] action nothing (skipped due to action :not hing) Recipe: develop::default * yum_repository[remi] action createRecipe: * template[/etc/yum.repos.d/remi.repo] action create - create new file /etc/yum.repos.d/remi.repo - update content in file /etc/yum.repos.d/remi.repo from none to f62c82 --- /etc/yum.repos.d/remi.repo 2014-06-28 08:14:33.670868273 +0000 +++ /tmp/chef-rendered-template20140628-2604-h828u 2014-06-28 08:14 :33.672869273 +0000 @@ -1 +1,12 @@ +# This file was generated by Chef +# Do NOT modify this file by hand. + +[remi] +name=Les RPM de Remi - Repository +baseurl=http://rpms.famillecollet.com/enterprise/6/remi/x86_64/ +enabled=1 +fastestmirror_enabled=true +gpgcheck=1 +gpgkey=http://rpms.famillecollet.com/RPM-GPG-KEY-remi +sslverify=1 - change mode from '' to '0644' * execute[yum-makecache-remi] action run - execute yum -q makecache --disablerepo=* --enablerepo=remi * ruby_block[yum-cache-reload-remi] action create - execute the ruby block yum-cache-reload-remi * execute[yum-makecache-remi] action nothing (skipped due to action :nothing) * ruby_block[yum-cache-reload-remi] action nothing (skipped due to action :nothing) Recipe: develop::default * yum_package[php] action install - install version 5.4.30-1.el6.remi of package php
インストールされたようですので、バージョン確認をしたいと思います。
remiでインストールされていれば、5.4系が入っているはずです。
また、phpMyAdminもインストールされているはずです。
[vagrant@localhost ~]$ php -v PHP 5.4.30 (cli) (built: Jun 25 2014 15:27:51) [vagrant@localhost ~]$ cd /usr/share/phpMyAdmin/ [vagrant@localhost phpMyAdmin]$ pwd /usr/share/phpMyAdmin
ひとまず正常に入っていました!
templateリソースでVirtualHostを設定したいと思います。
まずは、以下の内容を「vhost.conf.erb」ファイルという名前で「site-cookbook/template/default/」の中に保存します。
NameVirtualHost *:80 <VirtualHost *:80> ServerName <%= node[:domain] %> DocumentRoot /var/www/html/ ErrorLog /var/log/php_error.log TransferLog /var/log/php_access.log <Directory /var/www/html/> AllowOverride all Order allow,deny allow from all </Directory> </VirtualHost> <VirtualHost *:80> ServerName php.<%= node[:domain] %> DocumentRoot /usr/share/phpMyAdmin/ ErrorLog /var/log/php_error.log TransferLog /var/log/php_access.log <Directory /usr/share/phpMyAdmin/> AllowOverride all Order allow,deny allow from all </Directory> </VirtualHost> NameVirtualHost *:443 <VirtualHost *:443> ServerName <%= @domain %> DocumentRoot /var/www/html/ ErrorLog logs/ssl_error_test_log CustomLog logs/ssl_access_test_log combined LogLevel warn SSLEngine on SSLProtocol all -SSLv2 SSLCertificateFile /etc/pki/tls/certs/localhost.crt SSLCertificateKeyFile /etc/pki/tls/private/localhost.key <Directory /var/www/html/> AllowOverRide ALL </Directory> </VirtualHost>
次に、「nodes/develop.json」ファイルを以下のように修正します。
{ "domain":"develop.jp", "run_list":[ "recipe[develop]", "recipe[develop::ruby]", "recipe[develop::node]", "recipe[develop::grunt]" "recipe[develop::developer]" ] }
前回オレオレ証明書を作成した際に作成したrecipe(ssl.rb)をリファクタリングして、
ssl.rbからdeveloper.rbというファイル名に変更して、そこに追記していきます。
#Virtual hostsを設定する template "vhost.conf" do path "/etc/httpd/conf.d/vhost.conf" source "vhost.conf.erb" mode 0644 variables( :domain => node["domain"] ) notifies :reload, 'service[httpd]' end
以上で、変数を用いて作成してくれるので、毎回VirtualHostを書き換える必要がなくなります!
それでは実行したいと思います。
$ knife solo cook develop ・・・中略・・・ * template[vhost.conf] action create - create new file /etc/httpd/conf.d/vhost.conf - update content in file /etc/httpd/conf.d/vhost.conf from none to db139f --- /etc/httpd/conf.d/vhost.conf 2014-06-28 10:00:42.403156956 +0000 +++ /tmp/chef-rendered-template20140628-17069-101hcd4 2014-06-28 10:00:42.404156957 +0000 @@ -1 +1,27 @@ ・・・中略・・・ - change mode from '' to '0644'
正常になっているか、ファイルの内容を確認したいと思います。
[vagrant@localhost ~]$ vim /etc/httpd/conf.d/vhost.conf NameVirtualHost *:80 <VirtualHost *:80> ServerName develop.jp DocumentRoot /var/www/html/ ErrorLog /var/log/php_error.log TransferLog /var/log/php_access.log <Directory /var/www/html/> AllowOverride all Order allow,deny allow from all </Directory> </VirtualHost> <VirtualHost *:80> ServerName php.develop.jp DocumentRoot /usr/share/phpMyAdmin/ ErrorLog /var/log/php_error.log TransferLog /var/log/php_access.log <Directory /usr/share/phpMyAdmin/> AllowOverride all Order allow,deny allow from all </Directory> </VirtualHost> NameVirtualHost *:443 <VirtualHost *:443> ServerName develop.jp DocumentRoot /var/www/html/ ErrorLog logs/ssl_error_test_log CustomLog logs/ssl_access_test_log combined LogLevel warn SSLEngine on SSLProtocol all -SSLv2 SSLCertificateFile /etc/pki/tls/certs/localhost.crt SSLCertificateKeyFile /etc/pki/tls/private/localhost.key <Directory /var/www/html/> AllowOverRide ALL </Directory> </VirtualHost>
正常に動作しているか、Windowsのhostsファイルを修正してドメイン解決を行った上で、
ブラウザより確認したいと思います。
設定したドキュメントルート先:「http://develop.jp/」
設定したドキュメントルート先(SSL版):「https://develop.jp/」
phpMyAdmin先「http://php.develop.jp/」
一応自分の環境で確認したところ、正常にページが確認する事が出来ました。
今回の開発で、phpMyAdminとVirtualHostの設定をChefで実行できるようになりました。
また、今後は開発環境をfixできるように環境構築をまとめていきたいと思います。