知方号

知方号

实现Haproxy来代理后端的服务

1、基础环境

主机名IP服务haproxy.stars.org10.0.0.100haproxyweb1.stars.org10.0.0.101nginxweb2.stars.org10.0.0.102nginx2、Web服务配置

后端两个web服务器主机名分别设置为web1.stars.org和web2.stars.org,这里我就直接使用apt装了,这里我使用的系统的ubuntu的1804的版本的,用centos系统的话下载要使用yum方式安装,安装nginx的话也可以使用编译安装,编译安装可以参考我https://blog.51cto.com/u_15105742/5437984这篇文章,这里只是测试,页面也设置的简单点了。

root@web1:~# apt -y install nginx ;systemctl enable --now nginx;echo "Welcome to `hostname -I` page" > /var/www/html/index.nginx-debian.htmlroot@web2:~# apt -y install nginx ;systemctl enable --now nginx;echo "Welcome to `hostname -I` page" > /var/www/html/index.nginx-debian.html#验证一下页面root@haproxy:~# curl 10.0.0.101Welcome to 10.0.0.101 pageroot@haproxy:~# curl 10.0.0.102Welcome to 10.0.0.102 page

3、Haproxy服务配置

在这里我还是使用的上篇文章的环境编译安装Haproxy的来做,这里实现代理服务器可以使用frontend+backend的方式来实现或者使用listen的方式来实现都可以,不过在生产中推荐使用listen,因为可以简化配置。

3.1、frontend+backend的实现root@haproxy:~# vim /etc/haproxy/haproxy.cfgglobal maxconn 100000 chroot /apps/haproxy stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin user haproxy group haproxy daemon pidfile /var/lib/haproxy/haproxy.pid log 127.0.0.1 local2 infodefaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000mslisten stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth admin:wm521314frontend zg_web_80 bind 10.0.0.100:80#如果你这里配置了高可用的话可以设置成你的VIP,后面就可以使用VIP来访问你的服务了,这也可以配置成你的域名,不过是要设置dns解析的,或者在需要访问的主机上修改hosts文件,从而使用域名也可以访问。 mode http use_backend web_hostsbackend web_hosts mode http server web1 10.0.0.101:80 check inter 3000 fall 2 rise 5#这里还可以添加weight可以设置权重 server web2 10.0.0.102:80 check inter 3000 fall 2 rise 5root@haproxy:~# systemctl restart haproxy.serviceroot@haproxy:~# curl 10.0.0.100Welcome to 10.0.0.102 pageroot@haproxy:~# curl 10.0.0.100Welcome to 10.0.0.101 pageroot@haproxy:~# curl 10.0.0.100Welcome to 10.0.0.102 pageroot@haproxy:~# curl 10.0.0.100Welcome to 10.0.0.101 page

3.2、listen实现

使用listen配置的话比frontend+backend的配置简洁多了。

root@haproxy:~# vim /etc/haproxy/haproxy.cfgglobal maxconn 100000 chroot /apps/haproxy stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin user haproxy group haproxy daemon pidfile /var/lib/haproxy/haproxy.pid log 127.0.0.1 local2 infodefaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000mslisten stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth admin:wm521314listen zg_web_80 bind 10.0.0.100:80 mode http server web1 10.0.0.101:80 check inter 3000 fall 2 rise 5 server web2 10.0.0.102:80 check inter 3000 fall 2 rise 5root@haproxy:~# systemctl restart haproxy.serviceroot@haproxy:~# curl 10.0.0.100Welcome to 10.0.0.101 pageroot@haproxy:~# curl 10.0.0.100Welcome to 10.0.0.102 pageroot@haproxy:~# curl 10.0.0.100Welcome to 10.0.0.101 pageroot@haproxy:~# curl 10.0.0.100Welcome to 10.0.0.102 page

3.3、使用子配置文件3.3.1、创建子配置文件存放的目录root@haproxy:~# mkdir /etc/haproxy/conf.droot@haproxy:~# vim /etc/haproxy/conf.d/zg_web_80.cfglisten zg_web_80 bind 10.0.0.100:80 mode http server web1 10.0.0.101:80 check inter 3000 fall 2 rise 5 server web2 10.0.0.102:80 check inter 3000 fall 2 rise 5#把前面的配置删了或者注释掉,我们这里把配置配到子配置文件上了,不删或者不注释掉的话还是使用的是/etc/haproxy/haproxy.cfg这个配置里的配置,相当于子配置文件没配置一样。root@haproxy:~# cat /etc/haproxy/haproxy.cfg global maxconn 100000 chroot /apps/haproxy stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin user haproxy group haproxy daemon pidfile /var/lib/haproxy/haproxy.pid log 127.0.0.1 local2 infodefaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000mslisten stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth admin:wm5213143.3.2、修改haproxy的service文件

这里和nginx的配置子配置文件不同,nginx的是创建了子配置文件后需要在主配置文件中引入一下子配置文件的路径,而haproxy的话是要修改service文件的,需要在service中指定一下子配置文件的路径并重启一下haproxy服务。

root@haproxy:~# vim /lib/systemd/system/haproxy.service[Unit]Description=HAProxy Load BalancerAfter=network-online.targetWants=network-online.target[Service]ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -c -qExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -p /var/lib/haproxy/haproxy.pidExecReload=/bin/kill -USR2 $MAINPIDLimitNOFILE=100000[Install]WantedBy=multi-user.targetroot@haproxy:~# systemctl daemon-reload root@haproxy:~# systemctl restart haproxy.service3.3.3、客户端验证访问

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。