其他分享
首页 > 其他分享> > haproxy服务代理: tcp层, http层

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

标签:haproxy,http,--,c7,tcp,docker,root1
来源: https://blog.csdn.net/eyeofeagle/article/details/111664043