随机代码

1[root@localhost ~]# yum -y install ipvsadm          //下载ipvsadm
2[root@localhost ~]# modprobe ip_vs               #加载ip_vs模块
3[root@localhost ~]# cat /proc/net/ip_vs           #查看ip_vs版本信息
4[root@localhost ~]# cd /etc/sysconfig/network-scripts/         //配置ens33:0虚拟网卡
5[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0            //复制模板
6[root@localhost network-scripts]# vim ifcfg-ens33:0          //按照模板写如下列字段
7DEVICE=ens33:0
8ONBOOT=yes
9IPADDR=192.168.20.12
10NETMASK=255.255.255.255
11
12[root@localhost network-scripts]# ifup ens33:0           //开启ens33:0
13[root@localhost network-scripts]# ifconfig            //查看是否成功
14ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
15        inet 192.168.20.12  netmask 255.255.255.255  broadcast 192.168.20.12
16        ether 00:0c:29:7e:9c:75  txqueuelen 1000  (Ethernet)
17[root@localhost network-scripts]# vim /etc/sysctl.conf           //调整proc响应参数(lvs和个节点需要公用VIP地址,所以要关闭内核的重定向响应参数,不充当路由器)
18net.ipv4.ip_forward = 0
19net.ipv4.conf.all.send_redirects = 0
20net.ipv4.conf.default.send_redirects = 0
21net.ipv4.conf.ens33.send_redirects = 0
22[root@localhost network-scripts]# sysctl -p         //从指定的文件加载系统参数
23[root@localhost ~]# modprobe ip_vs           //开启ipvsadm
24[root@localhost ~]# cat /proc/net/ip_vs           
25IP Virtual Server version 1.2.1 (size=4096)
26Prot LocalAddress:Port Scheduler Flags
27  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
28
29[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm          //配置负载分配策略
30[root@localhost ~]# systemctl start ipvsadm               
31[root@localhost opt]# vim /opt/dr.sh      //设置规则
32#!/bin/bash
33ipvsadm -C                   //清空规则
34ipvsadm -A -t 192.168.20.12:80 -s rr             //添加虚拟服务器并指定vip地址和tcp端口指定轮询模式
35ipvsadm -a -t 192.168.20.12:80 -r 192.168.20.22:80 -g        //指定真实服务器指定RIP和tcp端口使用DR模式
36ipvsadm -a -t 192.168.20.12:80 -r 192.168.20.33:80 -g
37ipvsadm             //开启
38[root@localhost ~]# ipvsadm           //开启
39IP Virtual Server version 1.2.1 (size=4096)
40Prot LocalAddress:Port Scheduler Flags
41  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
42TCP  localhost.localdomain:http rr
43  -> 192.168.20.22:http           Route   1      1          0         
44  -> 192.168.20.33:http           Route   1      0          1    
45[root@localhost ~]# ipvsadm -Lnc       //查看节点状态
46IPVS connection entries
47pro expire state       source             virtual            destination
48[root@localhost ~]# cd /opt
49[root@localhost opt]# sh -x dr.sh         //可执行脚本虽然之前开启过了
50+ ipvsadm -C
51+ ipvsadm -A -t 192.168.20.12:80 -s rr
52+ ipvsadm -a -t 192.168.20.12:80 -r 192.168.20.22:80 -g
53+ ipvsadm -a -t 192.168.20.12:80 -r 192.168.20.33:80 -g
54+ ipvsadm
55IP Virtual Server version 1.2.1 (size=4096)
56Prot LocalAddress:Port Scheduler Flags
57  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
58TCP  localhost.localdomain:http rr
59  -> 192.168.20.22:http           Route   1      0          0         
60  -> 192.168.20.33:http           Route   1      0          0         
61[root@localhost opt]# ipvsadm -Lnc
62IPVS connection entries
63pro expire state       source             virtual            destination
参考地址:负载均衡和高可用集群(LVS,Keepalived)-yyds

代码交流 2021