阿娇开发978 发表于 2019-1-2 11:44:09

LVS之简单搭建LVS

一、规划
lvs01
eth0:192.168.240.134
eth1:192.168.253.130

rs01
eth0:192.168.253.128
gateway:192.168.253.130

rs02
eth0:192.168.253.129
gateway:192.168.253.130

192.168.240.0网段供外网访问,使用NAT
192.168.253.0网段为内网段,使用hostonly

vip(外网):192.168.240.144
dip(内网):192.168.253.131

二、负载均衡器安装配置
1、查看ipvs相关信息:
# grep -i 'vs' /boot/config-2.6.32-358.el6.x86_64
CONFIG_GENERIC_TIME_VSYSCALL=y
# CONFIG_X86_VSMP isnot set
CONFIG_HIBERNATION_NVS=y
CONFIG_IP_VS=m
CONFIG_IP_VS_IPV6=y
# CONFIG_IP_VS_DEBUGis not set
CONFIG_IP_VS_TAB_BITS=12
# IPVS transportprotocol load balancing support
CONFIG_IP_VS_PROTO_TCP=y
CONFIG_IP_VS_PROTO_UDP=y
CONFIG_IP_VS_PROTO_AH_ESP=y
CONFIG_IP_VS_PROTO_ESP=y
CONFIG_IP_VS_PROTO_AH=y
CONFIG_IP_VS_PROTO_SCTP=y
# IPVS scheduler                        【支持10种算法】
CONFIG_IP_VS_RR=m
CONFIG_IP_VS_WRR=m
CONFIG_IP_VS_LC=m
CONFIG_IP_VS_WLC=m
CONFIG_IP_VS_LBLC=m
CONFIG_IP_VS_LBLCR=m
CONFIG_IP_VS_DH=m
CONFIG_IP_VS_SH=m
CONFIG_IP_VS_SED=m
CONFIG_IP_VS_NQ=m
# IPVS applicationhelper
CONFIG_IP_VS_FTP=m
CONFIG_OPENVSWITCH=m
CONFIG_MTD_BLKDEVS=m
CONFIG_SCSI_MVSAS=m
#CONFIG_SCSI_MVSAS_DEBUG is not set
CONFIG_VMWARE_PVSCSI=m
CONFIG_MOUSE_VSXXXAA=m
CONFIG_MAX_RAW_DEVS=8192
CONFIG_USB_SEVSEG=m
CONFIG_USB_VST=m

2、安装命令行工具
# yum install ipvsadm -y
# lsmod |grep ip_vs
没有安装输出,需要加载模块
# modprobe ip_vs
# lsmod |grep ip_vs
ip_vs               1156430
libcrc32c               12461 ip_vs
ipv6                  321422142ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6

3、开启路由转发功能
# vim /proc/sys/net/ipv4/ip_forward
或者
# vim/etc/sysctl.conf
将net.ipv4.ip_forward= 0 --> 改成 1

启用:
#sysctl -p
net.ipv4.ip_forward= 1
net.ipv4.conf.default.rp_filter= 1
net.ipv4.conf.default.accept_source_route= 0
kernel.sysrq = 0
kernel.core_uses_pid= 1
net.ipv4.tcp_syncookies= 1
error:"net.bridge.bridge-nf-call-ip6tables" is an unknown key
error:"net.bridge.bridge-nf-call-iptables" is an unknown key
error:"net.bridge.bridge-nf-call-arptables" is an unknown key
kernel.msgmnb =65536
kernel.msgmax =65536
kernel.shmmax =68719476736
kernel.shmall =4294967296

4、将两台RS服务器网关设置为LVS的内网口地址

5、配置NTP,使各个服务器时间进行同步
5.1、搭建NTP服务
可参考:http://zyan.cc/post/281/
服务器端需要安装:
# yuminstall ntp

允许任何IP的客户机都可以进行时间同步将“restrict default kod nomodify notrap nopeer noquery”这行修改成:
restrictdefault nomodify

# vim/etc/ntp.conf

# For moreinformation about this file, see the man pages
# ntp.conf(5),ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile/var/lib/ntp/drift

# Permit timesynchronization with our time source, but do not
# permit the sourceto query or modify the service on this system.
restrictdefault nomodify
restrict -6 defaultkod nomodify notrap nopeer noquery

# Permit all accessover the loopback interface.This could
# be tightened aswell, but to do so would effect some of
# the administrativefunctions.
restrict 127.0.0.1
restrict -6 ::1
# Hosts on localnetwork are less restricted.
#restrict192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public serversfrom the pool.ntp.org project.
# Please considerjoining the pool (http://www.pool.ntp.org/join.html).
server0.rhel.pool.ntp.org iburst
server1.rhel.pool.ntp.org iburst
server2.rhel.pool.ntp.org iburst

PS : 以上配置需要联网才能同步,另外,我们可以设置成:若无法通过网络同步时间,则使用本地时间进行同步
则需要将以下代码添加到配置文件
server127.0.0.1
fudge127.0.0.1 stratum 5

5.2、客户端时间同步
手动同步:
#ntpdate192.168.253.130
10 Sep 20:01:42ntpdate: step time server 192.168.253.130 offset -28800.064241 sec

#ntpdate 192.168.253.130
10 Sep 20:01:36ntpdate: adjust time server 192.168.253.130 offset 0.000067 sec

PS:手动同步时,可能会出现
# ntpdate 192.168.253.130
11 Sep03:30:35 ntpdate: no server suitable for synchronization found

可能是由于ntp服务器防火墙没有关闭,或者ntp服务器还未与ntp server同步时间。

设置计划任务:
#vim /etc/crontab
20 * * * */usr/sbin/ntpdate 192.168.253.130
每20分钟同步一次

6、提供页面
分别在RS上安装测试环境(httpd) 并创建两个不同页面,以示区分

以其中一台为例:
先关闭防火墙:
#service iptables stop
iptables: Flushingfirewall rules:                        
iptables: Settingchains to policy ACCEPT: filter         
iptables: Unloadingmodules:                              
#chkconfig iptables off

# yuminstall httpd
# echo"RS1:192.168.253.129" > /var/www/html/index.html
#service httpd start
测试:
http://s3.运维网.com/wyfs02/M00/49/41/wKioL1QRatXh18rgAABxHZaFIVw872.jpg
7、配置LVS调度器
配置外网口vip:
#ifconfig eth0:0 192.168.240.144 netmask 255.255.255.0 up
# ipvsadm -A -t 192.168.240.144:80 -s rr
--add-service   -A       add virtual service with options添加虚拟服务选项--tcp-service-t service-address   service-address is host[:port]指定tcp服务地址、端口[-sscheduler]指定算法

#ipvsadm -a -t 192.168.240.144:80 -r 192.168.253.128 -m
#ipvsadm -a -t 192.168.240.144:80 -r 192.168.253.129 -m


此处,添加realserver时,一般来说是vip:port,因为NAT可以做端口运算,可以指向与Director不同的端口,但,如果realserver端口与Director端口一直的话,则可以省略


--add-server      -a       add real server with options
添加真是服务器选项--tcp-service-t service-address   service-address is host[:port]添加tcp服务地址、端口--real-server-r server-address    server-address is host (and port)添加真是服务器地址(端口)--masquerading-m                   masquerading (NAT)
NAT伪装
增加真实服务器,以NAT方式,增加指向至各真实服务器(RS)


最好写成脚本,以保证每次重启后都能自动运行


8、测试
#ipvsadm
IP Virtual Serverversion 1.2.1 (size=4096)
ProtLocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP192.168.240.144:http rr
-> 192.168.253.128:http         Masq   1      0          7
-> 192.168.253.129:http         Masq   1      0          7
http://s3.运维网.com/wyfs02/M02/49/42/wKiom1QRmJXSfJ2FAAByktGuqeg718.jpg
http://s3.运维网.com/wyfs02/M00/49/44/wKioL1QRmKTD6dtWAAB0-hBIE5M341.jpg
  




页: [1]
查看完整版本: LVS之简单搭建LVS