实验环境 L:Linux(centos 7.6) http://mirrors.cqu.edu.cn/CentOS/7.6.1810/isos/x86_64/ H: Haproxy(1.8.20)
主机信息: 部署规划: 172.24.77.241(sr1.dj.com):Tomcat服务器1 172.24.77.242(sr2.dj.com):Tomcat服务器2 172.24.77.243(sr3.dj.com):Haproxy服务器 1、 安装依赖包 #yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate -y 2、 编译安装Haproxy #cd /usr/local/src/ 解压安装包 #tar xvf haproxy-1.8.20.tar.gz #cd haproxy-1.8.20 #make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy #make install PREFIX=/usr/local/haproxy 3、 创建启动脚本 #vim /usr/lib/systemd/system/haproxy.service [Unit] Description=HAProxy Load Balancer After=syslog.target network.target [Service] ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target 4、 创建目录和用户 #mkdir /etc/haproxy #cd /etc/haproxy 查看默认haproxy配置文件 #/usr/local/src/haproxy-1.8.20 #find ./* -name .cfg 自定义配置文件 #vim /etc/haproxy/haproxy.cfg global chroot /usr/local/haproxy #stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin user haproxy group haproxy daemon #nbproc 4 #cpu-map 1 0 #cpu-map 2 1 #cpu-map 3 2 #cpu-map 4 3 pidfile /usr/local/haproxy/run/haproxy.pid log 127.0.0.1 local3 info defaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth haadmin:q1w2e3r4ys listen web_port bind 0.0.0.0:80 mode http log global server web1 172.24.77.241:8080 check inter 3000 fall 2 rise 5 server web1 172.24.77.242:8080 check inter 3000 fall 2 rise 5 ** 创建启动用户* #useradd haproxy -s /sbin/nologin #mkdir /var/lib/haproxy 授权目录 #chown haproxy.haproxy /var/lib/haproxy/ -R #systemctl restart haproxy 5、 启动并测试haproxy #systemctl start haproxy #ps -ef |grep haproxy 查看端口占用 #lsof -i:80 设置开机启动 #systemctl enable haproxy 6、 配置七层代理 配置并测试Web后端 #http://172.24.77.241:8080/ 测试Haproxy反向代理 http://192.168.0.243/ 7、设置ACL实现/a路径代理转发到a集群,/b路径代理转发到/b集群 #vim /etc/haproxy/haproxy.cfg listen web_port bind 172.24.77.243:80 mode http acl static_path_a path_beg -i /a use_backend uri_a_host if static_path_a default_backend default_host acl static_path_b path_beg -i /b use_backend uri_b_host if static_path_b default_backend default_host
backend uri_a_host mode http server web1 172.24.77.241:8080 check inter 2000 fall 3 rise 5
backend uri_b_host mode http server web2 172.24.77.242:8080 check inter 2000 fall 3 rise 5
backend default_host mode http server web3 172.24.77.243:8080 check inter 2000 fall 3 rise 5
8、 配置后端Tomcat服务器:241 #cd /usr/local/tomcat/webapps/ #mkdir a/{lib,classes,WEB-INF,META-INF} –pv 创建backend server-a首页 #vim a/index.jsp <%@ page language="java" %> <%@ page import="java.util." %> <html> <head> <title>JSP Test Page</title> </head> <body> <% out.println("Backend-Server-a-241");%> </body> </html> 重启Tomcat #catalina.sh stop #catalina.sh start 测试访问Backend #http://172.24.77.241:8080/a/ 9、 配置后端Tomcat服务器:242 #cd /usr/local/tomcat/webapps/ #mkdir b/{lib,classes,WEB-INF,META-INF} -pv 创建backend server-b首页 #vim b/index.jsp <%@ page language="java" %> <%@ page import="java.util." %> <html> <head> <title>JSP Test Page</title> </head> <body> <% out.println("Backend-Server-b-242");%> </body> </html> 重启Tomcat #catalina.sh stop #catalina.sh start 测试访问Backend #http://172.24.77.241:8080/b/ 测试HAproxy转发 #http://172.24.77.243/a/ #http://172.24.77.243/b/