安装配置heartbeat
1、安装heartbeat
yum install heartbeat
安装完后会自动建立用户hacluster和组haclient
确保两个节点上hacluster用户的的UID和GID相同
2、同步两台节点的时间
rm -rf /etc/localtime
\cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum install -y ntp
ntpdate -d cn.pool.ntp.org
3、配置/etc/ha.d/ha.cf
debugfile /var/log/ha-debug #打开错误日志报告
keepalive 2 #两秒检测一次心跳线连接
deadtime 10 #10 秒测试不到主服务器心跳线为有问题出现
warntime 6 #警告时间(最好在 2 ~ 10 之间)
initdead 120 #初始化启动时 120
秒无连接视为正常,或指定heartbeat
#在启动时,需要等待120秒才去启动任何资源。
udpport 694 #用 udp 的 694 端口连接
ucast eth0 192.168.1.20 #单播方式连接(主从都写对方的 ip 进行连接)
node node1 #声明主服(注意是主机名uname -n不是域名)
node node2 #声明备服(注意是主机名uname -n不是域名)
auto_failback on #自动切换(主服恢复后可自动切换回来)这个不要开启
respawn hacluster /usr/lib/heartbeat/ipfail #监控ipfail进程是否挂掉,如果挂掉就重启它
4、/etc/ha.d/authkeys
auth 1
1 crc
5、/etc/ha.d/haresources
node1 IPaddr::192.168.1.100/24/eth0 drbddisk::mysql
Filesystem::/dev/drbd1::/data/mysql::ext3 mysqld portmap
node2 IPaddr::192.168.1.200/24/eth0 drbddisk::www
Filesystem::/dev/drbd2::/data/wwwnfs::ext3 portmap nfs
6、创建nfs管理脚本
vi /etc/ha.d/resource.d/nfs
写入:
#!/bin/bash
NFSD=/etc/rc.d/init.d/nfs
NFSDPID=`/sbin/pidof nfsd`
case $1 in
start)
$NFSD start;
;;
stop)
$NFSD stop;
if [ "$NFSDPID" != " "
];then
for NFSPID
in $NFSDPID
do
/bin/kill -9 $NFSPID;
done
fi
;;
*)
echo "Syntax incorrect. You need one of {start|stop }"
;;
esac
先启动node1的heartbeat,再启动node2的heartbeat
启动成功后,这里有几项需要检查
node1:
1、执行ip a,检查是否已经设置有虚拟ip 192.168.1.100
2、执行cat /proc/drbd检查状态是否正常
3、执行df -h查看/dev/drbd1是否已经挂载到/data/mysql
4、执行service mysqld status查看mysql是否已经启动
node2:
1、执行ip a查看是否已经设置虚拟ip 192.168.1.200
2、执行cat /proc/drbd检查状态是否正常
3、执行df
-h查看/dev/drbd2是否已经挂载到/data/wwwnfs和192.168.1.200:/data/wwwnfs是否已经挂载到/data/www
nginx均衡器配置
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local]
"$request" '
'$status
$body_bytes_sent "$http_referer" '
'"$http_user_agent"
"$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream www.centos.bz_server
{
server 192.168.1.10:8080 weight=3 max_fails=2
fail_timeout=30s;
server 192.168.1.20:8080 weight=9 max_fails=2
fail_timeout=30s;
}
server
{
listen 80;
server_name www.centos.bz;
location /
{
root
/data/www/www.centos.bz;
index
index.php index.htm index.html;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_pass
http://www.centos.bz_server;
}
access_log off;
}
server
{
listen 8080;
server_name www.centos.bz;
index
index.html index.htm index.php;
root /data/www/www.centos.bz;
#limit_conn crawler 20;
location ~ \.php$ {
root /data/www/www.centos.bz;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/www.centos.bz/$fastcgi_script_name;
include fastcgi_params;
}
location ~
.*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~
.*\.(js|css)?$
{
expires 1h;
}
access_log off;
}
}
这里定义了两台用于负载均衡的机子,分别是192.168.1.10:8080和192.168.1.20:8080,通过proxy_pass
http://www.centos.bz_server代理循询转发到这两台机,达到负载均衡的作用。
你可以建立index.php,里面写入:
echo $_SERVER['SERVER_ADDR'];
?>
如果连续刷新几次,得到不同的IP,证明已经均衡负载到不同的服务器。