第十八周作业

一、简述keepalived工作原理

  Keepalived是以VRRP协议为基础实现的,VRRP是虚拟冗余协议。
  VRRP(虚拟冗余协议)是路由交换网络中为了解决单点网关设备出现故障造成网络瘫痪而出现的技术。将多台提供相同功能的路由器虚拟成一台路由器,在这些路由器中,实际只有一个master设备作为实际的网络数据的转发,其它作为backup设备提供冗余。当master设备出现故障时,backup设备通过vrrp的优先级再选举出一个master设备提供网关服务。
  Keepalived主要有core、check和vrrp三个模块。core模块是Keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查。vrrp模块实现VRRP协议。
    VRRP协议保证了主备机之间的高可用;Keepalived封装了IPVS,对后端服务器进行健康性检查,保证后端服务器的高可用。

二、编译安装haproxy

# 编译安装lua5.3版本
[root@centos7-mini2 ~]# yum -y install gcc readline-devel
[root@centos7-mini2 ~]# wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
[root@centos7-mini2 ~]# tar xf lua-5.3.5.tar.gz -C /usr/local/src/
[root@centos7-mini2 ~]# cd /usr/local/src/lua-5.3.5/
[root@centos7-mini2 lua-5.3.5]# make linux test
[root@centos7-mini2 lua-5.3.5]# src/lua -v
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio

# 编译安装haproxy
[root@centos7-mini2 ~]# yum -y install gcc openssl-devel pcre-devel systemd-devel
[root@centos7-mini2 ~]# ls             // 准备好源码文件
haproxy-2.2.25.tar.gz
[root@centos7-mini2 ~]# tar xf haproxy-2.2.25.tar.gz -C /usr/local/src
[root@centos7-mini2 ~]# cd //usr/local/src/haproxy-2.2.25/
[root@centos7-mini2 haproxy-2.2.25]# 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/src/lua-5.3.5/src LUA_LIB=/usr/local/src/lua-5.3.5/src
[root@centos7-mini2 haproxy-2.2.25]# make install PREFIX=/apps/haproxy
[root@centos7-mini2 haproxy-2.2.25]# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/
[root@centos7-mini2 haproxy-2.2.25]# which haproxy 
/usr/sbin/haproxy
[root@centos7-mini2 haproxy-2.2.25]# haproxy -v
HA-Proxy version 2.2.25-50b5f5d 2022/07/28 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2025.
Known bugs: http://www.haproxy.org/bugs/bugs-2.2.25.html
Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64

# 准备HAProxy启动文件
[root@centos7-mini2 ~]# cat /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-mini2 ~]# mkdir /etc/haproxy
[root@centos7-mini2 ~]# cat /etc/haproxy/haproxy.cfg
global
        maxconn 100000
        chroot /apps/haproxy
        stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
        #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 admin:wuhaolam
listen web_port
        bind 192.168.119.147:80
        mode http
        log global
        server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5

# 启动haproxy
[root@centos7-mini2 ~]# mkdir /var/lib/haproxy            // 准备socket文件目录
[root@centos7-mini2 ~]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy              设置用户和目录权限
[root@centos7-mini2 ~]# systemctl enable --now haproxy
[root@centos7-mini2 ~]# systemctl status haproxy.service     
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-07-29 21:10:34 CST; 2s ago
  Process: 3166 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
 Main PID: 3169 (haproxy)
   CGroup: /system.slice/haproxy.service
           ├─3169 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
           └─3174 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid

Jul 29 21:10:34 centos7-mini2 systemd[1]: Starting HAProxy Load Balancer...
Jul 29 21:10:34 centos7-mini2 systemd[1]: Started HAProxy Load Balancer.
Jul 29 21:10:34 centos7-mini2 haproxy[3169]: [NOTICE] 209/211034 (3169) : New worker #1 (3174) forked
Jul 29 21:10:34 centos7-mini2 haproxy[3169]: [WARNING] 209/211034 (3174) : Server web_port/web1 is DOWN, reason: Layer4 connection problem, in...n queue.
Jul 29 21:10:34 centos7-mini2 haproxy[3169]: [NOTICE] 209/211034 (3174) : haproxy version is 2.2.25-50b5f5d
Jul 29 21:10:34 centos7-mini2 haproxy[3169]: [NOTICE] 209/211034 (3174) : path to executable is /usr/sbin/haproxy
Jul 29 21:10:34 centos7-mini2 haproxy[3169]: [ALERT] 209/211034 (3174) : proxy 'web_port' has no server available!
Hint: Some lines were ellipsized, use -l to show in full.

查看haproxy的状态页面
image
image

三、总结haproxy各调度算法的实现方式及其应用场景

3.1 static-rr

基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其他值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的wrr。
适用于做了session共享的web集群。

  listen web_host
    bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
    mode http
    log global
    balance static-rr
    server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.119.138:80 weight 2 check inter 3000 fall 2 rise 5

3.2 first

根据服务在列表中的位置,自上而下进行调度,但是只会当第一台服务器的连接数达到上限时,新请求才会分配给下一台服务器,因此会忽略服务器的权重配置,使用较少。
不支持使用socat进行动态修改权重值,可以设置0和1,若设置成其他值没有效果。

listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance first
  server web1 192.168.119.128:80 maxcoon 2 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

3.3 roundrobin

基于权重的轮询调度算法,支持权重在运行时的调整,不同于LVS中的rr轮询模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),每个后端backend中最多支持4095个real server,支持对real server权重动态调整,roundrobin为默认调度算法,使用广泛。

listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance roundrobin
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 2 check inter 3000 fall 2 rise 5

3.4 leastconn

加权的最少连接的动态算法,支持权重的运行时调整和慢启动。根据后端服务器当前的连接数最少而不是权重进行调度(新客户端连接时),比较适合长连接的场景使用,如MySQL等场景。

listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance leastconn
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

3.5 random

负载平衡算法,基于随机数作为一致性hash的key,适用于大型服务器场所或经常添加或删除服务器时,支持weight的动态调整,weight较大的主机有更大概率获取新请求。

listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance random
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

3.6 source

源地址hash,基于用户的源地址进行hash将请求转发到后端服务器,后续同一个源地址请求将被转发到同一个后端服务器。使用此方式,当后端服务器数据量发生变化时,会导致很多用户的请求转发到新的后端服务器,默认为静态方式,但可通过hash-type选项进行更改。

一般在不插入cookie的TCP模式下使用,也可给不支持会话cookie的客户提供会话保持,适用于session会话保持但不支持cookie和缓存的场景,如基于客户端公网IP的会话保持。

源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash

3.7 Uri

对用户所请求的URI的左半部分或整个uri做hash,再将hash结果对权重进行取模后,根据最终结果将请求转发到后端指定服务器,适用于后端是缓存服务器场景,默认是静态算法,也可以通过hash-type指定map-based和consistent来定义是使用取模法还是一致性hash。
该算法基于应用层,只支持mode http,不支持mode tcp。

# uri取模法配置实例
listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance uri
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

# uri一致性hash配置实例
listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance uri
  hash-type consistent
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

3.8 url_param

对用户请求的URL中的params部分中的一个参数key对应的value值作hash计算,并由服务器总权值相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server,如果没有key,将按roundrobin算法。
实现session保持。

# url_param取模法配置实例
listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance url_param userid      // url_param hash
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

# uri一致性hash配置实例
listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance url_param userid       // 对url_param的值取hash
  hash-type consistent
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

3.9 hdr

针对用户每个http头部(header)请求中的指定信息做hash,此处由name指定的http首部将会被取出并做hash计算,然后由服务器总权重取模以后派发至挑出的服务器,如果无有效值,则会使用默认的轮询调度。

# url_param取模法配置实例
listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance hdr(User-Agent)
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

# uri一致性hash配置实例
listen web_host
  bind 192.168.119.147:80,:8801-8810,192.168.119.147:9001-9010
  mode http
  log global
  balance hdr(User-Agent)
  hash-type consistent
  server web1 192.168.119.128:80 weight 1 check inter 3000 fall 2 rise 5
  server web2 192.168.119.138:80 weight 1 check inter 3000 fall 2 rise 5

对Windows远程桌面的负载,使用cookie保持会话,默认是静态,也可以通过hash-type指定map-based和consistent来定义使用取模法还是一致性hash。

# rdp-cookie 取模法配置示例
listen RDP
  bind 192.168.119.147:3389
  balance rdp-cookie
  mode tcp
  server rdp0 192.168.119.128:3389 check fall 3 rise 5 inter 2000 weight 1

# rdp-cookie 一致性hash配置示例
listen RDP
  mode tcp
  bind 192.168.119.147:3389
  balance rdp-cookie
  hash-type consistent
  server rdp0 192.168.119.128:3389 check fall 3 rise 5 inter 2000 weight 1

四、使用haproxy的ACL实现基于文件后缀名的动静分离"

4.1 安装haproxy

  参见第一题。

4.2 编写子配置文件,并使之生效

# 创建子配置目录
[root@centos7-mini2 ~]# mkdir /etc/haproxy/conf.d

# 创建子配置文件,必须要以.cfg结尾
[root@centos7-mini2 ~]# cat /etc/haproxy/conf.d/test.cfg
frontend wuhao_http_port
	bind 192.168.119.147:80
	mode http
	balance roundrobin
	log global
	option httplog

	acl acl_static path_end -i .jpg .jpeg .png .gif .css .js
	acl acl_active path_end -i .html

	use_backend mobile_hosts if acl_static
	use_backend pc_hosts if acl_active
	default_backend pc_hosts

backend mobile_hosts
	mode http
	server web1 192.168.119.128:80 check inter 2000 fall 3 rise 5

backend pc_hosts
	mode http
	server web2 192.168.119.138:80 check inter 2000 fall 3 rise 5


# 添加子配置文件到util文件中
[root@centos7-mini2 ~]# cat  /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  -f /etc/haproxy/conf.d/ -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/   -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target

[root@centos7-mini2 ~]# [root@centos7-mini2 ~]# systemctl restart haproxy.service

4.3 配置后端真实服务器

[root@Rocky8-mini ~]# yum -y install httpd
[root@Rocky8-mini ~]# systemctl enable --now httpd.service
[root@Rocky8-mini ~]# cd /var/www/html/
[root@Rocky8-mini html]# ls
wu.jpg

[root@Rocky8-mini2 ~]# yum -y install httpd
[root@Rocky8-mini2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@Rocky8-mini2 ~]# vim /var/www/html/test.html
192.168.119.138

4.4 验证

[root@centos7-mini2 ~]# curl 192.168.119.147/test.html
192.168.119.138
posted @ 2022-07-31 23:39  wuhaolam  阅读(24)  评论(0编辑  收藏  举报