<p>I've created an example Rails 5 app that uses Google Cloud PostgreSQL.
I'm able to run the app locally with <code>docker-compose up</code>, but I'm not able to connect to it remote when I deploy it to GCP.
I tried to replicate <a href="https://cloud.google.com/ruby/tutorials/bookshelf-on-kubernetes-engine" rel="nofollow noreferrer">https://cloud.google.com/ruby/tutorials/bookshelf-on-kubernetes-engine</a> where they use <code>targetPort: http-server</code></p>

<p>The rails app is <a href="https://github.com/stabenfeldt/k8s-colors/" rel="nofollow noreferrer">published on Github</a>.
Am I doing anything obviously wrong? :-|</p>

<h2>Running the app locally works</h2>

<pre><code>git clone git@github.com:stabenfeldt/k8s-colors.git
docker-compose up -d
docker-compose run colors rake db:create db:migrate
open http://localhost:3000
</code></pre>

<h3>Create a GKE cluster</h3>

<pre><code>gcloud container clusters create color-cluster --num-nodes=2
</code></pre>

<h3>Setup PostgreSQL Cloud SQL</h3>

<p>I followed the  instructions from <a href="https://cloud.google.com/sql/docs/postgres/connect-kubernetes-engine?authuser=1" rel="nofollow noreferrer">https://cloud.google.com/sql/docs/postgres/connect-kubernetes-engine?authuser=1</a>
and updated my config/database.yml and k8s/colors.yml with these values.</p>

<h3>Deployed but stuck on ContainerCreating</h3>

<pre><code>kubectl apply -f k8s/colors.yml
kubectl get pods

NAME                    READY     STATUS              RESTARTS   AGE
colors-d9f744dc-d5l5v   0/2       ContainerCreating   0          5m
colors-d9f744dc-spmws   0/2       ContainerCreating   0          5m


kubectl logs d9f744dc-d5l5v -c colors  # =&gt; Nothing logged
</code></pre>

<h3>kubectl get deployment</h3>

<pre><code>NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
colors    2         2         2            0           7m
</code></pre>

<h3>But fails to connect to the app</h3>

<pre><code>kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)        AGE
colors       LoadBalancer   10.55.245.192   35.228.111.217   80:30746/TCP   1h
kubernetes   ClusterIP      10.55.240.1     &lt;none&gt;           443/TCP        1h
</code></pre>

<blockquote>
  <p>curl 35.228.111.217 # => No response! :-/</p>
</blockquote>

<p><strong>kubectl describe svc colors</strong></p>

<pre><code>Name:                     colors
Namespace:                default
Labels:                   &lt;none&gt;
Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"colors","namespace":"default"},"spec":{"ports":[{"port":80,"targetPort":3000}]...
Selector:                 app=colors
Type:                     LoadBalancer
IP:                       10.55.252.91
LoadBalancer Ingress:     35.228.203.46
Port:                     &lt;unset&gt;  80/TCP
TargetPort:               3000/TCP
NodePort:                 &lt;unset&gt;  30964/TCP
Endpoints:                &lt;none&gt;
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  Type                  1m    service-controller  ClusterIP -&gt; LoadBalancer
  Normal  EnsuringLoadBalancer  1m    service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   30s   service-controller  Ensured load balancer
</code></pre>

<h2>k8s/service.yml</h2>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: colors
  labels:
    app: colors

spec:
  replicas: 2
  selector:
    matchLabels:
      app: colors
  template:
    metadata:
      labels:
        app: colors
    spec:
      containers:
        - name: colors
          image: docker.io/stabenfeldt/colors:latest
          ports:
            - name: http-server
              containerPort: 3000


          env:
            - name: POSTGRES_HOST
              value: 127.0.0.1:5432
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: cloudsql-db-credentials
                  key: username
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: cloudsql-db-credentials
                  key: password

        - name: cloudsql-proxy
          image: gcr.io/cloudsql-docker/gce-proxy:1.11
          command: ["/cloud_sql_proxy",
                    "-instances=PROJECT_ID:europe-west1:staging=tcp:5432",
                    "-credential_file=/secrets/cloudsql/credentials.json"]
          volumeMounts:
            - name: cloudsql-instance-credentials
              mountPath: /secrets/cloudsql
              readOnly: true

      volumes:
        - name: cloudsql-instance-credentials
          secret:
            secretName: cloudsql-instance-credentials

---


apiVersion: v1
kind: Service
metadata:
  name: colors
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 3000
  selector:
    app: colors
</code></pre>

<p><strong>kubectl describe deployment</strong></p>

<pre><code>Name:                   colors
Namespace:              default
CreationTimestamp:      Fri, 13 Jul 2018 10:37:06 +0200
Labels:                 app=colors
Annotations:            deployment.kubernetes.io/revision=1
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"colors"},"name":"colors","namespace":"default"},"spec":{"repl...
Selector:               app=colors
Replicas:               2 desired | 2 updated | 2 total | 0 available | 2 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=colors
  Containers:
   colors:
    Image:  docker.io/stabenfeldt/colors:latest
    Port:   3000/TCP
    Environment:
      POSTGRES_HOST:      127.0.0.1:5432
      POSTGRES_USER:      &lt;set to the key 'username' in secret 'cloudsql-db-credentials'&gt;  Optional: false
      POSTGRES_PASSWORD:  &lt;set to the key 'password' in secret 'cloudsql-db-credentials'&gt;  Optional: false
    Mounts:               &lt;none&gt;
   cloudsql-proxy:
    Image:  gcr.io/cloudsql-docker/gce-proxy:1.11
    Port:   &lt;none&gt;
    Command:
      /cloud_sql_proxy
      -instances=MY-INSTANCE:europe-west1:staging=tcp:5432
      -credential_file=/secrets/cloudsql/credentials.json
    Environment:  &lt;none&gt;
    Mounts:
      /secrets/cloudsql from cloudsql-instance-credentials (ro)
  Volumes:
   cloudsql-instance-credentials:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  cloudsql-instance-credentials
    Optional:    false
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      False   MinimumReplicasUnavailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  &lt;none&gt;
NewReplicaSet:   colors-d9f744dc (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  1m    deployment-controller  Scaled up replica set colors-d9f744dc to 2
</code></pre>

<p><em>kubectl describe service</em></p>

<pre><code>Name:                     colors
Namespace:                default
Labels:                   &lt;none&gt;
Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"colors","namespace":"default"},"spec":{"ports":[{"port":80,"targetPort":3000}]...
Selector:                 app=colors
Type:                     LoadBalancer
IP:                       10.55.252.91
LoadBalancer Ingress:     35.228.203.46
Port:                     &lt;unset&gt;  80/TCP
TargetPort:               3000/TCP
NodePort:                 &lt;unset&gt;  30964/TCP
Endpoints:                &lt;none&gt;
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  Type                  4m    service-controller  ClusterIP -&gt; LoadBalancer
  Normal  EnsuringLoadBalancer  4m    service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   3m    service-controller  Ensured load balancer


Name:              kubernetes
Namespace:         default
Labels:            component=apiserver
                   provider=kubernetes
Annotations:       &lt;none&gt;
Selector:          &lt;none&gt;
Type:              ClusterIP
IP:                10.55.240.1
Port:              https  443/TCP
TargetPort:        443/TCP
Endpoints:         35.228.79.249:443
Session Affinity:  ClientIP
Events:            &lt;none&gt;
</code></pre>

martins
<p>I don't see anything wrong outright, but here are a few tips to verifying your Kubernetes Objects look like they should compared to your yamls:</p>

<p>Use the <a href="https://kubernetes-v1-4.github.io/docs/user-guide/kubectl/kubectl_describe/" rel="nofollow noreferrer">describe</a> command to get more information about objects and make sure they are set up correctly.</p>

<p>For example, if you do <code>kubectl describe deployment &lt;deployment_name&gt;</code> you should verify the following line is present:</p>

<pre><code>Port:       3000/TCP
</code></pre>

<p>And for your Service - <code>kubectl describe service &lt;service_name&gt;</code>:</p>

<pre><code>LoadBalancer Ingress:     &lt;PUBLIC_IP&gt;
Port:                     &lt;unset&gt;  80/TCP
TargetPort:               3000/TCP
</code></pre>

<p>Finally, I'm not sure if you want to apply the following in your LoadBalancer:</p>

<pre><code>  labels:
    app: colors
</code></pre>

<p>Since you are using this label as a selector, it may be doing something funky and trying to load balance to itself instead of your containers with the apps in it.</p>

<p>Also as a side note on your terminology, GCP (Google Cloud Platform) is the overarching name of Google's Services, GKE (Google Kubernetes Engine) is the service providing you with a managed Kuberenetes Cluster.</p>

<p>Hope this helps.</p>
