前言
我们这次使用HAProxy作为负载均衡调度器来实现后端httpd服务的负载均衡和动静分离,实现将来自用户的80端口的http请求转发只后端8080端口的server服务
HAProxy介绍
HAProxy的是一个免费的,非常快速和可靠的解决方案,提供高可用性,负载均衡和代理对TCP和HTTP的应用程序。它特别适用于非常高流量网站。多年来,它已成为标准的开源的负载均衡程序,现在随最主流的Linux发行版,并且通常默认的云平台部署。其运作模式使得其集成到现有的架构非常容易,无风险,同时还提供了可能性不暴露脆弱的Web服务器到网络
实验拓扑
实验环境
VIP1: 192.168.31.6
主机 | IP | 功用 |
---|---|---|
C7node1 | 192.168.31.21, VIP | HAproxy, KeepAlived |
C7node2 | 192.168.31.22, VIP | HAproxy, KeepAlived |
C7node3 | 192.168.31.23 | httpd, php 动态资源 |
C7node4 | 192.168.31.24 | nginx, 静态资源 |
注意: 本文实验中所有主机SElinux和iptables都是关闭的, 系统为:CentOS 7.2 x86_64
实验步骤
配置后端httpd服务器
node4 静态服务器,静态资源 [root@c7node4 ~]# yum -y install nginx #安装nginx服务器 [root@c7node4 ~]# vim /etc/nginx/nginx.conf #编辑nginx配置文件,修改坚挺端口 server { listen 8080 default_server; #由80改为8080端口 server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } [root@c7node4 ~]# echo '<h1> Jingtai server 192.168.31.24:8080</h1>' > /usr/share/nginx/html/index.html [root@c7node4 ~]# cp /usr/share/backgrounds/*.jpg /usr/share/nginx/html/ #复制一些图片文件到网站根目录做测试 [root@c7node4 ~]# systemctl start nginx.service
node3 动态服务器,动态资源 [root@c7node3 ~]#yum install httpd php #安装httpd,php [root@c7node3 ~]#vim /etc/httpd/conf/httpd.conf #修改配置文件,更改监听端口8080 ServerRoot "/etc/httpd" # # Listen: Allows you to bind Apache to specific IP addresses and/or # ports, instead of the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses. # #Listen 12.34.56.78:80 Listen 8080 [root@c7node3 ~]# systemctl start httpd.service #启动httpd服务 [root@c7node3 ~]#cat >> /var/www/html/index.php << "EOF" #创建网页文件 ><h1>Dongtai server 192.168.31.23:8080</h1> ><img src="/morning.jpg"/> #我们动态的网页目录下并没有这张图片 ><?php >phpinfo(); >?> >EOF
配置HAProxy实现backend负载均衡
[root@c7node1 ~]# yum install -y haproxy #安装haproxy [root@c7node1 ~]# vim /etc/haproxy/haproxy.cfg #配置文件如下 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 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend main *:80 stats enable stats hide-version stats uri /haproxyadm acl url_static path_end -i .jpg .gif .png .css .js .html default_backend dongtai use_backend static if url_static #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- backend static balance roundrobin server static 192.168.31.24:8080 check #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend dongtai balance roundrobin server dongtai 192.168.31.23:8080 check [root@c7node1 ~]#systemctl start haproxy.service
测试动静分离效果
我们访问 192.168.31.21 这个是haproxy服务器IP地址
我们关闭 192.168.31.24 这个静态服务器nginx服务后结果 [root@c7node4 ~]# systemctl stop nginx.service
我们再次开启192.168.31.24 这个静态服务器nginx服务后,图片显示正常 [root@c7node4 ~]# systemctl start nginx.service 我们打开了stats页面, 可以通过设置的URI进行访问
配置keepalived
配置c7node1上keepalived的配置文件 [root@c7node4 ~]#vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from zhong@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER #配置为主节点 interface eno16777736 virtual_router_id 51 priority 100 #主节点的权重 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.31.6 dev eno16777736 label eno16777736:0 #虚拟vIP } } [root@c7node4 ~]#systemctl start keepalived.service [root@c7node4 ~]#ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:13:c2:04 brd ff:ff:ff:ff:ff:ff inet 192.168.31.21/24 brd 192.168.31.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.31.6/32 scope global eno16777736:0 #虚拟VIP,已经配置上了 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe13:c204/64 scope link valid_lft forever preferred_lft forever #配置c7node2上的keepalived配置文件# [root@c7node2 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from zhong@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP #备节点 interface eno16777736 virtual_router_id 52 priority 95 #权重,应该小于主节点 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.31.6 dev eno16777736 label eno16777736:0 } } [root@c7node2 ~]# systemctl start keepalived.service [root@c7node2 ~]# ip addr #发现虚拟VIP没有在备用节点上启用 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:60:20:b7 brd ff:ff:ff:ff:ff:ff inet 192.168.31.22/24 brd 192.168.31.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe60:20b7/64 scope link valid_lft forever preferred_lft forever
最终测试
直接访问192.168.31.6 此时VIP在C7node1上面
将C7node1上面的keepalived停止,VIP自动转移到C7node2上面,
总结
我们轻松地通过HAProxy实现资源的动静分离和后端httpd主机的负载均衡,也通过KeepAlived实现HAProxy的高可用, 对于一个集群架构来说,整套架构还不算是很完整的。比如,在keepalived上面没有做后端主机健康检查;后端还没有配置varnish缓存服务器。这些知识还需要在后续的学习中继续来实践。
原创文章,作者:N25_木头钟,如若转载,请注明出处:http://www.178linux.com/63371
评论列表(1条)
赞~ keepalived 中的其中一个配置参数 virtual_router_id 需要注意下~~继续加油~