centos6中三台物理机配置nginx+keepalived+lvs

  1. 云栖社区>
  2. 博客列表>
  3. 正文

centos6中三台物理机配置nginx+keepalived+lvs

技术小阿哥 2017-11-27 10:42:00 浏览120 评论0

摘要: 以下只是简单的安装配置,并没有测试这套负载,各种参数大家可以自己测试 vip:10.0.50.170 lvs server:10.0.50.183 real server:10.0.50.184/185 183/184/185同步时间,并且安装nginx # ntpdate time.

以下只是简单的安装配置,并没有测试这套负载,各种参数大家可以自己测试

vip:10.0.50.170

lvs server:10.0.50.183

real server:10.0.50.184/185

183/184/185同步时间,并且安装nginx

# ntpdate time.nist.gov

# yum install nginx

# /etc/init.d/nginx start

在184/185上编写测试页面/usr/share/nginx/html/index.html


183上安装ipvsadm*和keepalived

# yum install keepalived ipvsadm*

# vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

# sysctl -p

# vi /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 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}


vrrp_instance VI_1 {

    state MASTER

    interface em1

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.0.50.170

    }

}


virtual_server 10.0.50.170 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP


    real_server 10.0.50.184 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }


    real_server 10.0.50.185 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }


}

# /etc/init.d/keepalived start

# chkconfig keepalived on



# cd /etc/init.d/

# vi lvs

#!/bin/sh

# description: Start LVS of Director server

WEB_VIP=10.0.50.170

WEB_RIP1=10.0.50.184

WEB_RIP2=10.0.50.185


SET_VIP () {

/sbin/ifconfig em1:0 $WEB_VIP netmask 255.255.255.0 broadcast $WEB_VIP up

/sbin/route add -host $WEB_VIP dev em1:0

/sbin/ipvsadm -A -t $WEB_VIP:80 -s wrr -p 3

/sbin/ipvsadm -a -t $WEB_VIP:80 -r $WEB_RIP1:80 -g -w 1

/sbin/ipvsadm -a -t $WEB_VIP:80 -r $WEB_RIP2:80 -g -w 1

}


case "$1" in

 start)

  /sbin/ipvsadm -C

  SET_VIP

  touch /var/lock/ipvsadm >/dev/null 2>&1

  echo "ipvsadm start"

;;

 stop)

 /sbin/ipvsadm -C

 /sbin/ipvsadm -Z

 /sbin/ifconfig em1:0 down

 route del $WEB_VIP > /dev/null 2>&1

  rm -fr /var/lock/ipvsadm >/dev/null 2>&1

 echo "ipvsadm stoped"

;;

 status)

 if [ ! -e /var/lock/ipvsadm ];then

     echo "ipvsadm is stoped "

   exit 1

else

  /sbin/ipvsadm -ln

    echo "ipvsadm is ok"

fi

;;

 restart)

 /sbin/ipvsadm -C

 /sbin/ipvsadm -Z

 /sbin/ifconfig em1:0 down

  route del $WEB_VIP > /dev/null 2>&1

  rm -fr /var/lock/ipvsadm >/dev/null 2>&1

 SET_VIP

  touch /var/lock/ipvsadm >/dev/null 2>&1

  echo "ipvsadm  restart .."

;;

*)

 echo "Usage :$0 {start|stop|status}"

   exit 1

esac

 exit 0

# /etc/init.d/lvs start



184/185机器都执行lvsrs脚本

# vi lvsrs

#!/bin/bash

#!/bin/bash

#description : start Real Server

VIP=10.0.50.170

./etc/rc.d/init.d/functions

case "$1" in


        start)


        echo " Start LVS of Real Server "


        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up


        /sbin/route add -host $VIP dev lo:0

        #注释:这四句目的是为了关闭ARP广播响应,使VIP不能向网络内发送广播,以防止网络出现混乱

        echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore


        echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce


        echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore


        echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce


        ;;


        stop)


        /sbin/ifconfig lo:0 down


        echo "close LVS Director server"


        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore


        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce


        echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore


        echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce


        ;;


        *)


        echo "Usage: $0 {start|stop}"


        exit 1


esac

# /etc/init.d/lvsrs start

参考http://blog.sina.com.cn/s/blog_6e834de50100pnfr.html



1.我同事曾经问过两台物理机的实现:

例如以上183/184上同时安装并启动nginx\keepalived\lvs脚本\lvsrs脚本

2.关于以下两个区别:

/sbin/ipvsadm -A -t $WEB_VIP:80 -s wrr -p 3

或者/sbin/ipvsadm -A -t $WEB_VIP:80 -s rr



本文转自 guowang327 51CTO博客,原文链接:http://blog.51cto.com/guowang327/1669873,如需转载请自行联系原作者

用云栖社区APP,舒服~

【云栖快讯】诚邀你用自己的技术能力来用心回答每一个问题,通过回答传承技术知识、经验、心得,问答专家期待你加入!  详情请点击

网友评论