微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
backend     Nginx_web
    balance  roundrobin
    server  app1 127.0.0.1:80 check
    server  app2 127.0.0.1:81 check

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             |
+--------------------+

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐