Search code examples
postgresqlkubernetescontainersibm-cloudibm-cloud-kubernetes

Postgres on Kubernetes on IBM Bluemix containers


I am trying to deploy Postgres on Bluemix Container service (Kubernetes)

I have created the Image and deployed it through the following yaml file:

apiVersion: v1
kind: Service
metadata:
  name: tripbru-postgres
  labels:
    app: tripbruREST
spec:
  ports:
    - port: 5432
      targetPort: 5432
      nodePort: 31432
  selector:
    app: tripbruREST
    tier: frontend
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tripbru-postgres
  labels:
    app: tripbruREST
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: tripbruREST
        tier: postgres
    spec:
      containers:
      - image: registry.ng.bluemix.net/eliza/postgres:9.5
        name: postgres
        env:
        - name: POSTGRES_PASSWORD
          value: MYPASSWORD
        ports:
        - containerPort: 5432
          name: postgres
        volumeMounts:
        - name: pg-data
          mountPath: /var/lib/postgresql/data
        - name: tz-config
          mountPath: /etc/localtime
      volumes:
      - name: pg-data
        emptyDir: {}
      - name: tz-config
        hostPath:
          path: /usr/share/zoneinfo/Europe/Madrid

This effectively deploys it:

icordoba$ kubectl get services
NAME               CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes         10.10.10.1     <none>        443/TCP          1d
tripbru-postgres   10.10.10.232   <nodes>       5432:31432/TCP   1d

But I can't connect to the node IP address on port 31432. I have tested Postgres is running using:

kubectl exec -it tripbru-postgres-3667814974-pzmsk bash

I get in the docker instance and check Postgres is running ok.

I am sure I am missing something. Do I need any other yaml file? Thanks.


Solution

  • I solved it using "Pod" and not Deployment. I also changed hostPath and note the ephemeral "emptyDir" volume format (this is a test in free Kubernetes service by Bluemix so I can't use real volumes). This is the working yaml:

    apiVersion: v1
    kind: Pod
    metadata:
      name: postgres
      labels:
        name: postgres
    spec:
      containers:
        - name: postgres
          image: registry.ng.bluemix.net/eliza/postgres:9.5
          env:
            - name: POSTGRES_PASSWORD
              value: MYPASSWORD
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: pg-data
              mountPath: /var/lib/postgresql/data
            - name: tz-config
              mountPath: /etc/localtime
      volumes:
      - name: pg-data
        #emptyDir: {}
        hostPath:
          path: "/opt/tripbruPostgres"
      - name: tz-config
        hostPath:
          path: /usr/share/zoneinfo/Europe/Madrid
    

    (Note I still don't know what was wrong with my "Deployment" approach, but using Pod works as I don't need replication at this stage)