K8s Nginx Proxy not reaching Pod

2/19/2018

I am having issues connection my wordpress Pod to an nginx proxy pod that is public. The Main issue I'm having is: *1 connect() failed (111: Connection refused) while connecting to upstream

I have my docker container that is setup to mimic a LAMP stack exposing port 80 and inside the container my apache conf looks like this:

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

From the pod deployment I have the container port set to 80 as well, this is the section of the kubernetes deployment that exposes that port

ports:
- containerPort: 80
  name: http

And on the service for the pod i have it selecting that deployment 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

Finally my nginx proxy looks like this and this is where I am a little shaky. I am not familiar with nginx proxies and I did not set this up. I tried my best to get it similar to the other sites in the cluster

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;
    }
}
-- Pablo Marti Cordero
apache
docker
kubernetes
nginx
proxy

1 Answer

2/19/2018

You need to access the LAMP pod via the linked service with its name which is accessible per DNS (in a common Kubernetes setup). I.e. set

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

Also check if

kubectl get endpoints project-legacy-wp

shows one endpoint, i.e. the internal IP of your pod.

If not, then check that the labels in your deployment under

spec:
  template:
    metadata:
      labels:
        app: APP_NAME

are the same as the labels in the service's selector:

spec:
  selector:
    app: APP_NAME
-- Dominik
Source: StackOverflow