Kubernetes Ingress 报 504 Gateway Timeout 解决方案

本文记录了一次在Kubernetes集群中遇到的Ingress访问报504 Gateway Timeout错误的调试经历。作者通过检查Pods、Services及Ingress配置,最终定位到网络策略未正确设置的问题,并通过调整NetworkPolicy实现内外网通信,解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记一次惨痛的调试经历

 

在集群上件简单的新建一个命名空间,定一个Deployment,定一个Service,再定一个Ingress来外网访问。

结果当外网访问的时候,一直报 504 Gateway Timeout 的错误。

一开始以为是Pods出了问题,结果随便进入一个容器,curl对应的Service发现一切正常,有返回。所以问题出在Ingress上。

目测应该是Ingress无法访问Service的原因。所以推测应该是某些网络策略没有定义好。于是查K8S文档,发现这么一个文档。

https://kubernetes.io/zh/docs/concepts/services-networking/network-policies/

上面说默认是全部放通的。估计是集群某些设置,导致没有放通,于是使用下面文件配置一下,问题解决!

apiVersion: extensions/v1beta1
kind: NetworkPolicy
metadata:
  name: allow-all
spec:
  ingress:
  - {}
  egress:
  - {}
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress

 

这个是直接放通所有,设置时需谨慎!

### ALB Ingress 导致 504 Gateway Timeout 的排查方法 当 AWS Application Load Balancer (ALB) 出现 `504 Gateway Timeout` 错误时,通常表示请求未能在指定的时间范围内完成处理。以下是可能导致此错误的原因以及相应的排查步骤: #### 可能原因分析 1. **目标组健康检查失败** 如果后端实例未通过 ALB 的健康检查,则流量不会被转发到这些不健康的实例上,从而可能引发超时问题[^1]。 2. **后端服务响应时间过长** 后端服务的处理速度较慢或者存在阻塞逻辑,导致无法及时返回 HTTP 响应给客户端[^2]。 3. **ALB 超时配置不当** 默认情况下,ALB 的空闲连接超时时间为 60 秒。如果后端服务需要更长时间来处理某些请求,则可能会触发超时错误[^3]。 4. **网络延迟或 DNS 解析问题** 客户端与 ALB 或者 ALB 和后端之间的网络可能存在高延迟情况;另外,DNS 缓存也可能影响整体性能表现。 5. **安全组规则限制** 若安全组设置不允许来自 ALB 的入站流量到达 EC2 实例或其他计算资源,则会阻止正常通信并造成超时现象发生[^2]。 --- ### 排查步骤 #### 检查健康状态 确认所有注册至 Target Group 的实例均处于 Healthy 状态。可以通过 AWS Management Console 查看具体详情,并针对任何 Unhealthy 成员采取修复措施以恢复其可用性水平[^1]。 #### 修改 Idle Timeout 设置 调整 ALB 的 idle timeout value 来匹配应用程序的实际需求。例如,在控制台中导航到您的负载均衡器页面,找到对应条目下的“Attributes”,然后更改 “Idle Timeout” 参数值为适合您工作负载的一个合理数值范围之内(比如90秒)[^3]: ```bash aws elbv2 modify-load-balancer-attributes --load-balancer-arn <your-lb-arn> \ --attributes Key=idle_timeout.timeout_seconds,Value=90 ``` #### 更新 Security Groups 配置 确保允许从 ALB 到 backend instances 的必要端口上的双向通讯路径畅通无阻。特别注意的是,默认情况下 ELBs 使用动态源IP地址访问targets ,因此建议将 security group rule 设定得更加宽松一点,即接受来自于`sg-id of the alb itself`的所有tcp connections on required ports instead specifying fixed ip ranges only.[^2] #### 监控日志文件 启用 detailed CloudWatch metrics logging for both application load balancers as well target groups so that you can analyze trends over time regarding request latencies and error rates which could help identify bottlenecks within your architecture setup leading towards gateway timeouts occurrences regularly happening under certain conditions . Also consider enabling access logs at additional cost if needed further investigation into individual requests patterns causing issues during peak loads periods etc..[^1] #### 测试环境验证 创建一个简单的测试场景模拟生产环境中遇到的问题行为模式,逐步排除潜在干扰因素直至定位根本原因所在位置为止 。可以利用 tools like Apache Benchmark (`ab`) or JMeter perform stress testing against different endpoints served via same ingress controller configuration but varying parameters such as concurrency levels , payload sizes et cetera ..[^3] --- ### 示例代码片段 - 自定义 ALB 注解延长超时时间 对于 Kubernetes 用户来说,还可以通过自定义 annotations 来实现对 ALB 行为的一些高级定制化操作,如下所示是如何增加 client-side connection keep-alive duration through appropriate annotation key-value pairings inside respective manifest YAML file before applying changes onto cluster resources : ```yaml apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: example-ingress annotations: alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}' alb.ingress.kubernetes.io/target-group-configuration: '{"healthCheckIntervalSeconds":60,"unhealthyThresholdCount":3}' spec: rules: - host: mydomain.com http: paths: - path: /* backend: serviceName: ssl-redirect servicePort: use-annotation --- apiVersion: v1 kind: Service metadata: name: nginx-service labels: app: nginx annotations: alb.ingress.kubernetes.io/group.name: testgroupnamehere alb.ingress.kubernetes.io/success-codes: '200,201' # Optional depending upon requirement. alb.ingress.kubernetes.io/idle-timeout: '120' # Set custom idle timeout here. status: loadBalancer: {} ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值