本人使用的环境是CentOS-6.4-x86_64-bin-DVD1.rar,nginx-1.6.2.tar.gz,keepalived-1.2.18.tar.gz。三台机器ip:192.168.1.123,192.168.1.124。同时关闭两台虚拟机的防火墙:chkconfig iptables off(永久关闭防火墙)
第一步:将nginx-1.6.2.tar.gz,keepalived-1.2.18.tar.gz上传到两台机器上的/usr/local/software下
第二步:下载安装需要依赖的库文件【两台机器】
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
第三步:进行configure配置并查看是否报错【两台机器】
cd /usr/local/nginx-1.6.2 && ./configure --prefix=/usr/local/nginx
第四步:编译安装【两台机器】
make && make install
第五步:启动nginx【两台机器】
cd /usr/local/nginx //查看4个目录 conf(配置文件),html(网页文件),logs(日志文件),sbin(二进制文件)
启动命令:/usr/local/nginx/sbin/nginx //关闭(-s stop) 重启(-s reload)
第六步:成功:查看是否启动(ps --ef|grep nginx) 失败:可能80端口被占用。浏览器访问:http://192.168.1.123:8080
到此说明123,124两台机器的单机版nginx环境已经搭建完成。下一步需要安装keepalived【两台机器】
第七步:解压keepalived-1.2.18.tar.gz【两台机器】
tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
第八步:需要安装一个软件包【两台机器】
yum install -y openssl openssl-devel
第九步:安装keepalived【两台机器】
cd /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalivedma
第十步:安装 make && make install【两台机器】
第十一步:将keepalived安装成linux系统服务,因为没有使用keepalived的默认安装路径。安装完成之后,需要做一下修改
1.创建文件夹 mkdir -p /etc/keepalived
2.拷贝配置文件 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
3.设置开机启动 chkconfig keepalived on
4.启动keepalived cd /usr/local/ && service keepalived start
5.关闭keepalived cd /usr/local/ && service keepalived stop
第十二步:修改123和125两台主机配置文件 vim /etc/keepalived/keepalived.conf
123主机
! Configuration File for keepalived
global_defs {
router_id 192.168.1.123
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 123
mcast_src_ip 192.168.1.123
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.110
}
}
virtual_server 192.168.200.100 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.201.100 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.3 1358 {
delay_loop 3
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.200.4 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.5 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
125主机
! Configuration File for keepalived
global_defs {
router_id 192.168.1.125
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state Backup
interface eth0
virtual_router_id 123
mcast_src_ip 192.168.1.125
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.110
}
}
virtual_server 192.168.200.100 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.201.100 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.3 1358 {
delay_loop 3
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.200.4 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.5 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
第十三步:在/etc/keepalived/新增shell脚本,脚本名称为nginx_check.sh【两台机器】
更改脚本权限 chmod 777 nginx_check.sh【两台机器】
#!/bin/sh
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if[ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
第十四步:启动nginx /usr/local/nginx/sbin/nginx/sbin/n 【两台机器】
启动keepalived service keepalived start
关闭keepalived service keepalived stop
以上便完成了nginx的主从节点搭建。在主节点停止工作时从节点自动顶上