内容纲要
实验环境
192.168.50.70 haproxy1+keepalived 192.168.50.72 haproxy2+keepalived 192.168.50.73 client1(http) 192.168.50.74 client2(http) 192.168.50.68 vip 所有主机关闭selinux 关闭firwalld 设置好yum源 安装net-tools 设置好主机名
1.安装依赖(四台都要装)
yum install gcc gcc-c++ make openssl-devel kernel-devel net-tools bash*
2.安装haproxy(在haproxy1+keepalived和haproxy2+keepalived上做)
yum install haproxy
systemctl start haproxy;systemctl enable haproxy
3.编辑haproxy的配置文件( haproxy1+keepalived和haproxy2+keepalived上做 )
cd /etc/haproxy/
cp haproxy.cfg haproxy.cfg.bak
vim /etc/haproxy/haproxy.cfg
#haproxy1
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local3 info
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 65535
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 65535
stats uri /haproxy
stats refresh 1s
stats realm haproxy-status
stats auth admin:admin
stats hide-version
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen web:80
bind :80
mode http
balance roundrobin
cookie LBN insert indirect nocache
option httpclose
server web01 192.168.50.73 check inter 2000 fall 3 weight 20
server web02 192.168.50.74 check inter 2000 fall 3 weight 20
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
# balance roundrobin
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check
#haproxy2
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local3 info
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 65535
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 65535
stats uri /haproxy
stats refresh 1s
stats realm haproxy-status
stats auth admin:admin
stats hide-version
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen web:80
bind :80
mode http
balance roundrobin
cookie LBN insert indirect nocache
option httpclose
server web01 192.168.50.73 check inter 2000 fall 3 weight 20
server web02 192.168.50.74 check inter 2000 fall 3 weight 20
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
# balance roundrobin
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check
4.配置haproxy日志( haproxy1+keepalived和haproxy2+keepalived上做)
vim /etc/rsyslog.conf
$ModLoad imudp 取消注释
$UDPServerRun 514 取消注释
local3.* /var/log/haproxy.log
保存退出
vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-r -m 0"
保存退出
systemctl restart rsyslog
5.编辑keepalived的配置文件( haproxy1+keepalived和haproxy2+keepalived上做 )
#keepalived1
! Configuration File for keepalived
global_defs {
router_id haproxy70
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.50.68 dev ens33 label ens33:1
}
}
#keepalived2
! Configuration File for keepalived
global_defs {
router_id haproxy70
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 50
priority 70
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.50.68 dev ens33 label ens33:1
}
}
6.设置haproxy和keepalived开机启动 (haproxy1+keepalived和haproxy2+keepalived上做 ),在client1和client2上安装apache服务并设置主页和开启启动。
#haproxy1+keepalived haproxy2+keepalived
systemctl start haproxy;systemctl enable haproxy
systemctl start keepalived;systemctl enable keepalived
#client1
yum install httpd
echo client1 > /var/www/html/index.html
systemctl start httpd;systemctl enable httpd
#client2
yum install httpd
echo client2 > /var/www/html/index.html
systemctl start httpd;systemctl enable httpd
7.访问192.168.50.68

8.访问192.168.50.68/haproxy 账号admin密码admin

keepalived virtual_router_id要一样