1.拓扑结构
2.服务软件:keepalived,haproxy
1.后端服务器配置测试页面和mariadb数据库
server {
listen 80;
server_name localhost;
root /home;
index index.html;
}
echo server_1 > /home/index.html
server {
listen 80;
server_name localhost;
root /home;
index index.html;
}
echo server_2 > /home/index.html
yum -y install mariadb mariadb-server
3.haproxy配置
master:
global
log 127.0.0.1 local2 info
pidfile /var/run/haproxy.pid
maxconn 400
user haproxy
group haproxy
daemon
nbproc 1
defaults
mode http
log global
retries 3
option redispatch
maxconn 4000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:81
stats enable
stats uri /haproxy
stats auth qianfeng:123
frontend web
mode http
bind *:80
option httplog
acl html url_reg -i \.html$
use_backend httpservers if html
default_backend httpservers
backend httpservers
balance roundrobin
server http1 192.168.163.136:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 192.168.163.137:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
listen mysql
bind *:3306
mode tcp
balance roundrobin
server mysql1 192.168.163.136:3306 weight 1 check inter 1s rise 2 fall 2
server mysql2 192.168.163.137:3306 weight 1 check inter 1s rise 2 fall 2
slave
global
log 127.0.0.1 local2 info
pidfile /var/run/haproxy.pid
maxconn 400
user haproxy
group haproxy
daemon
nbproc 1
defaults
mode http
log global
retries 3
option redispatch
maxconn 4000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:81
stats enable
stats uri /haproxy
stats auth qianfeng:123
frontend web
mode http
bind *:80
option httplog
acl html url_reg -i \.html$
use_backend httpservers if html
default_backend httpservers
backend httpservers
balance roundrobin
server http1 192.168.163.136:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 192.168.163.137:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
listen mysql
bind *:3306
mode tcp
balance roundrobin
server mysql1 192.168.163.136:3306 weight 1 check inter 1s rise 2 fall 2
server mysql2 192.168.163.137:3306 weight 1 check inter 1s rise 2 fall 2
4.keepalived配置
master
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.163.100/24
}
}
slave
! Configuration File for keepalived
global_defs {
router_id directory2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
nopreempt
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.163.100/24
}
}
5.启动服务测试
http服务测试成功
[root@localhost ~]# curl 192.168.163.100
server___1
[root@localhost ~]# curl 192.168.163.100
server___2
mysql服务测试成功
[root@localhost ~]# mysql -uroot -h192.168.163.100 -p'QianFeng@123'
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaaaaaaaaa |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
======================================================================
[root@localhost ~]# mysql -uroot -h192.168.163.100 -p'QianFeng@123'
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbbbbbb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
ip地址飘移测试成功
[root@proxy-master ~]# 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:a5:95:7a brd ff:ff:ff:ff:ff:ff
inet 192.168.163.139/24 brd 192.168.163.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.163.100/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::da8a:9a9a:208e:b6f3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
===============================================================================
[root@proxy-slave ~]# 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:c3:f5:a6 brd ff:ff:ff:ff:ff:ff
inet 192.168.163.138/24 brd 192.168.163.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.163.100/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::839e:9a22:509a:ff61/64 scope link noprefixroute
valid_lft forever preferred_lft forever
注:
在systemctl管理keepalived的时候,stop服务的时候会关闭不彻底进程,我们需要在keepalived服务的配置文件中注释掉KillMode=process这一行
[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target
[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
#KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target