在Google云平台上的Kubernetes中创建持久卷

时间:2018-10-20 23:57:39

标签: postgresql kubernetes google-kubernetes-engine persistent-volumes

我正在尝试以持久卷和持久卷声明为Google Cloud kubernetes部署一个postgres服务,以便为我的应用程序提供存储。

部署时,吊舱卡在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

没有任何部署,但是吊舱卡在了CrashLoopBackOff中。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

问题是您的持久卷。您正在使用不受支持的主机路径,并且永远不会支持according to the doc

答案 1 :(得分:0)

我有同样的问题。在this tutorial之后,我可以使用minikube了,但是在GCP上却给出了相同的错误。

如Patrick W所言,the docs说:

  

持久卷的类型

     
      
  • ...
  •   
  • HostPath(仅单节点测试–本地   任何方式都不支持存储,并且在多节点中将不工作   集群)
  •   
  • ...
  •   

为解决这个问题,我在kubernetes docs

中找到了一个解决方案。

您首先必须创建一个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

答案 2 :(得分: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