title: Cannot access a Kubernetes postgresql service from the outside
body: <p>I'm using Docker 1.10.1 and Kubernetes 1.0.1 on a local bare metal computer with Ubuntu 14.04, and I'm still new on this.
  That computer is the Kubernetes master as well as the node I'm using to make tests (I'm currently making a proof of concept).
  I created a couple of containers (1 nginx and 1 postgresql 9.2) to play with them.</p>
  
  <p>I created the service and pod definitions for the postgresql container as follows:</p>
  
  <pre><code>----- app-postgres-svc.yaml
  
  apiVersion: v1
  kind: Service
  metadata:
    labels:
      name: AppPostgres
    name: app-postgres
  spec:
    ports:
      - port: 5432
    selector:
      app: AppPostgresPod
    type: NodePort
    clusterIP: 192.168.3.105
  
  ----- app-postgres-pod.yaml
  
  apiVersion: v1
  kind: ReplicationController
  metadata:
    name: app-postgres-pod
  spec:
    replicas: 1
    selector:
      app: AppPostgresPod
    template:
      metadata:
        name: app-postgres-pod
        labels:
          app: AppPostgresPod
      spec:
        containers:
        - name: app-postgres-pod
          image: localhost:5000/app_postgres
          ports:
          - containerPort: 5432
  </code></pre>
  
  <p>I also installed HAProxy in order to redirect the requests to ports 80 and 5432 to their corresponding Kubernetes service.
  The following is the HAProxy definition of the frontend and backend for the postgresql redirection:</p>
  
  <pre><code>frontend postgres
          bind *:5432
          mode tcp
          option tcplog        
          default_backend postgres-kubernetes
  
  backend postgres-kubernetes
          mode tcp
          balance roundrobin
          option pgsql-check
          server postgres-01 192.168.3.105:5432 check
  </code></pre>
  
  <p>After I start the kubernetes cluster, services and pods, I first tried hitting the nginx site from the outside and it works like
  a charm. But, when I try to connect to the postgresql service from the outside I get the following error:</p>
  
  <pre><code>Error connecting to the server: server closed the connection unexpectedly
  This probably means the server terminated abnormally before or while processing the request.
  </code></pre>
  
  <p>And if I look at the log, there are too many of these messages:</p>
  
  <pre><code>LOG:  incomplete startup packet
  LOG:  incomplete startup packet
  ...
  </code></pre>
  
  <p>This is the command I use to try to connect from the outside (from another computer on the same LAN):</p>
  
  <pre><code>psql -h 192.168.0.100 -p 5432 -U myuser mydb
  </code></pre>
  
  <p>I also tried using pgAdmin and the same error was displayed.</p>
  
  <p><strong>Note:</strong> if I try to connect to the postgres service from the docker/kubernetes host itself, I can connect without any problems:</p>
  
  <pre><code>psql -h 192.168.3.105 -p 5432 -U myuser mydb
  </code></pre>
  
  <p>So it seems like the Kubernetes portion is working fine.</p>
  
  <p>What might be wrong?</p>
  
  <p><strong>EDIT:</strong> including the details of the LAN computers involved in the proof of concept, as well as the kubernetes definitions
  for the nginx stuff and the entries on haproxy configuration.
  So the issue happens when I try to connect from my laptop to the postgres service on the docker/kubernetes box (I thought HAProxy would redirect the request that arrives to the docker/kubernetes box at port 5432 to the kubernetes postgres service)</p>
  
  <pre><code>----- LAN Network
  My Laptop: 192.168.0.5
  HAProxy/Docker/Kubernetes box: 192.168.0.100
     |
     ---&gt; Kubernetes Nginx Service: 192.168.3.104
             |
             ---&gt;  Nginx Pod: 172.16.4.5
     |
     ---&gt; Kubernetes Postgres Service: 192.168.3.105
             |
             ---&gt; Postgres Pod: 172.16.4.9
  
  ----- app-nginx-svc.yaml
  
  apiVersion: v1
  kind: Service
  metadata:
    labels:
      name: AppNginx
    name: app-nginx
  spec:
    ports:
      - port: 80
    selector:
      app: AppNginxPod
    type: NodePort
    clusterIP: 192.168.3.104
  
  ----- app-nginx-pod-yaml
  
  apiVersion: v1
  kind: ReplicationController
  metadata:
    name: app-nginx-pod
  spec:
    replicas: 2
    selector:
      app: AppNginxPod
    template:
      metadata:
        name: app-nginx-pod
        labels:
          app: AppNginxPod
      spec:
        containers:
        - name: app-nginx-pod
          image: localhost:5000/app_nginx
          ports:
          - containerPort: 80
  
  ----- haproxy entries on /etc/haproxy/haproxy.cfg
  
  frontend nginx
          bind *:80
          mode http
          default_backend nginx-kubernetes
  
  backend nginx-kubernetes
          mode http
          balance roundrobin
          option forwardfor
          option http-server-close
          http-request set-header X-Forwarded-Port %[dst_port]
          http-request add-header X-Forwarded-Proto https if { ssl_fc }
          server nginx-01 192.168.3.104:80 check
  </code></pre>
  
  <p><strong>SOLUTION:</strong> I had to specify the user parameter in the pgsql-check line of the HAProxy configuration. After that, I was able to connect without any problems. Went back and removed the user parameter just to verify that the error was back, and it was. So, definitely, the problem was caused by the absence of the user parameter in the pgsql-check line.</p>