How do I expose Docker a Docker port via Kubernetes in Google Cloud?

7/17/2015

I am trying to wrap my head around provisioning Docker instances in Kubernetes on Google Cloud. I have build a replica controller that specifies a single pod and defined a port to be exposed, but it is not being exposed.

Here is my containers.yaml:

apiVersion: v1
kind: ReplicationController
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    app: postgres
  template:
    metadata:
      name: postgres
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: us.gcr.io/bobsapp-1008/postgres:9.4
        ports:
          -
            containerPort: 5432
            name: postgres
            protocol: TCP

Here is the result of docker ps while running as root in my compute engine instance that is running this pod:

root@gke-legal-data-6fddb637-node-49l5:/home/eric# docker ps
CONTAINER ID        IMAGE                                       COMMAND                CREATED              STATUS              PORTS               NAMES
c16b7e0071f4        us.gcr.io/bobsapp-1008/postgres:9.4          "/docker-entrypoint.   About a minute ago   Up About a minute                       k8s_postgres.82df2fa1_postgres-7wd6n_default_4e36d84b-2c1f-11e5-8f6f-42010af0664d_2a996ec3
036f8d6375bc        gcr.io/google_containers/pause:0.8.0        "/pause"               About a minute ago   Up About a minute                       k8s_POD.e4cc795_postgres-7wd6n_default_4e36d84b-2c1f-11e5-8f6f-42010af0664d_8b058463
14c6331ce91f        gcr.io/google_containers/heapster:v0.15.0   "/heapster --source=   3 minutes ago        Up 3 minutes                            k8s_heapster.bb306f05_monitoring-heapster-v5-p0orx_kube-system_983e585e-2bd7-11e5-8f6f-42010af0664d_b8b0aff1
8ab049b45e57        gcr.io/google_containers/pause:0.8.0        "/pause"               6 hours ago          Up 6 hours                              tender_rosalind
03c9810efd2d        gcr.io/google_containers/fluentd-gcp:1.8    "\"/bin/sh -c '/usr/   8 hours ago          Up 8 hours                              k8s_fluentd-cloud-logging.7721935b_fluentd-cloud-logging-gke-legal-data-6fddb637-node-49l5_kube-system_d0feac1ad02da9e97c4bf67970ece7a1_520ad44a
8ad81f1eb559        gcr.io/google_containers/pause:0.8.0        "/pause"               8 hours ago          Up 8 hours                              k8s_POD.e4cc795_monitoring-heapster-v5-p0orx_kube-system_983e585e-2bd7-11e5-8f6f-42010af0664d_d321075c
931e38c13e91        gcr.io/google_containers/pause:0.8.0        "/pause"               8 hours ago          Up 8 hours                              k8s_POD.e4cc795_fluentd-cloud-logging-gke-legal-data-6fddb637-node-49l5_kube-system_d0feac1ad02da9e97c4bf67970ece7a1_69d90d4f

You can see that there are no ports exposed. Why isn't my postgres (port 5432) exposed?

-- Eric
docker
google-app-engine
google-cloud-platform
kubernetes
postgresql

2 Answers

11/13/2016

If you are not exposing the port externally using a service.yml then you can forward the port to yourself using this method.

Open up Kubernetes, click on Pods, collapse the menu if you can't read the pod names and click the pod you want to forward. Copy the name of the pod and insert it into this command.

kubectl port-forward <your pod name> <port number>

example:

kubectl port-forward potato-dks2ko3 8080
-- DevOps ninja
Source: StackOverflow

7/17/2015

Each pod in kubernetes is given its own IP address on the cluster's private network. The port that your postgres container has open is on that IP address, not directly on the host VM's IP.

If you run kubectl get pod postgres, it should return the pod's IP address, which you should then be able to use to reach postgres from within the cluster.

Note that using a service is a common way to give a DNS name to a pod or to make a group of replicated pods all reachable at the same address.

-- Alex Robinson
Source: StackOverflow