title: Deploying PostgreSQL database with Kubernetes
body: <p>I am confused when it comes to deploying PostgreSQL database of my Django application with Kubernetes. Here is how I have constructed my deployment-definition.yml file:</p>
  
  <pre><code>apiVersion: apps/v1beta1
  kind: Deployment
  metadata:
    name: postgres
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: postgres-container
    template:
      metadata:
        labels:
          app: postgres-container
          tier: backend
      spec:
        containers:
          - name: postgres-container
            image: postgres:9.6.6
            env:
              - name: POSTGRES_USER
                valueFrom:
                  secretKeyRef:
                    name: postgres-credentials
                    key: user
  
              - name: POSTGRES_PASSWORD
                valueFrom:
                  secretKeyRef:
                    name: postgres-credentials
                    key: password
  
              - name: POSTGRES_DB
                value: agent_technologies_db
            ports:
              - containerPort: 5432
            volumeMounts:
              - name: postgres-volume-mount
                mountPath: /var/lib/postgresql/data
  
        volumes:
          - name: postgres-volume-mount
            persistentVolumeClaim:
              claimName: postgres-pvc
          - name: postgres-credentials
            secret:
              secretName: postgres-credentials
  </code></pre>
  
  <p>What I dont understand is this. If I specify (like i did) an existing image of PostgreSQL inside spec of Kubernetes Deployment object, how do I actually run my application? What do I need to specify as HOST inside my settings.py file?</p>
  
  <p>Here is what my settings.py file looks like for now:</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': 'agent_technologies_db',
          'USER': 'stefan_radonjic',
          'PASSWORD': 'cepajecar995',
          'HOST': 'localhost', 
          'PORT': '',
          }
  }
  </code></pre>
  
  <p>It is constructed this way because I am still designing the application and I do not wanna deploy it to Kubernetes cluster just yet. But when I do, what am I suppose to specify for : <code>HOST</code> and <code>PORT</code> ? And also, is this the right way to deploy PostgreSQL to Kubernetes Cluster.</p>
  
  <p>Thank you in advance! </p>
  
  <p>*** QUESTION UPDATE ****</p>
  
  <p>As suggested, I have created service.yml:</p>
  
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: postgres-service
  spec:
    selector:
      app: postgres-container
      tier: backend
    ports:
      - protocol: TCP
        port: 5432
        targetPort: 5432
    type: ClusterIP
  </code></pre>
  
  <p>And I have updated my settings.py file:</p>
  
  <pre><code>DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': 'agent_technologies_db',
          'USER': 'stefan_radonjic',
          'PASSWORD': 'cepajecar995',
          'HOST': 'postgres-service', 
          'PORT': 5432,
          }
  }
  </code></pre>
  
  <p>But I am getting the following error: </p>
  
  <p><a href="https://i.stack.imgur.com/tiSVX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tiSVX.png" alt="enter image description here"></a></p>