30.1lvs-adm与ipvsadm命令

          • ipvsadm命令
          • 状态信息查看
          • 实验:基于NAT模式的LVS

ipvsadm命令

#工具支持8中算法,负载均衡有3种,lvs-fullnat内核不支持,需要研发

[root@LVS ~ ]#yum info ipvsadm
: - two transport layer (layer-4) protocols (TCP and UDP)
: - three packet-forwarding methods (NAT, tunneling, and direct routing)
: - eight load balancing algorithms (round robin, weighted round robin,
: least-connection, weighted least-connection, locality-based
: least-connection, locality-based least-connection with
: replication, destination-hashing, and source-hashing)

#内核支持10种负载调度算法
[root@LVS ~ ]#grep -i ipvs /boot/config-3.10.0-862.el7.x86_64 -C 10
# IPVS scheduler
#
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

[root@LVS ~ ]#yum install ipvsadm
[root@LVS ~ ]#rpm -ql ipvsadm
/etc/sysconfig/ipvsadm-config #配置文件
/usr/lib/systemd/system/ipvsadm.service #服务
/usr/sbin/ipvsadm #集群服务
/usr/sbin/ipvsadm-restore #规则重载工具
/usr/sbin/ipvsadm-save #规则保存工具
/usr/share/doc/ipvsadm-1.27
/usr/share/doc/ipvsadm-1.27/README
/usr/share/man/man8/ipvsadm-restore.8.gz
/usr/share/man/man8/ipvsadm-save.8.gz
/usr/share/man/man8/ipvsadm.8.gz

#ipvsadm服务
[root@LVS ~ ]#cat /usr/lib/systemd/system/ipvsadm.service
[Unit]
Description=Initialise the Linux Virtual Server
After=syslog.target network.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c “exec /sbin/ipvsadm-restore < /etc/sysconfig/ipvsadm”
ExecStop=/bin/bash -c “exec /sbin/ipvsadm-save -n > /etc/sysconfig/ipvsadm”
ExecStop=/sbin/ipvsadm -C
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target


lvs-nat
#LVS开启路由转发功能
vim /etc/sysctl.conf
net.ipv4.ip_forward=1

sysctl -p
sysctl -a |grep forw

[root@LVS ~ ]#ipvsadm –help
[root@LVS ~]#ipvsadm -A -t 172.18.0.123:80 -s rr
[root@LVS ~]#ipvsadm -a -t 172.18.0.123:80 -r 192.168.31.17 -m
[root@LVS ~]#ipvsadm -a -t 172.18.0.123:80 -r 192.168.31.27 -m

[root@os7 ~ ]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.18.0.123:80 rr
-> 192.168.31.17:80 Masq 1 0 0
-> 192.168.31.27:80 Masq 1 0 0

状态信息查看

#连接信息
[root@LVS ~ ]#ipvsadm -Lnc
#连接状态,发送接收数据包
[root@LVS ~ ]#ipvsadm -Ln –stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 172.18.0.123:80 104 624 416 51272 49608
-> 192.168.31.17:80 52 312 208 25636 24804
-> 192.168.31.27:80 52 312 208 25636 24804

#每秒钟处理的包
[root@LVS ~ ]#ipvsadm -Ln –rate
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port CPS InPPS OutPPS InBPS OutBPS
-> RemoteAddress:Port
TCP 172.18.0.123:80 0 0 0 0 0
-> 192.168.31.17:80 0 0 0 0 0
-> 192.168.31.27:80 0 0 0 0 0

#内存中的信息
[root@LVS ~ ]#cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP AC12007B:0050 rr
-> C0A81F1B:0050 Masq 1 0 24
-> C0A81F11:0050 Masq 1 0 25

#先清空防火墙iptables -F
[root@client ~ ]#for i in {0..100};do curl 172.18.0.123;sleep 0.5;done
RS1-17-server-httpd
RS2-27-server-httpd
RS1-17-server-httpd
RS2-27-server-httpd
RS1-17-server-httpd
RS2-27-server-httpd

https
[root@cos17 ~ ]#yum install mod_ssl
[root@cos27 ~ ]#yum install mod_ssl
[root@cos7 ~ ]#ipvsadm -A -t 172.18.0.123:443 #默认wlc
[root@cos7 ~ ]#ipvsadm -a -t 172.18.0.123:443 -r 192.168.31.17 -m -w 2
[root@cos7 ~ ]#ipvsadm -a -t 172.18.0.123:443 -r 192.168.31.27 -m
[root@cos7 ~ ]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.18.0.123:80 rr
-> 192.168.31.17:80 Masq 1 0 0
-> 192.168.31.27:80 Masq 1 0 0
TCP 172.18.0.123:443 wlc
-> 192.168.31.17:443 Masq 2 0 0
-> 192.168.31.27:443 Masq 1 0 0

实验:基于NAT模式的LVS

清空防火墙策略iptables -F
关闭selinux
如图
30.1lvs-adm与ipvsadm命令_第1张图片
1、网络准备
LVS ip_forward功能启用
[root@cos7 ~ ]#vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@cos7 ~ ]#sysctl -p
net.ipv4.ip_forward = 1
[root@cos7 ~ ]#sysctl -a | grep forward

2、RS
[root@cos17 ~ ]#yum install httpd -y
[root@cos27 ~ ]#yum install httpd -y
[root@cos17 ~ ]#echo R1 > /var/www/html/index.html
[root@cos27 ~ ]#echo R2 > /var/www/html/index.html

3、lvs
[root@cos7 ~ ]#yum install ipvsadm
[root@cos7 ~ ]#ipvsadm -A -t 172.18.0.123:80 -s rr
[root@cos7 ~ ]#ipvsadm -a -t 172.18.0.123:80 -r 192.168.31.17 -m
[root@cos7 ~ ]#ipvsadm -a -t 172.18.0.123:80 -r 192.168.31.27 -m
[root@cos7 ~ ]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.18.0.123:80 rr
-> 192.168.31.17:80 Masq 1 0 0
-> 192.168.31.27:80 Masq 1 0 0

4、客户端
[root@cos6 ~ ]#for i in {1..100};do curl 172.18.0.123;done
R1
R2
R1
R2
R1

你可能感兴趣的:(linux)