基于cookie会话保持,浏览器有这个cookie值,就访问对应的web后端服务器
[root@centos7 ~]#cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2 info
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 100000
user haproxy
group haproxy
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
# turn on stats unix socket
stats socket /var/lib/haproxy/stats mode 600 level admin
# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option http-keep-alive
option forwardfor except 127.0.0.0/8
timeout connect 300s
timeout client 300s
timeout server 300s
timeout check 5s
maxconn 100000
#---------------------------------------------------------------------
# cookie test
#---------------------------------------------------------------------
listen web_host
bind 10.0.0.17:80
mode http
balance roundrobin #动态权重轮询
log global
cookie SERVER-COOKIE insert indirect nocache
server web1 10.0.0.8:80 cookie web1 check inter 3000 fall 3 rise 5
server web2 10.0.0.18:80 cookie web2 check inter 3000 fall 3 rise 5
效果:
配置后:第一次访问,轮询一台web后端,服务器下载cookie,后面基于cookie访问至固定的服务器,除非清除cookie
[root@centos7 ~]#curl --cookie "SERVER-COOKIE=web2" 10.0.0.17
10.0.0.18
[root@centos7 ~]#curl --cookie "SERVER-COOKIE=web2" 10.0.0.17
10.0.0.18
[root@centos7 ~]#curl --cookie "SERVER-COOKIE=web2" 10.0.0.17
10.0.0.18
[root@centos7 ~]#curl --cookie "SERVER-COOKIE=web2" 10.0.0.17
10.0.0.18
[root@centos7 ~]#curl --cookie "SERVER-COOKIE=web2" 10.0.0.17
10.0.0.18
[root@centos7 ~]#curl --cookie "SERVER-COOKIE=web1" 10.0.0.17
10.0.0.8
[root@centos7 ~]#curl --cookie "SERVER-COOKIE=web1" 10.0.0.17
10.0.0.8
[root@centos7 ~]#curl --cookie "SERVER-COOKIE=web1" 10.0.0.17
10.0.0.8