【问题标题】:Configuring a Nginx in front of my front and back end on Kubernetes在 Kubernetes 上我的前端和后端前面配置一个 Nginx
【发布时间】:2017-07-18 08:21:21
【问题描述】:

我在尝试在 kubernetes 中部署我的 Web 应用程序时遇到了问题。

我想在我的后端和前端服务前面模拟使用 nginx 作为反向代理的旧部署。

我的系统中有 3 个部分,nginx,正面和背面。我使用nodePort: 30050 构建了 3 个部署、3 个服务并仅公开了我的 nginx 服务。

事不宜迟,这是我的 nginx.conf:

upstream my-server {
    server myserver:3000;
}

upstream my-front {
    server myfront:4200;
}

server {
    listen 80;

    server_name my-server.com;

    location /api/v1 {
        proxy_pass http://my-server;
    }

    location / {
        proxy_pass http://my-front;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr;
    }        
} 

我尝试在其中一个 pod 中安装 curl 和 nslookup,并尝试在集群内部端点上进行手动请求……眼泪流了下来,一切正常……我几乎是一个配得上云的开发人员。

一切都很顺利......除了 nginx DNS 解析之外的一切。

如果我执行 kubectl exec -it my-nginx-pod -- /bin/bash 并尝试 curl 其他 2 个服务中的 1 个:curl myfront:4200 它可以正常工作。

如果我尝试 nslookup 其中一个,它也可以工作。

在此之后,我尝试在 nginx.conf 中用 Pod IP 替换服务名称。重启 nginx 服务后一切正常。

为什么 nginx 不能正确解析上游名称? 我要疯了。

【问题讨论】:

标签: nginx dns kubernetes


【解决方案1】:

Nginx 缓存解析的 IP。要强制 Nginx 解析 DNS,可以引入一个变量:

location /api/v1 {
    set $url "http://my-server";
    proxy_pass $url;
}

更多细节可以在这个相关的this answer找到。

由于它可能是 Nginx 中的缓存,正如您所描述的,它也可以解释为什么重新启动(或重新加载)Nginx 会解决问题。至少要等一段时间再更改 DNS 条目。

我认为,它与 Kubernetes 无关。前段时间我在 Nginx 缓存 AWS ELB 的 DNS 条目时遇到了同样的问题,它经常更改 IP。

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 2019-09-17
    • 2019-07-19
    • 2020-07-01
    • 2019-10-25
    • 2019-05-10
    • 1970-01-01
    • 2021-01-18
    • 2021-02-03
    相关资源
    最近更新 更多