K8s Nginx代理没有到达Pod

时间:2018-02-19 17:52:54

标签: apache docker nginx proxy kubernetes

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

我的docker容器设置为模仿暴露端口80的LAMP堆栈,在容器内我的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的服务上,我让它选择了部署窗格

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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

同时检查是否

kubectl get endpoints project-legacy-wp

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

如果没有,请检查label下的deployment

spec:
  template:
    metadata:
      labels:
        app: APP_NAME

label的{​​{1}} service中的selector相同:

spec:
  selector:
    app: APP_NAME