Digital ocean Kubernetes + Django / PostgreSQL getting an database error inside cluster

2/14/2019

my app is inside a docker container and works perfectly fine in local host..but when i run this docker image in kubernetes cluster it gives me this error conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known

here is my database settings.py

    DATABASES = {
        'default': {
           'ENGINE': 'django.db.backends.postgresql',
           'NAME': 'postgres',
           'USER': 'postgres',
           'HOST': 'db',
           'PORT': 5432,
    }
    }

I deployed the image into kubernetes cluster with frontend.yml manifest file..it looks like this frontend.yml

          apiVersion: extensions/v1beta1
          kind: Deployment
          metadata:
          name: dockersample-app
             labels:
               app: polls
          spec:
              replicas: 3
              template:
              metadata:
              labels:
                 app: dcokersample-app
              spec:
                containers:
                - name: dcokersample

                  image: mahesh61437/dockersample:v6

                  imagePullPolicy: Always
                  ports:
                  - containerPort: 8000

---service.yml

          apiVersion: v1
          kind: Service
          metadata:
            name: dockersample-app
            labels:
              app: dockersample-app
          spec:
            type: LoadBalancer
            ports:
            - port: 8000
              targetPort: 8000
            selector:
              app: dockersample-app

here is my DOCKER File

    FROM python:3
    RUN apt-get update
     EXPOSE 8000
     ENV PYTHONUNBUFFERED 1
     RUN mkdir /code
     WORKDIR /code
     COPY requirements.txt /code/
     RUN pip install -r requirements.txt
     COPY . /code/
     CMD python manage.py runserver 

kubectl get pod,svc,deployment,pvc,pv -o yaml

    apiVersion: v1
    items:
    - apiVersion: v1
      kind: Pod
      metadata:
        annotations:
          cilium.io/identity: "63547"
        creationTimestamp: "2019-02-14T09:49:39Z"
        generateName: dockersample-app-557878d964-
        labels:
          app: dcokersample-app
          pod-template-hash: 557878d964
        name: dockersample-app-557878d964-fm94j
        namespace: default
        ownerReferences:
        - apiVersion: apps/v1
          blockOwnerDeletion: true
          controller: true
          kind: ReplicaSet
          name: dockersample-app-557878d964
          uid: d8b8a828-303d-11e9-94cc-9252dc3b5955
        resourceVersion: "271350"
        selfLink: /api/v1/namespaces/default/pods/dockersample-app-557878d964-fm94j
        uid: d8bc708b-303d-11e9-94cc-9252dc3b5955
      spec:
        containers:
        - image: mahesh61437/dockersample:v6
          imagePullPolicy: Always
          name: dcokersample
          ports:
          - containerPort: 8000
            protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
          - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
            name: default-token-svb6z
            readOnly: true
        dnsPolicy: ClusterFirst
        enableServiceLinks: true
        nodeName: vibrant-ramanujan-8zmn
        priority: 0
        restartPolicy: Always
        schedulerName: default-scheduler
        securityContext: {}
        serviceAccount: default
        serviceAccountName: default
        terminationGracePeriodSeconds: 30
        tolerations:
        - effect: NoExecute
          key: node.kubernetes.io/not-ready
          operator: Exists
          tolerationSeconds: 300
        - effect: NoExecute
          key: node.kubernetes.io/unreachable
          operator: Exists
          tolerationSeconds: 300
        volumes:
        - name: default-token-svb6z
          secret:
            defaultMode: 420
            secretName: default-token-svb6z
      status:
        conditions:
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:39Z"
          status: "True"
          type: Initialized
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:49Z"
          status: "True"
          type: Ready
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:49Z"
          status: "True"
          type: ContainersReady
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:39Z"
          status: "True"
          type: PodScheduled
        containerStatuses:
        - containerID: docker://d82ec6f089cc76e64e7ba68d56ba5c1263343c08929d648c9fef005d4a08488c
          image: mahesh61437/dockersample:v6
          imageID: docker-pullable://mahesh61437/dockersample@sha256:54aa303cc5534609a1b579718f192323fad9dd57bd92a2897cd64f110438c965
          lastState: {}
          name: dcokersample
          ready: true
          restartCount: 0
          state:
            running:
              startedAt: "2019-02-14T09:49:49Z"
        hostIP: 10.139.16.196
        phase: Running
        podIP: 10.244.1.64
        qosClass: BestEffort
        startTime: "2019-02-14T09:49:39Z"
    - apiVersion: v1
      kind: Pod
      metadata:
        annotations:
          cilium.io/identity: "63547"
        creationTimestamp: "2019-02-14T09:49:39Z"
        generateName: dockersample-app-557878d964-
        labels:
          app: dcokersample-app
          pod-template-hash: 557878d964
        name: dockersample-app-557878d964-ftngl
        namespace: default
        ownerReferences:
        - apiVersion: apps/v1
          blockOwnerDeletion: true
          controller: true
          kind: ReplicaSet
          name: dockersample-app-557878d964
          uid: d8b8a828-303d-11e9-94cc-9252dc3b5955
        resourceVersion: "271354"
        selfLink: /api/v1/namespaces/default/pods/dockersample-app-557878d964-ftngl
        uid: d8bdda66-303d-11e9-94cc-9252dc3b5955
      spec:
        containers:
        - image: mahesh61437/dockersample:v6
          imagePullPolicy: Always
          name: dcokersample
          ports:
          - containerPort: 8000
            protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
          - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
            name: default-token-svb6z
            readOnly: true
        dnsPolicy: ClusterFirst
        enableServiceLinks: true
        nodeName: vibrant-ramanujan-8zm3
        priority: 0
        restartPolicy: Always
        schedulerName: default-scheduler
        securityContext: {}
        serviceAccount: default
        serviceAccountName: default
        terminationGracePeriodSeconds: 30
        tolerations:
        - effect: NoExecute
          key: node.kubernetes.io/not-ready
          operator: Exists
          tolerationSeconds: 300
        - effect: NoExecute
          key: node.kubernetes.io/unreachable
          operator: Exists
          tolerationSeconds: 300
        volumes:
        - name: default-token-svb6z
          secret:
            defaultMode: 420
            secretName: default-token-svb6z
      status:
        conditions:
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:39Z"
          status: "True"
          type: Initialized
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:49Z"
          status: "True"
          type: Ready
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:49Z"
          status: "True"
          type: ContainersReady
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:39Z"
          status: "True"
          type: PodScheduled
        containerStatuses:
        - containerID: docker://ef71c722fbcc70ceb96d929e983e22263cbc40a54fd666cf73cc0dd73c437cae
          image: mahesh61437/dockersample:v6
          imageID: docker-pullable://mahesh61437/dockersample@sha256:54aa303cc5534609a1b579718f192323fad9dd57bd92a2897cd64f110438c965
          lastState: {}
          name: dcokersample
          ready: true
          restartCount: 0
          state:
            running:
              startedAt: "2019-02-14T09:49:48Z"
        hostIP: 10.139.120.24
        phase: Running
        podIP: 10.244.2.187
        qosClass: BestEffort
        startTime: "2019-02-14T09:49:39Z"
    - apiVersion: v1
      kind: Pod
      metadata:
        annotations:
          cilium.io/identity: "63547"
        creationTimestamp: "2019-02-14T09:49:39Z"
        generateName: dockersample-app-557878d964-
        labels:
          app: dcokersample-app
          pod-template-hash: 557878d964
        name: dockersample-app-557878d964-lq78m
        namespace: default
        ownerReferences:
        - apiVersion: apps/v1
          blockOwnerDeletion: true
          controller: true
          kind: ReplicaSet
          name: dockersample-app-557878d964
          uid: d8b8a828-303d-11e9-94cc-9252dc3b5955
        resourceVersion: "271358"
        selfLink: /api/v1/namespaces/default/pods/dockersample-app-557878d964-lq78m
        uid: d8be0705-303d-11e9-94cc-9252dc3b5955
      spec:
        containers:
        - image: mahesh61437/dockersample:v6
          imagePullPolicy: Always
          name: dcokersample
          ports:
          - containerPort: 8000
            protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
          - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
            name: default-token-svb6z
            readOnly: true
        dnsPolicy: ClusterFirst
        enableServiceLinks: true
        nodeName: vibrant-ramanujan-8z79
        priority: 0
        restartPolicy: Always
        schedulerName: default-scheduler
        securityContext: {}
        serviceAccount: default
        serviceAccountName: default
        terminationGracePeriodSeconds: 30
        tolerations:
        - effect: NoExecute
          key: node.kubernetes.io/not-ready
          operator: Exists
          tolerationSeconds: 300
        - effect: NoExecute
          key: node.kubernetes.io/unreachable
          operator: Exists
          tolerationSeconds: 300
        volumes:
        - name: default-token-svb6z
          secret:
            defaultMode: 420
            secretName: default-token-svb6z
      status:
        conditions:
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:39Z"
          status: "True"
          type: Initialized
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:50Z"
          status: "True"
          type: Ready
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:50Z"
          status: "True"
          type: ContainersReady
        - lastProbeTime: null
          lastTransitionTime: "2019-02-14T09:49:39Z"
          status: "True"
          type: PodScheduled
        containerStatuses:
        - containerID: docker://fa3c8f25b260b0e3c032907ff796b5e22bf0479646d457914de518c3c6180be0
          image: mahesh61437/dockersample:v6
          imageID: docker-pullable://mahesh61437/dockersample@sha256:54aa303cc5534609a1b579718f192323fad9dd57bd92a2897cd64f110438c965
          lastState: {}
          name: dcokersample
          ready: true
          restartCount: 0
          state:
            running:
              startedAt: "2019-02-14T09:49:49Z"
        hostIP: 10.139.16.250
        phase: Running
        podIP: 10.244.0.168
        qosClass: BestEffort
        startTime: "2019-02-14T09:49:39Z"
    - apiVersion: v1
      kind: Service
      metadata:
        annotations:
          kubectl.kubernetes.io/last-applied-configuration: |
            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"dockersample-app"},"name":"dockersample-app","namespace":"default"},"spec":{"ports":[{"port":8000,"targetPort":8000}],"selector":{"app":"dockersample-app"},"type":"LoadBalancer"}}
        creationTimestamp: "2019-02-14T09:49:39Z"
        labels:
          app: dockersample-app
        name: dockersample-app
        namespace: default
        resourceVersion: "271514"
        selfLink: /api/v1/namespaces/default/services/dockersample-app
        uid: d8c78f7a-303d-11e9-94cc-9252dc3b5955
      spec:
        clusterIP: 10.245.57.250
        externalTrafficPolicy: Cluster
        ports:
        - nodePort: 32204
          port: 8000
          protocol: TCP
          targetPort: 8000
        selector:
          app: dockersample-app
        sessionAffinity: None
        type: LoadBalancer
      status:
        loadBalancer:
          ingress:
          - ip: 174.138.123.199
    - apiVersion: v1
      kind: Service
      metadata:
        creationTimestamp: "2019-02-12T09:31:19Z"
        labels:
          component: apiserver
          provider: kubernetes
        name: kubernetes
        namespace: default
        resourceVersion: "6"
        selfLink: /api/v1/namespaces/default/services/kubernetes
        uid: f3f45187-2ea8-11e9-94cc-9252dc3b5955
      spec:
        clusterIP: 10.245.0.1
        ports:
        - name: https
          port: 443
          protocol: TCP
          targetPort: 443
        sessionAffinity: None
        type: ClusterIP
      status:
        loadBalancer: {}
    - apiVersion: extensions/v1beta1
      kind: Deployment
      metadata:
        annotations:
          deployment.kubernetes.io/revision: "1"
          kubectl.kubernetes.io/last-applied-configuration: |
            {"apiVersion":"extensions/v1beta1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"polls"},"name":"dockersample-app","namespace":"default"},"spec":{"replicas":3,"template":{"metadata":{"labels":{"app":"dcokersample-app"}},"spec":{"containers":[{"image":"mahesh61437/dockersample:v6","imagePullPolicy":"Always","name":"dcokersample","ports":[{"containerPort":8000}]}]}}}}
        creationTimestamp: "2019-02-14T09:49:39Z"
        generation: 1
        labels:
          app: polls
        name: dockersample-app
        namespace: default
        resourceVersion: "271360"
        selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/dockersample-app
        uid: d8b79710-303d-11e9-94cc-9252dc3b5955
      spec:
        progressDeadlineSeconds: 2147483647
        replicas: 3
        revisionHistoryLimit: 2147483647
        selector:
          matchLabels:
            app: dcokersample-app
        strategy:
          rollingUpdate:
            maxSurge: 1
            maxUnavailable: 1
          type: RollingUpdate
        template:
          metadata:
            creationTimestamp: null
            labels:
              app: dcokersample-app
          spec:
            containers:
            - image: mahesh61437/dockersample:v6
              imagePullPolicy: Always
              name: dcokersample
              ports:
              - containerPort: 8000
                protocol: TCP
              resources: {}
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File
            dnsPolicy: ClusterFirst
            restartPolicy: Always
            schedulerName: default-scheduler
            securityContext: {}
            terminationGracePeriodSeconds: 30
      status:
        availableReplicas: 3
        conditions:
        - lastTransitionTime: "2019-02-14T09:49:49Z"
          lastUpdateTime: "2019-02-14T09:49:49Z"
          message: Deployment has minimum availability.
          reason: MinimumReplicasAvailable
          status: "True"
          type: Available
        observedGeneration: 1
        readyReplicas: 3
        replicas: 3
        updatedReplicas: 3
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        annotations:
          kubectl.kubernetes.io/last-applied-configuration: |
            {"apiVersion":"v1","kind":"PersistentVolumeClaim","metadata":{"annotations":{},"labels":{"app":"postgres"},"name":"postgres-pv-claim","namespace":"default"},"spec":{"accessModes":["ReadWriteMany"],"resources":{"requests":{"storage":"5Gi"}},"storageClassName":"manual"}}
          pv.kubernetes.io/bind-completed: "yes"
          pv.kubernetes.io/bound-by-controller: "yes"
        creationTimestamp: "2019-02-14T10:11:47Z"
        finalizers:
        - kubernetes.io/pvc-protection
        labels:
          app: postgres
        name: postgres-pv-claim
        namespace: default
        resourceVersion: "273451"
        selfLink: /api/v1/namespaces/default/persistentvolumeclaims/postgres-pv-claim
        uid: f02728ee-3040-11e9-94cc-9252dc3b5955
      spec:
        accessModes:
        - ReadWriteMany
        dataSource: null
        resources:
          requests:
            storage: 5Gi
        storageClassName: manual
        volumeMode: Filesystem
        volumeName: postgres-pv-volume
      status:
        accessModes:
        - ReadWriteMany
        capacity:
          storage: 5Gi
        phase: Bound
    - apiVersion: v1
      kind: PersistentVolume
      metadata:
        annotations:
          kubectl.kubernetes.io/last-applied-configuration: |
            {"apiVersion":"v1","kind":"PersistentVolume","metadata":{"annotations":{},"labels":{"app":"postgres","type":"local"},"name":"postgres-pv-volume"},"spec":{"accessModes":["ReadWriteMany"],"capacity":{"storage":"5Gi"},"hostPath":{"path":"/mnt/data"},"storageClassName":"manual"}}
          pv.kubernetes.io/bound-by-controller: "yes"
        creationTimestamp: "2019-02-14T10:11:47Z"
        finalizers:
        - kubernetes.io/pv-protection
        labels:
          app: postgres
          type: local
        name: postgres-pv-volume
        resourceVersion: "273449"
        selfLink: /api/v1/persistentvolumes/postgres-pv-volume
        uid: f01f5beb-3040-11e9-94cc-9252dc3b5955
      spec:
        accessModes:
        - ReadWriteMany
        capacity:
          storage: 5Gi
        claimRef:
          apiVersion: v1
          kind: PersistentVolumeClaim
          name: postgres-pv-claim
          namespace: default
          resourceVersion: "273446"
          uid: f02728ee-3040-11e9-94cc-9252dc3b5955
        hostPath:
          path: /mnt/data
          type: ""
        persistentVolumeReclaimPolicy: Retain
        storageClassName: manual
        volumeMode: Filesystem
      status:
        phase: Bound
    kind: List
    metadata:
      resourceVersion: ""
      selfLink: ""

I cant figure out what I must do now. If you think you can suggest better in my code me something please leave a comment.

-- Mahesh Royals
digital-ocean
django
docker
kubernetes
postgresql

1 Answer

2/14/2019

It's missing the database deployement and service (optional but highly recommended PersistentVolumeClaim).

  apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    name: db-deployment
    labels:
      app: db-deployment
  spec:
    replicas: 1
    template:
      metadata:
        labels:
          app: db
      spec:
        containers:
        - image: postgres:9.4
          name: db
          ports:
          - containerPort: 5432
          volumeMounts:
           - name: postgres-db-data
             mountPath: /var/lib/postgresql
        volumes:
        - name: postgres-db-data
          persistentVolumeClaim:
            claimName: db-data
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: db
    labels:
      name: db
  spec:
    ports:
      - name: db
        port: 5432
    selector:
      app: db-deployment
  ---
  apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata:
    name: "db-data"
  spec:
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: 20Gi
    storageClassName: "your storage class"
-- andolsi zied
Source: StackOverflow