mysql,apache-lvs,haproxy集群

mysql+lvs(DR)

  • 一般情况下,数据库服务器需要共享数据,同时要部署高可用
  • 这里没有作共享,高可用

环境描述

主机 服务 LVS角色
vm5(192.168.225.134) Load balancer
vm2(192.168.225.128) mysql real server
vm4(192.168.225.131) mysql real server
  • 测试真机(192.168.161.108)
  • VIP(172.16.225.225)

mysql环境部署

mysql> create database test1;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.00 sec)

  • vm4创建一个数据库test2
mysql> create database test2;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test2              |
+--------------------+
5 rows in set (0.03 sec)

lvs配置

vm2,vm4配置IP

  • vm2修改arp参数
[root@vm2 ~]# cat /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
  • 配置IP
[root@vm2 ~]# yum -y install net-tools
[root@vm2 ~]# ifconfig lo
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)
.................................
[root@vm2 ~]# ifconfig lo 172.16.225.225/32               ##临时修改
[root@vm2 ~]# ifconfig lo
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 172.16.225.225  netmask 255.255.255.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
.................................
[root@vm2 ~]# echo 'ifconfig lo 172.16.225.225/32' >> /etc/rc.local  ##永久修改
[root@vm2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.128
NETMASK=255.255.225.0
GATEWAY=192.168.225.2
DNS1=192.168.225.2
NAME=ens33
DEVICE=ens33
ONBOOT=yes
  • vm4修改arp参数
[root@vm4 ~]# cat /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
  • 配置IP
[root@vm4 ~]# yum -y install net-tools
[root@vm4 ~]# ifconfig lo
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)
[root@vm4 ~]# ifconfig lo 172.16.225.225/32
[root@vm4 ~]# ifconfig lo
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 172.16.225.225  netmask 0.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
[root@vm4 ~]# echo 'ifconfig lo 172.16.225.225/32' >> /etc/rc.local
[root@vm4 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.131
NETMASK=255.255.225.0
GATEWAY=192.168.225.2
DNS1=192.168.225.2
NAME=ens33
DEVICE=ens33
ONBOOT=yes

调度器vm5配置IP

[root@vm5 ~]# ifconfig lo 172.16.225.225/32
[root@vm5 ~]# 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 172.16.225.225/24 scope global lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
[root@vm5 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.134
NETMASK=255.255.225.0
GATEWAY=192.168.225.2
DNS1=192.168.225.2
NAME=ens32
DEVICE=ens32
ONBOOT=yes

配置调度器

下载ipvsadm

[root@vm5 ~]# yum -y install ipvsadm

添加规则

[root@vm5 ~]# ipvsadm -A -t 172.16.225.225:3306 -s wrr
[root@vm5 ~]# ipvsadm -a -t 172.16.225.225:3306 -r 192.168.225.128:3306 -g
[root@vm5 ~]# ipvsadm -a -t 172.16.225.225:3306 -r 192.168.225.131:3306 -g
[root@vm5 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.225.225:3306 wrr
  -> 192.168.225.128:3306         Route   1      0          0         
  -> 192.168.225.131:3306         Route   1      0          0     

验证

  • 真机上添加到达VIP的路由
[wisan@fyj ~]$ sudo route add -host 172.16.225.225/32 gw 192.168.225.1 dev vmnet8
[wisan@fyj ~]$ route -nv 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.161.1   0.0.0.0         UG    100    0        0 enp2s0
172.16.105.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet1
172.16.225.225  192.168.225.1   255.255.255.255 UGH   0      0        0 vmnet8
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.161.0   0.0.0.0         255.255.255.128 U     100    0        0 enp2s0
192.168.225.0   0.0.0.0         255.255.255.0   U     101    0        0 vmnet8
  • vm2,vm4配置远程mysql权限
mysql> grant all privileges on *.* to 'root'@'192.168.161.108' identified by '123456';
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------------+-------------------------------------------+
| user          | host            | authentication_string                     |
+---------------+-----------------+-------------------------------------------+
| root          | localhost       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.session | localhost       | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost       | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| root          | 192.168.161.108 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------------+-------------------------------------------+
4 rows in set (0.00 sec)
  • 真机(192.168.161.108)测试
[wisan@fyj ~]$ mysql -uroot -h192.168.225.131 -p123456
ERROR 1130 (HY000): Host '192.168.225.1' is not allowed to connect to this MySQL server
##发现192.168.225.1(真机上到虚拟机网络的网关)没有权限连接数据库导致真机连接不上
mysql> grant all privileges on *.* to 'root'@'192.168.225.1' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------------+-------------------------------------------+
| user          | host            | authentication_string                     |
+---------------+-----------------+-------------------------------------------+
| root          | localhost       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.session | localhost       | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost       | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| root          | 192.168.161.108 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root          | 192.168.225.1   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------------+-------------------------------------------+
5 rows in set (0.00 sec)
[wisan@fyj ~]$ mysql -uroot -h192.168.225.131 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test2              |
+--------------------+
5 rows in set (0.001 sec)

  • 通过VIP连接数据库
[wisan@fyj ~]$ mysql -uroot -h172.16.225.225 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test2              |
+--------------------+
5 rows in set (0.002 sec)
[wisan@fyj ~]$ mysql -uroot -h172.16.225.225 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.003 sec)

mysql+lvs(NAT)

环境描述

主机 服务 LVS角色
vm5(192.168.225.134) Load balancer
vm2(192.168.225.128) mysql real server
vm4(192.168.225.131) mysql real server
  • 测试真机(192.168.161.108)
  • VIP(192.168.161.109)

mysql环境部署

mysql> create database test1;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.00 sec)

  • vm4创建一个数据库test2
mysql> create database test2;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test2              |
+--------------------+
5 rows in set (0.03 sec)

LVS配置

  • vm2,vm4 arp回归默认值
[root@vm2 ~]# sysctl -p /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.all.arp_announce = 0

配置IP

  • vm2
[root@vm2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.128
NETMASK=255.255.225.0
GATEWAY=192.168.225.134
DNS1=192.168.225.2
NAME=ens33
DEVICE=ens33
ONBOOT=yes
[root@vm2 ~]# route -n 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.225.134 0.0.0.0         UG    100    0        0 ens33
192.168.225.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
  • vm4
[root@vm4 ~]#  vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.131
NETMASK=255.255.225.0
GATEWAY=192.168.225.134
DNS1=192.168.225.2
NAME=ens33
DEVICE=ens33
ONBOOT=yes
[root@vm4 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.225.134 0.0.0.0         UG    100    0        0 ens33
192.168.225.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
  • vm5(两块网卡)
[root@vm5 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.134
NETMASK=255.255.225.0
GATEWAY=192.168.225.2
DNS1=192.168.225.2
NAME=ens32
DEVICE=ens32
ONBOOT=yes

[root@vm5 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.161.109
NETMASK=255.255.225.128
NAME=ens34
DEVICE=ens34
ONBOOT=yes

开启DS调度器IP转发功能

[root@vm5 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@vm5 ~]# sysctl -p
net.ipv4.ip_forward = 1

配置LVS规则

下载ipvsadm

[root@vm5 ~]# yum -y install ipvsadm

添加规则

[root@vm5 ~]# ipvsadm -A -t 192.168.161.109:3306 -s wrr
[root@vm5 ~]# ipvsadm -a -t 192.168.161.109:3306 -r 192.168.225.128:3306 -m
[root@vm5 ~]# ipvsadm -a -t 192.168.161.109:3306 -r 192.168.225.131:3306 -m
[root@vm5 ~]# 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.161.109:3306 wrr
  -> 192.168.225.128:3306         Masq    1      0          0         
  -> 192.168.225.131:3306         Masq    1      0          0    
[root@vm5 ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@vm5 ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.161.109:3306 -s wrr
-a -t 192.168.161.109:3306 -r 192.168.225.128:3306 -m -w 1
-a -t 192.168.161.109:3306 -r 192.168.225.131:3306 -m -w 1

验证

  • vm2,vm4配置远程mysql权限
mysql> grant all privileges on *.* to 'root'@'192.168.161.108' identified by '123456'
mysql> grant all privileges on *.* to 'root'@'192.168.225.1' identified by '123456';

mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------------+-------------------------------------------+
| user          | host            | authentication_string                     |
+---------------+-----------------+-------------------------------------------+
| root          | localhost       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.session | localhost       | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost       | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| root          | 192.168.161.108 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root          | 192.168.225.1   | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------------+-------------------------------------------+
  • 真机验证远程登录mysql
[wisan@fyj tmp]$ mysql -uroot -h192.168.161.109 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test2              |
+--------------------+
5 rows in set (0.009 sec)

[wisan@fyj tmp]$ mysql -uroot -h192.168.161.109 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.007 sec

haproxy-mysql集群

环境描述

主机 服务 角色
vm5(192.168.225.134) haproxy
vm2(192.168.225.128) mysql real server
vm4(192.168.225.131) mysql real server
  • 测试真机(192.168.43.166)

mysql环境部署

mysql> create database test1;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.00 sec)

  • vm4创建一个数据库test2
mysql> create database test2;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test2              |
+--------------------+
5 rows in set (0.03 sec)

haproxy安装部署

修改haproxy服务器sysctl.conf

[root@vm5 haproxy]# vim /etc/sysctl.conf 
[root@vm5 haproxy]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

修改happroxy.cfg配置并重启happroxy

[root@vm5 haproxy]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind 0.0.0.0:3306
#    bind 0.0.0.0:443
    mode tcp
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server mysql1 192.168.225.128:3306 check inter 2000 fall 5
    server mysql2 192.168.225.131:3306 check inter 2000 fall 5
[root@vm5 haproxy]# systemctl restart haproxy
[root@vm5 haproxy]# ss -antl 
State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port    
LISTEN    0         128                0.0.0.0:3306             0.0.0.0:*       
LISTEN    0         128                0.0.0.0:5355             0.0.0.0:*       
LISTEN    0         128                0.0.0.0:22               0.0.0.0:*       
LISTEN    0         128                0.0.0.0:8189             0.0.0.0:*       
LISTEN    0         128                   [::]:5355                [::]:*       
LISTEN    0         128                   [::]:22                  [::]:*    

验证

授权真机登录mysql

##vm2
mysql> grant all privileges on *.* to 'root'@'192.168.43.166' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'192.168.225.1' identified by '123456';     ##真机通往虚拟机网络的网关
mysql> grant all privileges on *.* to 'root'@'192.168.225.134' identified by '123456';   ##haproxy服务器
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

##vm4
mysql> grant all privileges on *.* to 'root'@'192.168.43.166' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'192.168.225.1' identified by '123456';    ##真机通往虚拟机网络的网关
mysql> grant all privileges on *.* to 'root'@'192.168.225.1' identified by '123456';    ##haproxy服务器
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

真机登录

[wisan@fyj ~]$ mysql -h192.168.225.134 -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| student            |
| sys                |
| test1              |
+--------------------+
6 rows in set (0.003 sec)

MySQL [(none)]> quit
Bye
[wisan@fyj ~]$ mysql -h192.168.225.134 -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| student            |
| sys                |
| test2              |
+--------------------+
6 rows in set (0.002 sec)

MySQL [(none)]> 

lvs(DR)+apache

环境描述

主机 服务 LVS角色
vm5(192.168.225.134) Load balancer
vm2(192.168.225.128) apache real server
vm4(192.168.225.131) apache real server
  • 测试真机(192.168.43.166)
  • VIP(172.16.225.225)

apacheh部署

##vm2,vm4的httpd配置相同
[root@vm2 ~]# vim /etc/httpd/conf/httpd.conf 
Include conf.modules.d/*.conf
ServerName www.wisan.com:80
DocumentRoot "/var/www/html"
IncludeOptional conf.d/*.conf

[root@vm2 ~]# vim /etc/httpd/conf.d/vhost.conf <VirtualHost *:80>
    ServerName www.wisan.com
    DocumentRoot "/var/www/html/wisan"
    ErrorLog "/var/log/httpd/error_log"
    CustomLog "/var/log/httpd/access_log" common
</VirtualHost>

[root@vm2 ~]# vim /etc/httpd/conf.modules.d/00-ssl.conf 
LoadModule ssl_module modules/mod_ssl.so

[root@vm2 ~]# vim /etc/httpd/conf.d/ssl.conf
Listen 443 https
DocumentRoot "/var/www/html/wisan"
ServerName www.fanfan.com:443
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key

[root@vm2 ~]# ls /etc/httpd/ssl/
httpd.crt  httpd.csr  httpd.key

[root@vm2 ~]# cat /var/www/html/wisan/index.html 
rs1
[root@vm4 ~]# cat /var/www/html/wisan/index.html 
rs2

lvs配置

vm2,vm4配置IP

  • vm2修改arp参数
[root@vm2 ~]# cat /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
  • 配置IP
[root@vm2 ~]# yum -y install net-tools
[root@vm2 ~]# ifconfig lo
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)
.................................
[root@vm2 ~]# ifconfig lo 172.16.225.225/32           ##临时修改
[root@vm2 ~]# ifconfig lo
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 172.16.225.225  netmask 255.255.255.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
.................................
[root@vm2 ~]# echo 'ifconfig lo 172.16.225.225/32' >> /etc/rc.local  ##永久修改
[root@vm2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.128
NETMASK=255.255.225.0
GATEWAY=192.168.225.2
DNS1=192.168.225.2
NAME=ens33
DEVICE=ens33
ONBOOT=yes
  • vm4修改arp参数
[root@vm4 ~]# cat /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
  • 配置IP
[root@vm4 ~]# yum -y install net-tools
[root@vm4 ~]# ifconfig lo
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)
[root@vm4 ~]# ifconfig lo 172.16.225.225/32
[root@vm4 ~]# ifconfig lo
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 172.16.225.225  netmask 0.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
[root@vm4 ~]# echo 'ifconfig lo 172.16.225.225/32' >> /etc/rc.local
[root@vm4 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.131
NETMASK=255.255.225.0
GATEWAY=192.168.225.2
DNS1=192.168.225.2
NAME=ens33
DEVICE=ens33
ONBOOT=yes

调度器vm5配置IP

[root@vm5 ~]# ifconfig lo 172.16.225.225/32
[root@vm5 ~]# 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 172.16.225.225/24 scope global lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
[root@vm5 ~]# echo 'ifconfig lo 172.16.225.225/32' >> /etc/rc.local
[root@vm5 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.225.134
NETMASK=255.255.225.0
GATEWAY=192.168.225.2
DNS1=192.168.225.2
NAME=ens32
DEVICE=ens32
ONBOOT=yes

配置调度器

下载ipvsadm

[root@vm5 ~]# yum -y install ipvsadm

添加规则

[root@vm5 ~]# ipvsadm -A -t 172.16.225.225:80 -s wrr
[root@vm5 ~]# ipvsadm -a -t 172.16.225.225:80 -r 192.168.225.128:80 -g
[root@vm5 ~]# ipvsadm -a -t 172.16.225.225:80 -r 192.168.225.131:80 -g
[root@vm5 ~]# ipvsadm -A -t 172.16.225.225:443 -s wrr
[root@vm5 ~]# ipvsadm -a -t 172.16.225.225:443 -r 192.168.225.131:443 -g
[root@vm5 ~]# ipvsadm -a -t 172.16.225.225:443 -r 192.168.225.128:443 -g
[root@vm5 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.225.225:80 wrr
  -> 192.168.225.128:80           Route   1      0          0         
  -> 192.168.225.131:80           Route   1      0          0         
TCP  172.16.225.225:443 wrr
  -> 192.168.225.128:443          Route   1      0          0         
  -> 192.168.225.131:443          Route   1      0          0      
[root@vm5 ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm

验证

  • 真机上添加到达VIP的路由
[wisan@fyj ~]$ sudo route add -host 172.16.225.225/32 gw 192.168.225.1 dev vmnet8
[wisan@fyj ~]$ route -nv 
[wisan@fyj ~]$ route -nv
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.43.1    0.0.0.0         UG    600    0        0 wlp4s0
172.16.105.0    0.0.0.0         255.255.255.0   U     0      0        0 vmnet1
172.16.225.225  192.168.225.1   255.255.255.255 UGH   0      0        0 vmnet8
192.168.43.0    0.0.0.0         255.255.255.0   U     600    0        0 wlp4s0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.225.0   0.0.0.0         255.255.255.0   U     0      0        0 vmnet8
  • 真机上访问web服务
[wisan@fyj ~]$ curl 172.16.225.225
rs2
[wisan@fyj ~]$ curl 172.16.225.225
rs1
[wisan@fyj ~]$ curl 172.16.225.225
rs2
[wisan@fyj ~]$ curl 172.16.225.225
rs1
[wisan@fyj ~]$ curl -k https://172.16.225.225
rs2
[wisan@fyj ~]$ curl -k https://172.16.225.225
rs1
[wisan@fyj ~]$ curl -k https://172.16.225.225
rs2
[wisan@fyj ~]$ curl -k https://172.16.225.225
rs1
posted @ 2020-11-22 20:50  小芃总  阅读(138)  评论(0编辑  收藏  举报