【问题标题】:K8s Nginx Proxy not reaching PodK8s Nginx 代理未到达 Pod
【发布时间】:2018-07-30 00:17:18
【问题描述】:

我在将我的 wordpress Pod 连接到公开的 nginx 代理 pod 时遇到问题。我遇到的主要问题是:*1 connect() failed (111: Connection refused) while connecting to upstream

我的 docker 容器设置为模仿 LAMP 堆栈,暴露端口 80,在容器内我的 apache conf 如下所示:

<VirtualHost *:80>
DocumentRoot /var/www/html
ErrorLog /var/log/error.log
CustomLog /var/log/acces.log combined

<Directory /var/www/html>
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>
</VirtualHost>

在 pod 部署中,我也将容器端口设置为 80,这是 Kubernetes 部署中暴露该端口的部分

ports:
- containerPort: 80
  name: http

在 pod 的服务上,我让它选择部署 pod

apiVersion: v1
kind: Service
metadata:
  name: project-legacy-wp
  labels:
    app: project
    role: legacy-wp
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: project
    role: legacy-wp

最后我的 nginx 代理看起来像这样,这就是我有点不稳定的地方。我不熟悉 nginx 代理,我没有设置它。我尽力让它与集群中的其他站点相似

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen              443 ssl;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         'CIPHERS';
    ssl_prefer_server_ciphers on;
    ssl_certificate     path/to/certs;
    ssl_certificate_key path/to/certs;

    server_name         example.com;

    client_max_body_size 4G;
    keepalive_timeout 10;

    location / {
      access_log on;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_buffering off;
      proxy_pass http://project-legacy-wp;
    }
}

【问题讨论】:

    标签: apache docker nginx proxy kubernetes


    【解决方案1】:

    您需要通过链接的service 访问 LAMP pod,其名称可通过 DNS 访问(在常见的 Kubernetes 设置中)。 IE。设置

    server {
        # ...
        location / {
          # ... 
          proxy_pass http://project-legacy-wp;
        }
    }
    

    同时检查是否

    kubectl get endpoints project-legacy-wp
    

    显示一个端点,即您的 pod 的内部 IP。

    如果没有,请检查您的deployment 下的labels

    spec:
      template:
        metadata:
          labels:
            app: APP_NAME
    

    serviceselector中的labels相同:

    spec:
      selector:
        app: APP_NAME
    

    【讨论】:

    • 我目前有这样的设置。我编辑了服务器以具有不同的 pod 名称。有没有不同的方法来检查它是否可以通过服务访问?我对 k8s 很陌生,这有点丢给我
    • @PabloMarti 哦,好的。然后按照我更新的答案中的说明检查endpoints 和selector
    • 感谢您的帮助,但您所说的似乎都是正确的。但是我仍然遇到超时错误。
    • @PabloMarti 所以,你得到的是“超时”而不是“连接被拒绝”?那听起来更像是防火墙问题。在您的主机上尝试curl -v ENDPOINT_IP
    • * 正在尝试 10.55.253.194... * TCP_NODELAY 设置 * 已连接到 10.55.253.194 (10.55.253.194) 端口 80 (#0) > GET / HTTP/1.1 > 主机:10.55.253.194 > 用户-代理:curl/7.52.1 > 接受:/
    猜你喜欢
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    相关资源
    最近更新 更多