Link Search Menu Expand Document

Accessing application-specific logs

// cm-postgres.yaml 
master $ cat  cm-postgres.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: postgresdb
  POSTGRES_USER: testuser
  POSTGRES_PASSWORD: testpassword123


$ kubectl apply -f postgres.yaml
$ kubectl get pods -l app=postgres
$ kubectl exec -it postgres-0 -- /bin/bash

CREATE TABLE test (
   id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
   a int NOT NULL,
   created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO test (a) SELECT * FROM generate_series(-1, -1000, -1);

Get the log’s configuration details frompostgresql.conf. You will see that the logs are stored in the /var/log/postgresql directory:

$ cat /var/lib/postgresql/data/postgresql.conf |grep log

List and access the logs in the /var/log/postgresql directory:

$ ls /var/log/postgresql


Optionally, while you’re inside the container, you can create a backup of our example postgresdb database in the tmp directory using the following command:

$ pg_dump --username testuser postgresdb > /tmp/backup.sql

//postgres.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: "postgres"
  replicas: 2
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:latest
        envFrom:
          - configMapRef:
              name: postgres-config
        ports:
        - containerPort: 5432
          name: postgredb
        volumeMounts:
        - name: postgredb
          mountPath: /var/lib/postgresql/data
          subPath: postgres
  volumeClaimTemplates:
  - metadata:
      name: postgredb
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: openebs-jiva-default
      resources:
        requests:
          storage: 5Gi

cat pvc-postgres.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  storageClassName: openebs-jiva-default
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10G


cat svc-postgres.yaml
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
   - port: 5432
  selector:
   app: postgres