nginx keepalived 主:192.168.56.120nginx keepalived 从:192.168.56.121VIP :192.168.56.130Web1 : 192.168.56.113【实验拓扑】一、安装配置nginx1、配置主机名(node2同node1)[root@localhost ~]# vim /etc/sysconfig/networkHOSTNAME=node1[root@localhost ~]# vim /etc/hosts192.168.56.120 node1192.168.56.121 node2192.168.56.113 web1192.168.56.114 web22、安装配置nginx(node2配置同node1)[root@node1 src]# useradd -s /sbin/nologin -M www[root@node1 src]# wget http://nginx.org/download/nginx-1.5.3.tar.gz[root@node1 src]# yum -y install pcre-devel openssl-devel perl-ExtUtils-Embed[root@node1 src]# tar xf nginx-1.5.3.tar.gz[root@node1 src]# cd nginx-1.5.3[root@node1 nginx-1.5.3]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-http_uwsgi_module --without-http_scgi_module --without-http_upstream_ip_hash_module --with-http_perl_module --with-pcre[root@node1 nginx-1.5.3]# make && make install[root@node1 ~]# vim /usr/local/nginx/conf/nginx.confuser www www;worker_processes 8;error_log logs/error.log;events {worker_connections 1024;http {include mime.types;default_type application/octet-stream;sendfile on;tcp_nopush on;keepalive_timeout 65;upstream web_server_pool {#ip_hash; #如果需要保持session一致,需要开启这个选项,可以保证同一台机器每次访问都分配到同一服务器server 192.168.56.113:80 weight=4 max_fails=2 fail_timeout=30s;server 192.168.56.114:80 weight=4 max_fails=2 fail_timeout=30s;server {listen 80;server_name 192.168.56.120; # node2 改为192.168.56.121location / {root html;index index.html index.htm;proxy_pass http://web_server_pool;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $remote_addr;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}[root@node1 ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok启动服务3、安装配置web服务器web服务器的配置(web2同web1,只需要将相应部分改为web2即可)[root@localhost ~]# hostname web1[root@web1 ~]# vim /etc/hosts192.168.56.120 node1192.168.56.121 node2192.168.56.113 web1192.168.56.114 web2[root@web1 ~]# yum install -y httpd[root@web1 ~]# service httpd start[root@web1 ~]# echo "<h1>welcome to web1</h1>" > /var/www/html/index.html4、测试Nginx负载均衡在确保node1 的防火墙和selinux关闭的情况下,打开浏览器,访问http://node1ip也可通过curl测试:[root@node1 ~]# yum -y install curl[root@node1 ~]# curl -dump http://192.168.56.120/<h1>welcome to web1</h1>[root@node1 ~]# curl -dump http://192.168.56.120/<h1>welcome to web2</h1>[root@node1 ~]# curl -dump http://192.168.56.120/<h1>welcome to web1</h1>[root@node1 ~]# curl -dump http://192.168.56.120/二、安装配置keepalived(node2配置参考node1,大体上一样)1、安装配置keepalived[root@node1 src]# yum -y install popt popt-devel popt-static openssl-devel kernel-devel libnl libnl-devel[root@node1 src]# wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz[root@node1 src]# tar xf keepalived-1.2.4.tar.gz[root@node1 src]# cd keepalived-1.2.4[root@node1 keepalived-1.2.4]# ./configure --prefix=/usr/local/keepalived[root@node1 keepalived-1.2.4]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/[root@node1 keepalived-1.2.4]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/[root@node1 keepalived-1.2.4]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/[root@node1 keepalived-1.2.4]# mkdir -p /etc/keepalived[root@node1 keepalived-1.2.4]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/配置[root@node1 keepalived-1.2.4]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {notification_email {pmghong@163.com}notification_email_from pmghong@163.comsmtp_server 192.168.56.120 # node2上改为192.168.56.121smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script chk_nginx { # 定义监控脚本script "/etc/keepalived/checkNginx.sh"interval 2weight 2}vrrp_instance VI_1 {state MASTER # node2 修改为BACKUPinterface eth0virtual_router_id 51priority 120 # node2上的数值应低于这个值,例如100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.56.130}track_script { # 执行监控脚本,这段代码一定要加!!否则不会调用上面的脚本chk_nginx}2、创建检测脚本[root@node1 keepalived-1.2.4]# vim /etc/keepalived/checkNginx.sh#!/bin/bash#auto check nginx processkillall -0 nginxif [[ $? -ne 0 ]];then/etc/init.d/keepalived stop3、启动服务[root@node1 ~]# service keepalived startStarting keepalived: [ OK ][root@node2 ~]# service keepalived startStarting keepalived: [ OK ][root@node1 ~]# ip addr2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:95:99:b7 brd ff:ff:ff:ff:ff:ffinet 192.168.56.120/24 brd 192.168.56.255 scope global eth0inet 192.168.56.130/32 scope global eth0inet6 fe80::a00:27ff:fe95:99b7/64 scope link4、测试(1) 打开浏览器测试访问http://VIP[root@node1 keepalived]# netstat -nultpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name(2)找一台不相关的机器长ping VIP(3)测试页面访问情况[root@node2 ~]# ip addr2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 08:00:27:7f:4b:aa brd ff:ff:ff:ff:ff:ffinet 192.168.56.121/24 brd 192.168.56.255 scope global eth0inet 192.168.56.130/32 scope global eth0inet6 fe80::a00:27ff:fe7f:4baa/64 scope linkvalid_lft forever preferred_lft forever网站的访问仍然正常[root@node2 ~]# curl -dump http://192.168.56.130<h1>welcome to web1</h1>[root@node2 ~]# curl -dump http://192.168.56.130<h1>welcome to web2</h1>[root@node2 ~]# curl -dump http://192.168.56.130<h1>welcome to web1</h1>[root@node2 ~]# curl -dump http://192.168.56.130<h1>welcome to web2</h1>