在Kubernetes环境中有关Nginx Ingress与API Gateway的连接问题

文章目录

小结

在Kubernetes环境中是通过Nginx Ingress来从外部访问Kubernetes内部的环境,并用API Gateway来分发请求,碰到了 502 Bad gateway.的问题,并尝试解决。

问题

从外部通过Nginx Ingress访问Kubernetes内部的环境API Gateway,返回错误: 502 Bad gateway. 这里API Gateway也起到了Load Balancer的作用。

shell 复制代码
[john@Node1 ~]$ curl -H 'Host:apigw.com' http://192.168.18.16:80/Test Application/process
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.25.1</center>
</body>
</html>

查看Nginx运行的实例:

shell 复制代码
[john@Master ~]$ kubectl get pods -o wide -A | tail -n 2
nginx-ingress    nginx-ingress-c46vc                      1/1     Running   0          31d     10.244.3.163     Node1   <none>           <none>
nginx-ingress    nginx-ingress-hvqjg                      1/1     Running   0          31d     10.244.4.164     Node2   <none>           <none>

查看错误日志,

shell 复制代码
[john@Master ~]$ kubectl logs -f nginx-ingress-hvqjg -n nginx-ingress

可以看到类似以下错误:connect() failed (111: Connection refused) while connecting to upstream

查看Nginx的配置文件:

shell 复制代码
[john@Master ~]$ kubectl exec -it nginx-ingress-c46vc -n nginx-ingress -- /bin/bash
nginx@nginx-ingress-hvqjg:/$ cat /etc/nginx/nginx.conf
nginx@nginx-ingress-hvqjg:/$ cat /etc/nginx/conf.d/default-apigw-ingress.conf

解决

通常情况下需要去查找后台服务的问题,有可能是后台服务没有正常启动所导致的连接问题。

确认了后台服务没有问题后,那么需要去看Nginx Ingress的配置问题。

以下是正确Ngnix Ingress配置示例 (在Nginx Ingress的pod中):

shell 复制代码
nginx@nginx-ingress-hvqjg:/$ cat /etc/nginx/conf.d/default-apigw-ingress.conf
# configuration for default/apigw-ingress

upstream default-apigw-ingress-apigw.com-apigw-service-80 {
	zone default-apigw-ingress-apigw.com-apigw-service-80 256k;
	random two least_conn;
	
	server 10.244.3.169:8090 max_fails=1 fail_timeout=10s max_conns=0;
	server 10.244.4.171:8090 max_fails=1 fail_timeout=10s max_conns=0;
	
}
server {
	
	listen 80;

	server_tokens on;

	server_name apigw.com;

	set $resource_type "ingress";
	set $resource_name "apigw-ingress";
	set $resource_namespace "default";

	
	location / {
		set $service "apigw-service";
		
		proxy_http_version 1.1;

		proxy_connect_timeout 60s;
		proxy_read_timeout 60s;
		proxy_send_timeout 60s;
		client_max_body_size 1m;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Host $host;
		proxy_set_header X-Forwarded-Port $server_port;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_buffering on;
		
		proxy_pass http://default-apigw-ingress-apigw.com-apigw-service-80/;
		
	}
	location /eureka {
		set $service "eureka-lb";
		
		proxy_http_version 1.1;
		
		proxy_connect_timeout 60s;
		proxy_read_timeout 60s;
		proxy_send_timeout 60s;
		client_max_body_size 1m;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Host $host;
		proxy_set_header X-Forwarded-Port $server_port;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_buffering on;
		
		
		proxy_pass http://default-apigw-ingress-apigw.com-eureka-lb-80/;
		
		
	}
	
	
}

参考

Stackoverflow: 502 Bad gateway when trying to connect to backend
Nginx Ingress Installation with Manifests
server fault: connect() failed (111: Connection refused) while connecting to upstream
stackoverflow: Why does attempting to connect to my ingress show connection refused?
CSDN: nginx报错:connect() failed (111: Connection refused) while connecting to upstream
CSDN: Nginx报502错误,日志connect() failed (111: Connection refused) while connecting to upstream的个人有效解决方案
Kubernetes Ingress

相关推荐
码里法7 小时前
centos安装nginx并配置https完整版
nginx·https·centos
zly35007 小时前
linux查看正在运行的nginx的当前工作目录(webroot)
linux·运维·nginx
fbllfbll8 小时前
Alpine下部署Nginx+MAZANOKE在线批量压缩图片
服务器·nginx·pve·alpine·lxc容器·在线压缩图片·mazanoke
木风小助理9 小时前
PostgreSQL 的范式跃迁:从关系型数据库到统一数据平台
服务器·云原生·kubernetes
irisart15 小时前
第二章【NGINX 开源功能】—— 七层反向代理(上)
运维·nginx
企鹅侠客16 小时前
探索Kubernetes的ServiceAccounts
云原生·容器·kubernetes
single3716 小时前
Nginx 生产环境平滑升级实战:从 1.24.0 到 1.28.0 的零宕机操作全记录
nginx
虫小宝17 小时前
淘客app容器化部署方案:Docker与Kubernetes在返利系统中的实践
docker·容器·kubernetes
bs_10117 小时前
k8s工作运维中常用命令
运维·容器·kubernetes
虫小宝17 小时前
电商返利APP容器编排实践:K8s在多环境部署中的资源调度优化
云原生·容器·kubernetes