keepalived+HAProxy代理TCP(使用HAProxy 为TiDB-Server 做负载均衡)
·
目录
一、使用HAProxy 为TiDB-Server 做负载均衡
安装 docker-compose:https://blog.csdn.net/lihongbao80/article/details/102679569
| IP | tidb-server端口 | haproxy端口 | 说明 |
|---|---|---|---|
| 192.168.181.51 | 4000 | 4600 | |
| 192.168.181.54 | 4000 | 4600 | |
| 192.168.181.45 | 虚ip,随机启动在51和54上 |
二、51和54安装 haproxy,相同的操作
1、创建文件夹
mkdir -p /home/haproxy/config
cd /home/haproxy
2、配置haproxy.cfg
cat > config/haproxy.cfg << simon
global
maxconn 10000 # 最大同时10000连接
daemon # 以daemon方式在后台运行
defaults
log 127.0.0.1 local0 debug # [emerg, alert, crit, err, warning, notice, info, debug]
# mode http # 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
retries 3 # 连接后端服务器失败重试次数,超过3次后会将失败的后端服务器标记为不可用。
timeout client 1h # 客户端响应超时 1小时
timeout server 1h # server端响应超时 1小时
timeout connect 1h # 连接server端超时 1小时
timeout check 10s # 对后端服务器的检测超时时间 10秒
listen stats # 定义监控页面
mode http
bind *:1080 # 绑定容器内的1080端口
stats refresh 5s # 每1秒更新监控数据
stats uri /stats # 访问监控页面的uri
stats realm HAProxy\ Stats # 监控页面的认证提示
stats auth admin:654321 # 监控页面的用户名和密码
frontend tidb_front
mode tcp
bind *:4000 # 监听容器内的4000端口
default_backend tidb_back
backend tidb_back
mode tcp
option tcp-check
balance roundrobin
server TiDB-Server-18151 192.168.181.51:4000 check inter 10s rise 3 fall 3 weight 1
server TiDB-Server-18154 192.168.181.54:4000 check inter 10s rise 3 fall 3 weight 1
simon
3、创建 docker-compose.yaml 文件
cat > /home/tidb/haproxy/docker-compose.yaml << eric
version: '2.1'
services:
HAProxy:
image: haproxy:2.0.6
restart: always
container_name: HAProxy
ports:
- 4600:4000 # 宿主机端口:容器内端口
- 1080:1080
volumes:
# 容器与宿主机时间同步
- /etc/localtime:/etc/localtime
- ./config/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
environment:
TIME_ZONE: Asia/Shangha
eric
查看管理界面: http://192.168.181.45:1080/stats
三、在51和54上部署keepalived
1、51上的安装keepalived
sudo zypper install -y keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
simon.li@sinoeyes.com
}
notification_email_from paas-support@sinoeyes.com
smtp_server smtp.exmail.qq.com
smtp_connect_timeout 30
router_id app18151
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
#vrrp_script check_nginx { ##定义监控nginx的脚本
# script "/etc/keepalived/check_nginx.sh"
# interval 1 ######监控时间间隔
# weight -2 ######负载参数
#}
vrrp_instance VI_1 {
state BACKUP
## 网卡名称(需要修改)
interface eth0
virtual_router_id 045 ##需要一致
priority 100 ##(需要修改)
nopreempt ###设置 nopreempt 防止抢占资源
advert_int 1 ######服务器之间的存活检查时间
authentication {
auth_type PASS
auth_pass 18145
}
unicast_src_ip 192.168.181.51 #配置单播的源地址,即本机地址
unicast_peer {
192.168.181.54 #配置单播的目标地址,即对方节点地址,备有多台就配置多个地址
}
virtual_ipaddress {
## 同一网段虚拟IP(需要修改)
192.168.181.45/24
}
# track_script { ######执行监控nginx进程的脚本
# check_nginx
# }
}
2、54上的安装keepalived
sudo yum install -y keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
simon.li@sinoeyes.com
}
notification_email_from paas-support@sinoeyes.com
smtp_server smtp.exmail.qq.com
smtp_connect_timeout 30
router_id app18154
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
#vrrp_script check_nginx { ##定义监控nginx的脚本
# script "/etc/keepalived/check_nginx.sh"
# interval 1 ######监控时间间隔
# weight -2 ######负载参数
#}
vrrp_instance VI_1 {
state BACKUP
## 网卡名称(需要修改)
interface ens160
virtual_router_id 045 ##需要一致
priority 90 ##(需要修改)
nopreempt ###设置 nopreempt 防止抢占资源
advert_int 1 ######服务器之间的存活检查时间
authentication {
auth_type PASS
auth_pass 18145
}
unicast_src_ip 192.168.181.54 #配置单播的源地址,即本机地址
unicast_peer {
192.168.181.51 #配置单播的目标地址,即对方节点地址,备有多台就配置多个地址
}
virtual_ipaddress {
## 同一网段虚拟IP(需要修改)
192.168.181.45/24
}
# track_script { ######执行监控nginx进程的脚本
# check_nginx
# }
}
3、启动keepalived并检查
systemctl start keepalived
[root@dev25 ~]# tcpdump -i ens160 vrrp -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens160, link-type EN10MB (Ethernet), capture size 262144 bytes
09:25:39.085137 IP 192.168.181.51 > 192.168.181.54: VRRPv2, Advertisement, vrid 45, prio 100, authtype simple, intvl 1s, length 20
09:25:40.085152 IP 192.168.181.51 > 192.168.181.54: VRRPv2, Advertisement, vrid 45, prio 100, authtype simple, intvl 1s, length 20
09:25:41.085307 IP 192.168.181.51 > 192.168.181.54: VRRPv2, Advertisement, vrid 45, prio 100, authtype simple, intvl 1s, length 20
09:25:42.085249 IP 192.168.181.51 > 192.168.181.54: VRRPv2, Advertisement, vrid 45, prio 100, authtype simple, intvl 1s, length 20
更多推荐




所有评论(0)