1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| ============================================================================================== 环境 准备三台主机 haproxy:外网地址:192.168.1.25;内网地址:172.16.106.143 httpd1:内网地址:172.16.106.145 httpd2:内网地址:172.16.106.144 ==============================================================================================
--------------- httpd1&2 --------------- [root@httpd1 ~]# yum install -y httpd [root@httpd1 ~]# systemctl start chronyd [root@httpd1 ~]# systemctl enable chronyd [root@httpd1 ~]# chronyc chrony version 3.1 Copyright (C) 1997-2003, 2007, 2009-2017 Richard P. Curnow and others chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public License version 2 for details.
chronyc> waitsync try: 1, refid: 771CCEC1, correction: 0.000001078, skew: 32.010 # 同步时间 [root@httpd1 ~]# vim /var/www/html/index.html Backend Server 1 # 给两台服务器提供一个主面,另一台服务器的内容是Backend Server 2 [root@httpd1 ~]# systemctl start httpd [root@httpd2 ~]# curl 172.16.106.144 Backend Server 2 [root@httpd2 ~]# curl 172.16.106.145 Backend Server 1
-------------- haproxy -------------- [root@haproxy haproxy]# vim /etc/rsyslog.conf $ModLoad imudp $UDPServerRun 514 local2.* /var/log/haproxy.log # 加入此行,设置haproxy的日志路径 [root@haproxy haproxy]# systemctl restart rsyslog [root@haproxy ~]# cd /etc/haproxy/ [root@haproxy haproxy]# cp haproxy.cfg{,.bak} [root@haproxy haproxy]# vim haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats 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 3000 frontend myweb # 定义前端主机,取名叫myweb bind *:80 # 定义haproxy的监听端口 default_backend websrvs # 定义默认使用的后端主机是websrvs backend websrvs # 定义后端主机组叫websrvs balance roundrobin # 使用轮询的方式 server srv1 172.16.106.145:80 check server srv2 172.16.106.144:80 check # 后端主机信息,使用server定义,srv1是haproxy内部引用的ID号,自定义的。它是服务器在haproxy上的内部名称;出现在日志及警告信息;check是对主机的健康检测,如果不加,那么会被认为是始终在线的。这是四层检测,发TCP请求,如果响应就表示在线 [root@haproxy haproxy]# systemctl start haproxy [root@haproxy haproxy]# for i in `seq 10`;do curl 192.168.1.25;done Backend Server 1 Backend Server 2
|