4.2、测试环境如下:
系统:Ceentos 6.4 64位
前端node1服务器:
DIP: 192.168.122.2
VIP: 192.168.122.22
前端node2服务器:
DIP: 192.168.122.3
VIP:192.168.122.23
后端服务器:
web server01:192.168.122.4
web server02:192.168.122.5
web server03:192.168.122.6
4.3、软件安装
分别在两台前端服务器上安装nginx+keepalived,使用脚本如下:
#!/bin/bash
# author: kuangl
# mail: kuangl@orient-media.com
# description: The installation of Nginx files.
# -------------------------------------------------------- #
## Nginx_install
# -------------------------------------------------------- #
# Nginx installation
#CURRENT_PATH=$(pwd)
for i in $(rpm -q gcc gcc-c++ kernel-devel openssl-devel zlib-devel popt-devel popt-static libnl-devel wget make |grep 'not installed' | awk '{print $2}')
do
yum -y install $i
done
[ -d /root/software ]
[ "$?" != 0 ] && mkdir /root/software
cd /root/software
[ ! -e pcre-8.33.tar.gz ] && wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
tar -zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install
echo $? || [ $? != 0 ] || echo " installation pcre failed" || exit 1
cd /root/software
[ ! -e nginx-1.2.9.tar.gz ] && wget http://nginx.org/download/nginx-1.2.9.tar.gz
tar -zxvf nginx-1.2.9.tar.gz
cd nginx-1.2.9
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module
make && make install
echo $? || [ $? != 0 ] || echo " installation nginx failed" || exit 1
# -------------------------------------------------------- #
## Keepalived_intsall
# -------------------------------------------------------- #
# Keepalived installation
cd /root/softwarae
[ ! -e keepalived-1.2.4.tar.gz ] && wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
tar -zxvf keepalived-1.2.4.tar.gz
cd keepalived-1.2.4
ln -s /usr/src/kernels/$(uname -r) /usr/src/kernels/linux
./configure --prefix=/usr --bindir=/usr/bin --sbindir=/usr/bin --libexecdir=/usr/libexec --localstatedir=/var --libdir=/lib64 --infodir=/usr/share/info --sysconfdir=/etc --mandir=/usr/local/share/man --with-kernel-dir=/usr/src/kernels/linux
make && make install
echo $? || [ $? != 0 ] || print " installation keepalived failed" || exit 1
chkconfig --add keepalived
chkconfig --level 345 keepalived on 4.4、在后端服务器上安装apached
后端node4
[root@node4 ~]# yum -y install httpd
[root@node4 html]# echo "this is 192.168.122.4" > /var/www/htmlindex.html
[root@node4 ~]# service httpd start
[root@node4 html]# curl 192.168.122.4
this is 192.168.122.4 后端node5
[root@node5 ~]# yum -y install httpd
[root@node5 html]# echo "this is 192.168.122.5" > /var/www/htmlindex.html
[root@node5 ~]# service httpd start
[root@node5 html]# curl 192.168.122.5
this is 192.168.122.5 后端node6
[root@node6 ~]# yum -y install httpd
[root@node6 html]# echo "this is 192.168.122.6" > /var/www/htmlindex.html
[root@node6 ~]# service httpd start
[root@node6 html]# curl 192.168.122.6
this is 192.168.122.6 4.5、node2、node3上配置nginx
[root@node2 ~]# vim /usr/local/nginx/conf/nginx.conf
upstream web1 ##定义负载均衡组为web1
{
ip_hash;
server 192.168.122.4:80;
server 192.168.122.5:80;
server 192.168.122.6:80;
}
server {
listen 80;
server_name dev.test01.com;
location /
{
root /home/kuangl/;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://web1;
}
} 4.6、在node2上配置keepalived
[root@node2 conf]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
404060945@qq.com
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 200
priority 250
advert_int 1
authentication {
auth_type PASS
auth_pass kuanglnginx
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.122.22
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 251
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass kuangl
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.122.23
}
} 4.7、在node3上配置keepalived
! Configuration File for keepalived
global_defs {
notification_email {
404060945@qq.com
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 200
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass kuanglnginx
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.122.22
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 251
priority 250
advert_int 1
authentication {
auth_type PASS
auth_pass kuangl
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.122.23
}
} 4.8、在两台双主服务器上添加自动检测脚本
#!/bin/bash
# description:
# 定时查看nginx是否存在,如果不存在则启动nginx
# 如果启动失败,则停止keepalived
status=$(ps -C nginx --no-heading|wc -l)
if [ "${status}" = "0" ]; then
/usr/local/nginx/sbin/nginx
status2=$(ps -C nginx --no-heading|wc -l)
if [ "${status2}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi 4.9、开启nginx、keepalived服务
[root@node2 ~]# service keepalived start
[root@node2 ~]# /usr/local/nginx/sbin/nginx
[root@node3 ~]# service keepalived start
[root@node3 ~]# /usr/local/nginx/sbin/nginx 4.10、用 ip a 查看VIP
4.11、测试访问
[kuangl@node01 ~]$ curl http://192.168.122.22
this is 192.168.122.6
[kuangl@node01 ~]$ curl http://192.168.122.22
this is 192.168.122.4
[kuangl@node01 ~]$ curl http://192.168.122.22
this is 192.168.122.5
[kuangl@node01 ~]$ curl http://192.168.122.23
this is 192.168.122.6
[kuangl@node01 ~]$ curl http://192.168.122.23
this is 192.168.122.4
[kuangl@node01 ~]$ curl http://192.168.122.23
this is 192.168.122.5 五、后端用rsync做数据同步