简体   繁体   English

如何使用postgres数据库,Docker和Kubernetes来保存数据?

[英]How to persist data using a postgres database, Docker, and Kubernetes?

I am trying to mount a persistent disk on my container which runs a Postgres custom image. 我正在尝试在我的容器上安装一个永久磁盘,该容器运行Postgres自定义映像。 I am using Kubernetes and following this tutorial. 我正在使用Kubernetes并遵循教程。

This is my db_pod.yaml file: 这是我的db_pod.yaml文件:

apiVersion: v1
kind: Pod
metadata:
  name: lp-db
  labels:
    name: lp-db
spec:
  containers:
    - image: my_username/my-db
      name: my-db
      ports:
        - containerPort: 5432
          name: my-db
      volumeMounts:
        - name: pg-data
          mountPath: /var/lib/postgresql/data
  volumes:
    - name: pg-data
      gcePersistentDisk:
        pdName: my-db-disk
        fsType: ext4

I create the disk using the command gcloud compute disks create --size 200GB my-db-disk . 我使用命令gcloud compute disks create --size 200GB my-db-disk

However, when I run the pod, delete it, and then run it again (like in the tutorial) my data is not persisted. 但是,当我运行pod时,删除它,然后再次运行它(就像在教程中一样)我的数据不会持久化。

I tried multiple versions of this file, including with PersistentVolumes and PersistentVolumeClaims, I tried changing the mountPath, but to no success. 我尝试了这个文件的多个版本,包括PersistentVolumes和PersistentVolumeClaims,我尝试更改mountPath,但没有成功。

Edit 编辑

Dockerfile for creating the Postgres image: 用于创建Postgres图像的Dockerfile:

FROM ubuntu:trusty
RUN rm /bin/sh && \
    ln -s /bin/bash /bin/sh

# Get Postgres
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" >> /etc/apt/sources.list.d/pgdg.list
RUN apt-get update && \
    apt-get install -y wget
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Install virtualenv (will be needed later)
RUN apt-get update && \
    apt-get install -y \
        libjpeg-dev \
        libpq-dev \
        postgresql-9.4 \
        python-dev \
        python-pip \
        python-virtualenv \
        strace \
        supervisor


# Grab gosu for easy step-down from root
RUN gpg --keyserver pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \
    && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/1.2/gosu-$(dpkg --print-architecture)" \
    && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/1.2/gosu-$(dpkg --print-architecture).asc" \
    && gpg --verify /usr/local/bin/gosu.asc \
    && rm /usr/local/bin/gosu.asc \
    && chmod +x /usr/local/bin/gosu \
    && apt-get purge -y --auto-remove ca-certificates wget

# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8

# Adjust PostgreSQL configuration so that remote connections to the database are possible.
RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.4/main/pg_hba.conf

# And add ``listen_addresses`` to ``/etc/postgresql/9.4/main/postgresql.conf``
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.4/main/postgresql.conf
RUN echo "log_directory='/var/log/postgresql'" >> /etc/postgresql/9.4/main/postgresql.conf

# Add all code from the project and all config files
WORKDIR /home/projects/my-project
COPY . .

# Add VOLUMEs to allow backup of config, logs and databases
ENV PGDATA /var/lib/postgresql/data
VOLUME /var/lib/postgresql/data

# Expose an entrypoint and a port
RUN chmod +x scripts/sh/*
EXPOSE 5432
ENTRYPOINT ["scripts/sh/entrypoint-postgres.sh"]

And entrypoint script: 和入口点脚本:

echo " I am " && gosu postgres whoami

gosu postgres /etc/init.d/postgresql start && echo 'Started postgres'
gosu postgres psql --command "CREATE USER myuser WITH SUPERUSER PASSWORD 'mypassword';"  && echo 'Created user'
gosu postgres createdb -O myuser mydb && echo 'Created db'

# This just keeps the container alive.
tail -F /var/log/postgresql/postgresql-9.4-main.log

In the end, it seems that the real problem was the fact that I was trying to create the database from my entrypoint script. 最后,似乎真正的问题是我试图从我的入口点脚本创建数据库。 Things such as creating a db or a user should be done at container creation time so I ended up using the standard Postgres image, which actually provides a simple and easy way to create an user and a db. 创建数据库或用户的事情应该在容器创建时完成所以我最终使用标准的Postgres映像,这实际上提供了一种简单易用的方法来创建用户和数据库。

This is the fully functional configuration file for Postgres. 这是Postgres的全功能配置文件。

apiVersion: v1
kind: Pod
metadata:
  name: postgres
  labels:
    name: postgres
spec:
  containers:
    - name: postgres
      image: postgres
      env:
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        - name: POSTGRES_USER
          value: myuser
        - name: POSTGRES_PASSWORD
          value: mypassword
        - name: POSTGRES_DB
          value: mydb
      ports:
        - containerPort: 5432
      volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: pg-data
  volumes:
    - name: pg-data
      persistentVolumeClaim:
        claimName: pg-data-claim

Thanks to all those who helped me :) 感谢所有帮助过我的人:)

  • does your custom postgresql persist data at /var/lib/postgresql/data? 你的自定义postgresql是否在/ var / lib / postgresql / data中保存数据?
  • are you able to get logs from your postgresql container and spot anything interesting? 你能从postgresql容器中获取日志并发现有趣的东西吗?
  • when your pod is running, can you see the mountpoints inside your container and check the persistent disk is there? 当你的pod运行时,你能看到容器内的挂载点并检查持久磁盘是否存在?

I followed this scenario and I was able to persist my data by changing the mountPath to /var/lib/postgresql and also reproduced using cassandra (ie /var/lib/cassandra for mountPath) 我遵循这个场景,我能够通过将mountPath更改为/ var / lib / postgresql并使用cassandra(即mount /的/ var / lib / cassandra)进行复制来保留我的数据

I was able to delete/restart pods from different nodes/hosts and still see my "users" table and the data I previously entered. 我能够从不同的节点/主机删除/重新启动pod,仍然可以看到我的“用户”表和我之前输入的数据。 However, I was not using a custom image, I just used standard docker images. 但是,我没有使用自定义图像,我只使用标准的docker图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM