实验系统:
(1)CentOS 6.6_x86_64;
(2)共有三台主机,本实验以ip地址来命名主机,即131主机、132主机、133主机。
实验前提:防火墙和selinux都关闭,主机之间时间同步
实验软件:nginx-x86_64-1.6.2 keepalived-x86_64-1.2.13
实验拓扑:
一、安装配置keepalived
1.直接使用yum安装,133主机只安装nginx:
yum -y install keepalived nginx
2.在131主机上配置keepalived:
vim /etc/keepalived/keepalived.conf
--------------------------------------------
! Configuration File for keepalived
global_defs { //此段配置忽略
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER //定义为MASTER主机
interface eth0
virtual_router_id 55 //虚拟id
priority 100 //优先级
advert_int 1 //检测间隔
authentication {
auth_type PASS //认证方式
auth_pass abcd //认证密码
}
virtual_ipaddress {
192.168.245.150 //虚拟ip,即VIP
}
notify_master "/etc/init.d/nginx start" //成为MASTER之后执行的动作
notify_backup "/etc/init.d/nginx stop" //成为BACKUP之后执行的动作
notify_fault "/etc/init.d/nginx stop" //FAULT之后执行的动作
}
3.将配置文件传到132主机,并配置:
vim /etc/keepalived/keepalived.conf
-------------------------------------------------- 只改动如下两行
state BACKUP //定义为BACKUP
priority 99 //优先级
4.两台主机启动keepalived服务,现在131这台主机为MASTER,所以服务都起在这台主机上:
service keepalived start
ip addr show
5.停止131主机的keepalived服务,可以看到服务已经转移到了132这台主机上:
service keepalived stop
6.再次启动131主机的keepalived服务,从日志可以看出,它又成为了MASTER:
二、配置nginx的反向代理
1.131和132主机配置nginx:
vim /etc/nginx/conf.d/default.conf
----------------------------------------------
location / { //增加此段
proxy_pass http://192.168.245.133;
}
#location / { //将此段注释掉
# root /usr/share/nginx/html;
# index index.html index.htm;
# example
#ModSecurityEnabled on;
#ModSecurityConfig /etc/nginx/modsecurity.conf;
#}
----------------------------------------------
service nginx restart
service keepalived restart
2.133主机启动测试页:
rm -rf /usr/share/nginx/html/* //删除原有页面文件
vim /usr/share/nginx/html/index.html
---------------------------------------------
<h1>This is a test!</h1>
---------------------------------------------
service nginx start
访问http://192.168.245.150,133主机上的页面文件出现,反向代理成功。关闭131主机的keepalived服务,132主机变为MASTER,页面仍然可以访问。但是,当nginx进程被kill掉时,并不能实现高可用。
至此,实验演示完毕,谢谢! 如有问题,请与我联系,QQ:82800452