Kubernetes - 封装DB的Pod正在崩溃

时间:2018-06-18 20:11:57

标签: kubernetes google-cloud-platform google-kubernetes-engine

当我尝试将Django应用程序部署到Kubernetes集群时,我遇到了问题。更具体地说,当我尝试部署PostgreSQL时。

以下是我的.YML部署文件:

apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  selector:
    app: postgres-container
    tier: backend
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
  type: ClusterIP
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  labels:
      type: local
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 2Gi
  hostPath:
    path: /tmp/data/persistent-volume-1 #U okviru cvora n
  persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pv-claim
  labels:
    type: local
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-container
      tier: backend
  template:
    metadata:
      labels:
        app: postgres-container
        tier: backend
    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: agent_technologies_db
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-volume-mount
              mountPath: /var/lib/postgresql/data/db-files

      volumes:
        - name: postgres-volume-mount
          persistentVolumeClaim:
            claimName: postgres-pv-claim
        - name: postgres-credentials
          secret:
            secretName: postgres-credentials

这是我运行kubectl get pods命令时得到的结果:

NAME                                             READY     STATUS             RESTARTS   AGE
agent-technologies-deployment-7c7c6676ff-8p49r   1/1       Running            0          2m
agent-technologies-deployment-7c7c6676ff-dht5h   1/1       Running            0          2m
agent-technologies-deployment-7c7c6676ff-gn8lp   1/1       Running            0          2m
agent-technologies-deployment-7c7c6676ff-n9qql   1/1       Running            0          2m
postgres-8676b745bf-8f7jv                        0/1       CrashLoopBackOff   4          3m

以下是我尝试使用kubectl logs $pod_name检查PostgreSQL部署情况时得到的结果:

initdb: directory "/var/lib/postgresql/data" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/var/lib/postgresql/data" or run initdb
with an argument other than "/var/lib/postgresql/data".
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.

注意:我使用Google Cloud作为提供商。

1 个答案:

答案 0 :(得分:4)

您无法在/var/lib/postgres/data/whatever中使用您的数据库。

/var/lib/postgres/whatever更改该路径,它会起作用。

  

<强> 17.2.1。使用辅助文件系统

     

许多安装在除了计算机&#34; root&#34;之外的文件系统(卷)上创建数据库集群。体积。如果选择这样做,则不建议尝试使用辅助卷的最顶层目录(挂载点)作为数据目录。最佳做法是在PostgreSQL用户拥有的mount-point目录中创建一个目录,然后在其中创建数据目录。这样可以避免权限问题,特别是对于诸如pg_upgrade之类的操作,如果辅助卷脱机,它还可以确保清除失败。

顺便说一下,我必须创建一个秘密,因为它不在帖子中:

apiVersion: v1
kind: Secret
metadata:
  name: postgres-credentials
type: Opaque
data:
  user: cG9zdGdyZXM=            #postgres
  password: cGFzc3dvcmQ=        #password

请注意,用户名必须为&#34; postgres&#34;。我不知道你是否涉及这个......