本實驗是使用nginx來創建一個簡單網頁,同時結合HA來提供高可用服務,接下來就是真個安裝和配置的過程:
總體拓撲以下圖所示:
--------- ---------
| HA1 |___________| HA2 |
|_______| |_______|
VIP:192.168.1.80
HA1:192.168.1.78
HA2:192.168.1.151
1、環境變量的初始化
1.安裝開發環境
#yum groupinstall "Development Tools" "Development Libraries"
#yum install gcc openssl-devel zlib-devel
#yum install pcre-devel
2.添加nginx用戶
#groupadd nginx
#useradd -g nginx -M -s /sbin/false nginx
3.配置/etc/hosts文件
#vim /etc/hosts
192.168.1.78 node1.luowei.com node1
192.168.1.151 node2.luowei.com node2
4.配置兩個HA之間不用密碼就能相互訪問:
#ssh-keygen -t rsa
#ssh-copy-id -i .ssh/id_rsa.pub
root@node2
同時也在HA2上作三、4操做
2、安裝最新版本的nginx,這個2011-9-14號剛發佈的nginx-1.1.3.tar.gz,詳細能夠參照官網
http://www.nginx.com
#tar xf nginx-1.1.3.tar.gz
#cd nginx-1.1.3
#./configure \
--prefix=/usr \ //安裝的目錄
--sbin-path=/usr/sbin/nginx \ //可執行文件的路徑
--conf-path=/etc/nginx/nginx.conf \ //配置文件的路徑
--error-log-path=/var/log/nginx/error.log \ //錯誤日誌的位置
--http-log-path=/var/log/nginx/access.log \ //訪問日誌的位置
--pid-path=/var/run/nginx/nginx.pid \ //pid模塊的位置
--lock-path=/var/lock/nginx.lock \
--user=nginx \ //用戶名
--group=nginx \ //用戶組名
--with-http_ssl_module \ //支持ssl的模塊
--with-http_flv_module \ //支持流媒體功能
--with-http_stub_status_module \ //狀態檢測的模塊
--with-http_gzip_static_module \ //壓縮模塊
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \ //代理的緩存的位置
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ //使用fastcgi的目錄
--with-pcre
#make && make install //編譯安裝
3、配置服務腳本,而且啓動服務進行測試
#vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
#chmod +x /etc/init.d/nginx //爲腳本賦予執行的權限
#chkconfig --add nginx //添加到服務隊列中
#service nginx start //啓動服務
4、以上過程也要在HA2上進行操做,我這裏就省略了
5、配置corosycn
在配置corosync的時候要確保nginx服務是中止的,而且不會隨機器啓動而自動啓動
#service nginx stop
#chkconfig nginx off
(注:這個要在兩個節點上都要操做的,要符合HA高可用集羣的條件!)
#service nginx stop
#chkconfig nginx off
(注:在兩個上面都要作)
安裝和配置corosync:
[root@node1 corosync]# ls
cluster-glue-1.0.6-1.6.el5.i386.rpm
cluster-glue-libs-1.0.6-1.6.el5.i386.rpm
corosync-1.2.7-1.1.el5.i386.rpm
corosynclib-1.2.7-1.1.el5.i386.rpm
heartbeat-3.0.3-2.3.el5.i386.rpm
heartbeat-libs-3.0.3-2.3.el5.i386.rpm
libesmtp-1.0.4-5.el5.i386.rpm
openais-1.1.3-1.6.el5.i386.rpm
openaislib-1.1.3-1.6.el5.i386.rpm
pacemaker-1.0.11-1.2.el5.i386.rpm
pacemaker-libs-1.0.11-1.2.el5.i386.rpm
perl-TimeDate-1.16-5.el5.noarch.rpm
resource-agents-1.0.4-1.1.el5.i386.rpm
以上是全部的軟件包,我這裏就是用本地yum安裝了
#yum localinstall * --nogpgcheck -y
#cd /etc/corosync/
#cp corosync.conf.example corosync.conf
#vim corosync.conf
compatibility: whitetank
totem {
version: 2
secauth: on
threads: 0
interface {
ringnumber: 0
bindnetaddr: 192.168.1.0 //這個是你使用的網段的網絡號
mcastaddr: 226.94.1.1
mcastport: 5405
}
}
logging {
fileline: off
to_stderr: no
to_logfile: yes
to_syslog: no
logfile: /var/log/cluster/corosync.log
debug: off
timestamp: on
logger_subsys {
subsys: AMF
debug: off
}
}
amf {
mode: disabled
}
service {
ver: 0
name: pacemaker
}
aisexc {
user: root
group: root
}
以上是個人配置文件
#mkdir /var/log/cluster //建立日誌文件的目錄
#corosync-keygen //生成認證文件
接下來就是把兩個文件拷貝到HA2上了,使用scp命令就好了
#scp -p /etc/corosync/authkeys /etc/corosync/corosync.conf node2:/etc/corosync
啓動HA高可用集羣:
#service corosync start
Starting Corosync Cluster Engine (corosync): [ OK ] //啓動成功
#ssh node2 -- 'service corosync start' //我在HA1上啓動的集羣
Starting Corosync Cluster Engine (corosync): [ OK ]
有上面能夠看出,兩個集羣都已經啓動起來了!
6、接下來就是在集羣上配置nginx的資源了
在配置資源以前還要禁用stonith,不實用quorum(覺得是兩節點的集羣);
[root@node1 ~]# crm
crm(live)# configure
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# property stonith-enabled=false
接下來就是爲nginx配置資源了,因爲咱們須要一個共同的網頁,而訪問的時候須要一個IP地址,也就是咱們上面說的VIP,還要有一個nginx的服務進程,因此定義兩個資源就好了;
crm(live)configure# primitive NginxIP ocf:heartbeat:IPaddr params ip="192.168.1.80"
crm(live)configure# primitive NginxServer lsb:nginx
crm(live)configure# group Nginxgroup NginxIP NginxServer
crm(live)configure# verify
crm(live)configure# commit
crm(live)configure# bye
[root@node1 ~]# crm status
============
Last updated: Fri Sep 16 23:18:28 2011
Stack: openais
Current DC: node1.luowei.com - partition with quorum
Version: 1.0.11-1554a83db0d3c3e546cfd3aaff6af1184f79ee87
2 Nodes configured, 2 expected votes
1 Resources configured.
============
Online: [ node2.luowei.com node1.luowei.com ]
Resource Group: Nginxgroup
NginxIP (ocf::heartbeat:IPaddr): Started node2.luowei.com
NginxServer (lsb:nginx): Started node2.luowei.com
能夠看出節點都啓動了,並且配置的資源也啓動了!
而後看一下咱們配置的資源的配置文件:
[root@node2 ~]# crm configure show
INFO: building help index
node node1.luowei.com
node node2.luowei.com
primitive NginxIP ocf:heartbeat:IPaddr \
params ip="192.168.1.80"
primitive NginxServer lsb:nginx
group Nginxgroup NginxIP NginxServer
property $id="cib-bootstrap-options" \
dc-version="1.0.11-1554a83db0d3c3e546cfd3aaff6af1184f79ee87" \
cluster-infrastructure="openais" \
expected-quorum-votes="2" \
no-quorum-policy="ignore" \
stonith-enabled="false"
2.使用ifconfig命令早node2上查看
3.使用crm status查看
4.中止HA2的corosync服務,而後在使用上面的命令查看,能夠看到網頁依然能出現,而本來出現的eth0:0的幾口跑到了HA1上了,資源轉移到了HA1上了,實驗成功,結束!