HAProxy–实践–02–案例
1、前置:搭建2个nginx服务
步骤1、启动nginx服务
docker run -d -p 8081:80 --name nginx1 nginx:1.21.3-alpine
docker run -d -p 8082:80 --name nginx2 nginx:1.21.3-alpine
步骤2、修改页面
拷贝页面
docker cp nginx1:/usr/share/nginx/html/index.html index1.html
docker cp nginx2:/usr/share/nginx/html/index.html index2.html
将index1.html,index2.html 分别改为
<!DOCTYPE html>
<html>
<body>
nginx1
</body>
</html>
-------------
<!DOCTYPE html>
<html>
<body>
nginx2
</body>
</html>
拷贝页面回容器
docker cp index1.html nginx1:/usr/share/nginx/html/index.html
docker cp index2.html nginx2:/usr/share/nginx/html/index.html
步骤3、重启docker和测试
docker restart nginx1 nginx2
##备注 、如果要修改配置文件,可以执行下面命令
# 拷贝配置文件(我们不改配置)
docker cp nginx:/etc/nginx/nginx.conf nginx.conf
2、搭建haproxy
2.1、创建配置文件
# 创建配置文件
vim /root/haproxy.cfg
haproxy.cfg
global
maxconn 100000
daemon
log 127.0.0.1 local0 info
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 500
# Haproxy控制台管理
listen admin_stats
stats enable
bind 0.0.0.0:8888
mode http
option httplog
log global
maxconn 10
stats refresh 30s # 统计页面自动刷新时间
stats uri /admin # 访问的uri:ip:8888/admin
stats realm haproxy
stats auth admin:123456 # 认证用户名和密码
stats hide-version # 隐藏HAProxy的版本号
stats admin if TRUE # 管理界面,如果认证成功了,可通过web-ui管理节点
2.2、启动
docker run -d -p 8888:8888 --name haproxy --net=host -v /root/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro haproxy:2.5.0
2.3、测试
http://192.168.187.133:8888/admin
所有的配置
3、一个最简单的http服务的配置+后端负载均衡
3.1、修改配置文件
global
maxconn 100000
daemon
log 127.0.0.1 local0 info
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 500
# Haproxy控制台管理
listen admin_stats
stats enable
bind 0.0.0.0:8888
mode http
option httplog
log global
maxconn 10
stats refresh 30s # 统计页面自动刷新时间
stats uri /admin # 访问的uri:ip:8888/admin
stats realm haproxy
stats auth admin:123456 # 认证用户名和密码
stats hide-version # 隐藏HAProxy的版本号
stats admin if TRUE # 管理界面,如果认证成功了,可通过web-ui管理节点
#前端服务 web
frontend web
option forwardfor
bind *:7000
# 默认走后端服务backApp
default_backend backApp
#前端服务 backApp
backend backApp
balance roundrobin #使拥roundrobin 算法
server app1 192.168.187.133:8081 check
server app2 192.168.187.133:8082 check
3.2、重启和测试
docker restart haproxy
4、动静分离示例(配置文件和验证)
global
maxconn 100000
daemon
log 127.0.0.1 local0 info
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 500
# Haproxy控制台管理
listen admin_stats
stats enable
bind 0.0.0.0:8888
mode http
option httplog
log global
maxconn 10
stats refresh 30s # 统计页面自动刷新时间
stats uri /admin # 访问的uri:ip:8888/admin
stats realm Hapadmin # 统计页面密码框上提示文本,默认为Haproxy\ Statistics
stats auth admin:123456 # 认证用户名和密码
stats hide-version # 隐藏HAProxy的版本号
stats admin if TRUE # 管理界面,如果认证成功了,可通过web-ui管理节点
#前端服务 web
frontend web
option forwardfor
bind *:7000
# url 以 /static /images /javascript /stylesheets 开头
acl url_static path_beg -i /static /images /javascript /stylesheets
# url 以 .jpg .gif .png .css .js .html 结尾,且忽略大小写
acl url_static path_end -i .jpg .gif .png .css .js .html
# 判断域名(host)是否包含img. imgs. video. videos. ftp. image. download.
acl host_static hdr_beg(host) -i img. imgs. video. videos. ftp. image. download.
# 如果url_static或者host_static是true,走后端static服务
use_backend static if url_static or host_static
# 默认走后端dynamic服务
default_backend dynamic
#后端服务 static
backend static
balance roundrobin
server app1 192.168.187.133:8081 check
#后端服务 dynamic
backend dynamic
balance roundrobin
server app2 192.168.187.133:8082 check
请求静态服务2次
http://192.168.187.133:7000/static
请求动态服务3次
http://192.168.187.133:7000/