20170417-keepalived+lvs-实现高可用负载均衡集群

调度器:

vm15 192.168.122.15

vm16 192.168.122.16

web服务器:

vm17 192.168.122.17

vm18 192.168.122.18

都开启转发 :

echo "1" > /proc/sys/net/ipv4/ip_forward (临时)

vim /etc/sysctl.conf -> net.ipv4.ip_forward=1 (永久)

  sysctl -p 生效

配置好软件仓库

调度器:

vm15:yum install keepalived

一: NAT模式

genhash --server 192.168.122.17 --port 80 -u /var/www/html/index.html

genhash --server 192.168.122.18 --port 80 -u /var/www/html/index.html

得到的hash值填在配置文件digest后面

vim /etc/keepalived/keepalived.conf

vrrp_script chk_keepalived {

   script "kill -0 keepalived"  检测keepalived 是否正常运行

    interval 2   每2秒检测一次

    weight

}

 vrrp_instance VI_1 {
      state MASTER
      interface eth0
      virtual_router_id 51
      priority 100
      advert_int 1
      authentication {
          auth_type PASS
          auth_pass 123123
      }

       track_script {

          chk_keepalived

    }
  virtual_ipaddress {
          192.168.122.100
      }
  }

virtual_server 192.168.122.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 0
    protocol TCP

    real_server 192.168.122.17 80 {
        weight 1
        HTTP_GET {
            url {
              path /var/www/html/index.html
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }

            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 

       }

     }

   real_server 192.168.122.18 80 {

         weight 1

        HTTP_GET {
            url {
              path /var/www/html/index.html
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

vm16:

vim /etc/keepalived/keepalived.conf

vrrp_script chk_keepalived {

   script "kill -0 keepalived"  检测keepalived 是否正常运行

    interval 2   每2秒检测一次

    weight

}

 vrrp_instance VI_1 {
      state BACKUP
      interface eth0
      virtual_router_id 51
      priority 99
      advert_int 1
      authentication {
          auth_type PASS

          auth_pass 123123
      }

       track_script {

          chk_keepalived

    }
  virtual_ipaddress {
          192.168.122.100
      }
  }

virtual_server 192.168.122.100 80 {
    delay_loop 6
    lb_algo rr

    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 0
    protocol TCP

    real_server 192.168.122.17 80 {
        weight 1
        HTTP_GET {
            url {
              path /var/www/html/index.html
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }

            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 

       }

   }

   

real_server 192.168.122.18 80 {

         weight 1

        HTTP_GET {
            url {
              path /var/www/html/index.html
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

保存退出

service keepalived restart

chkconfig keepalived on

vm17 vm18 :

  vim /var/www/html/index.html

    hello,vm17

vim /var/www/html/index.html

 hello,vm18

service httpd restart

vm15 vm16:

ip addr show eth0  看VIP 192.168.122.100产生没有

yum install ipvsadm

运行: ipvsadm 查看后端RS及vm17和vm18产生没有

vm17 vm18:

添加路由规则: 因为这里是NAT

192.168.122这个网段:route add -net 192.168.122.0 netmask 255.255.255.0 gw 192.168.122.100

单个主机: route add -host 192.168.122.19 netmask 255.255.255.255 gw 192.168.122.100 

测试: vm19 192.168.122.19

  curl 192.168.122.100

可以看到轮循看到:

hello,vm17

hell,vm18

就成功了

二:直接路由模式:

vm15 vm16:

vim /etc/keepalived/keepalived.conf

lb_kind NAT 改为:lb_kind DR

vm17 vm18:

vim lvsrs

将VIP改为调度设置的VIP

#!/bin/bash

#

# lvsrs        Startup script for the lvsrs Server

#

# chkconfig: 2345 86 14

# description: lvsrs script.  It is used to serve \

#

 

VIP=192.168.122.100

 

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

 

case "$1" in

    start)

        echo " start LVS of REALServer"

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

        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)

        echo "close LVS Directorserver"

        /sbin/ifconfig lo:0 down

        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

保存退出,

cp lvsrs /etc/init.d/

chmod +x /etc/init.d/lvsrs

service lvsrs start

chkconfig lvsrs on

测试: vm19 192.168.122.19

curl 192.168.122.100

一般都用直接路由模式

20170417-keepalived+lvs-实现高可用负载均衡集群_第1张图片

20170417-keepalived+lvs-实现高可用负载均衡集群_第2张图片

20170417-keepalived+lvs-实现高可用负载均衡集群_第3张图片

20170417-keepalived+lvs-实现高可用负载均衡集群_第4张图片

 

20170417-keepalived+lvs-实现高可用负载均衡集群_第5张图片

20170417-keepalived+lvs-实现高可用负载均衡集群_第6张图片

20170417-keepalived+lvs-实现高可用负载均衡集群_第7张图片

 

 

转载于:https://my.oschina.net/u/3217381/blog/883120

你可能感兴趣的:(20170417-keepalived+lvs-实现高可用负载均衡集群)