大网数据广告活动
服务器运维问答

Load balancing for Host network is not working when access within the instance

· 52CE 我爱测

I have designed this infrastructure. ECS SG: Ingress: 0.0.0.0/0 Egress: 0.0.0.0/0 ECS Cluster ASG SG: Ingress: 0.0.0.0/0 Egress: 0.0.0.0/0 Now when I SSH to instance EC2 No 1. Sometimes I have access to MySQL, sometimes no: ec2-u…

问题原文

原帖链接 · Daniel Hornik · CC BY-SA 4.0

I have designed this infrastructure.

ECS SG:

  • Ingress: 0.0.0.0/0
  • Egress: 0.0.0.0/0

ECS Cluster ASG SG:

  • Ingress: 0.0.0.0/0
  • Egress: 0.0.0.0/0

Now when I SSH to instance EC2 No 1. Sometimes I have access to MySQL, sometimes no:

ec2-user@EC2No1 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
ERROR 2003 (HY000): Can't connect to MySQL server on 'loadbalancerdomain.co.uk' (110)

ec2-user@EC2No1 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.

ec2-user@EC2No1 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
ERROR 2003 (HY000): Can't connect to MySQL server on 'loadbalancerdomain.co.uk' (110)

ec2-user@EC2No1 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
ERROR 2003 (HY000): Can't connect to MySQL server on 'loadbalancerdomain.co.uk' (110)

ec2-user@EC2No1 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.

Now I do the modification for the infrastructure:

I have no access to MySQL from instance 1, I have access to MySQL from instance 2

Instance 1

ec2-user@EC2No1 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
ERROR 2003 (HY000): Can't connect to MySQL server on 'loadbalancerdomain.co.uk' (110)

ec2-user@EC2No1 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
ERROR 2003 (HY000): Can't connect to MySQL server on 'loadbalancerdomain.co.uk' (110)

ec2-user@EC2No1 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
ERROR 2003 (HY000): Can't connect to MySQL server on 'loadbalancerdomain.co.uk' (110)

Instance 2

ec2-user@EC2No2 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.


ec2-user@EC2No2 $ mysql -u root -hloadbalancerdomain.co.uk -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.

Target group: Task definition network mode: HOST

Question

How Can I fix this?

The problem does not exists with the awsvpc network mode for task definition, but I need to scale instances up to use this mode.

已采纳解答

原帖链接 · MLu · CC BY-SA 4.0

The problem with NLB is that it passes through the client IP. If you’re on ECS1 (e.g. 10.0.0.10) and connect to NLB (10.0.0.20) and it happens to send you back to ECS1’s mysql container it will look like the connection is coming from ECS1.

In other words your mysql client connects from 10.0.0.10 to 10.0.0.20, but mysql thinks it connects from 10.0.0.10, not from 10.0.0.20. In turn the response comes from 10.0.0.10 back to 10.0.0.10. That doesn’t work.

Also: You’ve got 2 mysql databases in two independent containers? How do you expect them to be synchronised? If I INSERT something to the left one and with the next connection NLB sends me to the right one it won’t be there.

If you need a reliable mysql database look at AWS RDS or AWS Aurora. Running databases in containers has a lot of challenges and running master-master setup with all nodes writeable is yet another level of complexity. I’m pretty sure you don’t want to do that.

Hope that helps :)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-02-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。