系統硬件:vmware vsphere (CPU:2*4核,內存2G,雙網卡)html
LVS服務器(兩臺):linux
系統:Centos7.0 64位(LVS+keepalived)nginx
LvsMaster:192.168.1.21 (主VIP:192.168.1.20 ,備VIP:192.168.1.18)c++
LvsBackup:192.168.1.22 (主VIP:192.168.1.18 ,備VIP:192.168.1.20)web
Nginx服務器(三臺):算法
系統:Centos7.0 64位(Nginx服務,VIP:192.168.1.18)vim
IIS01:192.168.1.31centos
IIS02:192.168.1.32bash
IIS03:192.168.1.33服務器
IIS服務器(三臺):
系統:Windwos2008R2 64位( IIS服務,VIP:192.168.1.20)
IIS01:192.168.1.41
IIS02:192.168.1.42
IIS03:192.168.1.43
安裝步驟:
1.安裝Web服務器(Linxu,nginx,主網卡接口名稱:ens192)
此處以服務器:web01,IP地址爲:192.168.1.31說明(另外兩臺參考此處)
1.1 安裝系統(假定已經安裝好系統,而且已經可以正常提供Nginx服務)
[root@web01 ~]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
[root@web01 ~]# uname -rs
Linux 3.10.0-123.20.1.el7.x86_64
修改主機名
[root@web01 ~]# hostnamectl set-hostname web01
[root@web01 ~]# hostname
web01
[root@web01 ~]# ip add show ens192
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:94:02:4a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.31/24 brd 192.168.1.255 scope global ens192
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fe94:24a/64 scope link
valid_lft forever preferred_lft forever
[root@web01 ~]# curl http://192.168.1.31
<html>
<head>
<title>Web 01 10:43:18</title>
</head>
<body>
<h1>Web Server 01 index.html</h1>
<p>2015-03-17 10:43:18</p>
</body>
</html>
1.2 設置服務器,以支持vip訪問
方法1:直接建立lo:0文件,而且綁定VIP(192.168.1.18)
[root@web01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
打開編輯器,輸入如下內容
DEVICE=lo:0
IPADDR=192.168.1.18
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
保存、退出
重啓
[root@web01 ~]# shutdown -r now
測試是否綁定VIP
[root@web01 ~]# ip add show lo:0
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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.1.18/32 brd 192.168.1.18 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
顯示綁定即inet 192.168.1.18/32 brd 192.168.1.18 scope global lo:0
解決ARP問題
[root@web01 ~]# vim /etc/sysctl.conf
打開編輯器,輸入如下內容
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
設置說明
#arp_ignore=1,系統只回答目的IP爲是本地IP的包。也就是對廣播包不作響應。
#arp_announce=2,系統忽略IP包的源地址(source address),而根據目標主機(target host),選擇本地地址。
#arp_ignore 限制arp應答。限制等級:
#0:對於其它設備 的arp請求,應答全部其它接口的上IP的arp應答
#1:對於其它設備的arp請求,只應答本接口上IP的arp應答
#arp_announce 限制arp通告。限制等級:
#0:在接口上通告全部接口上IP的arp廣播
#1:對於其它設備的arp請求,在接口上儘可能限制廣播通告應答(不夠嚴格)
#2:只通告本接口上IP的arp廣播
方法2:經過命令綁定綁定VIP(192.168.1.18)
[root@web01 ~]# vim /usr/local/sbin/lvs_realserver.sh
打開編輯器,輸入如下內容
#!/bin/bash
VIP=192.168.1.18
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $VIP >/dev/null 2>&1
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 "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
保存、退出
運行,查看是否生效,是否綁定VIP
[root@web01 ~]# /usr/local/sbin/lvs_realserver.sh start
[root@web01 ~]# ip add show lo:0
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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.1.18/32 brd 192.168.1.18 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
顯示綁定即inet 192.168.1.18/32 brd 192.168.1.18 scope global lo:0
設置自動啓動
[root@web01 ~]# vim /etc/rc.d/rc.local
打開編輯器,添加如下內容
/usr/local/sbin/lvs_realserver.sh start
保存退出
設置運行權限(不然啓動後,依然不會自動運行)
[root@web01 ~]#chmod 755 /etc/rc.d/rc.local
2.安裝Web服務器(Windows2008r2,IIS,主網卡接口名稱:本地鏈接)
此處以服務器:IIS01,IP地址爲:192.168.1.41說明(另外兩臺參考此處)
2.1 安裝系統(假定已經安裝好系統,而且已經可以正常提供IIS服務)
IP地址:192.168.1.41
子網掩碼:255.255.255.0
默認網關:192.168.1.1
首選DNS:192.168.1.1
網頁內容:
<html>
<head>
<title>IIS 01 10:43:18</title>
</head>
<body>
<h1>IIS 01 index.html</h1>
<p>2015-03-17 10:43:18</p>
</body>
</html>
2.2 設置服務器,添加環回接口,以支持vip訪問
打開設備管理器-右鍵服務器名-添加過期硬件-打開硬件添加嚮導
下一步-安裝我手動從列表選擇的硬件(高級)-下一步-打開安裝的硬件類型
選擇網絡適配器-下一步
廠商選擇(Microsoft),網絡適配器選擇(Microsoft Loopback Adapter)-下一步-下一步
點擊完成
打開網絡鏈接,修改Microsoft Loopback Adapter名稱爲realserver
打開realserverTCP/IPv4屬性 修改IP地址
IP地址:192.168.1.20
子網掩碼:255.255.255.255
確認退出
運行cmd 打開命令行提示符窗口
運行如下命令修改網卡接口(即本地鏈接)、環回接口(即realserver) 鏈接模式,用於解決ARP問題(不然TCP的狀態會一直處於SYN_RECV 狀態)
netsh interface ipv4 set interface "本地鏈接" weakhostreceive=enabled
netsh interface ipv4 set interface "本地鏈接" weakhostsend=enabled
netsh interface ipv4 set interface "realserver" weakhostreceive=enabled
netsh interface ipv4 set interface "realserver" weakhostsend=enabled
netsh interface ipv4 set interface "loopback" weakhostreceive=enabled
netsh interface ipv4 set interface "loopback" weakhostsend=enabled
3.安裝LVS服務器(Linxu,ipvsadm,keepalived,主網卡接口名稱:ens160)
此處以服務器:lvs_master,IP地址爲:192.168.1.21說明(lvs_backup 參考此處)
3.1.安裝lvs_master
安裝 CentOS-7.0-1406-x86_64-DVD.iso
3.2.更新
[root@centos ~]# yum update
[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
[root@centos ~]# uname -rs
Linux 3.10.0-123.20.1.el7.x86_64
3.3.安裝基本軟件包
[root@centos ~]# yum install vim wget lsof gcc gcc-c++ bzip2 -y
[root@centos ~]# yum install net-tools bind-utils -y
3.4.修改主機名
[root@localhost ~]# hostnamectl set-hostname lvs_master
[root@lvs_master ~]# hostname
lvs_master
3.5.修改IP地址
[root@lvs_master ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="ens160"
ONBOOT="yes"
HWADDR="00:50:56:94:46:f8"
IPADDR="192.168.1.21"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
確認修改,退出
lvs_backup服務器修改說明:
HWADDR="lvs_backup相應MAC地址"
IPADDR="192.168.1.22"
[root@lvs_master ~]# ifconfig ens160
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.21 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::250:56ff:fe94:204c prefixlen 64 scopeid 0x20<link>
ether 00:50:56:94:20:4c txqueuelen 1000 (Ethernet)
RX packets 41559 bytes 59971168 (57.1 MiB)
RX errors 0 dropped 10 overruns 0 frame 0
TX packets 27992 bytes 2121802 (2.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.6.安裝ipvsadm
[root@lvs_master ~]# yum install ipvsadm -y
================================================================================
Package 架構 版本 源 大小
================================================================================
正在安裝:
ipvsadm x86_64 1.27-4.el7 base 44 k
事務概要
================================================================================
安裝 1 軟件包
[root@lvs_master ~]# ipvsadm --version
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
[root@lvs_master ~]# lsmod | grep ip_vs
ip_vs 136674 0
nf_conntrack 101024 9 ip_vs,nf_nat,nf_nat_ipv4,nf_nat_ipv6,xt_conntrack,ip6table_nat,iptable_nat,nf_conntrack_ipv4,nf_conntrack_ipv6
libcrc32c 12644 2 xfs,ip_vs
3.7.安裝keepalived
[root@lvs_master src]# yum install kernel-devel -y
================================================================================
Package 架構 版本 源 大小
================================================================================
正在安裝:
kernel-devel x86_64 3.10.0-123.20.1.el7 updates 8.9 M
事務概要
================================================================================
安裝 1 軟件包
[root@lvs_master src]# ls /usr/src/kernels/
3.10.0-123.20.1.el7.x86_64
[root@lvs_master src]# cd /usr/local/src
[root@lvs_master src]# wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
[root@lvs_master src]# tar zvxf keepalived-1.2.15.tar.gz
[root@lvs_master src]# cd keepalived-1.2.15
[root@lvs_master keepalived-1.2.15]# yum install popt-devel popt-static libnl-devel openssl-devel iptraf -y
[root@lvs_master keepalived-1.2.15]# ./configure --sysconfdir=/etc/ --sbindir=/usr/sbin/ --with-kernel-dir=/usr/src/kernels/3.10.0-123.20.1.el7.x86_64/
Keepalived configuration
------------------------
Keepalived version : 1.2.15
Compiler : gcc
Compiler flags : -g -O2 -DFALLBACK_LIBNL1
Extra Lib : -lssl -lcrypto -lcrypt -lnl
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : Yes
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : No
SNMP support : No
SHA1 support
Use Debug flags : No
[root@lvs_master keepalived-1.2.15]# make && make install
[root@lvs_master keepalived-1.2.15]# /usr/sbin/keepalived --version
Keepalived v1.2.15 (03/06,2015)
[root@lvs_master keepalived-1.2.15]# systemctl status keepalived
keepalived.service - SYSV: Start and stop Keepalived
Loaded: loaded (/etc/rc.d/init.d/keepalived)
Active: inactive (dead)
3.8.配置keepalived
[root@lvs_master /]# vim /etc/keepalived/keepalived.conf
打開編輯,修改
! Configuration File for keepalived
global_defs {
notification_email {
me@vicowong.com #警報接收郵件
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER #lvs_backup 將LVS_MASTER修改成LVS_BACKUP
}
vrrp_instance VI_IIS {
state MASTER #lvs_backup將MASTER改成BACKUP
interface ens160 #HA監測網絡接口
virtual_router_id 51 #主、備機的virtual_router_id必定要相同,必須相同!
priority 120 #lvs_backup將120改成80
advert_int 1 #VRRP Multicast廣播週期秒數
authentication {
auth_type PASS #VRRP認證方式
auth_pass 1111 #VRRP口令字
}
virtual_ipaddress {
192.168.1.20 #LVS虛擬地址
}
}
virtual_server 192.168.1.20 80 {
delay_loop 2 #延時等待時間
lb_algo wrr #輪詢算法
lb_kind DR #傳輸模式
persistence_timeout 1 #單一連接重連保持時間
protocol TCP
real_server 192.168.1.41 80 {
weight 100 #權重
TCP_CHECK { #realserve的狀態檢測設置部分,單位是秒
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.42 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.43 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
vrrp_instance VI_NGINX {
state BACKUP #lvs_backup上將BACKUP改成MASTER
interface ens160 #HA監測網絡接口
virtual_router_id 53 #主、備機的virtual_router_id必定要相同,必須相同
priority 80 #lvs_backup上將80改成120
advert_int 1 #VRRP Multicast廣播週期秒數
authentication {
auth_type PASS #VRRP認證方式
auth_pass 1111 #VRRP口令字
}
virtual_ipaddress {
192.168.1.18 #LVS虛擬地址
}
}
virtual_server 192.168.1.18 80 {
delay_loop 2 #延時等待時間
lb_algo wrr #輪詢算法
lb_kind DR #傳輸模式
persistence_timeout 1 #單一連接重連保持時間
protocol TCP
real_server 192.168.1.31 80 {
weight 100 #權重
TCP_CHECK { #realserve的狀態檢測設置部分,單位是秒
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.32 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.33 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
3.9 修改系統配置文件
[root@lvs_master src]# vim /etc/sysctl.conf
打開編輯器,修改
net.ipv4.ip_forward = 1
#net.ipv4.conf.default.rp_filter = 1
#net.ipv4.conf.default.accept_source_route = 0
#ernel.sysrq = 0
#kernel.core_uses_pid = 1
保存,退出
運行生效
[root@lvs_master src]# sysctl -p
net.ipv4.ip_forward = 1
3.10.LVS運行測試(主備LVS服務器各出打開三個終端,共6個終端,每一個單獨運行如下一條命令,用於監測服務運行狀態)
一個終端查看日誌信息
[root@lvs_master src]# tail -f /var/log/messages
一個終端查看LVS當前設置
[root@lvs_master src]# watch ipvsadm -Ln
一個終端查看轉發狀況
[root@lvs_master src]# watch ipvsadm -Lnc
3.11.設置自動運行keepalived
[root@lvs_master /]# vim /etc/rc.d/rc.local
打開編輯器,添加如下內容
iptables -F
systemctl start keepalived
保存,退出
[root@lvs_master /]# chmod -x /etc/rc.d/rc.local
3.12.重啓keepalived服務
[root@lvs_master /]# systemctl restart keepalived
信息能夠在以前三個終端反映出來
3.13.關閉selinux
[root@lvs_master ~]# vim /etc/selinux/config
打開編輯器,屏蔽如下兩行
#SELINUX=enforcing
#SELINUXTYPE=targeted
添加如下一行
SELINUXTYPE=disabled
保存退出
重啓後,查詢是否關閉(顯示Disabled則表示關閉)
[root@lvs_master ~]# shutdown -r now
[root@lvs_master ~]# getenforce
Disabled