Search code examples
kubernetes-servicekube-dnskubernetes-deploymentkubernetes-networking

Connect app deployment with database service on K8s


I have to connect my application deployment with PostgresDB deployment. For the moment I put the Postgres cluster IP in my application environment like reported here:

apiVersion: apps/v1
kind: Deployment
.......
containers:
  - name: "#{tenant}#-app-widget"
    image: baserepository:#{app_image_version}#
    ports:
      - containerPort: 8080
    env:
      - name: NODE_ENV
        value: "#{NODE_ENV}#"
      - name: AUTH_CALLBACK_URL
        value: "https://base.#{DNSSubdomain}#/auth/callback"
      - name: DB_HOST
        value: "10.0.196.195"
      - name: DB_PORT
        value: "5432"
      - name: DB_DATABASE
        value: "#{DB_DATABASE}#"

What I would like to do is to put in the value of the DB_HOST a reference to the postgres service in order to be more dynamically when I deploy my application. Here what I have done for the postgres deployment and service:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: "#{tenant}#-postgres"
  name: "#{tenant}#-postgres"
  labels:
    app: "#{tenant}#-postgres"
    product: "#{tenant}#-postgres"
    app.kubernetes.io/name: "#{tenant}#-postgres"
    app.kubernetes.io/version: "10.4"
    app.kubernetes.io/managed-by: "#{managed_by}#"
    app.kubernetes.io/component: "#{tenant}#-postgres"
    app.kubernetes.io/part-of: "#{tenant}#-postgres"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: "#{tenant}#-postgres"
  template:
    metadata:
      labels:
        app: "#{tenant}#-postgres"
    spec:
      containers:
        - name: "#{tenant}#-postgres"
          image: postgres:10.4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: "#{tenant}#-postgres-config"
          resources:
            requests:
              memory: "#{POSTGRES_RESOURCES_REQUESTS_MEMORY}#"
              cpu: "#{POSTGRES_RESOURCES_REQUESTS_CPU}#"
            limits:
              memory: "#{POSTGRES_RESOURCES_LIMITS_MEMORY}#"
              cpu: "#{POSTGRES_RESOURCES_LIMITS_CPU}#"
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: "#{tenant}#-postgredb"
      volumes:
        - name: "#{tenant}#-postgredb"
          persistentVolumeClaim:
            claimName: "#{tenant}#-postgres-pv-claim"
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              preference:
                matchExpressions:
                  - key: tier
                    operator: In
                    values:
                      - "#{POSTGRES_AFFINITY_PREFERRED_VALUE}#"
---
apiVersion: v1
kind: Service
metadata:
  namespace: "#{tenant}#-postgres"
  name: "#{tenant}#-postgres"
  labels:
    app: "#{tenant}#-postgres"
spec:
  type: NodePort
  ports:
    - port: 5432
  selector:
    app: "#{tenant}#-postgres"

Could you provide me an example on how to perform this operation? I think that an ingress is not needed because I have to relate two deployment inside the same cluster. Thank you very much, Dave.


Solution

  • I found the solution, you have to put in the DB_HOST value the service metadata {name}.{namespace}. This is enough to put in communication dynamically the app deployment with the Postgres instance. DB_HOST will be "#{tenant}#-postgres.#{tenant}#-postgres"