gpt4 book ai didi

nginx - nginx 中的动态 proxy_pass 到 Kubernetes 中的另一个 pod

转载 作者:行者123 更新时间:2023-12-02 00:42:00 24 4
gpt4 key购买 nike

我正在尝试创建一个 nginx 代理,将请求转发到 /<service>http://<service> 。我首先尝试了以下方法:

location ~ ^/(.+)$ {
set $backend "http://$1:80";
proxy_pass $backend;
}

但它无法说出类似的话(当调用 /myservice 时):

[error] 7741#0: *1 no resolver defined to resolve http://myservice

myservice无法从外部访问我尝试安装 go-dnsmasq作为同一个 pod 中的 sidecar,我尝试使用它进行 DNS 解析(就像我在 this 示例中看到的那样)并将我的 nginx 配置更改为如下所示:

location ~ ^/(.+)$ {
resolver 127.0.0.1:53;
set $backend "http://$1:80";
proxy_pass $backend;
}

但是现在 nginx 失败了:

[error] 9#9: *734 myservice could not be resolved (2: Server failure), client: 127.0.0.1, server: nginx-proxy, request: "GET /myservice HTTP/1.1", host: "localhost:8080"
127.0.0.1 - xxx [30/May/2016:10:34:23 +0000] "GET /myservice HTTP/1.1" 502 173 "-" "curl/7.38.0" "-"

我的 Kubernetes Pod 如下所示:

spec:
containers:
- name: nginx
image: "nginx:1.10.0"
ports:
- containerPort: 8080
name: "external"
protocol: "TCP"
- name: dnsmasq
image: "janeczku/go-dnsmasq:release-1.0.5"
args:
- --listen
- "0.0.0.0:53"

正在运行netstat -ntlp在 dnsmasq 容器中给了我:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN -
tcp 0 0 :::53 :::* LISTEN 1/go-dnsmasq

并且正在运行nmap --min-parallelism 100 -sT -sU localhost在 nginx 容器中:

Starting Nmap 6.47 ( http://nmap.org ) at 2016-05-30 10:33 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00055s latency).
Other addresses for localhost (not scanned): 127.0.0.1
Not shown: 1997 closed ports
PORT STATE SERVICE
53/tcp open domain
8080/tcp open http-proxy
53/udp open domain

看来 dnsmasq 和 nginx 确实已启动并正在运行?我可能做错了什么?

最佳答案

经过大量研究和反复试验,我设法解决了这个问题。首先,我将 pod 规范更改为:

spec:
containers:
- name: nginx
image: "nginx:1.10.0"
ports:
- containerPort: 8080
name: "external"
protocol: "TCP"
- name: dnsmasq
image: "janeczku/go-dnsmasq:release-1.0.5"
args:
- --listen
- "127.0.0.1:53"
- --default-resolver
- --append-search-domains
- --hostsfile=/etc/hosts
- --verbose

然后我还必须禁用 nginx 中解析器的 ipv6:

location ~ ^/(.+)$ {
resolver 127.0.0.1:53 ipv6=off;
set $backend "http://$1:80";
proxy_pass $backend;
}

然后它就按预期工作了!

关于nginx - nginx 中的动态 proxy_pass 到 Kubernetes 中的另一个 pod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37523615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com