首页 > 代码库 > keepalived高可用反向代理的nginx-实例
keepalived高可用反向代理的nginx-实例
拓扑结构:
环境准备:
Centos6.5x86_64 关闭防火墙和Selinux
VIP:192.168.1.200/24
node5: eth0:192.168.1.190/24
node1:eth1:192.168.1.191/24
node2:RIP:eth0: 192.168.19.2/24
node3:RIP:eth0: 192.168.19.3/24 所有节点网关/DNS都为:192.168.1.1
每个服务器的hosts文件
# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.19.190 node5.luo.com node5 192.168.19.191 node1.luo.com node1 192.168.19.192 node2.luo.com node2 192.168.19.193 node3.luo.com node3
时间同步(每个节点都需要做)
为方便实验使用笔记本作为时间服务器,参考http://blog.csdn.net/irlover/article/details/7314530
ntpdate 192.168.1.105
如我想每隔10分钟和win7同步时间可以使用crontab -e 编辑crontab工作内容如下:
*/10 * * * * /usr/sbin/ntpdate 192.168.1.105 &>/dev/null&&/sbin/hwclock-w
node5与node1安装配置基本相同
在两个keepalived+nginx主机上都需安装keepalkved和nginx服务
# yum install -y keepalived nginx
配置nginx为反向代理服务器(两台都配置)
编辑nginx.conf配置文件
# vim /etc/nginx/nginx.conf
在nginx.conf配置文件中的http段内添加如下内容
upstream webservers { server 192.168.1.192; server 192.168.1.193; } server { listen 80; server_name www.luo.com; location / { proxy_pass http://webservers; proxy_set_header X-Real-IP $remote_addr; } }
# service nginx start
配置keepalived
node5(192.168.19.190)的配置文件内容为:
# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@luo.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 15 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.1.200/24 } notify_master "/etc/init.d/nginx start" notify_backup "/etc/init.d/nginx stop" notify_fault "/etc/init.d/nginx stop" }
node1(192.168.19.191)的配置文件内容为:
# cat /etc/keepalived/keepalived.conf global_defs { notification_email { root@localhost } notification_email_from keepalived@luo.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 16 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.1.200/24 } notify_master "/etc/init.d/nginx start" notify_backup "/etc/init.d/nginx stop" notify_fault "/etc/init.d/nginx stop" }
node2与node3配置web服务器
编辑node2测试页面
# echo "<h1>node2.luo.com</h1>" > /var/www/html/index.html
# service httpd restart
# curl http://192.168.1.192
编辑node3测试页面
# echo "<h1>node3.luo.com</h1>" > /var/www/html/index.html
# service httpd restart
# curl http://192.168.1.193
测试:
启动node5与node1的keepalived服务
# service keepalived start
访问192.168.1.200,多刷新几次就可以查看到两台web服务器的页面了
停止node5的keepalived服务,查看node1的相关信息,发现VIP已经到node1上面了
[root@node5 ~]# service keepalived stop
访问192.168.1.200,多刷新几次就可以查看到两台web服务器的页面了
keepalived高可用反向代理的nginx-实例