一)地址配置:上面的图可能有点乱,这里再梳理一下(准备四台主机)
主机1:
172.16.24.100(面向外网的地址)
172.16.24.222(vip地址,传递两台主机的心跳信息)
192.168.24.1 (面向内网的地址)
所需要安装的软件为(keepalived+haproxy)
主机2
172.16.24.66(面向外网的地址)
172.16.24.222(vip地址,传递两台主机的心跳信息)
192.168.24.1 (面向内网的地址)
所需要安装的软件为(keepalived+haproxy)
主机3
192.168.24.11
所需安装的服务为(php+mysql)
主机4
192.168.24.22
所需安装的服务为(php+mysql)
(注:把主机3和主机四的网关指向192.168.24.1)
#route add default gw 192.168.24.1
二、安装配置keepalived服务(主机1和主机2配置相同,这里以一台做为说明)
1)安装keepalived
[root@contron ~]# yum -y install keepalived
2)配置keepalived
global_defs { #全局段配置
notification_email { #收件人地址
root@localhost #因为我们是在本机做实验,所以定义成为本地。
}
notification_email_from keepadmin@firewall.loc #发信人地址,这个是由系统自定的一般不改也可以。
smtp_server 127.0.0.1 #发邮件服务器
smtp_connect_timeout 30 #连接邮件服务器的超时时间
router_id LVS_DEVEL #路由器标识
}
vrrp_instance VI_1 { #定义虚拟路由器
state MASTER #初始状态
interface eth0 #通告要通过哪个接口进行
virtual_router_id 51 #虚拟路由ID(不能大于255)
priority 100 #初始优先级
advert_int 1 #初始化通告几次
authentication { #定义认证机制
auth_type PASS #认证类型为明文认证
auth_pass 1111 #认证密码
}
virtual_ipaddress { #虚拟地址,就是vip地址
172.16.24.222
}
}
vrrp_instance VI_2{
state BACKUP
interface eth0
virtual_router_id 55
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.24.233
}
}
3)172.16.24.66的keepalived配置文件
global_defs {
notification_email {
root@localhost
}
notification_email_from keepadmin@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.24.222
}
}
vrrp_instance VI_2{
state MASTER
interface eth0
virtual_router_id 55
priority 900
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.24.233
}
}
4)启动keepalived
[root@contron ~]# service keepalived restart
[root@contron ~]# ip add show #查看一下配置结果
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:51:3f:65 brd ff:ff:ff:ff:ff:ff
inet 172.16.24.100/16 brd 172.16.255.255 scope global eth0
inet 172.16.24.222/32 scope global eth0
inet6 fe80::20c:29ff:fe51:3f65/64 scope link
valid_lft forever preferred_lft forever
三、安装配置haproxy(172.16.24.66、172.16.24.100)
1)安装配置haproxy
[root@contron ~]# yum install haproxy
[root@contron ~]# cd /etc/haproxy
[root@contron haproxy]# cp haproxy.cfg haproxy.cfg.back
[root@contron haproxy]# vim haproxy.cfg(上一篇有介绍,这里就不再介绍了)
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets
acl url_static path_end -i .jpg .jpeg .gif .png .css .js .html
acl url_php path_end -i .php
use_backend static_servers if url_static
use_backend dynamic_servers if url_php
default_backend dynamic_servers
backend static_servers
balance roundrobin
server node1 192.168.24.11:80 check maxconn 6000
backend dynamic_servers
balance roundrobin
server node2 192.168.24.22:80 check maxconn 1000
2)启动haproxy服务
[root@master ~]# service haproxy start
四、配置web+php服务并提供服务器页面(192.168.24.11、192.168.24.12)
1)安装上php、php-mysql、并给web提供一个页面。
#yum -y install php php-mysql
#vim /var/www/html/text.html
<h1>node1.edu.com</h1>
#vim /var/www/html/index.php
<?php
phpinfo();
?>
2)启动web服务
#service httpd start
到此配置完成
转载于:https://blog.51cto.com/8519539/1405179