Table missing in Postgresql pod inside the kubernetes container(minikube)

12/22/2021

I have implemented a dot net Core API which is running as a container inside the Kubernetes pod. Also there is Postgresql running as a container inside a pod. API will connect to Postgresql for the data. Now in terms of deployment I have created deployment and services yaml for both the apps. For API the deployment service is LoadBalancer type and then I am accessing/hitting the API through the external IP. For the Postgresql I have done the same thing where its deployment service is of the type LoadBalancer so that I can access the DB from the external host.

But in the actual code I am using the inbuilt CoreDNS service of minikube to discover the Postgresql service from API service using servicename.namespace.default.cluster.local

I am also able to access the DB from the external host and also able to create tables. But my thinking was when creating the tables from the external host the same tables will also be available inside the actual Pod which is not happening.

Both the app services are connecting and working fine because I can see the queries being generated inside the pod of Postgresql. But those queries are not working because tables are not there.

Below are the yaml files

API:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
  labels:
    app: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api-deployment
  template:
    metadata:
      labels:
        app: api-deployment
    spec:
      #volumes:
      #  - name: cachedata-persistentvolume
      #    persistentVolumeClaim:
      #      claimName: cachedata-persistentvolumeclaim
      #initContainers:
      #- name: api-init"
      #  image: "groundnuty/k8s-wait-for:1.3"
      #  imagePullPolicy: IfNotPresent
      #  args: 
      #  - "job"
      #  -  dbmigration-job
      containers:
        - name: api-deployment
          image: apiimage
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
          #volumeMounts: 
          #  - name: cachedata-persistentvolume
          #    mountPath: "/var/log/heartbeat"
          resources:
            requests:
              cpu: 100m
              memory: 100Mi
            limits:
              cpu: 200m
              memory: 200Mi

---

apiVersion: v1
kind: Service
metadata:
  name: api-service
  labels:
    app: api-service
spec:
  ports:
  - port: 8080
    targetPort: 80
  type: LoadBalancer
  selector:
    app: api-deployment

Postgre yamls with volumes

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgresql-config
  labels:
    app: postgresql-config
data:
  POSTGRES_DB: weatherdb
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: admin123

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgresql-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql-deployment
spec:
  serviceName: postgresql-deployment
  replicas: 1
  selector:
    matchLabels:
      app: postgresql-deployment
  template:
    metadata:
      labels:
        app: postgresql-deployment
    spec:
      containers:
        - name: postgresql-deployment
          image: postgres
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgresql-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: weatherdb
      volumes:
        - name: weatherdb
          persistentVolumeClaim:
            claimName: postgresql-pv-claim
                
---

apiVersion: v1
kind: Service
metadata:
  name: postgresql-service
  labels:
    app: postgresql-service
spec:
  ports:
  - port: 5432
    targetPort: 5432
  type: LoadBalancer
  selector:
    app: postgresql-deployment

I am able to run the below command to create tables: kubectl exec -it postgresql-deployment-0 -- psql -h 127.0.0.1 -U postgres --password -p 5432 weatherdb

Logs of the Postgresql pod

2021-12-22 06:24:27.617 UTC [1] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2021-12-22 06:24:27.618 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2021-12-22 06:24:27.618 UTC [1] LOG: listening on IPv6 address "::", port 5432
2021-12-22 06:24:27.633 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-12-22 06:24:27.654 UTC [64] LOG: database system was shut down at 2021-12-22 06:24:27 UTC
2021-12-22 06:24:27.665 UTC [1] LOG: database system is ready to accept connections
2021-12-22 06:26:39.192 UTC [73] ERROR: relation "WeatherList" does not exist at character 68
2021-12-22 06:26:39.192 UTC [73] STATEMENT: SELECT w."Id", w."Summary", w."TemperatureC", w."WeatherDate"
FROM "WeatherList" AS w
2021-12-22 06:26:39.192 UTC [74] ERROR: relation "WeatherList" does not exist at character 68
2021-12-22 06:26:39.192 UTC [74] STATEMENT: SELECT w."Id", w."Summary", w."TemperatureC", w."WeatherDate"
FROM "WeatherList" AS w
2021-12-22 06:36:23.499 UTC [101] ERROR: relation "WeatherList" does not exist at character 68
2021-12-22 06:36:23.499 UTC [101] STATEMENT: SELECT w."Id", w."Summary", w."TemperatureC", w."WeatherDate"
FROM "WeatherList" AS w
2021-12-22 06:39:11.894 UTC [108] ERROR: relation "WeatherList" does not exist at character 68
2021-12-22 06:39:11.894 UTC [108] STATEMENT: SELECT w."Id", w."Summary", w."TemperatureC", w."WeatherDate"
FROM "WeatherList" AS w

Is my understanding correct that when creating the tables through external host these tables will be available in the pod as well which is ofcourse not happening?

Any help!!

-- Ritash Koul
asp.net-core-webapi
kubernetes
minikube
postgresql

0 Answers