环境:
主机名 | IP | 网卡名称 | 服务 |
---|
haproxy-master | 192.168.58.140 | 网卡接口:ens160 | haproxy、keepalived | haproxy-slave: | 192.168.58.141 | 网卡接口:ens160 | haproxy、keepalived | web-1: | 192.168.58.138 | 网卡接口:ens160 | httpd | web-2: | 192.168.58.139 | 网卡接口:ens160 | httpd |
部署httpd
//web-1和web-2部署httpd
[root@web-1 ~]
[root@web-1 ~]
[root@web-1 ~]
[root@web-1 ~]
[root@web-2 ~]
[root@web-2 ~]
[root@web-2 ~]
安装haproxy
//haproxy-master和haproxy-slave主机安装haproxy
//安装haproxy
[root@haproxy-master ~]
//安装编译环境
[root@haproxy-master ~]
//创建haproxy用户
[root@haproxy-master ~]
//解压和安装
[root@haproxy-master ~]
[root@haproxy-master ~]
[root@haproxy-master haproxy-2.1.3]
[root@haproxy-master haproxy-2.1.3]
[root@haproxy-master haproxy-2.1.3]
[root@haproxy-master haproxy-2.1.3]
//修改LB的内核参数
[root@haproxy-master ~]
[root@haproxy-master ~]
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//修改haproxy配置文件
[root@haproxy-master ~]
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server web01 192.168.58.138:80
server web02 192.168.58.139:80
//启动haproxy,配置haproxy.service服务单元文件
[root@haproxy-master ~]
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
//启动haproxy服务
[root@haproxy-master ~]
[root@haproxy-master ~]
[root@haproxy-master ~]
[root@haproxy-master ~]
//配置日志信息
[root@haproxy-master ~]
local0.* /var/log/haproxy.log
//使用WEB网页访问测试
[root@haproxy-master ~]
//清空,修改为如下:
global
log 127.0.0.1 local0 info
maxconn 20480
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
stats refresh 30s
listen webcluster
bind 0.0.0.0:80
mode http
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 192.168.58.138:80 check inter 2000 fall 5
server web02 192.168.58.139:80 check inter 2000 fall 5
[root@haproxy-master ~]
[root@haproxy-master ~]
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
haproxy-slave安装haproxy和上方一样
略
keepalived安装
配置主keepalived
[root@haproxy-master ~]
配置主keepalived
[root@haproxy-master ~]
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass yexiaotian
}
virtual_ipaddress {
192.168.58.250
}
}
virtual_server 192.168.58.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.58.140 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@haproxy-master ~]
配置备keepalived
[root@haproxy-slave ~]
[root@haproxy-slave ~]
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass yexiaotian
}
virtual_ipaddress {
192.168.58.250
}
}
virtual_server 192.168.58.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.58.141 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@haproxy-slave ~]
|