爛泥:高負載平衡學習haproxy之TCP應用,負載平衡haproxy

來源:互聯網
上載者:User

爛泥:高負載平衡學習haproxy之TCP應用,負載平衡haproxy

本文由ilanniweb提供友情贊助,首發於爛泥行天下

在前幾篇文章中,我們介紹了haproxy的配置參數,而且配置例子都是http協議(7層應用)的。

這篇文章,開始介紹haproxy的4層TCP應用。

想要獲得更多的文章,可以關注我ilanniweb。

一、業務要求

現在我們有兩個要求,這兩個要求的協議都是通過tcp協議的。

1.1 haproxy代理ssh

為了安全起見,要求所有商務服務器都關閉公網的串連,只開放haproxy所在的伺服器,並且其他商務服務器的ssh串連通過haproxy來實現。

實際業務,訪問192.168.5.171的8098連接埠就是訪問192.168.5.174的ssh連接埠。

1.2 haproxy代理mysql

為了安全起見,要求mysql資料庫的串連只能通過內網IP,但是因為使用的是雲資料庫,所以如果公司內部要串連資料庫的話要通過haproxy來實現。

實際業務,訪問192.168.5.171的8099連接埠就是訪問192.168.7.7的3306連接埠。

二、配置haproxy

因為是haproxy的7層和4層混合使用,所以在defaults中,我們不定義haproxy的運行模式。

注意:有關http模式的相關配置參數不要出現在default中。

有關業務要求的TCP 4層應用,我們的haproxy配置如下:

listen 8099

bind 0.0.0.0:8099

mode tcp

server 174_22 192.168.5.174:22 maxconn 1024 weight 5 check inter 2000 rise 2 fall 3

listen 8098

bind 0.0.0.0:8098

mode tcp

server 77_3306 192.168.7.7:3306 maxconn 1024 weight 5 check inter 2000 rise 2 fall 3

整個haproxy的設定檔如下:

grep -vE "^#|^$" haproxy.cfg

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

uid 1005

gid 1005

daemon

defaults

log global

retries 3

option redispatch

maxconn 2000

contimeout 5000

clitimeout 50000

srvtimeout 50000

listen admin_stats

bind 192.168.5.171:1080

mode http

option httplog

maxconn 10

stats refresh 30s

stats uri /stats

stats auth admin:admin

stats hide-version

frontend weblb

bind *:80

acl is_dg hdr_beg(host) dg.test.com

mode http

acl is_ilanni hdr_beg(host) ilanni.test.com

acl is_171 hdr_beg(host) 192.168.5.171

acl is_ip src 192.168.5.140

acl is_port dst_port 8090

use_backend acl if is_171 is_ip

use_backend mui_acl if is_171 is_ip is_port

use_backend dgserver if is_dg

use_backend ilanni if is_ilanni

use_backend 171server if is_171

default_backend backend_default

backend dgserver

balance source

mode http

server web1 192.168.5.171:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

server web2 192.168.5.174:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

server web3 192.168.5.178:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

backend 171server

balance roundrobin

mode http

server dg1 192.168.5.174:80 check

server dg2 192.168.5.178:80 check

backend ilanni

server web1 www.yuanbaopu.com:80 weight 3 check inter 2000 rise 2 fall 3

mode http

backend acl

balance source

mode http

server web1 www.ilanni.com:80 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

backend mui_acl

balance source

mode http

server web1 192.168.5.178:80 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

backend backend_default

server web1 192.168.5.178:8080 weight 3 check inter 2000 rise 2 fall 3

mode http

listen 8090

bind 0.0.0.0:8090

mode http

balance roundrobin

server web1 192.168.5.174:8090 maxconn 1024 weight 5 check inter 2000 rise 2 fall 3

server web2 192.168.5.178:8090 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

listen 8099

bind 0.0.0.0:8099

mode tcp

server 174_22 192.168.5.174:22 maxconn 1024 weight 5 check inter 2000 rise 2 fall 3

listen 8098

bind 0.0.0.0:8098

mode tcp

server 77_3306 192.168.7.7:3306 maxconn 1024 weight 5 check inter 2000 rise 2 fall 3

三、驗證要求

haproxy配置完畢後,我們來驗證haproxy的配置是否正確,如下:

3.1 驗證haproxy代理ssh

現在我們來驗證haproxy代理ssh,在Linux用戶端上使用如下命令:

ssh -p8099 wangxy@192.168.5.171

通過,我們可以很明顯的看出haproxy成功的代理了192.168.5.174的ssh連接埠。

3.2 驗證haproxy代理mysql

現在我們來驗證haproxy代理mysql,在Linux用戶端上使用如下命令:

mysql -P8098 -h192.168.5.171 -uroot –p

通過,我們可以很明顯的看出haproxy成功的代理了192.168.7.7的3306連接埠(也即是mysql)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.