七、部署负载均衡web群集
主机 |
操作系统 |
IP地址 |
haproxy服务器 | centos7 | 192.168.109.131 |
nginx服务器1 |
centos7 |
192.168.109.134 |
nginx服务器2 |
centos7 |
192.168.109.135 |
客户端 |
Windows7 |
192.168.109.200 |
关闭所有机子防火墙,将包传入haproxy服务器/opt下
systemctl stop firewalld systemctl disable firewalld setenforce 0 haproxy-1.5.19.tar.gz #所需软件包 链接:https://pan.baidu.com/s/1TMPMVWCrvsyh6p6zAn1AGA 提取码:hde6
7.1 nginx节点服务器(两台相同)
#yum安装 nginx vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 yum install nginx -y systemctl start nginx systemctl enable nginx #写入不同的网页内容以便测试的时候区分服务器 #服务器1 [root@localhost ~]# cd /usr/share/nginx/html/ [root@localhost html]# ls 50x.html index.html [root@localhost html]# echo "<h1>nginx test1</h1>" >index.html #服务器2 [root@localhost ~]# cd /usr/share/nginx/html/ [root@localhost html]# ls 50x.html index.html [root@localhost html]# echo "<h1>nginx test2</h1>" >index.html
7.2 haproxy服务器配置
[root@localhost haproxy-1.5.19]# ls CHANGELOG doc haproxy LICENSE ROADMAP tests contrib ebtree haproxy-systemd-wrapper Makefile src VERDATE CONTRIBUTING examples include README SUBVERS VERSION [root@localhost haproxy-1.5.19]# cd examples/ [root@localhost examples]# mkdir /etc/haproxy [root@localhost examples]# cp haproxy.cfg /etc/haproxy/ [root@localhost examples]# cd /etc/haproxy/ [root@localhost haproxy]# ls haproxy.cfg [root@localhost haproxy]# vim haproxy.cfg #修改后的文件 # this config needs haproxy-1.1.28 or haproxy-1.2.1 global log /dev/log local0 info log /dev/log local0 notice #log loghost local0 info maxconn 4096 #chroot /usr/share/haproxy uid 99 gid 99 daemon nbproc 1 #debug #quiet defaults log global mode http option httplog option dontlognull retries 3 redispatch maxconn 2000 #contimeout 5000 #clitimeout 50000 #srvtimeout 50000 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s listen nginx_cluster 0.0.0.0:80 option httpchk GET /index.html balance roundrobin server inst1 192.168.109.134:80 check inter 2000 fall 3 server inst2 192.168.109.135:80 check inter 2000 fall 3
7.3 添加haproxy系统服务
#将haproxy.init 启动文件拷贝到/etc/init.d中并改名 [root@localhost haproxy]# cd /opt/haproxy-1.5.19/examples/ [root@localhost examples]# cp haproxy.init /etc/init.d/haproxy #添加执行权限 [root@localhost examples]# cd /etc/init.d/ [root@localhost init.d]# ls functions haproxy netconsole network README [root@localhost init.d]# chmod +x haproxy #添加到chkconfig中管理 [root@localhost init.d]# chkconfig --add /etc/init.d/haproxy cd #优化路径 [root@localhost init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy #开启服务 [root@localhost init.d]# service haproxy start
7.4 客户端测试
八、Haproxy集群日志定义
默认haproxy的日志是输出到系统的syslog中(/var/log/messages),查看起来不是非常方便,为了更好的管理haproxy的日志,我们在生产环境中一般单独定义出来。需要将haproxy的info及notice日志分别记录到不同的日志文件中。
vim /etc/haproxy/haproxy.cfg #修改配置文件 global log /dev/log local0 info log /dev/log local0 notice service haproxy start
需要修改rsyslog配置,为了便于管理。将haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d/下,rsyslog启动时会自动加载此目录下的所有配置文件
vim /etc/rsyslog.d/haproxy.conf if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log &~ if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log &~ #说明:这部分配置是将haproxy的info日志文件记录到/var/log/haproxy/haproxy-info.log下,将notice日志记录到/var/log/haproxy/haproxy-notice.log下。 &~表示当日志写到日志文件后,rsyslog停止处理这个信息 systemctl restart rsyslog.service tail -f /var/log/haproxy/haproxy-info.log #查看haproxy的请求日志信息
总结
haproxy的使用还是比较简单的,比lvs的配置简单很多