centos7.3 + ipvsadm1.2.1 + httpd+lvs

时间:2021-04-08 03:09:56

lvs+http+centos7.3安装

准备工作
环境:

centos7.3 +  ipvsadm1.2.1 + httpd+lvs

地址规划:

DR server    虚拟网卡      VIP    (Director Server)
centos128    ens33:0       192.168.44.77

RS server  网卡     IP               虚拟网卡      VIP  (Real Server)
centos129  ens33    192.168.44.129   lo:0         192.168.44.77
centos130  ens33    192.168.44.130   lo:0         192.168.44.77

安装软件:

yun -y install httpd ipvsadm

*所有Director Server需要安装ipvsadm,
Real Server 只需要执行配置脚本.

  1. 创建一个连接文件,其命令为:
ln –sv /usr/src/kernels/3.10.0-514.21.1.el7.x86_64   /usr/src/linux

注意一定要与当前的运行的内核相一致

检验安装ipvsadm是否被正确安装
1. 执行ipvsadm,输出如上表第二列底三行所示。
2. 检查当前加载的内核模块,看是否存在 ip_vs 模块

[root@localhost ~]# lsmod|grep ip_vs
[root@centos128 3.10.0-514.21.1.el7.x86_64]# lsmod|grep ip_vs
ip_vs                 141092  0 
nf_conntrack          111302  6 ip_vs,nf_nat,nf_nat_ipv4,xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_ipv4
libcrc32c              12644  3 xfs,ip_vs,dm_persistent_data

一、配置及验证Director Server
lvsDR.sh 脚本用于开启和关闭lvs服务

[root@centos128 ~]# cat lvsDR.sh 
#!/bin/bash
VIP=192.168.44.77 #虚拟ip(供用户访问的ip)
RIP1=192.168.44.129 #真实服务器(real server)ip
RIP2=192.168.44.130 #真实服务器(real server)ip
PORT=80 #端口

case "$1" in
start)
echo "start config LVS Director Server..."
ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 up
route add -host $VIP dev ens33:0 
echo "1">/proc/sys/net/ipv4/ip_forward
ipvsadm -C
#ipvsadm -A -t $VIP:$PORT -s rr 120
ipvsadm -A -t $VIP:$PORT -s rr -p 5
ipvsadm -a -t $VIP:$PORT -r $RIP2:$PORT  -g -w 1  #添加real server,多个可配置多条
ipvsadm -a -t $VIP:$PORT -r $RIP1:$PORT  -g -w 1   #添加real server,多个可配置多条
ipvsadm
echo "config LVS Director Server success!"
;;
stop)
echo "shut down LVS Director Server"
echo "0">/proc/sys/net/ipv4/ip_forward
ipvsadm -C
ifconfig ens33:0 down
echo "shut down LVS Director Server success!"
;;
*)
echo "usage:$0 {start|stop}"
exit 1
esac

执行./lvsDR.sh ,查看执行完的状态

[root@centos128 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
[root@centos128 ~]# ./lvsDR.sh 
usage:./lvsDR.sh {start|stop}
[root@centos128 ~]# ./lvsDR.sh start
start config LVS Director Server...
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  centos128:http rr persistent 5
  -> centos129:http               Route   1      0          0         
  -> centos130:http               Route   1      0          0         
config LVS Director Server success!

二、配置及验证Real Server
lvsRS.sh 此脚本配置Real Server

[root@centos129 ~]# cat lvsRS.sh 
#!/bin/bash
VIP=192.168.44.77   #virtual ip ( for user acces)
case "$1" in
start)
echo "start config lvs Real Server..."
ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "config lvs Real Server success!"
;;
stop)
echo "shutting down lvs Real server..."
ifconfig lo:0 down
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "shut down lvs Real server success!"
;;
*)
echo "usage:$0 {start|stop}"
exit 1
esac

查看网卡状态

[root@centos129 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:e6:b5:68 brd ff:ff:ff:ff:ff:ff
    inet 192.168.44.129/24 brd 192.168.44.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fee6:b568/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:7e:42:92:ab brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
[root@centos129 ~]# 

运行脚本lvsRS.sh

[root@centos129 ~]# ./lvsRS.sh start
start config lvs Real Server...
config lvs Real Server success!
[root@centos129 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 192.168.44.77/32 brd 192.168.44.77 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:e6:b5:68 brd ff:ff:ff:ff:ff:ff
    inet 192.168.44.129/24 brd 192.168.44.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fee6:b568/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:7e:42:92:ab brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever

配置http
在RS server的centos129上配置http

[root@centos129 ~]# systemctl start httpd
[root@centos129 html]# cat /var/www/html/index.html 
129

在RS server的centos130上配置http

[root@centos130 ~]# systemctl start httpd
[root@centos130 html]# cat /var/www/html/index.html 
130

查看用ipvsadm -L -n

ipvsadm

IP Virtual Server version x.x.x (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
FWM 99 rr persistent 3600
第二总超时时间用ipvsadm –set tcp tcpfin udp设置,比如

ipvsadm –set 1 2 1

表示tcp空闲等待时间为120 秒
客户端关闭链接等待时间为20秒
udp空闲等待为100秒

测试如下
centos7.3 + ipvsadm1.2.1 + httpd+lvs

centos7.3 + ipvsadm1.2.1 + httpd+lvs