集群LVS、KeepAlived

理论知识点总结

0、集群:大量服务器集合体,协同向客户提供系统资源。通过网络组成一个计算机组,共同完成一个任务。 1、集群IP地址(VIP)

配置LVS-DR集群


  • DS
//负载调度器IP配置,生成子接口配置文件
[root@ds ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:43:95:4e brd ff:ff:ff:ff:ff:ff
    inet 192.168.70.100/24 brd 192.168.70.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::51ca:14e8:394:30fb/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
[root@ds ~]# cd /etc/sysconfig/network-scripts/
[root@ds network-scripts]# ls
ifcfg-ens33  ifdown-isdn      ifdown-tunnel  ifup-isdn    ifup-Team
ifcfg-lo     ifdown-post      ifup           ifup-plip    ifup-TeamPort
ifdown       ifdown-ppp       ifup-aliases   ifup-plusb   ifup-tunnel
ifdown-bnep  ifdown-routes    ifup-bnep      ifup-post    ifup-wireless
ifdown-eth   ifdown-sit       ifup-eth       ifup-ppp     init.ipv6-global
ifdown-ippp  ifdown-Team      ifup-ippp      ifup-routes  network-functions
ifdown-ipv6  ifdown-TeamPort  ifup-ipv6      ifup-sit     network-functions-ipv6
[root@ds network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@ds network-scripts]# vi ifcfg-ens33:0
[root@ds network-scripts]# systemctl restart network

[root@ds network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.70.100  netmask 255.255.255.0  broadcast 192.168.70.255
        inet6 fe80::51ca:14e8:394:30fb  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:43:95:4e  txqueuelen 1000  (Ethernet)
        RX packets 1167  bytes 432300 (422.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 711  bytes 84987 (82.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.70.200  netmask 255.255.255.0  broadcast 192.168.70.255
        ether 00:0c:29:43:95:4e  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 348 (348.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 348 (348.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        
//配置ipvs模块,安装IPvsadm软件包
[root@ds network-scripts]# cd
[root@ds ~]# modprobe ip_vs
[root@ds ~]# lsmod | grep ip_vs
ip_vs                 145458  0
nf_conntrack          139264  1 ip_vs
libcrc32c              12644  3 xfs,ip_vs,nf_conntrack
[root@ds ~]# 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
[root@ds ~]# yum -y install ipvsadm
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
[root@ds ~]#  ipvsadm -C

//创建虚拟服务器,集群VIP地址为192.168.70.200,80端口,使用rr轮询算法。
[root@ds ~]# ipvsadm -A -t 192.168.70.200:80 -s rr
[root@ds ~]# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  ds:http rr
[root@ds ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.70.200:80 rr

//添加服务器节点,添加错了可以用ipvsadm -C指令清除。
[root@ds ~]# ipvsadm -a -t 192.168.70.200:80 -r 192.168.70.103:80 -g -w 1
[root@ds ~]# ipvsadm -a -t 192.168.70.200:80 -r 192.168.70.104:80 -g -w 1
[root@ds ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.70.200:80 rr
  -> 192.168.70.103:80            Route   1      0          0
  -> 192.168.70.104:80            Route   1      0          0
  
//保存配置
[root@ds ~]#  ipvsadm  --save > /etc/sysconfig/ipvsadm
[root@ds ~]# systemctl start ipvsadm.service
[root@ds ~]# systemctl enable ipvsadm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/ipvsadm.service to /usr/lib/systemd/system/ipvsadm.service.


  • web01
//临时修改//禁止ARP的转发
[root@web01 ~]# echo 1 > /proc/sys/net/ipv4/conf/ens33/arp_ignore
[root@web01 ~]# echo 2 >  /proc/sys/net/ipv4/conf/ens33/arp_announce
//永久修改
[root@web01 ~]# vi /etc/sysctl.conf
[root@web01 ~]# sysctl -p
net.ipv4.conf.ens33.arp_ignore = 1
net.ipv4.conf.ens33.arp_announce = 2

//配置虚拟IP地址(VIP)
[root@web01 ~]# cd /etc/sysconfig/network-scripts/
[root@web01 network-scripts]# ls
ifcfg-ens33  ifdown-isdn      ifdown-tunnel  ifup-isdn    ifup-Team
ifcfg-lo     ifdown-post      ifup           ifup-plip    ifup-TeamPort
ifdown       ifdown-ppp       ifup-aliases   ifup-plusb   ifup-tunnel
ifdown-bnep  ifdown-routes    ifup-bnep      ifup-post    ifup-wireless
ifdown-eth   ifdown-sit       ifup-eth       ifup-ppp     init.ipv6-global
ifdown-ippp  ifdown-Team      ifup-ippp      ifup-routes  network-functions
ifdown-ipv6  ifdown-TeamPort  ifup-ipv6      ifup-sit     network-functions-ipv6
[root@web01 network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@web01 network-scripts]# vi ifcfg-lo:0
[root@web01 network-scripts]# systemctl restart network
[root@web01 network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.70.103  netmask 255.255.255.0  broadcast 192.168.70.255
        inet6 fe80::cc20:9a31:1eca:36fb  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b3:cb:30  txqueuelen 1000  (Ethernet)
        RX packets 1368  bytes 443858 (433.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 853  bytes 98929 (96.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 348 (348.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 348 (348.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 192.168.70.200  netmask 255.255.255.255
        loop  txqueuelen 1000  (Local Loopback)
        
//配置httpd服务
[root@web01 ~]# yum install -y httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
[root@web01 ~]# systemctl start httpd
[root@web01 ~]# echo "192.168.70.103" > /var/www/html/index.html



  • web02
//禁止ARP的转发
[root@web02 ~]# echo 1 > /proc/sys/net/ipv4/conf/ens33/arp_ignore
[root@web02 ~]#  echo 2 >  /proc/sys/net/ipv4/conf/ens33/arp_announce
[root@web02 ~]# vi /etc/sysctl.conf
[root@web02 ~]# sysctl -p
net.ipv4.conf.ens33.arp_ignore = 1
net.ipv4.conf.ens33.arp_announce = 2
[root@web02 ~]# cd /etc/sysconfig/network-scripts/
[root@web02 network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@web02 network-scripts]# vi ifcfg-lo:0
[root@web02 network-scripts]# systemctl restart network
[root@web02 network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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.70.200/32 brd 192.168.70.200 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:8d:da:5d brd ff:ff:ff:ff:ff:ff
    inet 192.168.70.104/24 brd 192.168.70.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::f82e:5b4c:80f5:5f2b/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

//配置httpd服务
[root@web02 network-scripts]# cd
[root@web02 ~]# yum install -y httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
[root@web02 ~]# systemctl start httpd
[root@web02 ~]# echo "192.168.70.104" > /var/www/html/index.html
  • 访问web站点


访问集群VIP地址