简体   繁体   English

在 k8s 集群上使用 proxy_pass 部署 nginx 和节点时出现随机/间歇性 502 网关错误

[英]Random/Intermittent 502 gateway errors with nginx and node deployments using proxy_pass on a k8s cluster

My current configuration is as follows我现在的配置如下

Bare metal cluster.裸机集群。 SELINUX status is off. SELINUX 状态为关闭。

  1. A nginx deployment一个 nginx 部署
  2. A nodejs deployment nodejs 部署

I am serving static content through the ngnix service and the dynamic content using the node service.我通过 ngnix 服务提供静态内容,使用节点服务提供动态内容。 Below is my nginx configuration.下面是我的 nginx 配置。

worker_processes  4;

#error_log  logs/error.log  info;
error_log  /dev/stdout  info;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush      on;

    #keepalive_timeout  0;
    #keepalive_timeout  5;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /dev/stdout  main;


    server {
        listen       1080;
        server_name  localhost $hostname;

        root  /usr/share/nginx/static/;
    
        # static content
        location ~ some-regex {
            alias /usr/share/nginx/static/;
            
            # handle cors see 'NGINX-Cookbook' for production quality
            add_header 'Access-Control-Allow-Origin' '*';
        }

        # forward request to node-service
        location / {
             client_max_body_size 128M;
             proxy_buffer_size 256k;
             proxy_buffers 4 512k;
             proxy_busy_buffers_size 512k;
             proxy_http_version 1.1;
            #  proxy_set_header Connection "";
            #  proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection keep-alive;
             proxy_set_header Host $http_host;
             proxy_cache_bypass $http_upgrade;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #  proxy_socket_keepalive on;
             proxy_pass http://nodeserver:3000;
        }
    }

    include servers/*;
}

In my ingress I am hitting the nginx service.在我的入口中,我正在访问 nginx 服务。 I am able to forward my requests correctly to node service few times and get the proper response but around 50% of the time the request fails with a 502 bad gateway error and I see this error in the nginx pod logs我能够将我的请求正确转发到节点服务几次并获得正确的响应,但大约 50% 的请求失败并出现 502 错误网关错误,我在 nginx pod 日志中看到此错误

[error] 20#20: *187 connect() failed (111: Connection refused) while connecting to upstream, client: 10.44.0.2, server: localhost, request: "GET /path HTTP/1.1", upstream: "http://node-service-clusterip:3000/path", host: "my-nginx-node.example.com" [错误] 20#20:*187 connect() 失败(111:连接被拒绝),同时连接到上游,客户端:10.44.0.2,服务器:本地主机,请求:“GET /path HTTP/1.1”,上游:“http: //node-service-clusterip:3000/path", host: "my-nginx-node.example.com"

I have tried multiple directives from the nginx documentation but to no avail.我尝试了 nginx 文档中的多个指令,但无济于事。 Any help would be much appreciated任何帮助将非常感激

There was a mistake in my kubernetes label selectors.我的 kubernetes 标签选择器有错误。 I was using same selectors for multiple deployments which caused issues in the routing.我在多个部署中使用了相同的选择器,这导致了路由问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM