主机名 IP地址
VIP 192.168.200.254
Haproxy-1 192.168.200.101
Haproxy-2 192.168.200.102
Nginx1 192.168.200.103
Nginx2 192.168.200.104
1.1 首先安装Nginx1
[root@Nginx-1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@Nginx-1 ~]# useradd -M -s /sbin/nologin nginx
[root@Nginx-1 ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
[root@Nginx-1 ~]# cd /usr/src/nginx-1.6.2
[root@Nginx-1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@Nginx-1 nginx-1.6.2]# cd /usr/local/nginx/html/
[root@Nginx-1 html]# echo "server 192.168.200.103" > index.html
[root@Nginx-1 html]# /usr/local/nginx/sbin/nginx
[root@Nginx-1 html]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4503/nginx
1.2 安装Nginx2, 同Nginx1搭建方式是一样的。
与Nginx1唯一不同的是:
[root@Nginx-2 html]# echo "server 192.168.200.104" > index.html
haproxy1服务器:192.168.200.111
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
[root@localhost ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26 && make install
[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
#chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
#redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web-cluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server inst1 192.168.200.113:80 check inter 2000 fall 3
server inst2 192.168.200.114:80 check inter 2000 fall 3
[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# /etc/init.d/haproxy start
[root@localhost ~]# yum -y install keepalived
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 10
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
}
[root@localhost ~]# vim /etc/keepalived/check_haproxy.sh
#!/bin/bash
num=`ps -C haproxy --no-header |wc -l`
if [ $num -eq 0 ]
then
/etc/init.d/haproxy start
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]
then
systemctl stop keepalived
fi
fi
[root@localhost ~]# chmod +x /etc/keepalived/check_haproxy.sh
[root@localhost ~]# systemctl restart keepalived
haproxy2服务器:192.168.200.112
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
[root@localhost ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26 && make install
[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
#chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
#redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web-cluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server inst1 192.168.200.113:80 check inter 2000 fall 3
server inst2 192.168.200.114:80 check inter 2000 fall 3
[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# /etc/init.d/haproxy start
[root@localhost ~]# yum -y install keepalived
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 5
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
}
[root@localhost ~]# vim /etc/keepalived/check_haproxy.sh
#!/bin/bash
num=`ps -C haproxy --no-header |wc -l`
if [ $num -eq 0 ]
then
/etc/init.d/haproxy start
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]
then
systemctl stop keepalived
fi
fi
[root@localhost ~]# chmod +x /etc/keepalived/check_haproxy.sh
[root@localhost ~]# systemctl restart keepalived