haproxy服务代理: tcp层, http层

1,基本配置使用: listen, fronted/backend

####################### tcp代理环境准备 
[root1@c7-docker ~]# docker run -it -d --name mysql1 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 mysql:5.5
[root1@c7-docker ~]# docker run -it -d --name mysql2 -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 mysql:5.5
[root1@c7-docker ~]# mysql -uroot -p123456 -P3306  --protocol=tcp
MySQL [(none)]> create database db3306;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> Bye
[root1@c7-docker ~]# mysql -uroot -p123456 -P3307  --protocol=tcp
MySQL [(none)]> create database db3307;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> Bye

####################### http代理环境准备  
[root1@c7-docker ~]# docker run -d --name nginx1 -p 80:80 nginx
[root1@c7-docker ~]# docker run -d --name nginx2 -p 81:80 nginx
[root1@c7-docker ~]# docker exec -it nginx1 bash -c 'echo this is nginx1:80 > /usr/share/nginx/html/index.html'
[root1@c7-docker ~]# curl localhost:80
this is nginx1:80
[root1@c7-docker ~]# docker exec -it nginx2 bash -c 'echo this is nginx2:81 > /usr/share/nginx/html/index.html'
[root1@c7-docker ~]# curl localhost:81
this is nginx2:81

#######################配置并启动haproxy
[root1@c7-docker ~]# yum -y install haproxy
[root1@c7-docker ~]# vi /etc/haproxy/haproxy.cfg
defaults
  ......
#把默认的frontend, backend都注释掉,添加以下tcp代理内容
frontend web *:88
    default_backend nginx_web
    #default_backend nginx_web_cookie
backend  nginx_web
    balance     roundrobin
    server  app1 127.0.0.1:80 check
    server  app2 127.0.0.1:81 check
backend  nginx_web_cookie
    balance      leastconn
    cookie  my_nginx_cookie insert nocache
    server  app1 127.0.0.1:80 check cookie app1
    server  app2 127.0.0.1:81 check cookie app2

listen mysql1
    bind 0.0.0.0:3333
    mode tcp
    balance roundrobin
    server s1 localhost:3306 weight 1 maxconn 10000 check inter 10s
    server s2 localhost:3307 weight 1 maxconn 10000 check inter 10s


#启动haproxy
[root1@c7-docker ~]# systemctl start haproxy
[root1@c7-docker ~]# ss -nltp |grep hapro
LISTEN     0      128          *:88          *:*        users:(("haproxy",pid=17794,fd=5))
LISTEN     0      128          *:3333        *:*        users:(("haproxy",pid=17794,fd=7))

测试代理效果

####################### http代理测试
[root1@c7-docker ~]# curl localhost:88
this is nginx1:80
[root1@c7-docker ~]# curl localhost:88
this is nginx2:81
####################### tcp代理测试
[root1@c7-docker ~]# mysql -uroot -p123456 -P3333  --protocol=tcp  -e "show databases"
+--------------------+
| Database           |
+--------------------+
| db3307             |
+--------------------+
[root1@c7-docker ~]# mysql -uroot -p123456 -P3333  --protocol=tcp  -e "show databases"
+--------------------+
| Database           |
+--------------------+
| db3306             |
+--------------------+

2,haproxy配置cookie

#######################修改配置/etc/haproxy/haproxy.cfg
#把88端口的后台服务改为: nginx_web_cookie, 并重启haproxy
frontend web *:88
    default_backend nginx_web
    #default_backend nginx_web_cookie


#######################查看cookie测试效果    
#curl参数说明
-i, --include
              (HTTP)  Include  the  HTTP-header  in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and
              more...
-b, --cookie <name=data>
              (HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the data previously received from  the  server  in  a  "Set-Cookie:"
              line.  The data should be in the format "NAME1=VALUE1; NAME2=VALUE2".

              If no '=' symbol is used in the line, it is treated as a filename to use to read previously stored cookie lines from, which should be used
              in this session if they match. Using this method also activates the "cookie parser" which will make  curl  record  incoming  cookies  too,
              which  may  be  handy if you're using this in combination with the -L, --location option. The file format of the file to read cookies from
              should be plain HTTP headers or the Netscape/Mozilla cookie file format.
-c, --cookie-jar <file name>
              (HTTP)  Specify to which file you want curl to write all cookies after a completed operation. Curl writes all cookies previously read from
              a specified file as well as all cookies received from remote server(s). If no cookies are known, no file will be written. The file will be
              written using the Netscape cookie file format. If you set the file name to a single dash, "-", the cookies will be written to stdout.


###########查看请求头
[root1@c7-docker ~]# curl -i localhost:88
HTTP/1.1 200 OK
Server: nginx/1.19.3
Date: Fri, 25 Dec 2020 03:17:25 GMT
Content-Type: text/html
Content-Length: 18
Last-Modified: Fri, 25 Dec 2020 02:32:20 GMT
ETag: "5fe54f34-12"
Accept-Ranges: bytes
Set-Cookie: my_nginx_cookie=app2; path=/
Cache-control: private

this is nginx2:81



###########携带cookie访问
[root1@c7-docker ~]# curl  -b /tmp/a.cook -c /tmp/a.cook  localhost:88
this is nginx1:80
[root1@c7-docker ~]# curl  -b /tmp/a.cook -c /tmp/a.cook  localhost:88
this is nginx1:80
[root1@c7-docker ~]# curl  -b /tmp/a.cook -c /tmp/a.cook  localhost:88
this is nginx1:80
[root1@c7-docker ~]# cat /tmp/a.cook
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

localhost       FALSE   /       FALSE   0       my_nginx_cookie app1

猜你喜欢

转载自blog.csdn.net/eyeofeagle/article/details/111664043