haproxy可以基于4层也可以基于7层的代理
下面的例子是四层代理的例子,基于四层代理的话,当外网访问www.xxx.com域名的时候,DNS会把用户的请求发送到这台haproxy服务的机器上,haproxy根据请求的端口80,443端口把数据流转发到内网五台机器上,由于我们是ranhcer管理的k8s集群,所以需要在rancher的负载均衡上设置kong的ingress的域名为www.xxx.com,这样当数据流转发到k8s集群后,就会被ingress转发到k8s里面的kong网关服务。
当数据上抛www.xxx.com:1883的时候,数据流到haproxy服务匹配到emq,就会把数据转发到后台的6667的nodeport端口的机器
yum -y install haproxy
# 配置文件位置
/etc/haproxy/haproxy.cfg
#重启haproxy
systemctl restart haproxy
# 查看状态
systemctl status haproxy
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 30000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode tcp
log global
option tcplog
option dontlognull
#option http-server-close
#option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 30000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend http
bind *:80
default_backend http
frontend https
bind *:443
default_backend https
frontend emq
bind *:1883
default_backend emq
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend http
balance source
#balance leastconn
server k8s-worker01 192.74.158.100:80 check inter 1000
server k8s-worker02 192.74.158.97:80 check inter 1000
server k8s-worker03 192.74.158.79:80 check inter 1000
server k8s-worker04 192.74.158.101:80 check inter 1000
server k8s-worker05 192.74.158.80:80 check inter 1000
backend https
balance source
#balance leastconn
server k8s-worker01 192.74.158.100:443 check inter 1000
server k8s-worker02 192.74.158.97:443 check inter 1000
server k8s-worker03 192.74.158.79:443 check inter 1000
server k8s-worker04 192.74.158.101:443 check inter 1000
server k8s-worker05 192.74.158.80:443 check inter 1000
backend emq
balance source
#balance leastconn
server k8s-worker01 192.74.158.100:6667 check inter 1000
server k8s-worker02 192.74.158.97:6667 check inter 1000
server k8s-worker03 192.74.158.79:6667 check inter 1000
server k8s-worker04 192.74.158.101:6667 check inter 1000
server k8s-worker05 192.74.158.80:6667 check inter 1000
这篇文章整理得不错,https://www.cnblogs.com/passzhang/p/12090657.html