minikube postgres statefulset错误-严重:角色“ password =“不存在

时间:2019-08-25 09:29:40

标签: postgresql docker kubernetes minikube kubernetes-statefulset

我在这里使用Arianit Uka的Kubernetes Postgres StateulSet示例存储库: https://github.com/arianitu/postgres-statefulset

使用minikube时,我的吊舱容器没有打开。机密已被应用,似乎POSTGRES_PASSWORD env var也很好。回购代码中没有password=,所以我迷失了问题所在。检查日志可以看到以下信息:

> $ k logs postgres-0 --all-containers=true                                                                                              ⬡ 8.11.4 [±hauser ●●]
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data/pgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
syncing data to disk ... ok

Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data/pgdata -l logfile start

waiting for server to start....2019-08-25 07:22:31.295 UTC [41] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-08-25 07:22:31.307 UTC [42] LOG:  database system was shut down at 2019-08-25 07:22:31 UTC
2019-08-25 07:22:31.310 UTC [41] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/create-dev-db.sh
CREATE DATABASE
GRANT

/usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/create-replica-user.sh
CREATE ROLE

2019-08-25 07:22:31.751 UTC [41] LOG:  received fast shutdown request
waiting for server to shut down....2019-08-25 07:22:31.752 UTC [41] LOG:  aborting any active transactions
2019-08-25 07:22:31.753 UTC [41] LOG:  worker process: logical replication launcher (PID 48) exited with exit code 1
2019-08-25 07:22:31.755 UTC [43] LOG:  shutting down
2019-08-25 07:22:31.774 UTC [41] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2019-08-25 07:22:31.862 GMT [1] LOG:  skipping missing configuration file "/etc/replica.conf"
2019-08-25 07:22:31.862 GMT [1] LOG:  skipping missing configuration file "/etc/replica.conf"
2019-08-25 07:22:31.865 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2019-08-25 07:22:31.865 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2019-08-25 07:22:31.869 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-08-25 07:22:31.888 UTC [68] LOG:  database system was shut down at 2019-08-25 07:22:31 UTC
2019-08-25 07:22:31.893 UTC [1] LOG:  database system is ready to accept connections
2019-08-25 07:23:20.352 UTC [75] FATAL:  role "password=" does not exist
2019-08-25 07:23:23.595 UTC [82] FATAL:  role "password=" does not exist

这是minikube仪表板的样子:

minikube dashbaord


@Rahman,我取出了密码机密,现在在statefulset-master.ymlstatefulset-replica.yml中仅使用普通值:

          env:
            ...

            - name: POSTGRES_PASSWORD
              value: master-password

            - name: REPLICATION_PASSWORD
              value: replica-password

我还注释了service.yml中的“副本”部分,因为目前我只对运行单个pod /数据库感兴趣:


service.yml

apiVersion: v1
kind: Service
metadata:
  labels:
    app: postgres
  name: postgres
spec:
  type: ClusterIP
  ports:
  - name: postgres
    port: 5432
    protocol: TCP
    targetPort: 5432
  selector:
    app: postgres
    
# ---

# apiVersion: v1
# kind: Service
# metadata:
#   labels:
#     app: postgres-replica
#   name: postgres-replica

# spec:
#   type: ClusterIP
#   ports:
#   - name: postgres-replica
#     port: 5432
#     protocol: TCP
#     targetPort: 5432
#   selector:
#     app: postgres-replica
    
# ---


statefulset-master.yml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  updateStrategy:
    type: RollingUpdate
  
  selector:
    matchLabels:
      app: postgres

  serviceName: postgres
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres 
    spec:
      volumes:
        - name: postgres-config
          configMap:
            name: postgres

        # - name: shared
        #   emptyDir: {}
            
      terminationGracePeriodSeconds: 10

      containers:
        - name: postgres
          image: postgres:10.5
          args: ['-c', 'config_file=/etc/postgres.conf', '-c', 'hba_file=/etc/pg_hba.conf']
          
          imagePullPolicy: IfNotPresent
        
          ports:
            - name: postgres
              containerPort: 5432
              protocol: TCP
          
          resources:
            requests:
              cpu: 100m
              memory: 256Mi
          
          env:
            - name: POSTGRES_USER
              value: postgres
          
            - name: PGUSER
              value: postgres
          
            - name: POSTGRES_DB
              value: postgres
            
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata

            - name: POSTGRES_PASSWORD
              value: master-password
              # valueFrom:
              #   secretKeyRef:
              #     key: password
              #     name: postgres
                  
            - name: REPLICATION_PASSWORD
              value: replica-password
              # valueFrom:
              #   secretKeyRef:
              #     key: replicaPassword
              #     name: postgres
              
            - name: POD_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP
              
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host $POD_IP
            failureThreshold: 6
            initialDelaySeconds: 60
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5

          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host $POD_IP
            failureThreshold: 3
            initialDelaySeconds: 5
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 3

          volumeMounts:
            - mountPath: /var/lib/postgresql/data/pgdata
              name: postgres
              subPath: postgres-db
              
            - name: postgres-config
              mountPath: /etc/postgres.conf
              subPath: postgres.conf
              
            - name: postgres-config
              mountPath: /etc/master.conf
              subPath: master.conf
              
            - name: postgres-config
              mountPath: /etc/pg_hba.conf
              subPath: pg_hba.conf
              
            - name: postgres-config
              mountPath: /docker-entrypoint-initdb.d/create-replica-user.sh
              subPath: create-replica-user.sh

            - name: postgres-config
              mountPath: /docker-entrypoint-initdb.d/create-dev-db.sh
              subPath: create-dev-db.sh

            # - name: shared
            #   mountPath: /User/Shared

        - name: hauser
          image: mikeumus/hauser
          # volumeMounts:
          #   - name: shared
          #     mountPath: /User/Shared
        
  volumeClaimTemplates:
  - metadata:
      name: postgres
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "standard"
      resources:
        requests:
          storage: 3Gi


statefulset-replica.yml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres-replica
spec:
  updateStrategy:
    type: RollingUpdate
  
  selector:
    matchLabels:
      app: postgres-replica

  serviceName: postgres-replica
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres-replica 
    spec:
      volumes:
        - name: postgres-config
          configMap:
            name: postgres
            
      terminationGracePeriodSeconds: 10
      
      initContainers:
        - name: setup-replica-data-directory
          image: postgres:10.5
          
          env:
            - name: PGPASSWORD
              valueFrom:
                secretKeyRef:
                  key: replicaPassword
                  name: postgres

          command:
          - sh
          - -c
          - |
            if [ -z "$(ls -A /var/lib/postgresql/data/pgdata)" ]; then
                echo "Running pg_basebackup to catch up replication server...";
                pg_basebackup -R -h postgres -D /var/lib/postgresql/data/pgdata -P -U replication; 
                chown -R postgres:postgres $PGDATA;
            else
                echo "Skipping pg_basebackup because directory is not empty"; 
            fi

          volumeMounts:
            - mountPath: /var/lib/postgresql/data/pgdata
              name: postgres-replica
              subPath: postgres-db

      containers:
        - name: postgres-replica
          image: postgres:10.5
          args: ['-c', 'config_file=/etc/postgres.conf']
          
          imagePullPolicy: IfNotPresent
        
          ports:
            - name: postgres-rep
              containerPort: 5432
              protocol: TCP
          
          resources:
            requests:
              cpu: 100m
              memory: 256Mi
          
          env:
            - name: POSTGRES_USER
              value: postgres
          
            - name: PGUSER
              value: postgres
          
            - name: POSTGRES_DB
              value: postgres
            
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          
            - name: POSTGRES_PASSWORD
              value: master-password
              # valueFrom:
              #   secretKeyRef:
              #     key: password
              #     name: postgres
                  
            - name: REPLICATION_PASSWORD
              value: replica-password
              # valueFrom:
              #   secretKeyRef:
              #     key: replicaPassword
              #     name: postgres
              
            - name: POD_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP
              
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host $POD_IP
            failureThreshold: 6
            initialDelaySeconds: 60
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5

          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready --host $POD_IP
            failureThreshold: 3
            initialDelaySeconds: 5
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 3

          volumeMounts:
            - mountPath: /var/lib/postgresql/data/pgdata
              name: postgres-replica
              subPath: postgres-db
            
            - name: postgres-config
              mountPath: /etc/postgres.conf
              subPath: postgres.conf

            - name: postgres-config
              mountPath: /etc/replica.conf
              subPath: replica.conf
             
  volumeClaimTemplates:
  - metadata:
      name: postgres-replica
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "standard"
      resources:
        requests:
          storage: 3Gi


config / create_configmap.sh

kubectl create configmap postgres --from-file=postgres.conf --from-file=master.conf --from-file=replica.conf --from-file=pg_hba.conf --from-file=create-replica-user.sh --from-file=create-dev-db.sh

config / create-replica-user.sh

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    CREATE ROLE replication WITH REPLICATION PASSWORD '$REPLICATION_PASSWORD' LOGIN
EOSQL

这是我启动/应用资源的顺序:

  1. minikube start
  2. cd config除非我在配置目录中,否则我会得到一些丢失的错误
  3. . ./create_configmap.sh
  4. cd ..
  5. k apply -f ./config/secret.yml
  6. k apply -f ./service.yml
  7. k apply -f ./statefulset-master.yml

链接到我的postgres-statefulset回购的分支: https://gitlab.com/mikeumus/postgres-statefulset

1 个答案:

答案 0 :(得分:0)

不是问题所在的是postgres-stateful代码。 “ password=”代码是我作为另一个容器添加到statefulset-master.yml

Hauser代码中