<p>I'm new with Kubernetes and I try to understand how to connect Postgres database which is outside from  Kubernetes (exactly in docker with ip address 172.17.0.2 and port 5432) to my webapp in Kubernetes.</p>
<p>I try connect database through env variable <code>PS_DATABASE_URL</code> in Deployment section.</p>
<p>But it cannot find mentioned url for postgres. How it need to be done correctly?</p>
<p><code>webapp.yaml</code></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: dmitriy83/flask_kuber
        ports:
        - containerPort: 5000
        env:
          - name: PS_DATABASE_URL
            value: postgresql://postgres:password@172.17.0.2:5432/db
---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  type: NodePort
  selector:
    app: webapp
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000
      nodePort: 30100

</code></pre>

Dmitriy_kzn
<p>I figured it out. it depends from cloud provider. For this example i use amazon cloud and to connect database on amazon (this is external service). So we must define it in yaml file like an external service.</p>
<p><code>postgres_external.yaml</code></p>
<pre><code>kind: Service
apiVersion: v1
metadata:
  name: postgres
spec:
  type: ExternalName
  externalName: db.cdmhjidhpqyu.us-east-2.rds.amazonaws.com
</code></pre>
<p>to connect to external service you need to link to it on deployment.</p>
<p><code>webapp.yaml</code></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: dmitriy83/flask_kuber
        ports:
        - containerPort: 5000
        env:
          - name: PS_DATABASE_URL
            value: postgresql://&lt;username&gt;:&lt;password&gt;@postgres:5432/db
---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  type: NodePort
  selector:
    app: webapp
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000
      nodePort: 30100
</code></pre>
<p>Please note in webapp.yaml, env section value <code>value: postgresql://&lt;username&gt;:&lt;password&gt;@postgres:5432/db</code>   contains <strong>postgres</strong> - this is name of our external service which we define in <code>postgres_external.yaml</code></p>
