HAProxyでロードバランシング
サーバの負荷分散ができるHAProxyを使ってみました。
サーバ構成
役割
|
IP
|
備考
|
---|---|---|
webサーバ | 192.168.1.121 | index.htmlを配置 |
webサーバ | 192.168.1.122 | index.htmlを配置 |
HAProxy | 192.168.1.123 | |
webサーバ | 192.168.1.124 | index.txt |
構築
HAProxy(192.168.1.123)
インストール
$ sudo yum -y install haproxy
設定ファイル
$ cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon# turn on stats unix socket
stats socket /var/lib/haproxy/statsdefaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000frontend main *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js .htmluse_backend static if url_static
default_backend appbackend static
balance roundrobin
server static 192.168.1.121:80 check
server static 192.168.1.122:80 checkbackend app
balance roundrobin
server app1 192.168.1.124:80 check
.htmlファイルは192.168.1.121と192.168.1.122に、
そのほかのファイルは192.168.1.124に接続されるよう設定しました。
起動
$ sudo /etc/init.d/haproxy start
webサーバ(192.168.1.121、192.168.1.122、192.168.1.124)
インストール
設定ファイル
192.168.1.121サーバ
$ cat /var/www/html/index.html
<html>
hoge1
</html>
192.168.1.122サーバ
$ cat /var/www/html/index.html
<html>
hoge2
</html>
192.168.1.123サーバ
$ cat /var/www/html/index.txt
hoge4.txt
起動
sudo /etc/init.d/httpd start
実行
index.htmlにアクセスする。
$ curl 192.168.1.123/index.html
<html>
hoge1
</html>
$ curl 192.168.1.123/index.html
<html>
hoge2
</html>
$ curl 192.168.1.123/index.html
<html>
hoge1
</html>
$ curl 192.168.1.123/index.html
<html>
hoge2
</html>
192.168.1.121と192.168.1.122交互にアクセスしています
index.txtにアクセスする。
$ curl 192.168.1.123/index.txt
hoge4.txt$ curl 192.168.1.123/index.txt
hoge4.txt
192.168.1.124のみアクセスしています
無事負荷分散できました!
参考URL
オデの日記@WEB系: L7ロードバランサHaproxyを使う(その2 設定から起動)
RabbitMQでメッセージキューイングシステムを実現する
RabbitMQとはAMQP対応のメッセージキューイングシステムです。
何ができるかというと、遅い処理をキューに貯めてあとで処理することで高速なレスポンスを返すとができます。
もう少しわかりやすく表すとこんな感じです。
メッセージキューシステムを使わない場合
メッセージキューシステムを使う場合
では、さっそく使ってみます
RabbitMQのインストール
今回はrubyのbunnyでRabbitMQを使ってみました。
bunnyのインストール
erlangのインストール
RabbitMQのインストール
Management Plugin(RabbitMQ)のインストール
※必須ではないですが、管理が便利なので入れています
https://www.rabbitmq.com/management.html
RabbitMQの起動
ユーザの追加と削除
クラスタ化
4台でクラスタ化しました。
https://www.rabbitmq.com/clustering.html
host名
|
ip
|
role
|
---|---|---|
myserver |
192.168.1.210 | Consumer |
myserver2 |
192.168.1.212 | Consumer |
myserver3 |
192.168.1.213 | Publisher |
myserver4 |
192.168.1.214 | Publisher |
myserver2、myserver3、myserver4で以下を実行しクラスタ化
管理画面で見るとこんな感じになります
実行
Publisher側のコード
Consumerのコード
実行する
Consumer1
Consumer2
Publisher1側
1秒おきにメッセージを送る
Publisher2側
2秒おきにメッセージを送る
実行結果
Publisher1、Publisher2から送られたメッセージがConsumer1、Consumer2で処理されています。
試したところこんな動きをしていました。
Consumer1
|
Consumer2
|
結果
|
---|---|---|
停止 | 停止 | 処理されずキューがたまる。Consumer起動時にたまったキューを処理 |
停止 | 稼働 | Consumer2で処理 |
稼働 | 停止 | Consumer1で処理 |
稼働 | 稼働 | Consumer1、Consumer2で交互に処理(Round-Robin?) |
おしまい。
参考
logstashで不正アクセスのログを集める
grepでログを確認するのが面倒だ!!
そんなときに便利なログ収集ツールlogstashを使ってみました。
fluentdを使う方法もあるそうですが、今回はlogstash+elasticsearch+kibanaで構築します。
サーバ | 機能名 | 用途 |
---|---|---|
192.168.1.210 | logstash | elasticsearchにログを送る |
192.168.1.212 | elasticsearch | logstashから送られたログをdbに保存 |
kibana | elasticsearchからログを取得し画面に表示する |
elasticsearchのインストール(192.168.1.212)
javaのインストール
sudo yum install java-1.7.0-openjdk
elasticsearchのダウンロード
$ curl -O https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.tar.gz
$ tar zxvf elasticsearch-1.1.1.tar.gz
$ cd elasticsearch-1.1.1/
elasticsearchの実行
$ ./bin/elasticsearch
kibanaのインストール(192.168.1.212)
アパッチのインストール
kibanaのインストール
$ cd /var/www/html/
$ sudo curl -O https://download.elasticsearch.org/kibana/kibana/kibana-3.1.0.tar.gz
$ sudo tar zxvf kibana-3.1.0.tar.gz
アパッチのリスタート
$ sudo /etc/init.d/httpd restart
kibanaにアクセス
フィルターを作成
termsパネルを追加
logstashのインストール(192.168.1.210)
javaのインストール
logstashのダウンロード
$ wget https://download.elasticsearch.org/logstash/logstash/logstash-1.4.2.tar.gz --no-check-certificate
$ tar zxvf logstash-1.4.2.tar.gz
$ cd logstash-1.4.2
logstashの設定ファイルの作成
この形式のログを分解しelasticsearchに送ることにします
Sep 18 01:01:01localhosti sshd[1234]: Invalid user oracle from 123.456.789.012
vim mylogstash.conf
input { |
この辺りを参考にfilterを作る
logstashを起動
$ sudo bin/logstash -f mylogstash.conf -l log.txt
実験
192.168.1.210に不正アクセスをする
ssh hogeq@192.168.1.210
ssh hogeq@192.168.1.210
ssh hog2@192.168.1.210
ssh hog2@192.168.1.210
ssh test@192.168.1.210
kibnaにアクセスして確認
不正アクセスしたユーザをグラフ化できました。
実際はoracleとかnagiosとかで接続しに来ることが多いようです
おしまい。
参考
Vagrantコマンド
久しぶりにvagrantを使ったらコマンドを忘れていたのでまとめたメモです。
Vagrantfileのサンプル
Vagrantfileを作る
boxの追加
※Discover ready-made boxes から利用できるboxを確認できます
box確認
状態の確認
起動
ログイン
終了
中断
再開
provisionだけ実行
boxの追加
※実行前にVirtualBox上でCentOS64という名前の仮想環境を作る必要があります。
boxの削除
リンク
chefでユーザ作成
chefを使ってユーザを作成する方法
chef solo, knife solo, chef serverで実行できます。
レシピ
# パスワードをhogeに
$ openssl passwd -1 "hoge"
$1$EVTsgD0f$QpzIEDmbxqVN73laspifv.
# hogeユーザの作成
$ vim recipes/default.rb
user "hoge" do
comment "hoge User"
uid 1234
gid "users"
home "/home/hoge"
shell "/bin/bash"
password "$1$EVTsgD0f$QpzIEDmbxqVN73laspifv."
action:create
end
あとはレシピを実行すればhogeユーザが作成されます。
仕様を見る限りユーザの削除もできます。
参考資料
KnifeSoloを使ってサーバー構築
ChefServer、ChefSoloに続いて、KnifeSoloを使ってみました。
サーバーの準備
例によって、Vagrantで環境を作ります。
Vagrantfile作成
構築
ログイン
KinfeSoloのインストール
rubyのインストール
KnifeSoloをインストー/ル
レポジトリの作成
クライアントにchef-soloをインストール
※ エラーになったのでrsyncを入れました。
sudo yum install rsync
レシピの作成
クライアントにレシピをインストール
パスワードきかれすぎ。。、nopass以外でいい方法が知りたいです。。
ブラウザから確認
参考URL
トラブルシューティング
ChefSoloの使い方
ChefServerは3台必要でしたが、ChefSoloは1台でChefを実行できます。
今回はChefSoloを使ってWebサーバを構築します。
サーバの準備
vagrantを使っています。
Vagrant file
$ vim Vagrantfile VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.define :centos2 do |dev| dev.vm.box = "chef/centos-6.5" dev.vm.hostname = "dev102" dev.vm.network :private_network, ip: "10.0.0.102" end end |
構築
$ vagrant up centos2 |
ログイン
$ vagrant ssh centos2 |
ChefSoloをインストール
$ wget https://www.opscode.com/chef/install.sh $ sudo bash install.sh -v 11.4.0 |
レシピの作成
knifeの初期化
$ knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/home/vagrant/.chef/knife.rb]
Please enter the chef server URL: http://dev102:4000
Please enter an existing username or clientname for the API: [vagrant]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef/validation.pem]
Please enter the path to a chef repository (or leave blank):
|
レシピの作成
# gitのインストール $ vi localhost.json { "run_list":[ "recipe[sample]" ] } $ vi solo.rb file_cashe_path "/tmp/chef-solo" cookbook_path ["/home/vagrant/chef-repo/cookbook"] |
ChefSoloを実行
$ sudo chef-solo -c solo.rb -j localhost.json Starting Chef Client, version 11.4.0 Compiling Cookbooks... Converging 3 resources Recipe: sample::default * package[httpd] action install - install version 2.2.15-30.el6.centos of package httpd * service[httpd] action enable - enable service service[httpd] * service[httpd] action start - start service service[httpd] * cookbook_file[/var/www/html/index.html] action create - create a new cookbook_file /var/www/html/index.html --- /tmp/chef-tempfile20140515-7393-tcwf1m 2014-05-15 15:54:20.639403562 +0000 +++ /home/vagrant/chef-repo/cookbook/sample/files/default/index.html 2014-05-15 15:47:15.061722443 +0000 @@ -0,0 +1,6 @@ +<html> +<body> + <h1>Hello, world!</h1> +</body> +</html> + Chef Client finished, 4 resources updated |
確認
ブラウザから確認
http://10.0.0.102/ |
おしまい。