环境:
server1 为haproxy部署端 server2 server3 server4为后端服务器;
在server1中: yum install haproxy
1.haproxy默认不纪录日志信息;修改方法:
将 /etc/haproxy/haproxy.cfg 中的 :
local2.* /var/log/haproxy.log
复制到 /etc/rsyslog.conf 下的:56行,并在54添加local2.none ,打开15,16 行的注释;
54 *.info;mail.none;authpriv.none;cron.none;local2.none /var/log/messages
55
56 local2.* /var/log/haproxy.log
完成后重启rsyslog服务,日志信息会记录在: /var/log/haproxy.log中
haproxy 默认配置文件(主要部分):
在60,61 行添加后台认证,65 修改为80端口(回合httpd冲突,记得关掉),66~69 75~80 都注释掉 , 83之后为后端信息;
60 stats uri /status
61 stats auth admin:westos
65 frontend main *:80
83 backend app
84 balance roundrobin
85 server app1 172.25.12.2:80 check
86 server app2 172.25.12.3:80 check
配置完成后 重启haproxy;使用 netstat -antlupe | grep 80 查看端口;
在主机中curl server1 看是否能够完成负载均衡: curl 172.25.12.1
配置完成后能够在真实主机中完成负载均衡:
2.haproxy中开启两个后端,编辑后端服务器的服务类型:
server1中/etc/haproxy/haproxy.cfg 配置如图:
配置完成后重启haproxy;
在172.25.12.1/status可以查看到两个不同的后端信息;
在server2中的 /var/www/html/建立目录haha并放入vim.jpg文件;
网页直接访问源地址可以看见: 172.25.12.2/haha/vim.jpg;
使用haproxy负载均衡时haproxy也能自动掉转相应的后端;
3.haproxy的后台备用服务器backup:(当所有后端服务器都不能使用时,让haproxy主机成为后端服务器,backup正常时不会出现,只有没有可用的服务器时才会出现;)
配置文件信息:
83 backend app
84 balance roundrobin
85 server app1 172.25.12.2:80 check
86 server app2 172.25.12.3:80 check
87 server backup 127.0.0.1:8080 backup
修改httpd 的端口为8080,并开启,设定默认发布页面;
断掉所有可用后端(systemctl stop httpd);backup会自动启用;
结果:
4.haproxy的访问权限控制:(lvs不具备该功能)
4-1 :acl黑名单:
acl黑名单:
acl blacklist src 172.25.12.2 (也可以写网段172.25.12.0/24)
tcp-request content reject if blacklist
vim haproxy.cfg 配置文件:
frontend main *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
acl blacklist src 172.25.254.102
tcp-request content reject if blacklist
use_backend static if url_static
default_backend app
在172.25.254.102中访问haproxy服务器时会被限制:
4-2:
白名单同理:
acl blacklist src 172.25.12.2 (也可以写网段172.25.12.0/24)
tcp-request content accept if blacklist (名单中的接受)
tcp-request content reject (其余的拒绝)
当访问固定路径时拒绝:
acl blacklist src 172.25.254.0
acl denyjpg path /images/vim.jpg (缺省路径:/varwww/html/)
http-request deny if denjpg blacklist
屏蔽名单中的ip(访问结果为403 forbidden):
block if blacklist
结果重定向:
errorloc 403 http://www.baidu.com (当访问结果为错误403时,重定向到百度)
redirect location http://www.baidu.com if blacklist(直接重定向,如果在blacklist中)
4-3 读写分离:
在server1中,vim/etc/haproxy/haproxy.cfg:
acl write method PUT
acl write method POST
use_backend static if write (如果有wtite,则使用static后端)
需要两个php页面(一个展示页面,一个上传页面)放在server2的/var/www/html/下,单纯的访问(读操作)会使用server1后端,如果访问时需要写(上传)操作,haproxy会控制跳转到server4的上传页面,使用static后端(server4)来上传,上传的文件会放到server4中;
5.haproxy高可用的实现:
keepalived对服务资源没有监控(如 后端服务器的vip掉了之后,keepalived没反应,用户访问会出现错误);
haproxy负载均衡时当haproxy client出现问题时服务会瘫痪,因此需要haproxy实现高可用(需要两台主机server1和server4):
5-1:在server1和server4中配置高可用yum源,并安装所需服务:
5-2:
安装pacemaker后系统会创建hacluster用户,因此需要给其设定密码(server1和server4都需要);
echo westos | passwd --stdin hacluster
设定密码后需要进行集群认证: pcs cluster auth server1 server4 ;Username: hacluster Password: westos
5-3:
认证完成之后组建集群:pcs cluster setup --name mycluster server1 server4;
接着手动开启cluster: pcs cluster start --all (可以设定其自启动:pcs cluster enable --all);
可以通过 pcs status 查看是否部署完成;(成功标志: Online server1 server4);
此时会有warning警告:通过 crm_verify -LV 查看详细信息;
使用命令暂时关掉:pcs property set stonith-enable=false;再次查看:crm_verify -LV 无错误,pcs status 无警告信息;
去掉警告信息后,可以看见Online server1 ,server4 ,表示两个结点实现同步;
5-4:
pcs status corosync 查看心跳状态;
pcs resource providers 查看资源提供;
pcs resource describe 查看资源模块;
5-5:
pcs resource create vip ocf: heartbeat:IPaddr2 ip=172.25.254.200 op monitor interval=30s ; 使用pcs调用heartbeat:IPaddr2创建虚拟资源vip;
pcs status;查看是否成功;
关掉server1后(stop peacemaker),资源会移到server4上,重启之后,资源不会回到server1上;
在server4中查看,资源已经started server4:
6.集群部署haproxy:
6-1:
systemctl stop haproxy 在两台机器上,server4可以复制server1上的haproxy,配置文件集群资源不能手工调度,交给集群处理;
6-2:
pcs resource agents ; 可以查看pcs 资源代理;
pcs resource create haproxy systemd:haproxy op monitor interval=30s ; 每隔30s检测一次;
6-3:
pcs resource group add hagroup vip haproxy 实现资源整合和顺序控制
(两个功能:1.将vip和haproxy捆绑在一起,2,让 vip 和haproxy 按顺序启动);
在server14上: pcs node standby ;在14上不支持任何资源,将资源切到另一个结点上;
停掉server1上的 haproxy 后,会30s检测,并重新开启:
在server4上执行了 pcs node standby,因此所有的资源都在server1上,pcs status还可以看见资源详细状况:
7:使用源码部署nginx
nginx:官网: www.nginx.org;
部署nginx: 源码文件:nginx-1.18.0.tar.gz ,tar zxf nginx-1.18.0.tar.gz ;
cd nginx-1.18.0/
./configure --prefix=/usr/local/nginx --with-http_ssl_module
报错:缺少c 编译 器: yum install gcc -y ; 继续:./configure --prefix=/usr/local/nginx --with-http_ssl_module ;
报错:; yum install prce-devel (devel 表示库,缺少文件时,加devel);继续:./configure --prefix=/usr/local/nginx --with-http_ssl_module ;
报错缺少OpenSSL : yum install openssl-devel ;
完成时可以:cd /usr/local/nginx 查看;
make ;
make install;
可以在 ./configure 时去掉debug,让nginx更纯净(更小): 在nginx-1.18.0/中: cd auto/cc/ , vim gcc : /debug 注释掉debug下面的一行;
源码编译三部曲: ./configure , make , make install ;
部署完成后 cd /usr/local/nginx/sbin , echo $PATH , cd , vim ,bash_profile : PATH=****/bin:/usr/local/nginx/sbin , source .bash_profile 使之生效 ;
无启动脚本, nginx 直接启动 ;
nginx 实现负载均衡 : cd /usr/local/nginx/conf ;
vim nginx.conf :
在http{
upstream westos {
server 172.25.12.2:80;
server 172.25.12.3:80;
}
server {
listen 80;
server_name demo.westos.org;
location / {
proxy_pass http://westos;
}
}
}
(在http 的大括号中最开始 和最后分别填入上述模块;)
nginx -t 检测有无语法错误, 无错误后重启服务,
在hosts文件中作解析: 172.25.12.2: demo.westos.org;
访问域名:demo.westos.org 可以实现负载均衡;