设为首页 收藏本站
查看: 909|回复: 0

[经验分享] LVS之简单搭建LVS

[复制链接]
发表于 2019-1-2 11:44:09 | 显示全部楼层 |阅读模式
一、规划
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相关信息:
[root@LVS01network-scripts]# 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、安装命令行工具
[root@LVS01~]# yum install ipvsadm -y
[root@LVS01~]# lsmod |grep ip_vs
没有安装输出,需要加载模块
[root@LVS01~]# modprobe ip_vs
[root@LVS01~]# lsmod |grep ip_vs
ip_vs                 115643  0
libcrc32c               1246  1 ip_vs
ipv6                  321422  142ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6

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

启用:
[root@LVS01 ~]#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/
服务器端需要安装:
[root@LVS01 ~]# yuminstall ntp

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

[root@LVS01 ~]# 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、客户端时间同步
手动同步:
[root@RS01 ~]#ntpdate  192.168.253.130
10 Sep 20:01:42ntpdate[2352]: step time server 192.168.253.130 offset -28800.064241 sec

[root@RS02 ~]#ntpdate 192.168.253.130
10 Sep 20:01:36ntpdate[2388]: adjust time server 192.168.253.130 offset 0.000067 sec

PS:手动同步时,可能会出现
[root@RS01~]# ntpdate 192.168.253.130
11 Sep03:30:35 ntpdate[2314]: no server suitable for synchronization found

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

设置计划任务:
[root@RS02 cron.d]#vim /etc/crontab
20 * * * */usr/sbin/ntpdate 192.168.253.130
20分钟同步一次

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

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

[root@RS02 ~]# yuminstall httpd
[root@RS02 ~]# echo"RS1:192.168.253.129" > /var/www/html/index.html
[root@RS02 ~]#service httpd start
测试:

7、配置LVS调度器
配置外网口vip
[root@LVS01 ~]#ifconfig eth0:0 192.168.240.144 netmask 255.255.255.0 up
[root@LVS01~]# 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]指定算法


[root@LVS01 ~]#ipvsadm -a -t 192.168.240.144:80 -r 192.168.253.128 -m
[root@LVS01 ~]#ipvsadm -a -t 192.168.240.144:80 -r 192.168.253.129 -m


此处,添加realserver时,一般来说是vipport,因为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、测试
[root@LVS01 ~]#ipvsadm
IP Virtual Serverversion 1.2.1 (size=4096)
ProtLocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.240.144:http rr
  -> 192.168.253.128:http         Masq   1      0          7
  -> 192.168.253.129:http         Masq   1      0          7


  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-658622-1-1.html 上篇帖子: lvs简单入门 下篇帖子: 玩转LVS,快速实现负载均衡
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表