第十八周作业(卢征)
Keepalived软件起初是专为LVS
Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。
所以,Keepalived 一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。
2、编译安装haproxy
1.编译安装lua,因为系统版本低与5.3版本
[root@centos7:~]# curl -R -O http://www.lua.org/ftp/lua-5.4.4.tar.gz [root@centos7:~]# tar zxf lua-5.4.4.tar.gz [root@centos7:~]# cd lua-5.4.4 [root@centos7:~/lua-5.4.4]# make all test [root@centos7:~/lua-5.4.4]# mkdir /usr/local/lua [root@centos7:~/lua-5.4.4]# mv src/ /usr/local/lua #把src下的文件都放到lua之下,与下面安装haproxy是指定的路径要一致。
2.编译安装haproxy
1.安装编译安装工具包 [root@centos7:~]# yum -y install gcc openssl-devel pcre-devel systemd-devel
2.下载并解压安装包, http://www.haproxy.org/download/2.4/src/haproxy-2.4.17.tar.gz #需要上网才能下载,可以先下载到windows,再传到虚拟机 [root@centos7:~]# ll total 3904 -rw-------. 1 root root 1579 Dec 28 2021 anaconda-ks.cfg -rw-r--r-- 1 root root 3623854 Jul 8 14:33 haproxy-2.4.17.tar.gz [root@centos7:~]# tar xf haproxy-2.4.17.tar.gz [root@centos7:~]# cd haproxy-2.4.17 #未提供configure文件,因为已经提供Makefile文件,直接make && make install进行 [root@centos7:~/haproxy-2.4.17]# make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/lua/src/ LUA_LIB=/usr/local/lua/src/ #这里两个路径与之前移动的lua路径要保持一致。 [root@centos7:~/haproxy-2.4.17]# make install PREFIX=/apps/haproxy
3.创建软连接 [root@centos7:~/haproxy-2.4.17]# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/ [root@centos7:~/haproxy-2.4.17]# haproxy -v HAProxy version 2.4.17-9f97155 2022/05/13 - https://haproxy.org/ Status: long-term supported branch - will stop receiving fixes around Q2 2026. Known bugs: http://www.haproxy.org/bugs/bugs-2.4.17.html Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
4.准备service文件, [root@centos7:~/haproxy-2.4.17]# vim /usr/lib/systemd/system/haproxy.service
[Unit] Description=HAProxy Load Balancer After=syslog.target network.target
[Service] ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid ExecReload=/bin/kill -USR2 $MAINPID LimitNOFILE=100000
[Install] WantedBy=multi-user.target [root@centos7:~/haproxy-2.4.17]# systemctl daemon-reload #假造service文件的命令
5.准备启动使用的配置文件 [root@centos7:~/haproxy-2.4.17]# mkdir /etc/haproxy [root@centos7:~/haproxy-2.4.17]# vim /etc/haproxy/haproxy.cfg global maxconn 100000 chroot /apps/haproxy #uid 99 #gid 99 user haproxy group haproxy daemon #nbproc 4 #cpu-map 1 0 #cpu-map 2 1 #cpu-map 3 2 #cpu-map 4 3 pidfile /var/lib/haproxy/haproxy.pid log 127.0.0.1 local2 info
defaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms
listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status #配置状态页的路径 stats auth haadmin:123456 #配置状态页的账号和密码
listen web_port bind 10.0.0.17:80 #此处ip与主机ip一致 mode http log global server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5 [root@centos7:~/haproxy-2.4.17]# mkdir /var/lib/haproxy #创建放置pid的文件夹 [root@centos7:~/haproxy-2.4.17]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy #创建对应的账号。
6.启动服务 [root@centos7:~/haproxy-2.4.17]# systemctl enable --now haproxy [root@centos7:~/haproxy-2.4.17]# systemctl status haproxy ● haproxy.service - HAProxy Load Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2022-07-08 15:30:53 CST; 17s ago ...... [root@centos7:~/haproxy-2.4.17]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:9999 : [root@centos7:~/haproxy-2.4.17]# pstree -p |grep haproxy |-haproxy(2338)---haproxy(2342)---{haproxy}(2343)
3.测试
http://10.0.0.17:9999/haproxy-status,详情看下图
#注意service文件写错,会出现如下报错 [root@centos7:~/haproxy-2.4.17]# systemctl enable --now haproxy Failed to execute operation: Bad message
3、总结haproxy各调度算法的实现方式及其应用场景
其负载均衡的调度算法包括以下几种。
1、roundrobin 动态加权轮询算法,支持运行时调整权重以及慢启动机制;最大支持4095个后端主机;此算法为动态算法,会根据后端主机的启动快慢调整权重。当服务器的处理时间保持平均分配时,此算法是运行最流畅和公平的。
2、static-rr 静态轮询算法,此算法类似于roundrobin,但是不支持运行时调整权重及慢启动机制,每个服务器根据权重轮流使用。此算法对服务器数量没有限制。 3、leastconn 最小连接数算法,连接数最少的服务器优先接收连接。建议用于长会话场景中使用,例如LDAP、SQL等协议,而不适合短会话协议。如HTTP。此算法为动态算法,会根据后端主机的启动快慢调整权重。 4、first 根据服务器在列表中的位置,自上而下进行调度;前面服务器的连接数达到上限,新请求才会分配给下一台服务。一般不设置该调度方法,可以用于测试环境 5、source 源地址hash算法,首先此算法会根据源地址生成hash值,然后报文根据权重分配给后端服务器。回来的报文会根据源地址计算得到的hash来决定使用哪一个后端服务器,此算法目的是确保来自于同一个IP的主机使用同一个后端主机。此算法默认是静态算法,即运行期间修改权重并不会影响算法运行,但是这种方式可通过修改hash-type来调整。 6、uri 此算法会对RI的左半部分或整个uri做hash计算,并除以服务器总权重取模,以后派发至指定的的服务器,适用于后端缓存服务器。该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。 7、url_param 对用户请求的uri的<params>部分中的参数的值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个Backend Server;该算法默认是静态的,所以运行时修改服务器的权重是无效的,但是算法会根据“hash-type”的变化做调整。 8、hdr(name) 对于每个http请求,此处由<name>指定的http首部将会被取出做hash计算; 并由服务器总权重相除以后派发至某挑出的服务器;没有有效值的会被轮询调度;
-
hash-type hash-type算法类型:可作用于所有需要进行hash计算的算法中,在balance 指令中选定与hash 有关的算法,都会受此影响。可在defaults,listen,backend中定义。 配置格式:
4、使用haproxy的ACL实现基于文件后缀名的动静分离"
静态服务器Nginx主机配置
#安装nginx,因为我本地有nginx的rpm包,所以直接安装的是本地的包
[root@localhost ~]# ls
anaconda-ks.cfg Documents f1 ha issue.out Music nginx-1.10.0-1.el7.ngx.x86_64.rpm out phone Public s1 Templates Videos
Desktop Downloads g1 haha mail mysh.sh num passwd Pictures qq shenfen test
[root@localhost ~]# yum install ./nginx-1.10.0-1.el7.ngx.x86_64.rpm
Loaded plugins: fastestmirror, langpacks
Examining ./nginx-1.10.0-1.el7.ngx.x86_64.rpm: 1:nginx-1.10.0-1.el7.ngx.x86_64
./nginx-1.10.0-1.el7.ngx.x86_64.rpm: does not update installed package.
Error: Nothing to do
#配置主页信息
[root@localhost ~]# rm /usr/share/nginx/html/index.html
rm: remove regular file ‘/usr/share/nginx/html/index.html’? y
[root@localhost ~]# vim /usr/share/nginx/html/index.html
<h1>Node2 Static Page</h1>
#启动nginx服务
[root@localhost ~]# nginx
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 64 *:56300 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:20048 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 128 *:42681 *:*
LISTEN 0 100 127.0.0.1:25 *:* 动态服务器Httpd+php主机配置
#yum安装httpd和php
[root@localhost ~]# yum install httpd php
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base | 3.6 kB 00:00:00
Determining fastest mirrors
Package httpd-2.4.6-40.el7.centos.x86_64 already installed and latest version
Package php-5.4.16-36.el7_1.x86_64 already installed and latest version
Nothing to do
#提供php测试页
[root@localhost ~]# vim /var/www/html/index.php
<h1>Node1 Dynamic Server<h1>
<?php
phpinfo();
?>
#启动服务
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 64 *:44421 *:*
LISTEN 0 64 :::40360 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 HAProxy主机配置
#yum安装haproxy
[root@localhost haproxy]# yum install haproxy
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package haproxy-1.5.14-3.el7.x86_64 already installed and latest version
Nothing to do
#修改配置文件
62 #---------------------------------------------------------------------
#在frontend配置段做以下修改
63 frontend main *:80 #将端口更改为80
64 acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets
65 acl url_static path_end -i .jpg .gif .png .css .js
66 acl dynamic path_end -i .php #以.php结尾的定义为dynamic
67 acl static path_end -i .html #以.html结尾定义为static
68 use_backend dyna if dynamic #如果url匹配到dynamic则调度至dyna
69 # use_backend static if url_static #如果url匹配到static则调度至static
70 use_backend static if static
71 default_backend static
72
73 #---------------------------------------------------------------------
74 # static backend for serving up p_w_picpaths, stylesheets and such
75 #---------------------------------------------------------------------
76 backend static #定义后端主机10.1.53.11为static
78 server web2 10.1.53.11:80
79 backend dyna #定义后端主机10.1.0.53为dyna
80 server web1 10.1.0.53:80
81
82 #---------------------------------------------------------------------
#启动服务
[root@localhost haproxy]# systemctl start haproxy
[root@localhost haproxy]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通