kubernetes - Kubernetes-封装DB的Pod崩溃

标签 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作为提供商。

最佳答案

您的dbt不能包含在/var/lib/postgres/data/whatever中。
通过/var/lib/postgres/whatever更改该路径,它将起作用。

17.2.1. Use of Secondary File Systems

Many installations create their database clusters on file systems (volumes) other than the machine's "root" volume. If you choose to do this, it is not advisable to try to use the secondary volume's topmost directory (mount point) as the data directory. Best practice is to create a directory within the mount-point directory that is owned by the PostgreSQL user, and then create the data directory within that. This avoids permissions problems, particularly for operations such as pg_upgrade, and it also ensures clean failures if the secondary volume is taken offline.


而且,顺便说一下,我必须创建一个 secret ,因为它不在帖子中:
apiVersion: v1
kind: Secret
metadata:
  name: postgres-credentials
type: Opaque
data:
  user: cG9zdGdyZXM=            #postgres
  password: cGFzc3dvcmQ=        #password
请注意,用户名必须为“postgres”。我不知道你是否在报道...

关于kubernetes - Kubernetes-封装DB的Pod崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50916801/

相关文章:

firebase - 由于Cloud Build API,Firebase Cloud Functions是否要求在2020年4月20日之前启用计费功能?

maven - 如何使用fabric8-maven-plugin标记图像?

docker - 在挂起状态下使用kubernetes pod进行部署时,节点上的磁盘压力

postgresql - Docker for desktop - kubernetes - postgres 本地客户端

azure - 如何在不使用 helm 的情况下将静态公共(public) IP 地址添加到 AKS 集群中的 kubernetes/nginx 入口 Controller

elasticsearch - 如何将数据从 kubernetes 集群中的 fluentd 发送到集群外远程独立服务器中的 elasticsearch?

django - 如何在 Google Cloud App Engine 上使用 PubSub 创建订阅者,通过 Publisher 从 Google Cloud App Engine Flex 收听消息?

google-cloud-platform - 在 Google Cloud 虚拟机实例上启用存储权限

kubernetes - 如何将我们的自定义Thingsboard部署到Kubernetes Engine?

kubernetes - 防止从 Kubernetes pod 停止日志记录