apache: 2009年1月アーカイブ

前回までのエントリー(コレコレ)で、heartbeatのインストールとVIPの引継ぎができました。しかしそれだけじゃ冗長化には程遠いってのが人情。次は、apacheのサービスを引き継がせてみましょう。

■ httpdの停止

[重要]
heartbeatの設定をする前に、既存のhttpdを停止します。勿論、httpdの自動起動も停止しておきます。これは、今後httpdの起動停止をheartbeatがつかさどるためです。

[root@ishikari ~]# /etc/init.d/httpd stop
[root@ishikari ~]# chkconfig httpd off
[root@ishikari ~]# chkconfig httpd --list
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
    ↑
すべてのランレベルでoffになっていることを確認する。

※ 注意
yumでhttpdをインストールして、/etc/sysconfig/httpd ファイルを使って apache を worker モードで動かしている場合、heartbeat経由ではこの設定が失われてしまいます(preforkで動作してしまうってこと)。よって、 /usr/sbin/httpd を /usr/sbin/httpd.orig としてバックアップし、 /usr/sbin/httpd.worker へのシンボリックリンクとして /usr/sbin/httpd を設定すること。

例)
# mv /usr/sbin/httpd /usr/sbin/httpd.orig ; ln -s /usr/sbin/httpd.worker /usr/sbin/httpd


■ heartbeat停止

一旦両系でheartbeatを停止します。

[root@ishikari ~]# /etc/init.d/heartbeat stop
Stopping High-Availability services:
                                                           [  OK  ]

■ httpd監視設定を行う

[root@ishikari ~]# cd /var/lib/heartbeat/crm/

※ すべてのファイルをバックアップする
[root@ishikari crm]# tar czf cib.tar.gz *

※ cib.xml.last cib.xml.sig cib.xml.sig.last を削除する
[root@ishikari crm]# rm -f cib.xml.last cib.xml.sig cib.xml.sig.last

cib.xml を編集する
[root@ishikari crm]# vi cib.xml

まず、<nodes>タグの直下にある、<resources>の閉じタグ「<resources/>」を削除します。(そもそも閉じタグだけで、<resources> タグが開始されていないが・・・)

※ ここを
     <nodes>
       <node id="12979f53-e6ec-40f0-a7c2-66dfadf286ba" uname="sorachi.in.example.com" type="normal"/>
       <node id="82d27f7b-4e9a-478e-bcc1-80da0140a4a7" uname="ishikari.in.example.com" type="normal"/>
     </nodes>
     <resources/>

※ こうする
     <nodes>
       <node id="12979f53-e6ec-40f0-a7c2-66dfadf286ba" uname="sorachi.in.example.com" type="normal"/>
       <node id="82d27f7b-4e9a-478e-bcc1-80da0140a4a7" uname="ishikari.in.example.com" type="normal"/>
     </nodes>

次に、<nodes>タグの直下に、以下を追記します。
ココで投入する固有の値は「group_apache(どうやらグループの名前は何でもよいっぽい)」「192.168.0.200(httpをlistenするVIPの値)」「eth0(VIPをeth0インターフェイスのエイリアスにする)」「24(サブネットマスクのビット数)」「/etc/httpd/conf/httpd.conf(httpd.confのフルパス)」です。

     <resources>
       <group id="group_apache">
         <primitive id="ipaddr" class="ocf" type="IPaddr" provider="heartbeat">
         <instance_attributes id="ia_ipaddr">
           <attributes>
             <nvpair id="ia_ipaddr_ip" name="ip" value="192.168.0.200"/>
             <nvpair id="ia_ipaddr_nic" name="nic" value="eth0"/>
             <nvpair id="ia_ipaddr_netmask" name="netmask" value="24"/>
           </attributes>
         </instance_attributes>
       </primitive>
       <primitive id="apache" class="ocf" type="apache" provider="heartbeat">
         <instance_attributes id="ia_apache">
           <attributes>
             <nvpair id="ia_apache_configfile" name="configfile" value="/etc/httpd/conf/httpd.conf"/>
           </attributes>
         </instance_attributes>
         </primitive>
       </group>
     </resources>

以上の設定が終わったら保存します。


■ httpd.confを修正する

[root@ishikari crm]# cd /etc/httpd/conf
[root@ishikari conf]# cp -p httpd.conf httpd.conf.20090104
[root@ishikari conf]# vi httpd.conf

※ ここを
Listen 80

※ こうする
Listen 192.168.0.200:80


※ ここを
#<Location /server-status>
#    SetHandler server-status
#    Order deny,allow
#    Deny from all
#    Allow from .example.com
#</Location>

※ こうする
<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from 192.168.0
</Location>


■ heartbeatを起動する

[root@ishikari ~]# /etc/init.d/heartbeat start
Starting High-Availability services:
                                                           [  OK  ]

[root@sorachi ~]# /etc/init.d/heartbeat start
Starting High-Availability services:
                                                           [  OK  ]


■ 起動後の確認

しばらく時間をおいて、両系にて監視コマンドを実行します。以下のように表示されていればOK。

= ココに注目! =================================================================================
    ipaddr      (heartbeat::ocf:IPaddr):        Started sorachi.in.example.com
    apache      (heartbeat::ocf:apache):        Started sorachi.in.example.com
================================================================================================


[root@ishikari ~]# crm_mon -i 3
Defaulting to one-shot mode
You need to have curses available at compile time to enable console mode


============
Last updated: Sun Jan  4 04:43:09 2009
Current DC: sorachi.in.example.com (12979f53-e6ec-40f0-a7c2-66dfadf286ba)
2 Nodes configured.
1 Resources configured.
============

Node: sorachi.in.example.com (12979f53-e6ec-40f0-a7c2-66dfadf286ba): online
Node: ishikari.in.example.com (82d27f7b-4e9a-478e-bcc1-80da0140a4a7): online

Resource Group: group_apache
    ipaddr      (heartbeat::ocf:IPaddr):        Started sorachi.in.example.com
    apache      (heartbeat::ocf:apache):        Started sorachi.in.example.com


[root@sorachi ~]# crm_mon -i 3
Defaulting to one-shot mode
You need to have curses available at compile time to enable console mode


============
Last updated: Sun Jan  4 04:43:18 2009
Current DC: sorachi.in.example.com (12979f53-e6ec-40f0-a7c2-66dfadf286ba)
2 Nodes configured.
1 Resources configured.
============

Node: sorachi.in.example.com (12979f53-e6ec-40f0-a7c2-66dfadf286ba): online
Node: ishikari.in.example.com (82d27f7b-4e9a-478e-bcc1-80da0140a4a7): online

Resource Group: group_apache
    ipaddr      (heartbeat::ocf:IPaddr):        Started sorachi.in.example.com
    apache      (heartbeat::ocf:apache):        Started sorachi.in.example.com


■■ 実際の動きを見てみた

テストコンテンツとして、各々のホスト名を書いたindex.htmlをドキュメントルートに配置。sorachiのeth0を抜き差しして片系障害をおこし、フェイルオーバー/フェイルバックされることを確認しました。また、ifconfigコマンドで確認すると、httpdが生きている間は、ちゃんとVIPが設定されています。

【httpdが生きているsorachi】

[oresama@sorachi ~]$ ps auxwww | grep [h]eartbeat
root     22169  0.0  0.5  12072 12072 ?        SLs  07:23   0:00 heartbeat: master control process
nobody   22172  0.0  0.2   5476  5476 ?        SL   07:23   0:00 heartbeat: FIFO reader
nobody   22173  0.0  0.2   5472  5472 ?        SL   07:23   0:00 heartbeat: write: ucast eth1
nobody   22174  0.0  0.2   5472  5472 ?        SL   07:23   0:00 heartbeat: read: ucast eth1
498      22178  0.0  0.0   4900  1756 ?        S    07:23   0:00 /usr/lib/heartbeat/ccm
498      22179  0.2  0.1   6628  2868 ?        S    07:23   0:00 /usr/lib/heartbeat/cib
nobody   22180  0.0  0.0   4596  1616 ?        S    07:23   0:00 /usr/lib/heartbeat/lrmd -r
nobody   22181  0.0  0.2   4628  4628 ?        SL   07:23   0:00 /usr/lib/heartbeat/stonithd
498      22182  0.0  0.0   4784  1716 ?        S    07:23   0:00 /usr/lib/heartbeat/attrd
498      22183  0.0  0.1   5604  2268 ?        S    07:23   0:00 /usr/lib/heartbeat/crmd
root     22184  3.1  0.1   5920  2096 ?        S    07:23   0:02 /usr/lib/heartbeat/mgmtd -v
root     22185  0.0  0.0   4544  1316 ?        S    07:23   0:00 /usr/lib/heartbeat/pingd -m 100 -d 5s -a default_ping_set

[oresama@sorachi ~]$ ps auxwww | grep [h]ttpd
root     22443  0.2  0.3  14736  7332 ?        Ss   07:23   0:00 /usr/sbin/httpd -DSTATUS -f /etc/httpd/conf/httpd.conf
apache   22454  0.0  0.4 2660000 10232 ?       Sl   07:23   0:00 /usr/sbin/httpd -DSTATUS -f /etc/httpd/conf/httpd.conf

[oresama@sorachi ~]$ /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:90:CC:E9:30:AB
          inet addr:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::290:ccff:fee9:30ab/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:47609 errors:0 dropped:0 overruns:0 frame:0
          TX packets:50473 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:11591250 (11.0 MiB)  TX bytes:24177872 (23.0 MiB)
          Interrupt:193 Base address:0xa000

eth0:0    Link encap:Ethernet  HWaddr 00:90:CC:E9:30:AB
          inet addr:192.168.0.200  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:193 Base address:0xa000

eth1      Link encap:Ethernet  HWaddr 00:C0:9F:1A:3C:F2
          inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::2c0:9fff:fe1a:3cf2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8757 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8879 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:2555439 (2.4 MiB)  TX bytes:2625068 (2.5 MiB)
          Base address:0xecc0 Memory:fe100000-fe120000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:18876 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18876 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:4157055 (3.9 MiB)  TX bytes:4157055 (3.9 MiB)


【httpdが死んでいるishikari】


[oresama@ishikari ~]$ ps auxwww | grep [h]eartbeat
root     14050  0.0  0.5  12076 12076 ?        SLs  06:53   0:00 heartbeat: master control process
nobody   14053  0.0  0.2   5480  5480 ?        SL   06:53   0:00 heartbeat: FIFO reader
nobody   14054  0.0  0.2   5476  5476 ?        SL   06:53   0:00 heartbeat: write: ucast eth1
nobody   14055  0.0  0.2   5476  5476 ?        SL   06:53   0:00 heartbeat: read: ucast eth1
498      14058  0.0  0.0   4928  2008 ?        S    06:53   0:00 /usr/lib/heartbeat/ccm
498      14059  0.2  0.1   6628  2936 ?        S    06:53   0:03 /usr/lib/heartbeat/cib
root     14060  0.0  0.0   4896  1880 ?        S    06:53   0:00 /usr/lib/heartbeat/lrmd -r
nobody   14061  0.0  0.2   4624  4624 ?        SL   06:53   0:00 /usr/lib/heartbeat/stonithd
498      14062  0.0  0.0   5188  1820 ?        S    06:53   0:00 /usr/lib/heartbeat/attrd
498      14063  0.0  0.1   5624  2668 ?        S    06:53   0:00 /usr/lib/heartbeat/crmd
root     14064  0.0  0.0   5924  1912 ?        S    06:53   0:00 /usr/lib/heartbeat/mgmtd -v
root     14065  0.0  0.0   4544  1336 ?        S    06:53   0:00 /usr/lib/heartbeat/pingd -m 100 -d 5s -a default_ping_set
498      15217  0.0  0.1   5132  2124 ?        S    06:55   0:00 /usr/lib/heartbeat/tengine
498      15218  0.0  0.1   5760  2380 ?        S    06:55   0:00 /usr/lib/heartbeat/pengine

[oresama@ishikari ~]$ ps auxwww | grep [h]ttpd

[oresama@ishikari ~]$ /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:90:CC:E9:2D:DD
          inet addr:192.168.0.100  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::290:ccff:fee9:2ddd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:45991 errors:0 dropped:0 overruns:0 frame:0
          TX packets:51372 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:11094384 (10.5 MiB)  TX bytes:15222245 (14.5 MiB)
          Interrupt:201 Base address:0xc000

eth1      Link encap:Ethernet  HWaddr 00:C0:9F:1A:3C:2D
          inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::2c0:9fff:fe1a:3c2d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8820 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8785 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:2631005 (2.5 MiB)  TX bytes:2542771 (2.4 MiB)
          Base address:0xecc0 Memory:fe100000-fe120000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:18799 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18799 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:4120744 (3.9 MiB)  TX bytes:4120744 (3.9 MiB)


これで切替の基本動作はOK。sorachiのネットワークケーブルを抜いてから3秒で検知し、その後3秒でapacheが切り替わり、sorachiのネットワーク復旧は5秒で検知し、その後apacheは5秒で切り替わりました。都合6秒で切り替わり、復旧は都合10秒。

ね、簡単でしょ?


通常、apacheを何も考えずにインストールすると、preforkで動作します。小規模サイトであればそのままでも構わないのですが、大規模サイトでスレッドによる処理の引渡しが要件に入ってくると、workerで適切なパラメータを投入して動かしたほうがパフォーマンスがよくなります。
※ ただしマシンスペックが低いとあんまり恩恵にあずかれないかも・・・

ちなみに、rpmからインストールしたapacheの動作をpreforkからworkerに変えるのは簡単。
/etc/sysconfig/httpd ファイルの
#HTTPD=/usr/sbin/httpd.worker ← ココのコメントを外し、apacheを再起動するだけ。

しかし問題が1つあって、CentOS付属のPHPはスレッドによる処理の引渡に対応していないので、どノーマルなhttpdとphpをインストールした後にworkerで動作させようとすると、apacheの起動時に以下のようなエラーを吐くのです。。。対処法はココを参考にしました。

$ sudo /etc/init.d/httpd start
Starting httpd: [Fri Jan 02 18:13:43 2009] [crit] Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe.  You need to recompile PHP.
Pre-configuration failed
                                                           [FAILED]


■■ PHPのソースRPM入手とインストール

■ ソースRPMの入手

CentOSのミラーサイトには、大抵SRPMのディレクトリがあります。今回は理研さまのサイトからもってきました。
[oresama@sorachi ~]$ wget http://ftp.riken.jp/Linux/centos/5/os/SRPMS/php-5.1.6-20.el5.src.rpm


■ ソースrpmを展開する
[oresama@sorachi ~]$ rpm -ivh php-5.1.6-20.el5.src.rpm


■ SPECファイルを編集する
[oresama@sorachi ~]$ cd rpm/SPECS/
[oresama@sorachi SPECS]$ cp -p php.spec php.spec.orig
[oresama@sorachi SPECS]$ vi php.spec

ここを
%configure \
        --cache-file=../config.cache \
・・・

こうする
%configure \
        --with-tsm-pthreads \
        --enable-maintainer-zts \
        --cache-file=../config.cache \

tsm-pthreadsとmaintainer-ztsを有効にしてやるってことです。


■ 必要なパッケージをインストールする

その他、configureオプションを意図的に削除しない限りは、最低限以下のパッケージが必要になります。

apr-devel apr-util-devel aspell-devel autoconf automake beecrypt-devel bzip2-devel curl-devel cyrus-sasl-devel db4-devel e2fsprogs-devel elfutils-devel elfutils-devel-static elfutils-libelf-devel elfutils-libelf-devel-static expat-devel fontconfig-devel freetype-devel gcc-c++ gd gd-devel gmp-devel httpd-devel imake keyutils-libs-devel krb5-devel libc-client libc-client-devel libgcrypt-devel libgpg-error-devel libidn-devel libjpeg-devel libpng-devel libselinux-devel libsepol-devel libstdc++-devel libtool libX11-devel libXau-devel libXdmcp-devel libxml2-devel libXpm libXpm-devel libxslt libxslt-devel lm_sensors mesa-libGL-devel mysql mysql-devel ncurses-devel net-snmp net-snmp-devel net-snmp-libs openldap-devel openssl-devel pam-devel pcre-devel perl-DBI pkgconfig postgresql postgresql-devel rpm-devel sqlite-devel unixODBC unixODBC-devel xorg-x11-proto-devel zlib-devel

■ rpmbuildコマンドでビルドする

[oresama@sorachi SPECS]$ rpmbuild -bb php.spec


■ パッケージをインストールする

[oresama@sorachi SPECS]$ cd ../RPMS/i386
[oresama@sorachi i386]$ rpm --test -ivh php-5.1.6-20.i386.rpm php-cli-5.1.6-20.i386.rpm php-common-5.1.6-20.i386.rpm
[oresama@sorachi i386]$ sudo rpm -ivh php-5.1.6-20.i386.rpm php-cli-5.1.6-20.i386.rpm php-common-5.1.6-20.i386.rpm


■ apacheを再起動して反映
[oresama@sorachi i386]$ sudo /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]


※ yumでパッケージをアップデートする際に、phpの上書きインストールがされないよう、yum.confでexcludeしましょう。

[oresama@sorachi ~]$ cd /etc/
[oresama@sorachi etc]$ sudo cp -p yum.conf yum.conf.orig
[oresama@sorachi etc]$ sudo vi yum.conf

この行を追加
exclude=php*


あとは、phpのテストページが正常に表示されればOKです。
ね、簡単でしょ?
カスタム検索

カレンダー



このアーカイブについて

このページには、2009年1月以降に書かれたブログ記事のうちapacheカテゴリに属しているものが含まれています。

前のアーカイブはapache: 2008年12月です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

Powered by Movable Type 4.01