1

我正在尝试将 postgres 服务部署到具有持久卷和持久卷声明的谷歌云 kubernetes,以为我的应用程序提供存储。

当我部署时,吊舱卡在CrashLoopBackOff.

pod 的一个事件失败并显示以下消息:

Error: failed to start container "postgres": Error response from daemon: error while creating mount source path '/data/postgres-pv': mkdir /data: read-only file system

这是我尝试使用 kubectl 部署的 yaml:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv
  labels:
    type: local
    app: postgres
spec:
  capacity:
    storage: 5Gi
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/postgres-pv
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pvc
  labels:
    type: local
    app: postgres
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  volumeName: postgres-pv
---
apiVersion: v1
kind: Secret
metadata:
  name: postgres-credentials
type: Opaque
data: 
  user: YWRtaW4=
  password: password==
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres-container
          image: postgres:9.6.6
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-credentials
                  key: user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-credentials
                  key: password   
            - name: POSTGRES_DB
              value: kubernetes_django
          ports:
            - containerPort: 5432
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgres-volume-mount
      volumes:
        - name: postgres-volume-mount
          persistentVolumeClaim:
            claimName: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
  selector:
   app: postgres

没有什么部署失败,但是 pod 卡在了 CrashLoopBackOff 中。

谢谢您的帮助!

4

3 回答 3

1

问题是您的持久音量。您使用的主机路径不支持,并且根据文档,永远不会支持

于 2018-10-26T02:37:47.407 回答
1

我有同样的问题。按照本教程,我可以使用它,minikube但它在 GCP 上给出了同样的错误。

正如 Patrick W 所提到的,文档说:

持久卷的类型

  • ...
  • HostPath(仅限单节点测试——不支持本地存储,并且在多节点集群中不起作用)
  • ...

为了解决这个问题,我在 kubernetes文档中找到了一个解决方案

您首先必须创建一个gcePersistentDisk

gcloud compute disks create --size=[SIZE] --zone=[ZONE] [DISK_NAME]

然后链接中描述的配置应该可以解决问题:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv
  labels:
    type: local
spec:
  capacity:
    storage: 4Gi
  storageClassName: standard
  accessModes:
    - ReadWriteMany
  gcePersistentDisk:
    pdName:data-disk
    fsType: ext4
    readOnly: true
于 2019-02-21T23:04:55.427 回答
0

首先,您不应在持久卷中使用主机路径。其次,我认为您在部署中遗漏了一部分,您应该指定一个 PDATA 目录。

为了简单起见,我分享了 yml 部署:

1. 持久性卷声明:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pvc  
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi

2. 配置映射来保存 posgres env:

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: profile
  POSTGRES_USER: postgresadmin
  POSTGRES_PASSWORD: othmane
  PGDATA: /var/lib/postgresql/data/pgdata

3. postgres 部署:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:10.4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
              name: postgres
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
           - name: postgres-db
             mountPath: /var/lib/postgresql/data
      volumes:
      - name: postgres-db
        persistentVolumeClaim: 
          claimName: postgres-pvc


---

apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  selector:
    app: postgres
  ports:
   - protocol: "TCP"
     port: 5432
     targetPort: 5432
  type: ClusterIP
于 2019-04-28T23:25:56.557 回答